73

How do I specify to xcodebuild (the command line tool) whether I want to build for the simulator or device?

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192

2 Answers2

152

An Xcode build from the command line looks like:

xcodebuild -configuration ${BUILD_TYPE} -target ${TARGET_NAME} -arch ${CPU_ARCHITECTURE} -sdk ${SIMULATOR_OR_IOS_SDK} 

BUILD_TYPE is something like "Release" or "Debug" (those are the defaults, you may have added others to the project)

TARGET_NAME is the name of the target you are building (by default the same name as your project)

CPU_ARCHITECTURE is the CPU you are building for, one of:

i386, armv6, armv7

Use i386 for simulator builds, and use either armv6 or armv7 for device builds - note that some other devices cannot run armv7 code, so usually when building libraries it's a good idea to build all of these architectures and then glue them together using lipo.

SIMULATOR_OR_IOS_SDK is what you are looking for, it's either iphoneos or iphonesimulator. Those values use the latest version of the SDK that the installed Xcode supports, you can get a list of supported SDK's with:

xcodebuild -showsdks

Which returns a list like:

Mac OS X SDKs:
    Current Mac OS                  -sdk 
    Mac OS X 10.6                   -sdk macosx10.6

iOS SDKs:
    iOS 4.2                         -sdk iphoneos4.2

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

xcodebuild has more flags than that, but those are the ones you'd commonly use after using Xcode to set up the build properties. You don't have to use all of them, but it's probably a good idea to be clear about what you are building - otherwise I believe your last settings are used.

shim
  • 9,289
  • 12
  • 69
  • 108
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • I know `-configuration` and `-target`, I'd just forgotten that hardware vs. simulator was set by the SDK. Thanks! – Steven Fisher Feb 16 '11 at 03:39
  • 6
    You might want to also mention `-sdk iphoneos` for the latest version. – Steven Fisher Feb 16 '11 at 03:42
  • Thanks, I knew XCode had a "latest version" SDK setting, but I couldn't figure out what it was. I assume "iphonesimulator" is also the latest iPhone simulator version. – Kendall Helmstetter Gelner Feb 16 '11 at 04:41
  • Amended answer to refer to use latest version variants by default. – Kendall Helmstetter Gelner Feb 16 '11 at 04:48
  • 5
    Note to build with `-sdk iphonesimulator`, I had to also use `-arch i386`, as xcodebuild was erroring out in some llvm commands. – Jeb Jul 25 '12 at 16:21
  • Thanks @Jeb, I added that flag to the answer since it seems best to include it to be explicit. – Kendall Helmstetter Gelner Jul 25 '12 at 20:46
  • @Kendall Helmstetter Gelner how to install the build (created using above command) to the real device connected (via usb)? – Qadir Hussain May 04 '16 at 11:23
  • @KendallHelmstetterGelner, Thank you for this. I have slightly different requirement. I need an archive which I could install in a simulator. I work in a workspace (not in a project) because I have cocoa pods added. I also have 2 extensions and a today widget. I tried the following, xcodebuild -workspace myproject.xcworkspace -scheme mainappscheme -sdk "iphonesimulator" -configuration Release -arch i386 archive I ended up with the following error, You cannot archive for the iOS Simulator platform – ThE uSeFuL Dec 02 '16 at 02:29
  • @ThEuSeFuL : Although not as convenient, what I do for simulator uses is to simply copy all of the directories out of a simulator build, then paste them back into other simulators as needed - I use OpenSim to quickly find app specific folders in each simulator: https://github.com/luosheng/OpenSim – Kendall Helmstetter Gelner Dec 03 '16 at 20:16
  • @ThEuSeFuL Archiving produces a .ipa which is only required to distribute to devices. You can actually install the .app produced by a standard build to the simulator. See http://stackoverflow.com/questions/26031601/xcode-6-launch-simulator-from-command-line about installing to simulator. – Jacob King Feb 08 '17 at 12:03
  • @JacobKing, .app does not include the two extensions I have :( – ThE uSeFuL Feb 09 '17 at 06:02
  • in 2021 including -arch i386 for simulator builds triggers an error. "iOS 10 is the maximum deployment target for 32-bit targets." Remove -arch i386 flag to resolve the issue – Lee Irvine Jul 08 '21 at 15:23
1

i find the -xcconfig flag quite useful. this option allows you to specify a path to an xcconfig (build settings file). within an xcconfig, you may #include other xcconfig files.

justin
  • 104,054
  • 14
  • 179
  • 226