build.sh 11 KB

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