51

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

Xcode Version 8.2.1 (8C1002)

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • In my case, I use `AppleScript` to show Dialog or Notifications in Xcode with error tips. https://stackoverflow.com/a/63655906/4026902 – RY_ Zheng Aug 30 '20 at 09:24

2 Answers2

124

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.

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

for XCode versions >= 13.2
The XCode build log now includes a Run pre-actions section. If you don't redirect to a file, those messages will end up as the last item in a 'Run custom shell script' in this section - access via the XCode Report Navigator.

enter image description here

enter image description here

foundry
  • 31,615
  • 9
  • 90
  • 125
  • 5
    Thank you for this @foundry. But it didn't work, I'm afraid. No file created. –  Feb 28 '17 at 09: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. –  Feb 28 '17 at 09:47
  • @robdashnash - I omitted the ${PROJECT_DIR} environment varialble. try my updated answer. – foundry Feb 28 '17 at 11:04
  • 2
    Thanks @foundy. Out of interest: what is the 2>&1 ? –  Mar 01 '17 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 01 '17 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 01 '19 at 16:23
  • @foundry , thanks for this great tip. Sending a bounty! Assuming you build in Xcode, is there actually any way to see the output (ie stdErr) of the prebuild script, other than capturing it to, a file as in your tip? There's no way to view that output as it happens in one of the panels or suich in Xcode?? thanks! – Fattie Jan 03 '23 at 14:17
  • @Fattie - yes, in the report navigator - see my expanded answer – foundry Jan 06 '23 at 01:13
  • @foundry - **incredible**. bounty'ed ! – Fattie Jan 06 '23 at 13:53
  • Thanks @Fattie - it seems that pre-action script output in the Report Navigator is a new feature since XCode 13.2.x. – foundry Jan 10 '23 at 01:45
12

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.

Alex Zavatone
  • 4,106
  • 36
  • 54
  • 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 '20 at 02:05