Technical Note TN2339

Building from the Command Line with Xcode FAQ

This document provides answers to frequently asked questions about command line tools.

What is the Command Line Tools Package?
Downloading command-line tools is not available in Xcode for macOS 10.9. How can I install them on my machine?
How can I uninstall the command-line tools?
I have multiple versions of Xcode installed on my machine. What version of Xcode do the command-line tools currently use?
How do I select the default version of Xcode to use for my command-line tools?
How do I build my projects from the command line?
My app has multiple build configurations. How do I set a default build configuration for xcodebuild?
How do I run unit tests from the command line?
How do I implement the Build For Testing and Test Without Building features from the command line?
What keys can I pass to the exportOptionsPlist flag?
How do I archive and export my app for distribution?
Document Revision History

What is the Command Line Tools Package?

The Command Line Tools Package is a small self-contained package available for download separately from Xcode and that allows you to do command line development in macOS. It consists of the macOS SDK and command-line tools such as Clang, which are installed in the /Library/Developer/CommandLineTools directory.

Downloading command-line tools is not available in Xcode for macOS 10.9. How can I install them on my machine?

In macOS 10.9 and later, the Downloads pane of Xcode Preferences does not support downloading command-line tools. Use any of the following methods to install command-line tools on your system:

Figure 1  Download page for the Command Line Tools package.

How can I uninstall the command-line tools?

I have multiple versions of Xcode installed on my machine. What version of Xcode do the command-line tools currently use?

To find out what version of Xcode is being used by your tools, run the following command in Terminal:

$ xcode-select --print-path

Listing 2  Printing the version of Xcode currently used by the command-line tools.

$ xcode-select --print-path
/Applications/Xcode8.3.3/Xcode.app/Contents/Developer

How do I select the default version of Xcode to use for my command-line tools?

To select a default Xcode for your command-line tools, run the following command in Terminal:

$ sudo xcode-select -switch <path/to/>Xcode.app

where <path/to/> is the path to the Xcode.app package you wish to use for development.

Listing 3  Setting the default Xcode version.

$ sudo xcode-select -switch /Applications/Xcode8.3.3/Xcode.app

How do I build my projects from the command line?

xcodebuild is a command-line tool that allows you to perform build, query, analyze, test, and archive operations on your Xcode projects and workspaces from the command line. It operates on one or more targets contained in your project, or a scheme contained in your project or workspace. xcodebuild provides several options for performing these operations as seen its man page. xcodebuild saves the output of your commands in the locations defined in the Locations preferences pane of your Xcode application, by default.

See below for various xcodebuild usage. Be sure to navigate to the directory containing your project or workspace in Terminal before running any of the following commands.

My app has multiple build configurations. How do I set a default build configuration for xcodebuild?

In Xcode, the Configurations section of your project's Info pane provides a pop-up menu, which sets the default configuration to be used by xcodebuild when building a target. Use this pop-up menu to select a default build configuration for xcodebuild as seen in Figure 2.

Figure 2  Debug set as the default build configuration for xcodebuild.

How do I run unit tests from the command line?

xcodebuild provides several options for running unit tests.

To build and run unit tests from the command line, execute the following command in Terminal:

xcodebuild test [-workspace <your_workspace_name>]
                [-project <your_project_name>]
                -scheme <your_scheme_name>
                -destination <destination-specifier>
                [-only-testing:<test-identifier>]
                [-skip-testing:<test-identifier>]

To build unit tests without running them from the command line, execute the following command in Terminal:

xcodebuild build-for-testing [-workspace <your_workspace_name>]
                             [-project <your_project_name>]
                             -scheme <your_scheme_name>
                             -destination <destination-specifier>

To run unit tests without building them from the command line, execute any of the following command in Terminal:

xcodebuild test-without-building [-workspace <your_workspace_name>]
                                 [-project <your_project_name>]
                                 -scheme <your_scheme_name>
                                 -destination <destination-specifier>
                                 [-only-testing:<test-identifier>]
                                 [-skip-testing:<test-identifier>]
xcodebuild test-without-building -xctestrun <your_xctestrun_name>.xctestrun
                                 -destination <destination-specifier>
                                 [-only-testing:<test-identifier>]
                                 [-skip-testing:<test-identifier>]

The test action requires specifying a scheme and a destination. See How do I implement the Build For Testing and Test Without Building features from the command line? for more information about build-for-testing and test-without-building actions.

The -workspace option allows you to specify the name of your workspace. Use this option when your scheme is contained in an Xcode workspace.

The -project option allows you to specify the name of your Xcode project. Use this option when your scheme is contained in an Xcode project. It is required when there are multiple Xcode projects in the same directory and optional, otherwise.

The -destination option allows you to specify a destination for your unit tests. It takes an argument <destination-specifier>, which describes the device, simulator, or Mac to use as a destination. It consists of a set of comma-separated key=value pairs, which are dependent upon the the device, simulator, or Mac being used.

The -only-testing and -skip-testing options, which are optional, allow you to run only a specific test and to skip a test, respectively. They take an argument <test-identifier>, which specifies the test to be executed or excluded. test-identifier's format is as follows:

TestTarget[/TestClass[/TestMethod]]

TestTarget, which is required, is the name of the test bundle. TestClass and TestMethod, which are both optional, respectively represent the name of the class and the name of the method to be tested.

The -destination option also allows you to run the same unit test on multiple destinations. This is done by adding it multiple times to your xcodebuild test command as demonstrated in Listing 17.

Listing 17  Tests the iOS scheme in both the Simulator and on an iPod touch.

xcodebuild test -scheme iOS -destination 'platform=iOS Simulator,name=iPhone 6s,OS=10.3' -destination 'platform=iOS,name=iPod touch'

How do I implement the Build For Testing and Test Without Building features from the command line?

What keys can I pass to the exportOptionsPlist flag?

To get all available keys for -exportOptionsPlist, run the following command in Terminal:

xcodebuild -help

Listing 22  Fetching all keys supported by -exportOptionsPlist.

$ xcodebuild -help
Usage: xcodebuild [-project <projectname>] [[-target <targetname>]...|-alltargets] [-configuration <configurationname>] [-arch <architecture>]
     ...
 
Available keys for -exportOptionsPlist:
 
    compileBitcode : Bool
         ...
    embedOnDemandResourcesAssetPacksInBundle : Bool
         ...
    iCloudContainerEnvironment
         ...
    manifest : Dictionary
         ...
    method : String
         ...
    onDemandResourcesAssetPacksBaseURL : String
         ...
    teamID : String
         ...
    thinning : String
         ...
    uploadBitcode : Bool
         ...
    uploadSymbols : Bool
         ...

See Figure 3 for a sample file that contains some options for the -exportOptionsPlist flag.

Figure 3  Plist file with a list of options for the -exportOptionsPlist flag.

How do I archive and export my app for distribution?

To archive and export your app for distribution, run the following command in Terminal:

xcodebuild -exportArchive -archivePath <xcarchivepath> -exportPath <destinationpath> -exportOptionsPlist <path>

where <xcarchivepath> specifies the archive or the path of the archive to be exported, <destinationpath> specifies where to save the exported product, and <path> is the path to the file with a list of options for the -exportOptionsPlist flag.

Listing 23  Exports the iOSApp archive to the Release location with the options saved in the OptionsPlist.plist.

xcodebuild -exportArchive -archivePath iOSApp.xcarchive -exportPath Release/MyApp -exportOptionsPlist OptionsPlist.plist


Document Revision History


DateNotes
2017-06-19

Updated the "How do I run unit tests from the command line?" question. Added the "How do I implement the Build For Testing and Test Without Building features from the command line?" and "What keys can I pass to the exportOptionsPlist flag?" questions.

 

Updated the "How do I run unit tests from the command line?" question. Added the "How do I implement the Build For Testing and Test Without Building features from the command line?" and "What keys can I pass to the exportOptionsPlist flag?" questions.

2014-05-21

New document that provides answers to frequently asked questions about command-line tools.