7

Using Xcodebuild directly from bash, how would one detect a failure? the exit code is always 0 regardless. I realize there's an "FAILED" or "SUCCEEDED" textural output, but isn't there a more elegant way?

Also, I sometimes use "make" (especially with my Qt based builds). Is there a way to detect make has failed from the build script?

JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • Have you looked at http://stackoverflow.com/questions/7363459/how-to-get-the-return-value-of-xcodebuild/10808467#10808467 ? – Hai Vu Jan 23 '13 at 16:44
  • 1
    so xcodebuild .... || exit 1 would exit the script with exit code 1 upon failure? isn't that dependent on xcodebuild actually giving out an exit code >0 when failing? i thought it doesn't do it. – JasonGenX Jan 23 '13 at 18:46

3 Answers3

6

xcodebuild always returns 0 even when the build fails. To detect errors, you could use a script like this:

build_errors_file=build_errors.log

# Pipe errors to file
xcodebuild 2>$build_errors_file

errors=`grep -wc "The following build commands failed" $build_errors_file`
if [ "$errors" != "0" ]
then
    echo "BUILD FAILED. Error Log:"
    cat $build_errors_file
    rm $build_errors_file
    exit 1
fi
rm $build_errors_file

# ... continue

I verified that ** BUILD FAILED ** will not be printed when running xcodebuild with the archive option, so it seems that the string to look for is 'The following build commands failed'.

bizz84
  • 1,964
  • 21
  • 34
  • thanks for this. I also tried the word count on just the string "error" and it seems to be sufficient. (unless all your classes or something have the word "error" on it) – kevinl Aug 22 '14 at 18:11
1

Maybe it depends on the version of Xcode or possibly how it fails. This is what I get when I check $? after some include files can't be found:

davidb@DavidBs-Mobile-Macintosh:~/Source/icanvas/iCanvas-project (release/icanvas-1.9 %) $ xcodebuild
...
/Users/davidb/Source/icanvas/iCanvas-project/iCanvas-target/iPhone/ConversationViewController.m:21:9: fatal error: 'CanvasKit/CKActionSheetWithBlocks.h' file not found
#import "CanvasKit/CKActionSheetWithBlocks.h"
        ^
1 error generated.
...
** BUILD FAILED **
...
(5 failures)
davidb@DavidBs-Mobile-Macintosh:~/Source/icanvas/iCanvas-project (release/icanvas-1.9 %) $ echo $?
65
devguydavid
  • 3,917
  • 1
  • 19
  • 18
0

The exit code is not always 0.

I assume you were running the xcodebuild from a shell script.

You may try put #!/bin/bash -e ahead of your script file.

https://stackoverflow.com/a/40808843/2705627

Community
  • 1
  • 1
partyspy
  • 31
  • 4