Xcode Command Line – Tips and Tricks

0.00 avg. rating (0% score) - 0 votes

This post shares various ways to use xcodebuild to build your iOS or OS X projects from the command line.

The easiest way is just type a single command without any parameter “xcodebuild” to build the project in the current directory:

xcodebuild

The output is below (GCClient here is the target of my project):

=== BUILD TARGET GCClient OF PROJECT GCClient WITH THE DEFAULT CONFIGURATION (Release) ===

xcodebuild will look for project file ( .xcodeproj file in the current directory) and invoke the default target/config.

We can also specify the project to build in the command line like below:

xcodebuild -project GCClient.xcodeproj

Output:

=== BUILD TARGET GCClient OF PROJECT GCClient WITH THE DEFAULT CONFIGURATION (Release) ===

Again, the xcode will use the default (release) config for building.

If your project has more than one targets (maybe for unit test, testing….), you can specify the target as well:

xcodebuild -project GCClient.xcodeproj -target GCClient

In case you want to build for debugging only, you can specify the configuration to build:

xcodebuild -project GCClient.xcodeproj -target GCClient -configuration Debug

In this case, xCode will build the architectures you specified in BuildSettings > Architectures

build_settings_architectures
The output .a will be valid for all 3 architectures and put at build/Debug-iphoneos.
You can check the list of supported architectures by “lipo” command

lipo -info build/Debug-iphoneos/libGeoComplyClient.a

Output:

Architectures in the fat file: build/Debug-iphoneos/libGeoComplyClient.a are: armv7 arm64 armv7s

Sometimes, you want to build against an older SDK, simulator SDK, or even OS X SDK, you can specify it by the -sdk parameter.

To check the pre-installed SDKs on your machine, you can use the following command:

xcodebuild -showsdks

Output:

OS X SDKs:
OS X 10.11 -sdk macosx10.11

iOS SDKs:
iOS 9.0 -sdk iphoneos9.0
iOS Simulator SDKs:
Simulator - iOS 9.0 -sdk iphonesimulator9.0

watchOS SDKs:
watchOS 2.0 -sdk watchos2.0
watchOS Simulator SDKs:
Simulator - watchOS 2.0 -sdk watchsimulator2.0

Once we know the available SDKs, we can now specify the based SDK from the command line:

xcodebuild -project GCClient.xcodeproj -target GCClient -configuration Debug -sdk iphoneos9.0


There is a more handy way to use the xcodebuild command, it’s “Run Script” in Build Phase of Xcode.
If you want to build flat .a file which contains all 5 architectures for both real devices(armv7/armv7s/arm64) and simulators (i386/x86_64), you can write Run Script as following:

SDK_VERSION=$(echo ${SDK_NAME} | grep -o '.\{3\}$')

if [ "true" == ${ALREADYINVOKED:-false} ]
then
    echo "RECURSION: don't run xcodebuild here, it will be infinitely looped"
else
    # CRITICAL:
    # Prevent infinite recursion
    export ALREADYINVOKED="true"

    xcodebuild -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphoneos${SDK_VERSION} ARCHS="armv7 armv7s arm64" -destination "name=iOS" BUILD_DIR="${BUILD_DIR}"
    xcodebuild -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphonesimulator${SDK_VERSION} ARCHS="x86_64 i386"  -destination "name=iOS Simulator" BUILD_DIR="${BUILD_DIR}"

    # collect build .a files:
    CURRENTCONFIG_DEVICE_DIR=${SYMROOT}/${CONFIGURATION}-iphoneos
    CURRENTCONFIG_SIMULATOR_DIR=${SYMROOT}/${CONFIGURATION}-iphonesimulator
    CREATING_UNIVERSAL_DIR=${SYMROOT}/${CONFIGURATION}-universal

    rm -rf "${CREATING_UNIVERSAL_DIR}"
    mkdir "${CREATING_UNIVERSAL_DIR}"

    lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"
fi
 

Notice that you have to specify the ARCHS and sdk properly. If ARCHS are for real devices(arm*) then SDK must be iphoneos*. In contrast, iphonesimulator* is in-pair with x86_64/i386.

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>