build.sh 11 KB

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