Emitting errors and warnings in Xcode Run Script Phase

There are many reasons one could need to execute some arbitrary code while running a build from Xcode. The most popular examples include third-party tools integrations, collecting build statistics or modifying standard build routines. In this article I am going to present how to add a simple Run Script Phase and emit log, warning and error messages from it.

Custom Run Script Phases can emit various types of messages
Custom Run Script Phases can emit various types of messages

Adding a new Runs Script Phase can be done by navigating to the “Build Phases” tab inside project settings.

Navigate to Build Phases tab inside project settings to add new Run Script Phase
Navigate to Build Phases tab inside project settings to add new Run Script Phase

In the top left corner, there is a “+” button. To add a new Run Script Phase please click it and select the “New Run Script Phase” option.

Create a new Run Script Phase by clicking “+” button in top left corner
Create a new Run Script Phase by clicking “+” button in top left corner

After performing these actions the new build phase is good to go. Very often it is required to print some information for the build process logs.

It can be done by simply using the “echo” command.

Use echo to print a log message from Run Script Phase
Use echo to print a log message from Run Script Phase

Once we run the build — the log entry will be visible.

Custom Run Script Phase messages are visible in the build log
Custom Run Script Phase messages are visible in the build log

In some cases it may be required to get some developer attention by emitting a warning. It could be done easily too. The only thing to do is prefixing the message with “warning”.

Prefix the message with “warning” to present it in Issue Navigator
Prefix the message with “warning” to present it in Issue Navigator

Prefix the message with “warning” to present it in Issue Navigator

When the build is run this time — the warning is emitted and presented in Issue Navigator.

Warnings are visible inside Issue Navigator
Warnings are visible inside Issue Navigator

Warnings are visible inside Issue Navigator

Sometimes things go wrong and we would like to let the other know about it. It is going to be simple as well. In this case — just prefix the message with “error” and run the build again.

Prefix a message with “error” to notify the user when things go wrong
Prefix a message with “error” to notify the user when things go wrong

Once the build is executed — it fails. That is the default behavior when errors being emitted.

Error messages can be presented in Issue Navigator pane too
Error messages can be presented in Issue Navigator pane too

Error messages can be presented in Issue Navigator pane too

Let’s sum things up. Xcode allows developers to emit custom log messages when running Run Script Phase during the build. It is done by simply echoing the messages and prefixing them with either “error” or “warning” to set the required log level. All needed commands are available in the gist below.

# Emit log message
echo "This is just a log message"

# Emit warning
echo "warning: This is a warning message"

# Emit error
echo "error: This is an error message"

Thanks for reading. I hope you find that story valuable.