50

Hit build and nothing gets printed in build log. What gives?

Xcode Version 8.2.1 (8C1002)

enter image description here

1

2 Answers 2

119

Pre-action takes place before the build, so output doesn't go to the build log, it goes to stdErr. You can copy the output to a file:

exec > ${PROJECT_DIR}/prebuild.log 2>&1
echo "hello world"

To get the environment variables to work, you also need to set "Provide build settings from" to the appropriate target.

enter image description here

That should write hello world to a file named "prebuild.log" in your project folder.

If you want these activities to end up in the build log, consider adding a run script to your target's build phase instead.

6
  • 5
    Thank you for this @foundry. But it didn't work, I'm afraid. No file created.
    – user1951992
    Feb 28, 2017 at 9:45
  • I am literally creating a fresh project. So launch Xcode > Select iOS Framework > Name it > Save it > Edit Scheme > Expand 'Build' > Select 'pre action' > Add this script. > Hit CMD + B.
    – user1951992
    Feb 28, 2017 at 9:47
  • 2
    Thanks @foundy. Out of interest: what is the 2>&1 ?
    – user1951992
    Mar 1, 2017 at 13:11
  • 2
    @robdashnash - man bash, look at the section called 'redirection'. ... "directs both standard output and standard error to the file [filename]"
    – foundry
    Mar 1, 2017 at 14:00
  • 1
    Check where your ${PROJECT_DIR} points at, e.g. I was (mistakenly) looking at my workspace folder (root) but it refers to the folder where you've the xcodeproj (in case like me you've two different folders for them).
    – Eugenio
    May 1, 2019 at 16:23
10

To add to the answer, https://stackoverflow.com/a/42497746/1058199, it's important to not clutter up the project.pbxproj (where build scripts go) or your scheme file (where these scripts go) as much as possible.

With this in mind, I create a Build-Scripts folder in the project root where I put the app build scripts and pre-action scripts. This folder is dragged into the root of the project as a folder so that any script I add to it is automatically added to project.

Assuming that your pre-action script is called That_pre-action_script.sh, this is what I put in Pre-actions script based on the approved answer.

say "Pre build scripts running."
exec > "${PROJECT_DIR}/prebuild.log" 2>&1
echo "Starting build scheme Pre-actions"
"${PROJECT_DIR}/Build-Phases/That_pre-action_script.sh"

As a test, make sure to echo some message from your script so you can see it in the prebuild.log file.

Hope this helps.

And don't forget to chmod u+x your script in the terminal so that it will run.

The important part is if you can't make sure if your build script is running or not. This is where the say command us useful so that you know it's actually being issued before a build.

It's a good idea to put quotes around the path to the script in case there are any space characters (or otherwise) in the path to your project.

1
  • 2
    +1 for your note about the quotes! This was the entire problem on my system. You'd think we'd have spaces-aware software at this point...
    – Oscar
    Aug 20, 2020 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy