As the topic says, I want to be able to run a specific command during build, and have its output be the definition of a preprocessor macro.
Right now, I have several user-defined variables (in project.pbxproj) and I am able to use their values to fill macro definitions, as follows:
GCC_PREPROCESSOR_DEFINITIONS = (
“STRINGIFY(x)=@#x”,
“_MACRO=STRINGIFY(${MACRO})”,
);
MACRO = foo;
I can set MACRO to a different value per scheme (e.g Debug vs. Release) which is very useful. But, I cannot figure out how to set it by running a command.
I can think of 3 options:
Environment variable: If you build from command line you can export a variable (
export ENVMACRO=superfoo) before invoking the build command and use it in an Xcode configuration fileOTHER_CFLAGS=-DMACRO=$(ENVMACRO). You need to configure the target with the.xcconfigfile.Run Script Build Phase: Custom script that generates a header file.
In my tests you need an empty header file to be able to compile properly. There are other options like modifying an existing file using
sedor any other command.Custom Build Rule: Custom script that process an input file and creates an output file. Similar to Run Script build phase but slightly better because it will run the script only when the input file has been modified. For example, create a
.macrofile and process it to update a header file.In Xcode > Target > Build rules, add new custom rule.
Process:
*.macroCustom script:
Output files:
$(SRCROOT)/Project.hProject.macrocontains pairsMACRO=one-liner-command. Like these two non-sense examples:Generated file will look like:
Each time
Project.macrochanges, the generated header will be updated.