build.sh 11 KB

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