build.sh 13 KB

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