Read preprocessor macro in Xcode build script? Read preprocessor macro in Xcode build script? unix unix

Read preprocessor macro in Xcode build script?


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.


Yes, it's possible, but you will need to search the header file it's defined in, in order to find it. You could use numerous utilities to do that, but grep and awk are probably the most straight forward and will work in most trivial cases (you might need more if the constant is a string constant):

#!/bin/shAPIKEY=$(grep 'define.*APIKEY' file.h | awk '{print $3}')