Heaps of App insights, stacked for the curious minds.

Xcode `Run Scripts` have one of the amazing feature to optimise build process. In my opinion, it is well known but frequently unused.

To get started, provide input file/files and output file/files in the `Run Script`. Also, check `Based on dependency analysis` option in the `Run Script`. This feature has no impact on the script itself, such as restricting access to input files. Idea is to run the script only if the input has changed.

It is important to understand purpose of your script to optimise build. Some examples would include running the script on specific file types or only on files that have changes.

I have written two simple scripts to facilitate a smooth start. One script builds list of files by file-type and other by changed files.

Use the script that suites your needs followed by script you want to run. All the clean builds will run your scripts but next builds should be quick.

Your Run Script in Xcode will look something like this:

# Script to create file list for Xcode.
$PROJECT_DIR/Scripts/input_output_list_for_file_types.sh SwiftFiles swift

# Script you want to execute when there are changes in the input file list. 
$PROJECT_DIR/Scripts/$YOUR_SCRIPT

Xcode – Run Script:

Create list by file-types:

$PROJECT_DIR/$SCRIPT_LOCATION/input_output_list_for_file_types.sh $FILE_NAME "$TYPE_1 $TYPE_2"
#!/bin/sh
# Read more at:
# https://stackedheaps.com/2023/07/02/optimise-xcode-builds-with-input-output-file-lists
# Script Arguments:
# $1 = xcfilelist name without extension.
# Example: SwiftFiles
# $2 = Space separated list of file extensions (case sensitive).
# Examples: "swift json"
# $3 = Optional Output directory
# Path relative to $SRCROOT for saving xcfilelist.
# By default, file will be saved in projects root directory
# **********************************************************************************
# Create xcfilelist for the list of file type.
XC_FILE_LIST_NAME=$1
LIST_OF_FILE_TYPES=$2
OUTPUT_DIRECTORY=$3
if [ ! -z $OUTPUT_DIRECTORY ]; then
PROJECT_XC_FILE=$SRCROOT/$OUTPUT_DIRECTORY/$XC_FILE_LIST_NAME.xcfilelist
else
PROJECT_XC_FILE=$SRCROOT/$XC_FILE_LIST_NAME.xcfilelist
fi
BUILD_XC_FILE=$DERIVED_FILE_DIR/$XC_FILE_LIST_NAME.xcfilelist
OUTPUT_FILE=$DERIVED_FILE_DIR/$XC_FILE_LIST_NAME.txt
# Since we append the content, we need to start over.
# Append and remove is not required for one file type.
rm -f $BUILD_XC_FILE
for FILE_TYPE in $LIST_OF_FILE_TYPES;
do
echo "Creating xcfilelist for * $FILE_TYPE * file type"
# Find files and append the contents to xcfilelist.
find $SRCROOT -name "*.$FILE_TYPE" | sed -e "s#$SRCROOT#\$(SRCROOT)#;" >> $BUILD_XC_FILE
done
# Compare SRCROOT file with DERIVED_FILE_DIR file. Copy if anything has changed.
cmp –silent $PROJECT_XC_FILE $BUILD_XC_FILE || cp -f $BUILD_XC_FILE $PROJECT_XC_FILE
# Create an output file
touch $OUTPUT_FILE
# **********************************************************************************

Create list of changed files:

$PROJECT_DIR/$SCRIPT_LOCATION/input_output_list_for_changed_files.sh $FILE_NAME
#!/bin/sh
# Read more at:
# https://stackedheaps.com/2023/07/02/optimise-xcode-builds-with-input-output-file-lists
# Script Arguments:
# $1 = xcfilelist name without extension.
# Example: ChangedFiles
# $2 = Optional Output directory
# Path relative to $SRCROOT for saving xcfilelist.
# By default, file will be saved in projects root directory
# **********************************************************************************
# Create xcfilelist for the changed file.
XC_FILE_LIST_NAME=$1
OUTPUT_DIRECTORY=$2
if [ ! -z $OUTPUT_DIRECTORY ]; then
PROJECT_XC_FILE=$SRCROOT/$OUTPUT_DIRECTORY/$XC_FILE_LIST_NAME.xcfilelist
else
PROJECT_XC_FILE=$SRCROOT/$XC_FILE_LIST_NAME.xcfilelist
fi
BUILD_XC_FILE=$DERIVED_FILE_DIR/$XC_FILE_LIST_NAME.xcfilelist
OUTPUT_FILE=$DERIVED_FILE_DIR/$XC_FILE_LIST_NAME.txt
# Find files and add the contents to xcfilelist.
git –no-pager diff –name-only | sed -e 's#^#$(SRCROOT)/#;' > $BUILD_XC_FILE
# Compare SRCROOT file with DERIVED_FILE_DIR file. Copy if anything has changed.
cmp –silent $PROJECT_XC_FILE $BUILD_XC_FILE || cp -f $BUILD_XC_FILE $PROJECT_XC_FILE
# Create an output file
touch $OUTPUT_FILE
# **********************************************************************************

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.