build.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. RunXcodebuild \
  264. -workspace 'FirebaseAuth/Tests/Sample/AuthSample.xcworkspace' \
  265. -scheme "SwiftApiTests" \
  266. "${xcb_flags[@]}" \
  267. build \
  268. test
  269. fi
  270. ;;
  271. InAppMessaging-*-xcodebuild)
  272. RunXcodebuild \
  273. -workspace 'FirebaseInAppMessaging/Tests/Integration/DefaultUITestApp/InAppMessagingDisplay-Sample.xcworkspace' \
  274. -scheme 'FiamDisplaySwiftExample' \
  275. "${xcb_flags[@]}" \
  276. build \
  277. test
  278. ;;
  279. Firestore-*-xcodebuild)
  280. "${firestore_emulator}" start
  281. trap '"${firestore_emulator}" stop' ERR EXIT
  282. RunXcodebuild \
  283. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  284. -scheme "Firestore_IntegrationTests_$platform" \
  285. "${xcb_flags[@]}" \
  286. build \
  287. test
  288. ;;
  289. Firestore-macOS-cmake | Firestore-Linux-cmake)
  290. "${firestore_emulator}" start
  291. trap '"${firestore_emulator}" stop' ERR EXIT
  292. (
  293. test -d build || mkdir build
  294. cd build
  295. echo "Preparing cmake build ..."
  296. cmake -G Ninja "${cmake_options[@]}" ..
  297. echo "Building cmake build ..."
  298. ninja -k 10 all
  299. ctest --output-on-failure
  300. )
  301. ;;
  302. SymbolCollision-*-*)
  303. RunXcodebuild \
  304. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  305. -scheme "SymbolCollisionTest" \
  306. "${xcb_flags[@]}" \
  307. build
  308. ;;
  309. Messaging-*-xcodebuild)
  310. pod_gen FirebaseMessaging.podspec --platforms=ios
  311. RunXcodebuild \
  312. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  313. -scheme "FirebaseMessaging-Unit-unit" \
  314. "${ios_flags[@]}" \
  315. "${xcb_flags[@]}" \
  316. build \
  317. test
  318. if check_secrets; then
  319. # Integration tests are only run on iOS to minimize flake failures.
  320. RunXcodebuild \
  321. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  322. -scheme "FirebaseMessaging-Unit-integration" \
  323. "${ios_flags[@]}" \
  324. "${xcb_flags[@]}" \
  325. build \
  326. test
  327. fi
  328. pod_gen FirebaseMessaging.podspec --platforms=macos --clean
  329. RunXcodebuild \
  330. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  331. -scheme "FirebaseMessaging-Unit-unit" \
  332. "${macos_flags[@]}" \
  333. "${xcb_flags[@]}" \
  334. build \
  335. test
  336. pod_gen FirebaseMessaging.podspec --platforms=tvos --clean
  337. RunXcodebuild \
  338. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  339. -scheme "FirebaseMessaging-Unit-unit" \
  340. "${tvos_flags[@]}" \
  341. "${xcb_flags[@]}" \
  342. build \
  343. test
  344. ;;
  345. MessagingSample-*-*)
  346. if check_secrets; then
  347. RunXcodebuild \
  348. -workspace 'FirebaseMessaging/Apps/Sample/Sample.xcworkspace' \
  349. -scheme "Sample" \
  350. "${xcb_flags[@]}" \
  351. build
  352. fi
  353. ;;
  354. Database-*-xcodebuild)
  355. pod_gen FirebaseDatabase.podspec --platforms=ios
  356. RunXcodebuild \
  357. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  358. -scheme "FirebaseDatabase-Unit-unit" \
  359. "${ios_flags[@]}" \
  360. "${xcb_flags[@]}" \
  361. build \
  362. test
  363. if check_secrets; then
  364. # Integration tests are only run on iOS to minimize flake failures.
  365. RunXcodebuild \
  366. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  367. -scheme "FirebaseDatabase-Unit-integration" \
  368. "${ios_flags[@]}" \
  369. "${xcb_flags[@]}" \
  370. build \
  371. test
  372. fi
  373. pod_gen FirebaseDatabase.podspec --platforms=macos --clean
  374. RunXcodebuild \
  375. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  376. -scheme "FirebaseDatabase-Unit-unit" \
  377. "${macos_flags[@]}" \
  378. "${xcb_flags[@]}" \
  379. build \
  380. test
  381. pod_gen FirebaseDatabase.podspec --platforms=tvos --clean
  382. RunXcodebuild \
  383. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  384. -scheme "FirebaseDatabase-Unit-unit" \
  385. "${tvos_flags[@]}" \
  386. "${xcb_flags[@]}" \
  387. build \
  388. test
  389. ;;
  390. Storage-*-xcodebuild)
  391. pod_gen FirebaseStorage.podspec --platforms=ios
  392. RunXcodebuild \
  393. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  394. -scheme "FirebaseStorage-Unit-unit" \
  395. "${ios_flags[@]}" \
  396. "${xcb_flags[@]}" \
  397. build \
  398. test
  399. if check_secrets; then
  400. # Integration tests are only run on iOS to minimize flake failures.
  401. RunXcodebuild \
  402. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  403. -scheme "FirebaseStorage-Unit-integration" \
  404. "${ios_flags[@]}" \
  405. "${xcb_flags[@]}" \
  406. build \
  407. test
  408. RunXcodebuild \
  409. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  410. -scheme "FirebaseStorage-Unit-swift-integration" \
  411. "${ios_flags[@]}" \
  412. "${xcb_flags[@]}" \
  413. build \
  414. test
  415. fi
  416. pod_gen FirebaseStorage.podspec --platforms=macos --clean
  417. RunXcodebuild \
  418. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  419. -scheme "FirebaseStorage-Unit-unit" \
  420. "${macos_flags[@]}" \
  421. "${xcb_flags[@]}" \
  422. build \
  423. test
  424. pod_gen FirebaseStorage.podspec --platforms=tvos --clean
  425. RunXcodebuild \
  426. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  427. -scheme "FirebaseStorage-Unit-unit" \
  428. "${tvos_flags[@]}" \
  429. "${xcb_flags[@]}" \
  430. build \
  431. test
  432. ;;
  433. StorageSwift-*-xcodebuild)
  434. pod_gen FirebaseStorageSwift.podspec --platforms=ios
  435. if check_secrets; then
  436. # Integration tests are only run on iOS to minimize flake failures.
  437. RunXcodebuild \
  438. -workspace 'gen/FirebaseStorageSwift/FirebaseStorageSwift.xcworkspace' \
  439. -scheme "FirebaseStorageSwift-Unit-integration" \
  440. "${ios_flags[@]}" \
  441. "${xcb_flags[@]}" \
  442. build \
  443. test
  444. fi
  445. ;;
  446. GoogleDataTransport-watchOS-xcodebuild)
  447. RunXcodebuild \
  448. -workspace 'GoogleDataTransport/GDTWatchOSTestApp/GDTWatchOSTestApp.xcworkspace' \
  449. -scheme "GDTWatchOSTestAppWatchKitApp" \
  450. "${xcb_flags[@]}" \
  451. build
  452. ;;
  453. GoogleDataTransportCCTSupport-watchOS-xcodebuild)
  454. RunXcodebuild \
  455. -workspace 'GoogleDataTransportCCTSupport/GDTCCTWatchOSTestApp/GDTCCTWatchOSTestApp.xcworkspace' \
  456. -scheme "GDTCCTWatchOSIndependentTestAppWatchKitApp" \
  457. "${xcb_flags[@]}" \
  458. build
  459. RunXcodebuild \
  460. -workspace 'GoogleDataTransportCCTSupport/GDTCCTWatchOSTestApp/GDTCCTWatchOSTestApp.xcworkspace' \
  461. -scheme "GDTCCTWatchOSCompanionTestApp" \
  462. "${xcb_flags[@]}" \
  463. build
  464. ;;
  465. *)
  466. echo "Don't know how to build this product-platform-method combination" 1>&2
  467. echo " product=$product" 1>&2
  468. echo " platform=$platform" 1>&2
  469. echo " method=$method" 1>&2
  470. exit 1
  471. ;;
  472. esac