Swift

Use Carthage For Build Automation In Cocoa Apps

Note: these are just my notes to get started with Carthage and by no means a definitive resource of information.

Carthage is a tool that automates build dependencies for Cocoa apps and provides binary frameworks without modifying out-of-band project files. If you’re using homebrew, installing carthage is a one-liner:

Once installed, build a Cartfile in the home directory of xcode projects. The cartfile will list required projects, by version, as you can see here, which lists AlamoFire:

brew install carthage

Once Carthage is installed, you’ll need a cartfile for each project, to map the dependencies. This gives you the chance to resolve those when you’re building new software. Each time you add something new, simply put the dependency in there, taking into account when you want to use a given version or branch.

# Use the latest version github "AFNetworking/AFNetworking" # Use a local project git "file:///Users/krypted/project1" "branch1"

The above is an example that requires Alamofire (a http networking library commonly used) for the build process to complete as well as a local project file at /Users/krypted/project1 – and while this uses branch1 you can just delete that to not require a specific branch.

Next, we’ll do a quick update, which creates a Carthage directory in your .xcodeproj directory, as well as a cartfile.resolved file.

carthage update

That Carthage directory has a Build folder, which includes the artifacts required to build your project. Put all those in the appropriate platform folder.

Next, we need to tell Carthage to copy the frameworks at build time. To do that, we’ll run a script in the build phase, so click on the Build Phases settings tab and then click to add and select New Run Script Phase from the options. Simply put the following text in there:

/usr/local/bin/carthage copy-frameworks

Click on Input Files and then provide the path to the framework bundle, using “$(SRCROOT)” as the faux root of your .xcodeproj directory, as follows:

$(SRCROOT)/Carthage/Build/iOS/AFNetworking.framework

Click on Output Files and then provide the path to the framework bundle, using “$(SRCROOT)” as the faux root of your .xcodeproj directory, as follows:

$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/AFNetworking.framework

Then do a quick copy-frameworks to make them available and do the routine checking:

/usr/local/bin/carthage copy-frameworks

To then see warnings when artifacts are out-of-date, run Carthage with the outdated verb and a –xcode-warnings flag as follows:

/usr/local/bin/carthage outdated --xcode-warnings

To add this to your build process and have Xcode feed the warnings to you automatically, add this as another Build Phase.