build.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. function pod_gen() {
  19. # Call pod gen with a podspec and additional optional arguments.
  20. bundle exec pod gen --local-sources=./ --sources=https://github.com/firebase/SpecsStaging.git,https://cdn.cocoapods.org/ "$@"
  21. }
  22. set -euo pipefail
  23. if [[ $# -lt 1 ]]; then
  24. cat 1>&2 <<EOF
  25. USAGE: $0 product [platform] [method]
  26. product can be one of:
  27. Firebase
  28. Firestore
  29. InAppMessaging
  30. Messaging
  31. MessagingSample
  32. RemoteConfig
  33. Storage
  34. StorageSwift
  35. SymbolCollision
  36. GoogleDataTransport
  37. GoogleDataTransportCCTSupport
  38. platform can be one of:
  39. iOS (default)
  40. macOS
  41. tvOS
  42. watchOS
  43. method can be one of:
  44. xcodebuild (default)
  45. cmake
  46. unit
  47. integration
  48. Optionally, reads the environment variable SANITIZERS. If set, it is expected to
  49. be a string containing a space-separated list with some of the following
  50. elements:
  51. asan
  52. tsan
  53. ubsan
  54. EOF
  55. exit 1
  56. fi
  57. product="$1"
  58. platform="iOS"
  59. if [[ $# -gt 1 ]]; then
  60. platform="$2"
  61. fi
  62. method="xcodebuild"
  63. if [[ $# -gt 2 ]]; then
  64. method="$3"
  65. fi
  66. echo "Building $product for $platform using $method"
  67. if [[ -n "${SANITIZERS:-}" ]]; then
  68. echo "Using sanitizers: $SANITIZERS"
  69. fi
  70. scripts_dir=$(dirname "${BASH_SOURCE[0]}")
  71. firestore_emulator="${scripts_dir}/run_firestore_emulator.sh"
  72. system=$(uname -s)
  73. case "$system" in
  74. Darwin)
  75. xcode_version=$(xcodebuild -version | head -n 1)
  76. xcode_version="${xcode_version/Xcode /}"
  77. xcode_major="${xcode_version/.*/}"
  78. ;;
  79. *)
  80. xcode_major="0"
  81. ;;
  82. esac
  83. # Source function to check if CI secrets are available.
  84. source scripts/check_secrets.sh
  85. # Runs xcodebuild with the given flags, piping output to xcpretty
  86. # If xcodebuild fails with known error codes, retries once.
  87. function RunXcodebuild() {
  88. echo xcodebuild "$@"
  89. xcpretty_cmd=(xcpretty)
  90. if [[ -n "${TRAVIS:-}" ]]; then
  91. # The formatter argument takes a file location of a formatter.
  92. # The xcpretty-travis-formatter binary prints its location on stdout.
  93. xcpretty_cmd+=(-f $(xcpretty-travis-formatter))
  94. fi
  95. result=0
  96. xcodebuild "$@" | tee xcodebuild.log | "${xcpretty_cmd[@]}" || result=$?
  97. if [[ $result == 65 ]]; then
  98. ExportLogs "$@"
  99. echo "xcodebuild exited with 65, retrying" 1>&2
  100. sleep 5
  101. result=0
  102. xcodebuild "$@" | tee xcodebuild.log | "${xcpretty_cmd[@]}" || result=$?
  103. fi
  104. if [[ $result != 0 ]]; then
  105. echo "xcodebuild exited with $result; raw log follows" 1>&2
  106. OpenFold Raw log
  107. cat xcodebuild.log
  108. CloseFold
  109. ExportLogs "$@"
  110. return $result
  111. fi
  112. }
  113. # Exports any logs output captured in the xcresult
  114. function ExportLogs() {
  115. OpenFold XCResult
  116. exporter="${scripts_dir}/xcresult_logs.py"
  117. python "$exporter" "$@"
  118. CloseFold
  119. }
  120. current_group=none
  121. current_fold=0
  122. # Prints a command for CI environments to group log output in the logs
  123. # presentation UI.
  124. function OpenFold() {
  125. description="$*"
  126. current_group="$(echo "$description" | tr '[A-Z] ' '[a-z]_')"
  127. if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
  128. echo "::group::description"
  129. elif [[ -n "${TRAVIS:-}" ]]; then
  130. # Travis wants groups to be numbered.
  131. current_group="${current_group}.${current_fold}"
  132. let current_fold++
  133. # Show description in yellow.
  134. echo "travis_fold:start:${current_group}\033[33;1m${description}\033[0m"
  135. else
  136. echo "===== $description Start ====="
  137. fi
  138. }
  139. # Closes the current fold opened by `OpenFold`.
  140. function CloseFold() {
  141. if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
  142. echo "::endgroup::"
  143. elif [[ -n "${TRAVIS:-}" ]]; then
  144. echo "travis_fold:end:${current_group}"
  145. else
  146. echo "===== $description End ====="
  147. fi
  148. }
  149. if [[ "$xcode_major" -lt 11 ]]; then
  150. ios_flags=(
  151. -sdk 'iphonesimulator'
  152. -destination 'platform=iOS Simulator,name=iPhone 7'
  153. )
  154. else
  155. ios_flags=(
  156. -sdk 'iphonesimulator'
  157. -destination 'platform=iOS Simulator,name=iPhone 11'
  158. )
  159. fi
  160. ipad_flags=(
  161. -sdk 'iphonesimulator'
  162. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)'
  163. )
  164. macos_flags=(
  165. -sdk 'macosx'
  166. -destination 'platform=OS X,arch=x86_64'
  167. )
  168. tvos_flags=(
  169. -sdk "appletvsimulator"
  170. -destination 'platform=tvOS Simulator,name=Apple TV'
  171. )
  172. watchos_flags=(
  173. -destination 'platform=iOS Simulator,name=iPhone 11 Pro'
  174. )
  175. # Compute standard flags for all platforms
  176. case "$platform" in
  177. iOS)
  178. xcb_flags=("${ios_flags[@]}")
  179. gen_platform=ios
  180. ;;
  181. iPad)
  182. xcb_flags=("${ipad_flags[@]}")
  183. ;;
  184. macOS)
  185. xcb_flags=("${macos_flags[@]}")
  186. gen_platform=macos
  187. ;;
  188. tvOS)
  189. xcb_flags=("${tvos_flags[@]}")
  190. gen_platform=tvos
  191. ;;
  192. watchOS)
  193. xcb_flags=("${watchos_flags[@]}")
  194. ;;
  195. all)
  196. xcb_flags=()
  197. ;;
  198. Linux)
  199. xcb_flags=()
  200. ;;
  201. *)
  202. echo "Unknown platform '$platform'" 1>&2
  203. exit 1
  204. ;;
  205. esac
  206. xcb_flags+=(
  207. ONLY_ACTIVE_ARCH=YES
  208. CODE_SIGNING_REQUIRED=NO
  209. CODE_SIGNING_ALLOWED=YES
  210. COMPILER_INDEX_STORE_ENABLE=NO
  211. )
  212. # TODO(varconst): Add --warn-unused-vars and --warn-uninitialized.
  213. # Right now, it makes the log overflow on Travis because many of our
  214. # dependencies don't build cleanly this way.
  215. cmake_options=(
  216. -Wdeprecated
  217. -DCMAKE_BUILD_TYPE=Debug
  218. )
  219. if [[ -n "${SANITIZERS:-}" ]]; then
  220. for sanitizer in $SANITIZERS; do
  221. case "$sanitizer" in
  222. asan)
  223. xcb_flags+=(
  224. -enableAddressSanitizer YES
  225. )
  226. cmake_options+=(
  227. -DWITH_ASAN=ON
  228. )
  229. ;;
  230. tsan)
  231. xcb_flags+=(
  232. -enableThreadSanitizer YES
  233. )
  234. cmake_options+=(
  235. -DWITH_TSAN=ON
  236. )
  237. ;;
  238. ubsan)
  239. xcb_flags+=(
  240. -enableUndefinedBehaviorSanitizer YES
  241. )
  242. cmake_options+=(
  243. -DWITH_UBSAN=ON
  244. )
  245. ;;
  246. *)
  247. echo "Unknown sanitizer '$sanitizer'" 1>&2
  248. exit 1
  249. ;;
  250. esac
  251. done
  252. fi
  253. case "$product-$platform-$method" in
  254. FirebasePod-iOS-*)
  255. RunXcodebuild \
  256. -workspace 'CoreOnly/Tests/FirebasePodTest/FirebasePodTest.xcworkspace' \
  257. -scheme "FirebasePodTest" \
  258. "${xcb_flags[@]}" \
  259. build
  260. ;;
  261. Auth-*-xcodebuild)
  262. if check_secrets; then
  263. RunXcodebuild \
  264. -workspace 'FirebaseAuth/Tests/Sample/AuthSample.xcworkspace' \
  265. -scheme "Auth_ApiTests" \
  266. "${xcb_flags[@]}" \
  267. build \
  268. test
  269. RunXcodebuild \
  270. -workspace 'FirebaseAuth/Tests/Sample/AuthSample.xcworkspace' \
  271. -scheme "SwiftApiTests" \
  272. "${xcb_flags[@]}" \
  273. build \
  274. test
  275. fi
  276. ;;
  277. InAppMessaging-*-xcodebuild)
  278. RunXcodebuild \
  279. -workspace 'FirebaseInAppMessaging/Tests/Integration/DefaultUITestApp/InAppMessagingDisplay-Sample.xcworkspace' \
  280. -scheme 'FiamDisplaySwiftExample' \
  281. "${xcb_flags[@]}" \
  282. build \
  283. test
  284. ;;
  285. Firestore-*-xcodebuild)
  286. "${firestore_emulator}" start
  287. trap '"${firestore_emulator}" stop' ERR EXIT
  288. RunXcodebuild \
  289. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  290. -scheme "Firestore_IntegrationTests_$platform" \
  291. "${xcb_flags[@]}" \
  292. build \
  293. test
  294. ;;
  295. Firestore-macOS-cmake | Firestore-Linux-cmake)
  296. "${firestore_emulator}" start
  297. trap '"${firestore_emulator}" stop' ERR EXIT
  298. (
  299. test -d build || mkdir build
  300. cd build
  301. echo "Preparing cmake build ..."
  302. cmake -G Ninja "${cmake_options[@]}" ..
  303. echo "Building cmake build ..."
  304. ninja -k 10 all
  305. ctest --output-on-failure
  306. )
  307. ;;
  308. SymbolCollision-*-*)
  309. RunXcodebuild \
  310. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  311. -scheme "SymbolCollisionTest" \
  312. "${xcb_flags[@]}" \
  313. build
  314. ;;
  315. Messaging-*-xcodebuild)
  316. pod_gen FirebaseMessaging.podspec --platforms=ios
  317. RunXcodebuild \
  318. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  319. -scheme "FirebaseMessaging-Unit-unit" \
  320. "${ios_flags[@]}" \
  321. "${xcb_flags[@]}" \
  322. build \
  323. test
  324. if check_secrets; then
  325. # Integration tests are only run on iOS to minimize flake failures.
  326. RunXcodebuild \
  327. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  328. -scheme "FirebaseMessaging-Unit-integration" \
  329. "${ios_flags[@]}" \
  330. "${xcb_flags[@]}" \
  331. build \
  332. test
  333. fi
  334. pod_gen FirebaseMessaging.podspec --platforms=macos --clean
  335. RunXcodebuild \
  336. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  337. -scheme "FirebaseMessaging-Unit-unit" \
  338. "${macos_flags[@]}" \
  339. "${xcb_flags[@]}" \
  340. build \
  341. test
  342. pod_gen FirebaseMessaging.podspec --platforms=tvos --clean
  343. RunXcodebuild \
  344. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  345. -scheme "FirebaseMessaging-Unit-unit" \
  346. "${tvos_flags[@]}" \
  347. "${xcb_flags[@]}" \
  348. build \
  349. test
  350. ;;
  351. MessagingSample-*-*)
  352. if check_secrets; then
  353. RunXcodebuild \
  354. -workspace 'FirebaseMessaging/Apps/Sample/Sample.xcworkspace' \
  355. -scheme "Sample" \
  356. "${xcb_flags[@]}" \
  357. build
  358. fi
  359. ;;
  360. Database-*-xcodebuild)
  361. pod_gen FirebaseDatabase.podspec --platforms=ios
  362. RunXcodebuild \
  363. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  364. -scheme "FirebaseDatabase-Unit-unit" \
  365. "${ios_flags[@]}" \
  366. "${xcb_flags[@]}" \
  367. build \
  368. test
  369. if check_secrets; then
  370. # Integration tests are only run on iOS to minimize flake failures.
  371. RunXcodebuild \
  372. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  373. -scheme "FirebaseDatabase-Unit-integration" \
  374. "${ios_flags[@]}" \
  375. "${xcb_flags[@]}" \
  376. build \
  377. test
  378. fi
  379. pod_gen FirebaseDatabase.podspec --platforms=macos --clean
  380. RunXcodebuild \
  381. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  382. -scheme "FirebaseDatabase-Unit-unit" \
  383. "${macos_flags[@]}" \
  384. "${xcb_flags[@]}" \
  385. build \
  386. test
  387. pod_gen FirebaseDatabase.podspec --platforms=tvos --clean
  388. RunXcodebuild \
  389. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  390. -scheme "FirebaseDatabase-Unit-unit" \
  391. "${tvos_flags[@]}" \
  392. "${xcb_flags[@]}" \
  393. build \
  394. test
  395. ;;
  396. RemoteConfig-*-unit)
  397. pod_gen FirebaseRemoteConfig.podspec --platforms="${gen_platform}"
  398. RunXcodebuild \
  399. -workspace 'gen/FirebaseRemoteConfig/FirebaseRemoteConfig.xcworkspace' \
  400. -scheme "FirebaseRemoteConfig-Unit-unit" \
  401. "${xcb_flags[@]}" \
  402. build \
  403. test
  404. ;;
  405. RemoteConfig-*-integration)
  406. pod_gen FirebaseRemoteConfig.podspec --platforms="${gen_platform}"
  407. RunXcodebuild \
  408. -workspace 'gen/FirebaseRemoteConfig/FirebaseRemoteConfig.xcworkspace' \
  409. -scheme "FirebaseRemoteConfig-Unit-swift-api" \
  410. "${xcb_flags[@]}" \
  411. build \
  412. test
  413. ;;
  414. Storage-*-xcodebuild)
  415. pod_gen FirebaseStorage.podspec --platforms=ios
  416. RunXcodebuild \
  417. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  418. -scheme "FirebaseStorage-Unit-unit" \
  419. "${ios_flags[@]}" \
  420. "${xcb_flags[@]}" \
  421. build \
  422. test
  423. if check_secrets; then
  424. # Integration tests are only run on iOS to minimize flake failures.
  425. RunXcodebuild \
  426. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  427. -scheme "FirebaseStorage-Unit-integration" \
  428. "${ios_flags[@]}" \
  429. "${xcb_flags[@]}" \
  430. build \
  431. test
  432. RunXcodebuild \
  433. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  434. -scheme "FirebaseStorage-Unit-swift-integration" \
  435. "${ios_flags[@]}" \
  436. "${xcb_flags[@]}" \
  437. build \
  438. test
  439. fi
  440. pod_gen FirebaseStorage.podspec --platforms=macos --clean
  441. RunXcodebuild \
  442. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  443. -scheme "FirebaseStorage-Unit-unit" \
  444. "${macos_flags[@]}" \
  445. "${xcb_flags[@]}" \
  446. build \
  447. test
  448. pod_gen FirebaseStorage.podspec --platforms=tvos --clean
  449. RunXcodebuild \
  450. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  451. -scheme "FirebaseStorage-Unit-unit" \
  452. "${tvos_flags[@]}" \
  453. "${xcb_flags[@]}" \
  454. build \
  455. test
  456. ;;
  457. StorageSwift-*-xcodebuild)
  458. pod_gen FirebaseStorageSwift.podspec --platforms=ios
  459. if check_secrets; then
  460. # Integration tests are only run on iOS to minimize flake failures.
  461. RunXcodebuild \
  462. -workspace 'gen/FirebaseStorageSwift/FirebaseStorageSwift.xcworkspace' \
  463. -scheme "FirebaseStorageSwift-Unit-integration" \
  464. "${ios_flags[@]}" \
  465. "${xcb_flags[@]}" \
  466. build \
  467. test
  468. fi
  469. ;;
  470. GoogleDataTransport-watchOS-xcodebuild)
  471. RunXcodebuild \
  472. -workspace 'GoogleDataTransport/GDTWatchOSTestApp/GDTWatchOSTestApp.xcworkspace' \
  473. -scheme "GDTWatchOSTestAppWatchKitApp" \
  474. "${xcb_flags[@]}" \
  475. build
  476. ;;
  477. GoogleDataTransportCCTSupport-watchOS-xcodebuild)
  478. RunXcodebuild \
  479. -workspace 'GoogleDataTransportCCTSupport/GDTCCTWatchOSTestApp/GDTCCTWatchOSTestApp.xcworkspace' \
  480. -scheme "GDTCCTWatchOSIndependentTestAppWatchKitApp" \
  481. "${xcb_flags[@]}" \
  482. build
  483. RunXcodebuild \
  484. -workspace 'GoogleDataTransportCCTSupport/GDTCCTWatchOSTestApp/GDTCCTWatchOSTestApp.xcworkspace' \
  485. -scheme "GDTCCTWatchOSCompanionTestApp" \
  486. "${xcb_flags[@]}" \
  487. build
  488. ;;
  489. *)
  490. echo "Don't know how to build this product-platform-method combination" 1>&2
  491. echo " product=$product" 1>&2
  492. echo " platform=$platform" 1>&2
  493. echo " method=$method" 1>&2
  494. exit 1
  495. ;;
  496. esac