build.sh 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. Firebase-xcodebuild-*)
  173. # Coverage collection often cause retries to fail because of partial
  174. # pre-existing data.
  175. # TODO(paulb777): Find a less blunt solution to this.
  176. rm -rf ~/Library/Developer/Xcode/DerivedData
  177. RunXcodebuild \
  178. -workspace 'Example/Firebase.xcworkspace' \
  179. -scheme "AllUnitTests_$platform" \
  180. "${xcb_flags[@]}" \
  181. build \
  182. test
  183. if [[ $platform == 'iOS' ]]; then
  184. # Code Coverage collection is only working on iOS currently.
  185. ./scripts/collect_metrics.sh 'Example/Firebase.xcworkspace' "AllUnitTests_$platform"
  186. fi
  187. ;;
  188. FirebasePod-xcodebuild-*)
  189. RunXcodebuild \
  190. -workspace 'CoreOnly/Tests/FirebasePodTest/FirebasePodTest.xcworkspace' \
  191. -scheme "FirebasePodTest" \
  192. "${xcb_flags[@]}" \
  193. build
  194. ;;
  195. Auth-xcodebuild-*)
  196. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  197. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  198. RunXcodebuild \
  199. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  200. -scheme "Auth_ApiTests" \
  201. "${xcb_flags[@]}" \
  202. build \
  203. test
  204. fi
  205. ;;
  206. InAppMessaging-xcodebuild-*)
  207. RunXcodebuild \
  208. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  209. -scheme 'InAppMessaging_Example_iOS' \
  210. "${xcb_flags[@]}" \
  211. build \
  212. test
  213. RunXcodebuild \
  214. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  215. -scheme 'FiamDisplaySwiftExample' \
  216. "${xcb_flags[@]}" \
  217. build \
  218. test
  219. ;;
  220. Firestore-xcodebuild-*)
  221. "${firestore_emulator}" start
  222. trap '"${firestore_emulator}" stop' ERR EXIT
  223. if [[ "$xcode_major" -lt 9 ]]; then
  224. # When building and testing for Xcode 8, only test unit tests.
  225. RunXcodebuild \
  226. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  227. -scheme "Firestore_Tests_$platform" \
  228. "${xcb_flags[@]}" \
  229. build \
  230. test
  231. else
  232. # IntegrationTests run all the tests, including Swift tests, which
  233. # require Swift 4.0 and Xcode 9+.
  234. RunXcodebuild \
  235. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  236. -scheme "Firestore_IntegrationTests_$platform" \
  237. "${xcb_flags[@]}" \
  238. build \
  239. test
  240. fi
  241. ;;
  242. Firestore-cmake-macOS)
  243. "${firestore_emulator}" start
  244. trap '"${firestore_emulator}" stop' ERR EXIT
  245. test -d build || mkdir build
  246. echo "Preparing cmake build ..."
  247. (cd build; cmake "${cmake_options[@]}" ..)
  248. echo "Building cmake build ..."
  249. cpus=$(sysctl -n hw.ncpu)
  250. (cd build; env make -j $cpus all generate_protos)
  251. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  252. ;;
  253. SymbolCollision-xcodebuild-*)
  254. RunXcodebuild \
  255. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  256. -scheme "SymbolCollisionTest" \
  257. "${xcb_flags[@]}" \
  258. build
  259. ;;
  260. Database-xcodebuild-*)
  261. pod_gen FirebaseDatabase.podspec --platforms=ios
  262. RunXcodebuild \
  263. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  264. -scheme "FirebaseDatabase-Unit-unit" \
  265. "${ios_flags[@]}" \
  266. "${xcb_flags[@]}" \
  267. build \
  268. test
  269. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  270. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  271. # Integration tests are only run on iOS to minimize flake failures.
  272. RunXcodebuild \
  273. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  274. -scheme "FirebaseDatabase-Unit-integration" \
  275. "${ios_flags[@]}" \
  276. "${xcb_flags[@]}" \
  277. build \
  278. test
  279. fi
  280. pod_gen FirebaseDatabase.podspec --platforms=macos --clean
  281. RunXcodebuild \
  282. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  283. -scheme "FirebaseDatabase-Unit-unit" \
  284. "${macos_flags[@]}" \
  285. "${xcb_flags[@]}" \
  286. build \
  287. test
  288. pod_gen FirebaseDatabase.podspec --platforms=tvos --clean
  289. RunXcodebuild \
  290. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  291. -scheme "FirebaseDatabase-Unit-unit" \
  292. "${tvos_flags[@]}" \
  293. "${xcb_flags[@]}" \
  294. build \
  295. test
  296. ;;
  297. Storage-xcodebuild-*)
  298. pod_gen FirebaseStorage.podspec --platforms=ios
  299. RunXcodebuild \
  300. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  301. -scheme "FirebaseStorage-Unit-unit" \
  302. "${ios_flags[@]}" \
  303. "${xcb_flags[@]}" \
  304. build \
  305. test
  306. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  307. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  308. # Integration tests are only run on iOS to minimize flake failures.
  309. RunXcodebuild \
  310. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  311. -scheme "FirebaseStorage-Unit-integration" \
  312. "${ios_flags[@]}" \
  313. "${xcb_flags[@]}" \
  314. build \
  315. test
  316. fi
  317. pod_gen FirebaseStorage.podspec --platforms=macos --clean
  318. RunXcodebuild \
  319. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  320. -scheme "FirebaseStorage-Unit-unit" \
  321. "${macos_flags[@]}" \
  322. "${xcb_flags[@]}" \
  323. build \
  324. test
  325. pod_gen FirebaseStorage.podspec --platforms=tvos --clean
  326. RunXcodebuild \
  327. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  328. -scheme "FirebaseStorage-Unit-unit" \
  329. "${tvos_flags[@]}" \
  330. "${xcb_flags[@]}" \
  331. build \
  332. test
  333. ;;
  334. *)
  335. echo "Don't know how to build this product-platform-method combination" 1>&2
  336. echo " product=$product" 1>&2
  337. echo " platform=$platform" 1>&2
  338. echo " method=$method" 1>&2
  339. exit 1
  340. ;;
  341. esac