Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read preprocessor macro in Xcode build script?

Is it possible to read a preprocessor macro from a build script in Xcode?

In my precompiled header I've defined a macro like:

#define APIKEY 123abc

In my build script I would like to get access to this macro as a variable so that I can do something with it, e.g.:

echo $APIKEY > outputfile
like image 794
lmirosevic Avatar asked Jan 31 '13 08:01

lmirosevic


1 Answers

There is a more robust way to do this.

When executing a Run Script build phase, Xcode lists all the pre-processor definitions that were made by your Project Build Settings, inside one environment variable called GCC_PREPROCESSOR_DEFINITIONS.

For definitions that you need to access from your script, define them in Project Build Settings. If you are moving these out of your source code, but need to maintain robustness of your source (e.g. you also compile this source outside of the Xcode project) you can optionally retain the source definitions, wrapped with #ifndef / #endif - thereby making them passive defaults.

Now, to retrieve these as script variables, simply evaluate the content of GCC_PREPROCESSOR_DEFINITIONS in your Run Script build phase, like this:

eval "${GCC_PREPROCESSOR_DEFINITIONS}"

If you want to make sure there is a default value in the script, for one or more specific variables, define them above this evaluation.

like image 175
Chris Hatton Avatar answered Sep 19 '22 02:09

Chris Hatton