build.sh 10 KB

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