build.sh 13 KB

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