23

I am using Xcode 4 and I added an preaction to the run scheme, a bash script, but it seems that Xcode is ignoring the exit status of the script and always run, even if the exit code is not zero.

Is this normal? What alternatives do I have for adding a custom step, one that can fail?

Update: I also tried exit 1 for post-action for build but with the same results, always executing without any feedback.

How can I make a custom step that can mark the build as failed?

zekel
  • 9,227
  • 10
  • 65
  • 96
sorin
  • 161,544
  • 178
  • 535
  • 806
  • for the feedback of `pre-actions` or `post-actions` scripts. I use `AppleScript` to show `Dialog` or `Notification` for it. https://stackoverflow.com/a/63655906/4026902 – RY_ Zheng Aug 30 '20 at 09:26

3 Answers3

28

Not only does it not seem to care about pre-/post-action script exit status, but it doesn't seem to place the output into the build/run logs either. There are I think two separate threads about this buried in Apple's Xcode 4 dev forums. No word on whether this is a bug or a feature.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
5

You can add something like kill $PPID in the run script phase to terminate the xcodebuild (with exit code 70).

Farcaller
  • 3,070
  • 1
  • 27
  • 42
  • 1
    you could also combine this with a bash "trap" as a one-liner (see http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html): trap "kill $PPID" ERR – Jason Pepas Feb 12 '16 at 23:25
  • 5
    Might be a nice solution for xcodebuild, but be aware: if you compile in Xcode, it will kill Xcode immediately! – ishahak Jan 03 '17 at 10:12
  • 1
    with I was able to confirm the pre-action is called – naz Aug 30 '21 at 12:56
1

When using Xcode, kill $PPID is not a solution, as I was commenting to Farcaller.

My solution is to have the pre-action script generate a header file (e.g. SchemePreActions.h) and make it empty for normal situation, and include it into your code (e.g. into AppDelegate.m).

This way, if you want the pre-actions script to report an error, put there a #error line, like in this real-world example:

if [ $CONFIGURATION == Debug ]; then
    echo "#error AppStore builds must not be in debug configuration" >> SchemePreActions.h
fi

Xcode will report this error very nicely.

It is recommended to have the generated file ignored by git.


As a tip I will add that my pre-actions script always starts with these lines:
echo "//auto-generated. no need to commit"      > SchemePreActions.h
echo "#define SCHEME_${SCHEME_NAME}"            >> SchemePreActions.h
echo "#define SCHEME_NAME @\"${SCHEME_NAME}\""  >> SchemePreActions.h
if [[ ${SCHEME_NAME} =~ "_PROD" ]]; then
    echo "#define PRODUCTION"                   >> SchemePreActions.h
fi

This way, by including SchemePreActions.h, my code can test to see if running under a specific scheme.

ishahak
  • 6,585
  • 5
  • 38
  • 56