Read preprocessor macro in Xcode script assembly?

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

In my precompiled header, I defined a macro like:

#define APIKEY 123abc 

In my build script, I would like to access this macro as a variable so that I can do something with this, for example:

 echo $APIKEY > outputfile 
+4
source share
2 answers

Yes, it is possible, but you will need to search for the define d in header file to find it. You can use many utilities for this, but grep and awk are probably the most straightforward and will work in most trivial cases (you may need more if the constant is a string constant):

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

There is a more reliable way to do this.

When you run the โ€œRun Scriptโ€ build phase, Xcode lists all the pre-processor definitions that were made using the Project Build Options , inside one environment variable called GCC_PREPROCESSOR_DEFINITIONS .

For the definitions that you need to access from the script, define them in the Project Build Options . If you move them from the source code, but you need to maintain the reliability of your source (for example, you also compile this source outside the Xcode project), you can additionally save the source definitions wrapped in #ifndef / #endif - thereby making them passive values โ€‹โ€‹by by default.

Now, to get them as Script variables, simply evaluate the contents of GCC_PREPROCESSOR_DEFINITIONS in the "Run Script" build phase, for example:

 eval "${GCC_PREPROCESSOR_DEFINITIONS}" 

If you want to make sure that the script has a default value for one or more specific variables, define them above this estimate.

+10
source

All Articles