Building Xcode 4 projects from the command line

Posted on by in Mobile

The Xcode 4 developer tools introduced some changes to the xcodebuild command line tool. Instead of specifying a project and target developers can now provide a workspace and scheme to build.

> /Developer_Xcode4/usr/bin/xcodebuild -help
Usage: xcodebuild [-project ] [[-target ]…|-alltargets] [-configuration ] [-arch ]… [-sdk [|]] [=]… []…
xcodebuild -workspace -scheme [-configuration ] [-arch ]… [-sdk [|]] [=]… []…
xcodebuild -version [-sdk [|] [] ]
xcodebuild -list [[-project ]|[-workspace ]]
xcodebuild -showsdks

The workspace and scheme names should be known from the project. The default configuration options are “Debug” and “Release”. The “-showsdks” option lists available SDKs.

> /Developer_Xcode4/usr/bin/xcodebuild -showsdks
Mac OS X SDKs:
Mac OS X 10.6 -sdk macosx10.6

iOS SDKs:
iOS 4.3 -sdk iphoneos4.3

iOS Simulator SDKs:
Simulator – iOS 3.2 -sdk iphonesimulator3.2
Simulator – iOS 4.0 -sdk iphonesimulator4.0
Simulator – iOS 4.1 -sdk iphonesimulator4.1
Simulator – iOS 4.2 -sdk iphonesimulator4.2
Simulator – iOS 4.3 -sdk iphonesimulator4.3

Available build actions are listed in the xcodebuild man pages. Unfortunately these do not match the actions available in a scheme and the command line does not provide a mechanism for testing, running, profiling, or analyzing builds.

> man xcodebuild

buildaction …
Specify a build action (or actions) to perform on the target. Available build actions are:

build Build the target in the build root (SYMROOT). This is the default build action.

archive Archive a scheme from the build root (SYMROOT). This requires specifying a workspace and scheme.

installsrc Copy the source of the project to the source root (SRCROOT).

install Build the target and install it into the target’s installation directory in the distribution root (DSTROOT).

clean Remove build products and intermediate files from the build root (SYMROOT).

Given all of these configuration options it is then possible to perform builds from the command line.

> /Developer_Xcode4/usr/bin/xcodebuild -workspace WrappingScrollView.xcworkspace -scheme WrappingScrollViewDemo -sdk iphonesimulator4.3 -configuration Debug clean build
Build settings from command line:
SDKROOT = iphonesimulator4.3

=== CLEAN NATIVE TARGET JCFWrappingScrollView OF PROJECT JCFWrappingScrollView WITH CONFIGURATION Debug ===
Check dependencies

** CLEAN SUCCEEDED **

=== BUILD NATIVE TARGET JCFWrappingScrollView OF PROJECT JCFWrappingScrollView WITH CONFIGURATION Debug ===
Check dependencies

ProcessPCH /Users/Jonah/Library/Developer/Xcode/DerivedData/WrappingScrollView-fqscqfdtpoqunuanehtbgpseirdj/Build/PrecompiledHeaders/JCFWrappingScrollView-Prefix-dkkjrdaonxokgpbkgjfmpmcpadoz/JCFWrappingScrollView-Prefix.pch.gch JCFWrappingScrollView/JCFWrappingScrollView-Prefix.pch normal i386 objective-c com.apple.compilers.llvmgcc42
cd /Users/Jonah/Desktop/WrappingScrollView/JCFWrappingScrollView
setenv LANG en_US.US-ASCII

** BUILD SUCCEEDED **

Hopefully that is sufficient to automatically produce ad-hoc builds on a continuous integration server.


Feedback

  Comments: 11


  1. Is there a way to add a file or folder to an Xcode project via the Xcode command line?


  2. @his: nothing that I have found. It might be possible to do so via Xcode’s scripting interface or by editing the project file xml directly but I don’t think either one is well supported or documented.


  3. Very interesting; but I was wondering if there is any other way to use the IDE and automate it (this would give you capability for example to launch a target that uses the simulator, something that Xcodebuild cannot do).

    Looked at Applescript but it doesn’t seems to work with Xcode4, you can cheat using keyboard shortcut but at that point you loose capability to get back the results of the build and whatever is printed in the log.

    Jonah, do you know of any way to actually drive the Xcode4 IDE via UI instead than console? Thanks!


  4. Hi Jonah,
    Thanks for useful post. short and sweet info it goes direct to solution. This help me for workspace.
    Still i am looking for using -project option.


  5. Jonah,

    Is there a way to specify to provisioning profile or do you have to resign it through the codesign tool?

    Thanks,

    Jay


  6. When using the archive build action, is there a way to specify the the directory it will create the archive in?


  7. Is it possible to specify bundle identifier using command line(xcodebuild)?

Your feedback