cibuild 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/bash
  2. export SCRIPT_DIR=$(dirname "$0")
  3. ##
  4. ## Configuration Variables
  5. ##
  6. SCHEMES="$@"
  7. config ()
  8. {
  9. # The workspace to build.
  10. #
  11. # If not set and no workspace is found, the -workspace flag will not be passed
  12. # to `xctool`.
  13. #
  14. # Only one of `XCWORKSPACE` and `XCODEPROJ` needs to be set. The former will
  15. # take precedence.
  16. : ${XCWORKSPACE=$(find_pattern "*.xcworkspace")}
  17. # The project to build.
  18. #
  19. # If not set and no project is found, the -project flag will not be passed
  20. # to `xctool`.
  21. #
  22. # Only one of `XCWORKSPACE` and `XCODEPROJ` needs to be set. The former will
  23. # take precedence.
  24. : ${XCODEPROJ=$(find_pattern "*.xcodeproj")}
  25. # A bootstrap script to run before building.
  26. #
  27. # If this file does not exist, it is not considered an error.
  28. : ${BOOTSTRAP="$SCRIPT_DIR/bootstrap"}
  29. # Extra options to pass to xctool.
  30. : ${XCTOOL_OPTIONS="RUN_CLANG_STATIC_ANALYZER=NO"}
  31. # A whitespace-separated list of default schemes to build.
  32. #
  33. # Individual names can be quoted to avoid word splitting.
  34. : ${SCHEMES:=$(xcodebuild -list -project "$XCODEPROJ" 2>/dev/null | awk -f "$SCRIPT_DIR/schemes.awk")}
  35. # A whitespace-separated list of executables that must be present and locatable.
  36. : ${REQUIRED_TOOLS="xctool"}
  37. export XCWORKSPACE
  38. export XCODEPROJ
  39. export BOOTSTRAP
  40. export XCTOOL_OPTIONS
  41. export SCHEMES
  42. export REQUIRED_TOOLS
  43. }
  44. ##
  45. ## Build Process
  46. ##
  47. main ()
  48. {
  49. config
  50. if [ -n "$REQUIRED_TOOLS" ]
  51. then
  52. echo "*** Checking dependencies..."
  53. check_deps
  54. fi
  55. if [ -f "$BOOTSTRAP" ]
  56. then
  57. echo "*** Bootstrapping..."
  58. "$BOOTSTRAP" || exit $?
  59. fi
  60. echo "*** The following schemes will be built:"
  61. echo "$SCHEMES" | xargs -n 1 echo " "
  62. echo
  63. echo "$SCHEMES" | xargs -n 1 | (
  64. local status=0
  65. while read scheme
  66. do
  67. build_scheme "$scheme" || status=1
  68. done
  69. exit $status
  70. )
  71. }
  72. check_deps ()
  73. {
  74. for tool in $REQUIRED_TOOLS
  75. do
  76. which -s "$tool"
  77. if [ "$?" -ne "0" ]
  78. then
  79. echo "*** Error: $tool not found. Please install it and cibuild again."
  80. exit 1
  81. fi
  82. done
  83. }
  84. find_pattern ()
  85. {
  86. ls -d $1 2>/dev/null | head -n 1
  87. }
  88. run_xctool ()
  89. {
  90. if [ -n "$XCWORKSPACE" ]
  91. then
  92. xctool -workspace "$XCWORKSPACE" $XCTOOL_OPTIONS "$@" 2>&1
  93. elif [ -n "$XCODEPROJ" ]
  94. then
  95. xctool -project "$XCODEPROJ" $XCTOOL_OPTIONS "$@" 2>&1
  96. else
  97. echo "*** No workspace or project file found."
  98. exit 1
  99. fi
  100. }
  101. parse_build ()
  102. {
  103. awk -f "$SCRIPT_DIR/xctool.awk" 2>&1 >/dev/null
  104. }
  105. build_scheme ()
  106. {
  107. local scheme=$1
  108. echo "*** Building and testing $scheme..."
  109. echo
  110. local sdkflag=
  111. local action=test
  112. # Determine whether we can run unit tests for this target.
  113. run_xctool -scheme "$scheme" run-tests | parse_build
  114. local awkstatus=$?
  115. if [ "$awkstatus" -eq "1" ]
  116. then
  117. # SDK not found, try for iphonesimulator.
  118. sdkflag="-sdk iphonesimulator"
  119. # Determine whether the unit tests will run with iphonesimulator
  120. run_xctool $sdkflag -scheme "$scheme" run-tests | parse_build
  121. awkstatus=$?
  122. if [ "$awkstatus" -ne "0" ]
  123. then
  124. # Unit tests will not run on iphonesimulator.
  125. sdkflag=""
  126. fi
  127. fi
  128. if [ "$awkstatus" -ne "0" ]
  129. then
  130. # Unit tests aren't supported.
  131. action=build
  132. fi
  133. run_xctool $sdkflag -scheme "$scheme" $action
  134. }
  135. export -f build_scheme
  136. export -f run_xctool
  137. export -f parse_build
  138. main