build.sh 14 KB

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