build.sh 11 KB

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