• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Helper to do build so you don't have to remember all the steps/args.
4
5set -eu
6
7# Some base locations.
8readonly ScriptDir=$(dirname "$(echo $0 | sed -e "s,^\([^/]\),$(pwd)/\1,")")
9readonly ProtoRootDir="${ScriptDir}/../.."
10readonly BazelFlags="${BAZEL_FLAGS:---announce_rc --macos_minimum_os=10.13}"
11
12# Invoke with BAZEL=bazelisk to use that instead.
13readonly BazelBin="${BAZEL:-bazel}"
14
15printUsage() {
16  NAME=$(basename "${0}")
17  cat << EOF
18usage: ${NAME} [OPTIONS]
19
20This script does the common build steps needed.
21
22OPTIONS:
23
24 General:
25
26   -h, --help
27         Show this message
28   -c, --clean
29         Issue a clean before the normal build.
30   --full-build
31         By default only protoc is built within protobuf, this option will
32         enable a full build/test of the entire protobuf project.
33   --skip-xcode
34         Skip the invoke of Xcode to test the runtime on both iOS and OS X.
35   --skip-xcode-ios
36         Skip the invoke of Xcode to test the runtime on iOS.
37   --skip-xcode-debug
38         Skip the Xcode Debug configuration.
39   --skip-xcode-release
40         Skip the Xcode Release configuration.
41   --skip-xcode-osx | --skip-xcode-macos
42         Skip the invoke of Xcode to test the runtime on OS X.
43   --skip-xcode-tvos
44         Skip the invoke of Xcode to test the runtime on tvOS.
45   --skip-objc-conformance
46         Skip the Objective C conformance tests (run on OS X).
47   --skip-xcpretty
48         By default, if xcpretty is installed, it will be used, this option will
49         skip it even it it is installed.
50   --xcode-quiet
51         Pass -quiet to xcodebuild.
52
53EOF
54}
55
56header() {
57  echo ""
58  echo "========================================================================"
59  echo "    ${@}"
60  echo "========================================================================"
61}
62
63xcodebuild_xcpretty() {
64  set -o pipefail && xcodebuild "${@}" | xcpretty
65}
66
67if hash xcpretty >/dev/null 2>&1 ; then
68  XCODEBUILD=xcodebuild_xcpretty
69else
70  XCODEBUILD=xcodebuild
71fi
72
73DO_CLEAN=no
74FULL_BUILD=no
75DO_XCODE_IOS_TESTS=yes
76DO_XCODE_OSX_TESTS=yes
77DO_XCODE_TVOS_TESTS=yes
78DO_XCODE_DEBUG=yes
79DO_XCODE_RELEASE=yes
80DO_OBJC_CONFORMANCE_TESTS=yes
81XCODE_QUIET=no
82while [[ $# != 0 ]]; do
83  case "${1}" in
84    -h | --help )
85      printUsage
86      exit 0
87      ;;
88    -c | --clean )
89      DO_CLEAN=yes
90      ;;
91    --full-build )
92      FULL_BUILD=yes
93      ;;
94    --skip-xcode )
95      DO_XCODE_IOS_TESTS=no
96      DO_XCODE_OSX_TESTS=no
97      DO_XCODE_TVOS_TESTS=no
98      ;;
99    --skip-xcode-ios )
100      DO_XCODE_IOS_TESTS=no
101      ;;
102    --skip-xcode-osx | --skip-xcode-macos)
103      DO_XCODE_OSX_TESTS=no
104      ;;
105    --skip-xcode-tvos )
106      DO_XCODE_TVOS_TESTS=no
107      ;;
108    --skip-xcode-debug )
109      DO_XCODE_DEBUG=no
110      ;;
111    --skip-xcode-release )
112      DO_XCODE_RELEASE=no
113      ;;
114    --skip-objc-conformance )
115      DO_OBJC_CONFORMANCE_TESTS=no
116      ;;
117    --skip-xcpretty )
118      XCODEBUILD=xcodebuild
119      ;;
120    --xcode-quiet )
121      XCODE_QUIET=yes
122      ;;
123    -*)
124      echo "ERROR: Unknown option: ${1}" 1>&2
125      printUsage
126      exit 1
127      ;;
128    *)
129      echo "ERROR: Unknown argument: ${1}" 1>&2
130      printUsage
131      exit 1
132      ;;
133  esac
134  shift
135done
136
137# Into the proto dir.
138cd "${ProtoRootDir}"
139
140if [[ "${DO_CLEAN}" == "yes" ]] ; then
141  header "Cleaning"
142  ${BazelBin} clean
143  if [[ "${DO_XCODE_IOS_TESTS}" == "yes" ]] ; then
144    XCODEBUILD_CLEAN_BASE_IOS=(
145      xcodebuild
146        -project objectivec/ProtocolBuffers_iOS.xcodeproj
147        -scheme ProtocolBuffers
148    )
149    if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then
150      "${XCODEBUILD_CLEAN_BASE_IOS[@]}" -configuration Debug clean
151    fi
152    if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then
153      "${XCODEBUILD_CLEAN_BASE_IOS[@]}" -configuration Release clean
154    fi
155  fi
156  if [[ "${DO_XCODE_OSX_TESTS}" == "yes" ]] ; then
157    XCODEBUILD_CLEAN_BASE_OSX=(
158      xcodebuild
159        -project objectivec/ProtocolBuffers_OSX.xcodeproj
160        -scheme ProtocolBuffers
161    )
162    if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then
163      "${XCODEBUILD_CLEAN_BASE_OSX[@]}" -configuration Debug clean
164    fi
165    if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then
166      "${XCODEBUILD_CLEAN_BASE_OSX[@]}" -configuration Release clean
167    fi
168  fi
169  if [[ "${DO_XCODE_TVOS_TESTS}" == "yes" ]] ; then
170    XCODEBUILD_CLEAN_BASE_OSX=(
171      xcodebuild
172        -project objectivec/ProtocolBuffers_tvOS.xcodeproj
173        -scheme ProtocolBuffers
174    )
175    if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then
176      "${XCODEBUILD_CLEAN_BASE_OSX[@]}" -configuration Debug clean
177    fi
178    if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then
179      "${XCODEBUILD_CLEAN_BASE_OSX[@]}" -configuration Release clean
180    fi
181  fi
182fi
183
184if [[ "${FULL_BUILD}" == "yes" ]] ; then
185  header "Build/Test: everything"
186  ${BazelBin} test //:protoc //:protobuf //src/... $BazelFlags
187else
188  header "Building: protoc"
189  ${BazelBin} build //:protoc $BazelFlags
190fi
191
192# Ensure the WKT sources checked in are current.
193objectivec/generate_well_known_types.sh --check-only
194
195header "Checking on the ObjC Runtime pddm expansions"
196${BazelBin} test //objectivec:sources_pddm_expansion_test $BazelFlags
197
198readonly XCODE_VERSION_LINE="$(xcodebuild -version | grep Xcode\  )"
199readonly XCODE_VERSION="${XCODE_VERSION_LINE/Xcode /}"  # drop the prefix.
200
201if [[ "${DO_XCODE_IOS_TESTS}" == "yes" ]] ; then
202  XCODEBUILD_TEST_BASE_IOS=(
203    "${XCODEBUILD}"
204      -project objectivec/ProtocolBuffers_iOS.xcodeproj
205      -scheme ProtocolBuffers
206  )
207  if [[ "${XCODE_QUIET}" == "yes" ]] ; then
208    XCODEBUILD_TEST_BASE_IOS+=( -quiet )
209  fi
210  # Don't need to worry about form factors or retina/non retina.
211  # NOTE: Different Xcode have different simulated hardware/os support.
212  case "${XCODE_VERSION}" in
213    [6-9].* | 1[0-2].* )
214      echo "ERROR: Xcode 13.3.1 or higher is required." 1>&2
215      exit 11
216      ;;
217    13.* | 14.*)
218      XCODEBUILD_TEST_BASE_IOS+=(
219          -destination "platform=iOS Simulator,name=iPhone 13,OS=latest"
220      )
221      ;;
222    * )
223      echo ""
224      echo "ATTENTION: Time to update the simulator targets for Xcode ${XCODE_VERSION}"
225      echo ""
226      echo "ERROR: Build aborted!"
227      exit 2
228      ;;
229  esac
230  if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then
231    header "Doing Xcode iOS build/tests - Debug"
232    "${XCODEBUILD_TEST_BASE_IOS[@]}" -configuration Debug test
233  fi
234  if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then
235    header "Doing Xcode iOS build/tests - Release"
236    "${XCODEBUILD_TEST_BASE_IOS[@]}" -configuration Release test
237  fi
238  # Don't leave the simulator in the developer's face.
239  killall Simulator 2> /dev/null || true
240fi
241if [[ "${DO_XCODE_OSX_TESTS}" == "yes" ]] ; then
242  XCODEBUILD_TEST_BASE_OSX=(
243    "${XCODEBUILD}"
244      -project objectivec/ProtocolBuffers_OSX.xcodeproj
245      -scheme ProtocolBuffers
246      -destination "platform=macOS"
247  )
248  if [[ "${XCODE_QUIET}" == "yes" ]] ; then
249    XCODEBUILD_TEST_BASE_OSX+=( -quiet )
250  fi
251  case "${XCODE_VERSION}" in
252    [6-9].* | 1[0-2].* )
253      echo "ERROR: Xcode 13.3.1 or higher is required." 1>&2
254      exit 11
255      ;;
256  esac
257  if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then
258    header "Doing Xcode OS X build/tests - Debug"
259    "${XCODEBUILD_TEST_BASE_OSX[@]}" -configuration Debug test
260  fi
261  if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then
262    header "Doing Xcode OS X build/tests - Release"
263    "${XCODEBUILD_TEST_BASE_OSX[@]}" -configuration Release test
264  fi
265fi
266if [[ "${DO_XCODE_TVOS_TESTS}" == "yes" ]] ; then
267  XCODEBUILD_TEST_BASE_TVOS=(
268    "${XCODEBUILD}"
269      -project objectivec/ProtocolBuffers_tvOS.xcodeproj
270      -scheme ProtocolBuffers
271  )
272  case "${XCODE_VERSION}" in
273    [6-9].* | 1[0-2].* )
274      echo "ERROR: Xcode 13.3.1 or higher is required." 1>&2
275      exit 11
276      ;;
277    13.* | 14.*)
278      XCODEBUILD_TEST_BASE_TVOS+=(
279        -destination "platform=tvOS Simulator,name=Apple TV 4K (2nd generation),OS=latest"
280      )
281      ;;
282    * )
283      echo ""
284      echo "ATTENTION: Time to update the simulator targets for Xcode ${XCODE_VERSION}"
285      echo ""
286      echo "ERROR: Build aborted!"
287      exit 2
288      ;;
289  esac
290  if [[ "${XCODE_QUIET}" == "yes" ]] ; then
291    XCODEBUILD_TEST_BASE_TVOS+=( -quiet )
292  fi
293  if [[ "${DO_XCODE_DEBUG}" == "yes" ]] ; then
294    header "Doing Xcode tvOS build/tests - Debug"
295    "${XCODEBUILD_TEST_BASE_TVOS[@]}" -configuration Debug test
296  fi
297  if [[ "${DO_XCODE_RELEASE}" == "yes" ]] ; then
298    header "Doing Xcode tvOS build/tests - Release"
299    "${XCODEBUILD_TEST_BASE_TVOS[@]}" -configuration Release test
300  fi
301fi
302
303if [[ "${DO_OBJC_CONFORMANCE_TESTS}" == "yes" ]] ; then
304  header "Running ObjC Conformance Tests"
305  ${BazelBin} test //objectivec:conformance_test $BazelFlags
306fi
307
308echo ""
309echo "$(basename "${0}"): Success!"
310