build.sh 9.0 KB

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