build.sh 13 KB

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