Reputation: 3825
I'm trying get set a #define macro when doing a command line build using xcodebuild and having no luck.
I've tried -DMYMACRO=1
and MYMACRO=1
everything else I can think of and nothing works.
How do you set a #define from the command line?
Upvotes: 9
Views: 6661
Reputation: 16921
You can pass OTHER_SWIFT_FLAGS
to xcodebuild thus:
xcodebuild \
build \
"OTHER_SWIFT_FLAGS=${inherited} -D STAGING" \
-target "TargetName" \
-scheme "SchemeName"
This will result in the following:
#if STAGING
// this will compile
#else
// this won't compile
#endif
Upvotes: 1
Reputation: 8458
Roger,
What you are looking for is a way to set GCC_PREPROCESSOR_MACROS via the command line tool xcodebuild. The man page for xcodebuild shows the format for applying these settings, however the SYNOPSIS section only refers to this as "setting=value ..."
xcodebuild [-project projectname] -scheme schemename [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...] [-userdefault=value ...] xcodebuild -workspace workspacename -scheme schemename [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...] [-userdefault=value ...]
Ultimately, you have the ability to set any of the Build Settings values directly on the command line by using this format and knowing the actual build setting name you wish to alter. Naturally, this brings up the question...
How do I find Build Setting Names?
Glad you asked! Xcode 4's sidebar is the easiest place to look to find the actual command-line build setting name.
When looking for a build setting name, the Quick Help Inspector of Xcode 4's Utilities sidebar is the place to go looking.
Alternatively, use Option+Command+2 to show the Quick Help Inspector directly!
Finally you are ready to find your build setting:
In the case of the Preprocessor Macros setting you originally asked about, that setting is:
GCC_PREPROCESSOR_DEFINITIONS
Pulling this back together to your original question, you can set this build setting on the command line simply by providing the SETTING_NAME='DESIRED_VALUE' along with the rest of your xcodebuild command. In the case of a quick little test project I threw together called 'TestApp' where I wanted Preprocessor Macro 'BKM' to be set to value 1, my xcodebuild command would be:
xcodebuild -project TestApp.xcodeproj -scheme TestApp GCC_PREPROCESSOR_DEFINITIONS='${inherited} BKM=1'
Why did you stick ${inherited} in there?
If you are using Preprocessor Macros then you likely have more than one that you are using. Some of which you may not want to change from the command line, but still have coded into the target or project's build settings. The use of '${inherited}' instructs xcodebuild to also use the build settings defined at a higher level instead of using only the ones we've defined in the xcodebuild command. In most cases you'll want to use ${inherited} to pull in any other configured values you've setup.
Do I have to wrap the value in apostrophes?
If you want to set more than one value, then yes you will need to wrap the value in apostrophes otherwise if you set two or more Preprocessor Macros from the command line, the 2nd+ macro will be interpreted as a build setting instead of a Preprocessor Macro...not quite the right behavior. The apostrophes act as a way to collect multiple values for a setting together. In the case of my sample xcodebuild command, I wanted to have xcodebuild use the Inherited set of Preprocessor Macros along with my specific BKM setting so I wrapped the value in apostrophes to tell xcodebuild to treat them both as Preprocessor Macros.
Will this work with Workspaces too?
Absolutely! Just modify the command to use the -workspace parameter instead of the -project parameter and you'll be in business!
Upvotes: 28