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