Is it possible to export user defined XCode build settings to environment variables

ios, iphone, xcode

Solution

xcodebuild -showBuildSettings

shows all build settings, including the user-defined settings. Example:

$ xcodebuild -configuration Debug -showBuildSettings | grep MYTESTSETTING
    MYTESTSETTING = DebugValue
$ xcodebuild -configuration Release -showBuildSettings | grep MYTESTSETTING
    MYTESTSETTING = ReleaseValue

To get these variables into the environment of your current shell, you have to parse this output. This can for example be done with a Perl script (or many other scripting languages).

Create a Perl script "exportsettings.pl" with the following contents:

#!/usr/bin/perl
open(FH, "xcodebuild -configuration Release -showBuildSettings|");
while(<FH>) {
    if (/\s*(\w+)\s*=\s*(.*)$/) { # Search for <key> = <value>
        $key = $1; $value = $2;
        print "export $key='$value'\n";
    }
}
close(FH);

Now you can run the command

$ eval `perl exportsettings.pl`

from the command line, and (almost) all build settings are in the environment. (There will be some error messages, e.g. "UID: readonly variable").

If you need only your used-defined settings in the environment, you could use a unique prefix (e.g. "MY") and change the line

    if (/\s*(\w+)\s*=\s*(.*)$/) { # Search for <key> = <value>

to

    if (/\s*(MY\w+)\s*=\s*(.*)$/) { # Search for MY<key> = <value>

Problem

I have made a few build settings for various configurations, e.g. e.g. I can access them in various files (e.g. the info.plist) like so: ``` ${MYTESTSETTING} ``` However is it possible to get the value in the command line enviroment? e.g. after a xcodebuild from Jenkins I have tried ``` echo ${MYTESTSETTING} ``` And ``` echo $MYTESTSETTING ```

Original source