build.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. InAppMessagingDisplay
  31. SymbolCollision
  32. platform can be one of:
  33. iOS (default)
  34. macOS
  35. tvOS
  36. method can be one of:
  37. xcodebuild (default)
  38. cmake
  39. Optionally, reads the environment variable SANITIZERS. If set, it is expected to
  40. be a string containing a space-separated list with some of the following
  41. elements:
  42. asan
  43. tsan
  44. ubsan
  45. EOF
  46. exit 1
  47. fi
  48. product="$1"
  49. platform="iOS"
  50. if [[ $# -gt 1 ]]; then
  51. platform="$2"
  52. fi
  53. method="xcodebuild"
  54. if [[ $# -gt 2 ]]; then
  55. method="$3"
  56. fi
  57. echo "Building $product for $platform using $method"
  58. if [[ -n "${SANITIZERS:-}" ]]; then
  59. echo "Using sanitizers: $SANITIZERS"
  60. fi
  61. scripts_dir=$(dirname "${BASH_SOURCE[0]}")
  62. firestore_emulator="${scripts_dir}/run_firestore_emulator.sh"
  63. xcode_version=$(xcodebuild -version | head -n 1)
  64. xcode_version="${xcode_version/Xcode /}"
  65. xcode_major="${xcode_version/.*/}"
  66. # Runs xcodebuild with the given flags, piping output to xcpretty
  67. # If xcodebuild fails with known error codes, retries once.
  68. function RunXcodebuild() {
  69. echo xcodebuild "$@"
  70. xcodebuild "$@" | xcpretty; result=$?
  71. if [[ $result == 65 ]]; then
  72. echo "xcodebuild exited with 65, retrying" 1>&2
  73. sleep 5
  74. xcodebuild "$@" | xcpretty; result=$?
  75. fi
  76. if [[ $result != 0 ]]; then
  77. exit $result
  78. fi
  79. }
  80. if [[ "$xcode_major" -lt 11 ]]; then
  81. ios_flags=(
  82. -sdk 'iphonesimulator'
  83. -destination 'platform=iOS Simulator,name=iPhone 7'
  84. )
  85. else
  86. ios_flags=(
  87. -sdk 'iphonesimulator'
  88. -destination 'platform=iOS Simulator,name=iPhone 11'
  89. )
  90. fi
  91. ipad_flags=(
  92. -sdk 'iphonesimulator'
  93. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)'
  94. )
  95. macos_flags=(
  96. -sdk 'macosx'
  97. -destination 'platform=OS X,arch=x86_64'
  98. )
  99. tvos_flags=(
  100. -sdk "appletvsimulator"
  101. -destination 'platform=tvOS Simulator,name=Apple TV'
  102. )
  103. # Compute standard flags for all platforms
  104. case "$platform" in
  105. iOS)
  106. xcb_flags=("${ios_flags[@]}")
  107. ;;
  108. iPad)
  109. xcb_flags=("${ipad_flags[@]}")
  110. ;;
  111. macOS)
  112. xcb_flags=("${macos_flags[@]}")
  113. ;;
  114. tvOS)
  115. xcb_flags=("${tvos_flags[@]}")
  116. ;;
  117. all)
  118. xcb_flags=()
  119. ;;
  120. *)
  121. echo "Unknown platform '$platform'" 1>&2
  122. exit 1
  123. ;;
  124. esac
  125. xcb_flags+=(
  126. ONLY_ACTIVE_ARCH=YES
  127. CODE_SIGNING_REQUIRED=NO
  128. CODE_SIGNING_ALLOWED=YES
  129. COMPILER_INDEX_STORE_ENABLE=NO
  130. )
  131. # TODO(varconst): Add --warn-unused-vars and --warn-uninitialized.
  132. # Right now, it makes the log overflow on Travis because many of our
  133. # dependencies don't build cleanly this way.
  134. cmake_options=(
  135. -Wdeprecated
  136. )
  137. if [[ -n "${SANITIZERS:-}" ]]; then
  138. for sanitizer in $SANITIZERS; do
  139. case "$sanitizer" in
  140. asan)
  141. xcb_flags+=(
  142. -enableAddressSanitizer YES
  143. )
  144. cmake_options+=(
  145. -DWITH_ASAN=ON
  146. )
  147. ;;
  148. tsan)
  149. xcb_flags+=(
  150. -enableThreadSanitizer YES
  151. )
  152. cmake_options+=(
  153. -DWITH_TSAN=ON
  154. )
  155. ;;
  156. ubsan)
  157. xcb_flags+=(
  158. -enableUndefinedBehaviorSanitizer YES
  159. )
  160. cmake_options+=(
  161. -DWITH_UBSAN=ON
  162. )
  163. ;;
  164. *)
  165. echo "Unknown sanitizer '$sanitizer'" 1>&2
  166. exit 1
  167. ;;
  168. esac
  169. done
  170. fi
  171. case "$product-$method-$platform" in
  172. FirebasePod-xcodebuild-*)
  173. RunXcodebuild \
  174. -workspace 'CoreOnly/Tests/FirebasePodTest/FirebasePodTest.xcworkspace' \
  175. -scheme "FirebasePodTest" \
  176. "${xcb_flags[@]}" \
  177. build
  178. ;;
  179. Auth-xcodebuild-*)
  180. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  181. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  182. RunXcodebuild \
  183. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  184. -scheme "Auth_ApiTests" \
  185. "${xcb_flags[@]}" \
  186. build \
  187. test
  188. fi
  189. ;;
  190. InAppMessaging-xcodebuild-*)
  191. RunXcodebuild \
  192. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  193. -scheme 'InAppMessaging_Example_iOS' \
  194. "${xcb_flags[@]}" \
  195. build \
  196. test
  197. RunXcodebuild \
  198. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  199. -scheme 'FiamDisplaySwiftExample' \
  200. "${xcb_flags[@]}" \
  201. build \
  202. test
  203. ;;
  204. Firestore-xcodebuild-*)
  205. "${firestore_emulator}" start
  206. trap '"${firestore_emulator}" stop' ERR EXIT
  207. if [[ "$xcode_major" -lt 9 ]]; then
  208. # When building and testing for Xcode 8, only test unit tests.
  209. RunXcodebuild \
  210. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  211. -scheme "Firestore_Tests_$platform" \
  212. "${xcb_flags[@]}" \
  213. build \
  214. test
  215. else
  216. # IntegrationTests run all the tests, including Swift tests, which
  217. # require Swift 4.0 and Xcode 9+.
  218. RunXcodebuild \
  219. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  220. -scheme "Firestore_IntegrationTests_$platform" \
  221. "${xcb_flags[@]}" \
  222. build \
  223. test
  224. fi
  225. ;;
  226. Firestore-cmake-macOS)
  227. "${firestore_emulator}" start
  228. trap '"${firestore_emulator}" stop' ERR EXIT
  229. test -d build || mkdir build
  230. echo "Preparing cmake build ..."
  231. (cd build; cmake "${cmake_options[@]}" ..)
  232. echo "Building cmake build ..."
  233. cpus=$(sysctl -n hw.ncpu)
  234. (cd build; env make -j $cpus all generate_protos)
  235. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  236. ;;
  237. SymbolCollision-xcodebuild-*)
  238. RunXcodebuild \
  239. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  240. -scheme "SymbolCollisionTest" \
  241. "${xcb_flags[@]}" \
  242. build
  243. ;;
  244. Database-xcodebuild-*)
  245. pod_gen FirebaseDatabase.podspec --platforms=ios
  246. RunXcodebuild \
  247. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  248. -scheme "FirebaseDatabase-Unit-unit" \
  249. "${ios_flags[@]}" \
  250. "${xcb_flags[@]}" \
  251. build \
  252. test
  253. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  254. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  255. # Integration tests are only run on iOS to minimize flake failures.
  256. RunXcodebuild \
  257. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  258. -scheme "FirebaseDatabase-Unit-integration" \
  259. "${ios_flags[@]}" \
  260. "${xcb_flags[@]}" \
  261. build \
  262. test
  263. fi
  264. pod_gen FirebaseDatabase.podspec --platforms=macos --clean
  265. RunXcodebuild \
  266. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  267. -scheme "FirebaseDatabase-Unit-unit" \
  268. "${macos_flags[@]}" \
  269. "${xcb_flags[@]}" \
  270. build \
  271. test
  272. pod_gen FirebaseDatabase.podspec --platforms=tvos --clean
  273. RunXcodebuild \
  274. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  275. -scheme "FirebaseDatabase-Unit-unit" \
  276. "${tvos_flags[@]}" \
  277. "${xcb_flags[@]}" \
  278. build \
  279. test
  280. ;;
  281. Storage-xcodebuild-*)
  282. pod_gen FirebaseStorage.podspec --platforms=ios
  283. RunXcodebuild \
  284. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  285. -scheme "FirebaseStorage-Unit-unit" \
  286. "${ios_flags[@]}" \
  287. "${xcb_flags[@]}" \
  288. build \
  289. test
  290. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  291. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  292. # Integration tests are only run on iOS to minimize flake failures.
  293. RunXcodebuild \
  294. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  295. -scheme "FirebaseStorage-Unit-integration" \
  296. "${ios_flags[@]}" \
  297. "${xcb_flags[@]}" \
  298. build \
  299. test
  300. fi
  301. pod_gen FirebaseStorage.podspec --platforms=macos --clean
  302. RunXcodebuild \
  303. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  304. -scheme "FirebaseStorage-Unit-unit" \
  305. "${macos_flags[@]}" \
  306. "${xcb_flags[@]}" \
  307. build \
  308. test
  309. pod_gen FirebaseStorage.podspec --platforms=tvos --clean
  310. RunXcodebuild \
  311. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  312. -scheme "FirebaseStorage-Unit-unit" \
  313. "${tvos_flags[@]}" \
  314. "${xcb_flags[@]}" \
  315. build \
  316. test
  317. ;;
  318. *)
  319. echo "Don't know how to build this product-platform-method combination" 1>&2
  320. echo " product=$product" 1>&2
  321. echo " platform=$platform" 1>&2
  322. echo " method=$method" 1>&2
  323. exit 1
  324. ;;
  325. esac