build.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. xcodebuild "$@" | xcpretty; result=$?
  93. if [[ $result == 65 ]]; then
  94. echo "xcodebuild exited with 65, retrying" 1>&2
  95. sleep 5
  96. xcodebuild "$@" | xcpretty; result=$?
  97. fi
  98. if [[ $result != 0 ]]; then
  99. exit $result
  100. fi
  101. }
  102. if [[ "$xcode_major" -lt 11 ]]; then
  103. ios_flags=(
  104. -sdk 'iphonesimulator'
  105. -destination 'platform=iOS Simulator,name=iPhone 7'
  106. )
  107. else
  108. ios_flags=(
  109. -sdk 'iphonesimulator'
  110. -destination 'platform=iOS Simulator,name=iPhone 11'
  111. )
  112. fi
  113. ipad_flags=(
  114. -sdk 'iphonesimulator'
  115. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)'
  116. )
  117. macos_flags=(
  118. -sdk 'macosx'
  119. -destination 'platform=OS X,arch=x86_64'
  120. )
  121. tvos_flags=(
  122. -sdk "appletvsimulator"
  123. -destination 'platform=tvOS Simulator,name=Apple TV'
  124. )
  125. # Compute standard flags for all platforms
  126. case "$platform" in
  127. iOS)
  128. xcb_flags=("${ios_flags[@]}")
  129. ;;
  130. iPad)
  131. xcb_flags=("${ipad_flags[@]}")
  132. ;;
  133. macOS)
  134. xcb_flags=("${macos_flags[@]}")
  135. ;;
  136. tvOS)
  137. xcb_flags=("${tvos_flags[@]}")
  138. ;;
  139. all)
  140. xcb_flags=()
  141. ;;
  142. Linux)
  143. xcb_flags=()
  144. ;;
  145. *)
  146. echo "Unknown platform '$platform'" 1>&2
  147. exit 1
  148. ;;
  149. esac
  150. xcb_flags+=(
  151. ONLY_ACTIVE_ARCH=YES
  152. CODE_SIGNING_REQUIRED=NO
  153. CODE_SIGNING_ALLOWED=YES
  154. COMPILER_INDEX_STORE_ENABLE=NO
  155. )
  156. # TODO(varconst): Add --warn-unused-vars and --warn-uninitialized.
  157. # Right now, it makes the log overflow on Travis because many of our
  158. # dependencies don't build cleanly this way.
  159. cmake_options=(
  160. -Wdeprecated
  161. )
  162. if [[ -n "${SANITIZERS:-}" ]]; then
  163. for sanitizer in $SANITIZERS; do
  164. case "$sanitizer" in
  165. asan)
  166. xcb_flags+=(
  167. -enableAddressSanitizer YES
  168. )
  169. cmake_options+=(
  170. -DWITH_ASAN=ON
  171. )
  172. ;;
  173. tsan)
  174. xcb_flags+=(
  175. -enableThreadSanitizer YES
  176. )
  177. cmake_options+=(
  178. -DWITH_TSAN=ON
  179. )
  180. ;;
  181. ubsan)
  182. xcb_flags+=(
  183. -enableUndefinedBehaviorSanitizer YES
  184. )
  185. cmake_options+=(
  186. -DWITH_UBSAN=ON
  187. )
  188. ;;
  189. *)
  190. echo "Unknown sanitizer '$sanitizer'" 1>&2
  191. exit 1
  192. ;;
  193. esac
  194. done
  195. fi
  196. case "$product-$platform-$method" in
  197. FirebasePod-*-xcodebuild)
  198. RunXcodebuild \
  199. -workspace 'CoreOnly/Tests/FirebasePodTest/FirebasePodTest.xcworkspace' \
  200. -scheme "FirebasePodTest" \
  201. "${xcb_flags[@]}" \
  202. build
  203. ;;
  204. Auth-*-xcodebuild)
  205. if [[ "$have_secrets" == true ]]; then
  206. RunXcodebuild \
  207. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  208. -scheme "Auth_ApiTests" \
  209. "${xcb_flags[@]}" \
  210. build \
  211. test
  212. fi
  213. ;;
  214. InAppMessaging-*-xcodebuild)
  215. RunXcodebuild \
  216. -workspace 'FirebaseInAppMessaging/Tests/Integration/DefaultUITestApp/InAppMessagingDisplay-Sample.xcworkspace' \
  217. -scheme 'FiamDisplaySwiftExample' \
  218. "${xcb_flags[@]}" \
  219. build \
  220. test
  221. ;;
  222. Firestore-*-xcodebuild)
  223. "${firestore_emulator}" start
  224. trap '"${firestore_emulator}" stop' ERR EXIT
  225. if [[ "$xcode_major" -lt 9 ]]; then
  226. # When building and testing for Xcode 8, only test unit tests.
  227. RunXcodebuild \
  228. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  229. -scheme "Firestore_Tests_$platform" \
  230. "${xcb_flags[@]}" \
  231. build \
  232. test
  233. else
  234. # IntegrationTests run all the tests, including Swift tests, which
  235. # require Swift 4.0 and Xcode 9+.
  236. RunXcodebuild \
  237. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  238. -scheme "Firestore_IntegrationTests_$platform" \
  239. "${xcb_flags[@]}" \
  240. build \
  241. test
  242. fi
  243. ;;
  244. Firestore-macOS-cmake | Firestore-Linux-cmake)
  245. "${firestore_emulator}" start
  246. trap '"${firestore_emulator}" stop' ERR EXIT
  247. (
  248. test -d build || mkdir build
  249. cd build
  250. echo "Preparing cmake build ..."
  251. cmake -G Ninja "${cmake_options[@]}" ..
  252. echo "Building cmake build ..."
  253. ninja -k 10 all
  254. ctest --output-on-failure
  255. )
  256. ;;
  257. SymbolCollision-*-xcodebuild)
  258. RunXcodebuild \
  259. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  260. -scheme "SymbolCollisionTest" \
  261. "${xcb_flags[@]}" \
  262. build
  263. ;;
  264. Database-*-xcodebuild)
  265. pod_gen FirebaseDatabase.podspec --platforms=ios
  266. RunXcodebuild \
  267. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  268. -scheme "FirebaseDatabase-Unit-unit" \
  269. "${ios_flags[@]}" \
  270. "${xcb_flags[@]}" \
  271. build \
  272. test
  273. if [[ "$have_secrets" == true ]]; then
  274. # Integration tests are only run on iOS to minimize flake failures.
  275. RunXcodebuild \
  276. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  277. -scheme "FirebaseDatabase-Unit-integration" \
  278. "${ios_flags[@]}" \
  279. "${xcb_flags[@]}" \
  280. build \
  281. test
  282. fi
  283. pod_gen FirebaseDatabase.podspec --platforms=macos --clean
  284. RunXcodebuild \
  285. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  286. -scheme "FirebaseDatabase-Unit-unit" \
  287. "${macos_flags[@]}" \
  288. "${xcb_flags[@]}" \
  289. build \
  290. test
  291. pod_gen FirebaseDatabase.podspec --platforms=tvos --clean
  292. RunXcodebuild \
  293. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  294. -scheme "FirebaseDatabase-Unit-unit" \
  295. "${tvos_flags[@]}" \
  296. "${xcb_flags[@]}" \
  297. build \
  298. test
  299. ;;
  300. Storage-*-xcodebuild)
  301. pod_gen FirebaseStorage.podspec --platforms=ios
  302. RunXcodebuild \
  303. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  304. -scheme "FirebaseStorage-Unit-unit" \
  305. "${ios_flags[@]}" \
  306. "${xcb_flags[@]}" \
  307. build \
  308. test
  309. if [[ "$have_secrets" == true ]]; then
  310. # Integration tests are only run on iOS to minimize flake failures.
  311. RunXcodebuild \
  312. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  313. -scheme "FirebaseStorage-Unit-integration" \
  314. "${ios_flags[@]}" \
  315. "${xcb_flags[@]}" \
  316. build \
  317. test
  318. fi
  319. pod_gen FirebaseStorage.podspec --platforms=macos --clean
  320. RunXcodebuild \
  321. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  322. -scheme "FirebaseStorage-Unit-unit" \
  323. "${macos_flags[@]}" \
  324. "${xcb_flags[@]}" \
  325. build \
  326. test
  327. pod_gen FirebaseStorage.podspec --platforms=tvos --clean
  328. RunXcodebuild \
  329. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  330. -scheme "FirebaseStorage-Unit-unit" \
  331. "${tvos_flags[@]}" \
  332. "${xcb_flags[@]}" \
  333. build \
  334. test
  335. ;;
  336. *)
  337. echo "Don't know how to build this product-platform-method combination" 1>&2
  338. echo " product=$product" 1>&2
  339. echo " platform=$platform" 1>&2
  340. echo " method=$method" 1>&2
  341. exit 1
  342. ;;
  343. esac