Search code examples
xcodeswift2swift3carthage

How to select Swift toolchains when building with Carthage


I am creating an iOS app and use Carthage to build external libraries. Since the libraries I use are currently both Swift 2 and Swift 3 I am in a bit of a squeeze. Thus I want to have one Swift 2 branch and one Swift 3 branch for development and then merge them when the libraries are all Swift 3 compatible.

However, I am not able to select which toolchain for build for with Carthage (version 0.18).

Here is the contents of my Cartfile:

# Swift 2 libraries
github "stephencelis/SQLite.swift" ~> 0.10.1
github "Haneke/HanekeSwift"

# Swift 3 libraries
#github "stephencelis/SQLite.swift" "swift3"
#github "Haneke/HanekeSwift" "feature/swift-3"
#github "kitasuke/PagingMenuController"
#github "Alamofire/Alamofire" ~> 4.0

This currently builds by using carthage update. Here are some output verifying that I am currently using Swift 2:

$ swift --version
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9
$ ls -lh $(which swift)
-rwxr-xr-x  1 root  wheel    18K Jul  8 19:52 /usr/bin/swift

Now I want to switch to build with Swift 3. I have multiple toolchains installed in /Library/Developer/Toolchains, which is where OSX installs them when I run the installer:

$ ls -lh /Library/Developer/Toolchains
drwxr-xr-x  6 root  wheel   204B Sep 30 20:07 swift-2.2.1-SNAPSHOT-2016-04-23-a.xctoolchain
drwxr-xr-x  7 root  wheel   238B Sep 30 17:41 swift-3.0-RELEASE.xctoolchain
drwxr-xr-x  7 root  wheel   238B Oct  6 20:53 swift-3.0.1-PREVIEW-2.xctoolchain
lrwxr-xr-x  1 root  wheel    30B Oct  8 12:51 swift-latest.xctoolchain -> swift-3.0-RELEASE.xctoolchain/

Note that I have both Xcode8 and Xcode7 installed:

$ ls /Applications/Xcode*
/Applications/Xcode.app:
Contents
/Applications/Xcode8.app:
Contents
$ xcodebuild -version
Xcode 7.3.1
Build version 7D1014

Now, lets say I have the following scenario:

$ cat Cartfile
github "Alamofire/Alamofire" ~> 4.0
$ carthage update --toolchain com.apple.dt.toolchain.Swift_3_0
*** Fetching Alamofire
*** Checking out Alamofire at "4.0.1"
*** xcodebuild output can be found in /var/folders/wg/fjk346qs7mx8fhplf8_805wm0000gn/T/carthage-xcodebuild.zfHVn5.log
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
warning: failed to load toolchain 'com.apple.dt.toolchain.Swift_3_0': Could not find toolchain: com.apple.dt.toolchain.Swift_3_0

I just cannot get this to work. So my question is:

How can I build with Carthage by using another toolchain?

Here's what I have tried:

  1. Changing the symbolic link of swift-latest.xctoolchain to other things. Does no difference.
  2. Manually adding the toolchains to my PATH variable in .bashrc like this, but it makes no difference.

    $ cat ~/.bashrc
    export PATH="$PATH:/Library/Developer/Toolchains/swift-2.2.1-SNAPSHOT-2016-04-23-a.xctoolchain/usr/bin/"
    export PATH="$PATH:/Library/Developer/Toolchains/swift-3.0-RELEASE.xctoolchain/usr/bin/"
    export PATH="$PATH:/Library/Developer/Toolchains/swift-3.0.1-PREVIEW-2.xctoolchain/usr/bin/"
    
  3. Running the build with TOOLCHAINS=com.apple.dt.toolchain.Swift_3_0 carthage build and similar, but it makes no difference (still cannot find the toolchain).


Solution

  • As of Carthage 0.17 there is an option --toolchain that allows one to specify an exact toolchain ID to use for the build. Example usage:

    carthage update --toolchain org.swift.402017072a
    

    The ID of the toolchain comes from the key CFBundleIdentifier in Info.plist file of the toolchain.