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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With