swift_compile_times_parser icon indicating copy to clipboard operation
swift_compile_times_parser copied to clipboard

A small script that turns Xcode build logs into Xcode build warnings

This script parses and aggregates Swift function body compile times from an Xcode build log. It can either print errors/warnings that Xcode will pick up from a Run Script build step, or tell you your worst N blocks.

Tested exclusively with Swift 2.2 and Xcode 7.3.1.

To use it, you need an Xcode build log generated by a build with a special setting: OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies".

You'll probably also want to clean all relevant targets first.

You can create a target for use by a continuous integration server to report long compile times as errors by creating an 'aggregate' target and adding a Run Script phase like this:

# xcodebuild subprocesses to rm, which prints errors we
# don't care about at all
exec 3>&2
exec 2> /dev/null

# save log to /tmp/
LOG_PATH=`mktemp /tmp/build.log.XXXXXX` || exit 1

# call xcodebuild...from within Xcode. INCEPTION
xcodebuild \
    -workspace MyWorkspace.xcworkspace
    -scheme "My scheme" \
    -sdk iphonesimulator9.3 \  # or whatever
    OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" \
    > $LOG_PATH

# now get our stderr back so we can print errors
exec 2>&3

# report all function body compile times >500ms as an error
/path/to/parse_swift_compile_times.py --max-time=500 $LOG_PATH

# save script return code
e=$?

# remove temp file
rm $LOG_PATH

# exit with script's return code, which will be 1 if there were errors
exit $e