build.sh 12 KB

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