Running pre-/post-build Python scripts from XCode


#1

For anyone interested in running Python scripts during the pre- or post-build phase in XCode, this is what we’re doing:

We’ve set this in the “Post-build-shell script” entry within the Projucer:

$SRCROOT/../../../project/xcode/post_build_script.sh;

We’re calling a sh-file first, instead of the Python script directly, as this ensures that the build is interrupted on error. This is the script:

#!/usr/bin/env bash

function callPythonScript()
{
    # Passing <GCC_PREPROCESSOR_DEFINITIONS> will set
    # them in <sys.argv> in the Python script.
    python $1 ${GCC_PREPROCESSOR_DEFINITIONS};

    # Exit on error.
    return_code=$?
    if [ $return_code -ne 0 ]; then
        exit 1;
    fi
}

# Note that we're currently in /project/xcode/<product_folder>
callPythonScript "../../../installer/mac/pkg_create.py";

exit 0;

Out Python script, with some condition to only run on Release, and only on the aggregate target (note that this currently only works on JUCE’s current develop branch, not on master):

#!/usr/bin/env python

import os

is_release = os.environ['CONFIGURATION'] == 'Release'
is_aggregate = os.environ['TARGET_NAME'].endswith('(All)')

# Only run on release, and only run at the end
# (when the aggregate target is built).
if is_release and is_aggregate:
    product_name = os.environ['PRODUCT_NAME']
    #.. build installer, do stuff etc

Hope this is of some help for some.


Post-build script