build.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #!/usr/bin/env bash
  2. # Copyright 2018 Google
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # USAGE: build.sh product [platform] [method]
  16. #
  17. # Builds the given product for the given platform using the given build method
  18. set -euo pipefail
  19. if [[ $# -lt 1 ]]; then
  20. cat 1>&2 <<EOF
  21. USAGE: $0 product [platform] [method]
  22. product can be one of:
  23. Firebase
  24. Firestore
  25. InAppMessaging
  26. SymbolCollision
  27. platform can be one of:
  28. iOS (default)
  29. macOS
  30. tvOS
  31. method can be one of:
  32. xcodebuild (default)
  33. cmake
  34. Optionally, reads the environment variable SANITIZERS. If set, it is expected to
  35. be a string containing a space-separated list with some of the following
  36. elements:
  37. asan
  38. tsan
  39. ubsan
  40. EOF
  41. exit 1
  42. fi
  43. product="$1"
  44. platform="iOS"
  45. if [[ $# -gt 1 ]]; then
  46. platform="$2"
  47. fi
  48. method="xcodebuild"
  49. if [[ $# -gt 2 ]]; then
  50. method="$3"
  51. fi
  52. echo "Building $product for $platform using $method"
  53. if [[ -n "${SANITIZERS:-}" ]]; then
  54. echo "Using sanitizers: $SANITIZERS"
  55. fi
  56. scripts_dir=$(dirname "${BASH_SOURCE[0]}")
  57. firestore_emulator="${scripts_dir}/run_firestore_emulator.sh"
  58. # Runs xcodebuild with the given flags, piping output to xcpretty
  59. # If xcodebuild fails with known error codes, retries once.
  60. function RunXcodebuild() {
  61. echo xcodebuild "$@"
  62. xcodebuild "$@" | xcpretty; result=$?
  63. if [[ $result == 65 ]]; then
  64. echo "xcodebuild exited with 65, retrying" 1>&2
  65. sleep 5
  66. xcodebuild "$@" | xcpretty; result=$?
  67. fi
  68. if [[ $result != 0 ]]; then
  69. exit $result
  70. fi
  71. }
  72. ios_flags=(
  73. -sdk 'iphonesimulator'
  74. -destination 'platform=iOS Simulator,name=iPhone 7'
  75. )
  76. macos_flags=(
  77. -sdk 'macosx'
  78. -destination 'platform=OS X,arch=x86_64'
  79. )
  80. tvos_flags=(
  81. -sdk "appletvsimulator"
  82. -destination 'platform=tvOS Simulator,name=Apple TV'
  83. )
  84. # Compute standard flags for all platforms
  85. case "$platform" in
  86. iOS)
  87. xcb_flags=("${ios_flags[@]}")
  88. ;;
  89. macOS)
  90. xcb_flags=("${macos_flags[@]}")
  91. ;;
  92. tvOS)
  93. xcb_flags=("${tvos_flags[@]}")
  94. ;;
  95. all)
  96. xcb_flags=()
  97. ;;
  98. *)
  99. echo "Unknown platform '$platform'" 1>&2
  100. exit 1
  101. ;;
  102. esac
  103. xcb_flags+=(
  104. ONLY_ACTIVE_ARCH=YES
  105. CODE_SIGNING_REQUIRED=NO
  106. CODE_SIGNING_ALLOWED=YES
  107. COMPILER_INDEX_STORE_ENABLE=NO
  108. )
  109. # TODO(varconst): Add --warn-unused-vars and --warn-uninitialized.
  110. # Right now, it makes the log overflow on Travis because many of our
  111. # dependencies don't build cleanly this way.
  112. cmake_options=(
  113. -Wdeprecated
  114. )
  115. xcode_version=$(xcodebuild -version | head -n 1)
  116. xcode_version="${xcode_version/Xcode /}"
  117. xcode_major="${xcode_version/.*/}"
  118. if [[ -n "${SANITIZERS:-}" ]]; then
  119. for sanitizer in $SANITIZERS; do
  120. case "$sanitizer" in
  121. asan)
  122. xcb_flags+=(
  123. -enableAddressSanitizer YES
  124. )
  125. cmake_options+=(
  126. -DWITH_ASAN=ON
  127. )
  128. ;;
  129. tsan)
  130. xcb_flags+=(
  131. -enableThreadSanitizer YES
  132. )
  133. cmake_options+=(
  134. -DWITH_TSAN=ON
  135. )
  136. ;;
  137. ubsan)
  138. xcb_flags+=(
  139. -enableUndefinedBehaviorSanitizer YES
  140. )
  141. cmake_options+=(
  142. -DWITH_UBSAN=ON
  143. )
  144. ;;
  145. *)
  146. echo "Unknown sanitizer '$sanitizer'" 1>&2
  147. exit 1
  148. ;;
  149. esac
  150. done
  151. fi
  152. case "$product-$method-$platform" in
  153. Firebase-xcodebuild-*)
  154. # Coverage collection often cause retries to fail because of partial
  155. # pre-existing data.
  156. # TODO(paulb777): Find a less blunt solution to this.
  157. rm -rf ~/Library/Developer/Xcode/DerivedData
  158. RunXcodebuild \
  159. -workspace 'Example/Firebase.xcworkspace' \
  160. -scheme "AllUnitTests_$platform" \
  161. "${xcb_flags[@]}" \
  162. build \
  163. test
  164. RunXcodebuild \
  165. -workspace 'GoogleUtilities/Example/GoogleUtilities.xcworkspace' \
  166. -scheme "Example_$platform" \
  167. "${xcb_flags[@]}" \
  168. build \
  169. test
  170. if [[ $platform == 'iOS' ]]; then
  171. # Code Coverage collection is only working on iOS currently.
  172. ./scripts/collect_metrics.sh 'Example/Firebase.xcworkspace' "AllUnitTests_$platform"
  173. # Test iOS Objective-C static library build
  174. cd Example
  175. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  176. pod update --no-repo-update
  177. cd ..
  178. RunXcodebuild \
  179. -workspace 'Example/Firebase.xcworkspace' \
  180. -scheme "AllUnitTests_$platform" \
  181. "${xcb_flags[@]}" \
  182. build \
  183. test
  184. fi
  185. ;;
  186. Auth-xcodebuild-*)
  187. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  188. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  189. RunXcodebuild \
  190. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  191. -scheme "Auth_ApiTests" \
  192. "${xcb_flags[@]}" \
  193. build \
  194. test
  195. RunXcodebuild \
  196. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  197. -scheme "Auth_E2eTests" \
  198. "${xcb_flags[@]}" \
  199. build \
  200. test
  201. fi
  202. ;;
  203. InAppMessaging-xcodebuild-iOS)
  204. RunXcodebuild \
  205. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  206. -scheme 'InAppMessaging_Example_iOS' \
  207. "${xcb_flags[@]}" \
  208. build \
  209. test
  210. cd InAppMessaging/Example
  211. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  212. pod update --no-repo-update
  213. cd ../..
  214. RunXcodebuild \
  215. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  216. -scheme 'InAppMessaging_Example_iOS' \
  217. "${xcb_flags[@]}" \
  218. build \
  219. test
  220. # Run UI tests on both iPad and iPhone simulators
  221. # TODO: Running two destinations from one xcodebuild command stopped working with Xcode 10.
  222. # Consider separating static library tests to a separate job.
  223. RunXcodebuild \
  224. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  225. -scheme 'FiamDisplaySwiftExample' \
  226. "${xcb_flags[@]}" \
  227. build \
  228. test
  229. RunXcodebuild \
  230. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  231. -scheme 'FiamDisplaySwiftExample' \
  232. -sdk 'iphonesimulator' \
  233. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  234. build \
  235. test
  236. cd InAppMessagingDisplay/Example
  237. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  238. pod update --no-repo-update
  239. cd ../..
  240. # Run UI tests on both iPad and iPhone simulators
  241. RunXcodebuild \
  242. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  243. -scheme 'FiamDisplaySwiftExample' \
  244. "${xcb_flags[@]}" \
  245. build \
  246. test
  247. RunXcodebuild \
  248. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  249. -scheme 'FiamDisplaySwiftExample' \
  250. -sdk 'iphonesimulator' \
  251. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  252. build \
  253. test
  254. ;;
  255. Firestore-xcodebuild-*)
  256. "${firestore_emulator}" start
  257. trap '"${firestore_emulator}" stop' ERR EXIT
  258. if [[ "$xcode_major" -lt 9 ]]; then
  259. # When building and testing for Xcode 8, only test unit tests.
  260. RunXcodebuild \
  261. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  262. -scheme "Firestore_Tests_$platform" \
  263. "${xcb_flags[@]}" \
  264. build \
  265. test
  266. else
  267. # IntegrationTests run all the tests, including Swift tests, which
  268. # require Swift 4.0 and Xcode 9+.
  269. RunXcodebuild \
  270. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  271. -scheme "Firestore_IntegrationTests_$platform" \
  272. "${xcb_flags[@]}" \
  273. build \
  274. test
  275. fi
  276. ;;
  277. Firestore-cmake-macOS)
  278. test -d build || mkdir build
  279. echo "Preparing cmake build ..."
  280. (cd build; cmake "${cmake_options[@]}" ..)
  281. echo "Building cmake build ..."
  282. cpus=$(sysctl -n hw.ncpu)
  283. (cd build; env make -j $cpus all generate_protos)
  284. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  285. ;;
  286. SymbolCollision-xcodebuild-*)
  287. RunXcodebuild \
  288. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  289. -scheme "SymbolCollisionTest" \
  290. "${xcb_flags[@]}" \
  291. build
  292. ;;
  293. GoogleDataTransport-xcodebuild-*)
  294. RunXcodebuild \
  295. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  296. -scheme "GoogleDataTransport-$platform-Unit-Tests-Unit" \
  297. "${xcb_flags[@]}" \
  298. build \
  299. test
  300. RunXcodebuild \
  301. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  302. -scheme "GoogleDataTransport-$platform-Unit-Tests-Lifecycle" \
  303. "${xcb_flags[@]}" \
  304. build \
  305. test
  306. ;;
  307. GoogleDataTransportIntegrationTest-xcodebuild-*)
  308. RunXcodebuild \
  309. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  310. -scheme "GoogleDataTransport-$platform-Unit-Tests-Integration" \
  311. "${xcb_flags[@]}" \
  312. build \
  313. test
  314. ;;
  315. GoogleDataTransportCCTSupport-xcodebuild-*)
  316. RunXcodebuild \
  317. -workspace 'gen/GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport.xcworkspace' \
  318. -scheme "GoogleDataTransportCCTSupport-$platform-Unit-Tests-Unit" \
  319. "${xcb_flags[@]}" \
  320. build \
  321. test
  322. RunXcodebuild \
  323. -workspace 'gen/GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport.xcworkspace' \
  324. -scheme "GoogleDataTransportCCTSupport-$platform-Unit-Tests-Integration" \
  325. "${xcb_flags[@]}" \
  326. build \
  327. test
  328. ;;
  329. Database-xcodebuild-*)
  330. RunXcodebuild \
  331. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  332. -scheme "FirebaseDatabase-iOS-Unit-unit" \
  333. "${ios_flags[@]}" \
  334. "${xcb_flags[@]}" \
  335. build \
  336. test
  337. RunXcodebuild \
  338. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  339. -scheme "FirebaseDatabase-macOS-Unit-unit" \
  340. "${macos_flags[@]}" \
  341. "${xcb_flags[@]}" \
  342. build \
  343. test
  344. RunXcodebuild \
  345. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  346. -scheme "FirebaseDatabase-tvOS-Unit-unit" \
  347. "${tvos_flags[@]}" \
  348. "${xcb_flags[@]}" \
  349. build \
  350. test
  351. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  352. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  353. # Integration tests are only run on iOS to minimize flake failures.
  354. RunXcodebuild \
  355. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  356. -scheme "FirebaseDatabase-iOS-Unit-integration" \
  357. "${ios_flags[@]}" \
  358. "${xcb_flags[@]}" \
  359. build \
  360. test
  361. fi
  362. ;;
  363. Messaging-xcodebuild-*)
  364. RunXcodebuild \
  365. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  366. -scheme "FirebaseMessaging-iOS-Unit-unit" \
  367. "${ios_flags[@]}" \
  368. "${xcb_flags[@]}" \
  369. build \
  370. test
  371. RunXcodebuild \
  372. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  373. -scheme "FirebaseMessaging-tvOS-Unit-unit" \
  374. "${tvos_flags[@]}" \
  375. "${xcb_flags[@]}" \
  376. build \
  377. test
  378. ;;
  379. Storage-xcodebuild-*)
  380. RunXcodebuild \
  381. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  382. -scheme "FirebaseStorage-iOS-Unit-unit" \
  383. "${ios_flags[@]}" \
  384. "${xcb_flags[@]}" \
  385. build \
  386. test
  387. RunXcodebuild \
  388. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  389. -scheme "FirebaseStorage-macOS-Unit-unit" \
  390. "${macos_flags[@]}" \
  391. "${xcb_flags[@]}" \
  392. build \
  393. test
  394. RunXcodebuild \
  395. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  396. -scheme "FirebaseStorage-tvOS-Unit-unit" \
  397. "${tvos_flags[@]}" \
  398. "${xcb_flags[@]}" \
  399. build \
  400. test
  401. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  402. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  403. # Integration tests are only run on iOS to minimize flake failures.
  404. RunXcodebuild \
  405. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  406. -scheme "FirebaseStorage-iOS-Unit-integration" \
  407. "${ios_flags[@]}" \
  408. "${xcb_flags[@]}" \
  409. build \
  410. test
  411. fi
  412. ;;
  413. *)
  414. echo "Don't know how to build this product-platform-method combination" 1>&2
  415. echo " product=$product" 1>&2
  416. echo " platform=$platform" 1>&2
  417. echo " method=$method" 1>&2
  418. exit 1
  419. ;;
  420. esac