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?

1

3 Answers 3

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.

3
  • Xcode 6.0.1 is out and this still hasn't been fixed. Everyone please file a bug report with Apple so they can see this needs attention.
    – kainjow
    Sep 23, 2014 at 19:26
  • 4
    still like this in 2020
    – Return-1
    Jan 15, 2020 at 12:20
  • 2
    and in 2022. Why does a failing prebuild mark the build as "succeeded"? Oct 20, 2022 at 14:27
5

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

3
  • 1
    you could also combine this with a bash "trap" as a one-liner (see tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html): trap "kill $PPID" ERR Feb 12, 2016 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 3, 2017 at 10:12
  • 1
    with I was able to confirm the pre-action is called
    – naz
    Aug 30, 2021 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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.