build.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. set -euo pipefail
  19. if [[ $# -lt 1 ]]; then
  20. cat 1>&2 <<EOF
  21. USAGE: $0 product [platform] [method]
  22. product can be one of:
  23. Firebase
  24. Firestore
  25. InAppMessaging
  26. SymbolCollision
  27. platform can be one of:
  28. iOS (default)
  29. macOS
  30. tvOS
  31. method can be one of:
  32. xcodebuild (default)
  33. cmake
  34. Optionally, reads the environment variable SANITIZERS. If set, it is expected to
  35. be a string containing a space-separated list with some of the following
  36. elements:
  37. asan
  38. tsan
  39. ubsan
  40. EOF
  41. exit 1
  42. fi
  43. product="$1"
  44. platform="iOS"
  45. if [[ $# -gt 1 ]]; then
  46. platform="$2"
  47. fi
  48. method="xcodebuild"
  49. if [[ $# -gt 2 ]]; then
  50. method="$3"
  51. fi
  52. echo "Building $product for $platform using $method"
  53. if [[ -n "${SANITIZERS:-}" ]]; then
  54. echo "Using sanitizers: $SANITIZERS"
  55. fi
  56. scripts_dir=$(dirname "${BASH_SOURCE[0]}")
  57. firestore_emulator="${scripts_dir}/run_firestore_emulator.sh"
  58. # Runs xcodebuild with the given flags, piping output to xcpretty
  59. # If xcodebuild fails with known error codes, retries once.
  60. function RunXcodebuild() {
  61. echo xcodebuild "$@"
  62. xcodebuild "$@" | xcpretty; result=$?
  63. if [[ $result == 65 ]]; then
  64. echo "xcodebuild exited with 65, retrying" 1>&2
  65. sleep 5
  66. xcodebuild "$@" | xcpretty; result=$?
  67. fi
  68. if [[ $result != 0 ]]; then
  69. exit $result
  70. fi
  71. }
  72. ios_flags=(
  73. -sdk 'iphonesimulator'
  74. -destination 'platform=iOS Simulator,name=iPhone 7'
  75. )
  76. macos_flags=(
  77. -sdk 'macosx'
  78. -destination 'platform=OS X,arch=x86_64'
  79. )
  80. tvos_flags=(
  81. -sdk "appletvsimulator"
  82. -destination 'platform=tvOS Simulator,name=Apple TV'
  83. )
  84. # Compute standard flags for all platforms
  85. case "$platform" in
  86. iOS)
  87. xcb_flags=("${ios_flags[@]}")
  88. ;;
  89. macOS)
  90. xcb_flags=("${macos_flags[@]}")
  91. ;;
  92. tvOS)
  93. xcb_flags=("${tvos_flags[@]}")
  94. ;;
  95. all)
  96. xcb_flags=()
  97. ;;
  98. *)
  99. echo "Unknown platform '$platform'" 1>&2
  100. exit 1
  101. ;;
  102. esac
  103. xcb_flags+=(
  104. ONLY_ACTIVE_ARCH=YES
  105. CODE_SIGNING_REQUIRED=NO
  106. CODE_SIGNING_ALLOWED=YES
  107. COMPILER_INDEX_STORE_ENABLE=NO
  108. )
  109. # TODO(varconst): Add --warn-unused-vars and --warn-uninitialized.
  110. # Right now, it makes the log overflow on Travis because many of our
  111. # dependencies don't build cleanly this way.
  112. cmake_options=(
  113. -Wdeprecated
  114. )
  115. xcode_version=$(xcodebuild -version | head -n 1)
  116. xcode_version="${xcode_version/Xcode /}"
  117. xcode_major="${xcode_version/.*/}"
  118. if [[ -n "${SANITIZERS:-}" ]]; then
  119. for sanitizer in $SANITIZERS; do
  120. case "$sanitizer" in
  121. asan)
  122. xcb_flags+=(
  123. -enableAddressSanitizer YES
  124. )
  125. cmake_options+=(
  126. -DWITH_ASAN=ON
  127. )
  128. ;;
  129. tsan)
  130. xcb_flags+=(
  131. -enableThreadSanitizer YES
  132. )
  133. cmake_options+=(
  134. -DWITH_TSAN=ON
  135. )
  136. ;;
  137. ubsan)
  138. xcb_flags+=(
  139. -enableUndefinedBehaviorSanitizer YES
  140. )
  141. cmake_options+=(
  142. -DWITH_UBSAN=ON
  143. )
  144. ;;
  145. *)
  146. echo "Unknown sanitizer '$sanitizer'" 1>&2
  147. exit 1
  148. ;;
  149. esac
  150. done
  151. fi
  152. case "$product-$method-$platform" in
  153. Firebase-xcodebuild-*)
  154. # Coverage collection often cause retries to fail because of partial
  155. # pre-existing data.
  156. # TODO(paulb777): Find a less blunt solution to this.
  157. rm -rf ~/Library/Developer/Xcode/DerivedData
  158. RunXcodebuild \
  159. -workspace 'Example/Firebase.xcworkspace' \
  160. -scheme "AllUnitTests_$platform" \
  161. "${xcb_flags[@]}" \
  162. build \
  163. test
  164. RunXcodebuild \
  165. -workspace 'GoogleUtilities/Example/GoogleUtilities.xcworkspace' \
  166. -scheme "Example_$platform" \
  167. "${xcb_flags[@]}" \
  168. build \
  169. test
  170. if [[ $platform == 'iOS' ]]; then
  171. # Code Coverage collection is only working on iOS currently.
  172. ./scripts/collect_metrics.sh 'Example/Firebase.xcworkspace' "AllUnitTests_$platform"
  173. # Run integration tests (not allowed on forks)
  174. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  175. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  176. RunXcodebuild \
  177. -workspace 'Example/Firebase.xcworkspace' \
  178. -scheme "Auth_ApiTests" \
  179. "${xcb_flags[@]}" \
  180. build \
  181. test
  182. fi
  183. # Test iOS Objective-C static library build
  184. cd Example
  185. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  186. pod update --no-repo-update
  187. cd ..
  188. RunXcodebuild \
  189. -workspace 'Example/Firebase.xcworkspace' \
  190. -scheme "AllUnitTests_$platform" \
  191. "${xcb_flags[@]}" \
  192. build \
  193. test
  194. fi
  195. ;;
  196. Auth-xcodebuild-*)
  197. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  198. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  199. RunXcodebuild \
  200. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  201. -scheme "Auth_ApiTests" \
  202. "${xcb_flags[@]}" \
  203. build \
  204. test
  205. RunXcodebuild \
  206. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  207. -scheme "Auth_E2eTests" \
  208. "${xcb_flags[@]}" \
  209. build \
  210. test
  211. fi
  212. ;;
  213. InAppMessaging-xcodebuild-iOS)
  214. RunXcodebuild \
  215. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  216. -scheme 'InAppMessaging_Example_iOS' \
  217. "${xcb_flags[@]}" \
  218. build \
  219. test
  220. cd InAppMessaging/Example
  221. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  222. pod update --no-repo-update
  223. cd ../..
  224. RunXcodebuild \
  225. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  226. -scheme 'InAppMessaging_Example_iOS' \
  227. "${xcb_flags[@]}" \
  228. build \
  229. test
  230. # Run UI tests on both iPad and iPhone simulators
  231. # TODO: Running two destinations from one xcodebuild command stopped working with Xcode 10.
  232. # Consider separating static library tests to a separate job.
  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. cd InAppMessagingDisplay/Example
  247. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  248. pod update --no-repo-update
  249. cd ../..
  250. # Run UI tests on both iPad and iPhone simulators
  251. RunXcodebuild \
  252. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  253. -scheme 'FiamDisplaySwiftExample' \
  254. "${xcb_flags[@]}" \
  255. build \
  256. test
  257. RunXcodebuild \
  258. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  259. -scheme 'FiamDisplaySwiftExample' \
  260. -sdk 'iphonesimulator' \
  261. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  262. build \
  263. test
  264. ;;
  265. Firestore-xcodebuild-*)
  266. "${firestore_emulator}" start
  267. trap '"${firestore_emulator}" stop' ERR EXIT
  268. if [[ "$xcode_major" -lt 9 ]]; then
  269. # When building and testing for Xcode 8, only test unit tests.
  270. RunXcodebuild \
  271. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  272. -scheme "Firestore_Tests_$platform" \
  273. "${xcb_flags[@]}" \
  274. build \
  275. test
  276. else
  277. # IntegrationTests run all the tests, including Swift tests, which
  278. # require Swift 4.0 and Xcode 9+.
  279. RunXcodebuild \
  280. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  281. -scheme "Firestore_IntegrationTests_$platform" \
  282. "${xcb_flags[@]}" \
  283. build \
  284. test
  285. fi
  286. ;;
  287. Firestore-cmake-macOS)
  288. test -d build || mkdir build
  289. echo "Preparing cmake build ..."
  290. (cd build; cmake "${cmake_options[@]}" ..)
  291. echo "Building cmake build ..."
  292. cpus=$(sysctl -n hw.ncpu)
  293. (cd build; env make -j $cpus all generate_protos)
  294. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  295. ;;
  296. SymbolCollision-xcodebuild-*)
  297. RunXcodebuild \
  298. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  299. -scheme "SymbolCollisionTest" \
  300. "${xcb_flags[@]}" \
  301. build
  302. ;;
  303. GoogleDataTransport-xcodebuild-*)
  304. RunXcodebuild \
  305. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  306. -scheme "GoogleDataTransport-$platform-Unit-Tests-Unit" \
  307. "${xcb_flags[@]}" \
  308. build \
  309. test
  310. RunXcodebuild \
  311. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  312. -scheme "GoogleDataTransport-$platform-Unit-Tests-Lifecycle" \
  313. "${xcb_flags[@]}" \
  314. build \
  315. test
  316. ;;
  317. GoogleDataTransportIntegrationTest-xcodebuild-*)
  318. RunXcodebuild \
  319. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  320. -scheme "GoogleDataTransport-$platform-Unit-Tests-Integration" \
  321. "${xcb_flags[@]}" \
  322. build \
  323. test
  324. ;;
  325. GoogleDataTransportCCTSupport-xcodebuild-*)
  326. RunXcodebuild \
  327. -workspace 'gen/GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport.xcworkspace' \
  328. -scheme "GoogleDataTransportCCTSupport-$platform-Unit-Tests-Unit" \
  329. "${xcb_flags[@]}" \
  330. build \
  331. test
  332. RunXcodebuild \
  333. -workspace 'gen/GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport.xcworkspace' \
  334. -scheme "GoogleDataTransportCCTSupport-$platform-Unit-Tests-Integration" \
  335. "${xcb_flags[@]}" \
  336. build \
  337. test
  338. ;;
  339. Database-xcodebuild-*)
  340. RunXcodebuild \
  341. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  342. -scheme "FirebaseDatabase-iOS-Unit-unit" \
  343. "${ios_flags[@]}" \
  344. "${xcb_flags[@]}" \
  345. build \
  346. test
  347. RunXcodebuild \
  348. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  349. -scheme "FirebaseDatabase-macOS-Unit-unit" \
  350. "${macos_flags[@]}" \
  351. "${xcb_flags[@]}" \
  352. build \
  353. test
  354. RunXcodebuild \
  355. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  356. -scheme "FirebaseDatabase-tvOS-Unit-unit" \
  357. "${tvos_flags[@]}" \
  358. "${xcb_flags[@]}" \
  359. build \
  360. test
  361. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  362. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  363. # Integration tests are only run on iOS to minimize flake failures.
  364. RunXcodebuild \
  365. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  366. -scheme "FirebaseDatabase-iOS-Unit-integration" \
  367. "${ios_flags[@]}" \
  368. "${xcb_flags[@]}" \
  369. build \
  370. test
  371. fi
  372. ;;
  373. Messaging-xcodebuild-*)
  374. RunXcodebuild \
  375. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  376. -scheme "FirebaseMessaging-iOS-Unit-unit" \
  377. "${ios_flags[@]}" \
  378. "${xcb_flags[@]}" \
  379. build \
  380. test
  381. RunXcodebuild \
  382. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  383. -scheme "FirebaseMessaging-tvOS-Unit-unit" \
  384. "${tvos_flags[@]}" \
  385. "${xcb_flags[@]}" \
  386. build \
  387. test
  388. ;;
  389. Storage-xcodebuild-*)
  390. RunXcodebuild \
  391. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  392. -scheme "FirebaseStorage-iOS-Unit-unit" \
  393. "${ios_flags[@]}" \
  394. "${xcb_flags[@]}" \
  395. build \
  396. test
  397. RunXcodebuild \
  398. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  399. -scheme "FirebaseStorage-macOS-Unit-unit" \
  400. "${macos_flags[@]}" \
  401. "${xcb_flags[@]}" \
  402. build \
  403. test
  404. RunXcodebuild \
  405. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  406. -scheme "FirebaseStorage-tvOS-Unit-unit" \
  407. "${tvos_flags[@]}" \
  408. "${xcb_flags[@]}" \
  409. build \
  410. test
  411. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  412. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  413. # Integration tests are only run on iOS to minimize flake failures.
  414. RunXcodebuild \
  415. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  416. -scheme "FirebaseStorage-iOS-Unit-integration" \
  417. "${ios_flags[@]}" \
  418. "${xcb_flags[@]}" \
  419. build \
  420. test
  421. fi
  422. ;;
  423. *)
  424. echo "Don't know how to build this product-platform-method combination" 1>&2
  425. echo " product=$product" 1>&2
  426. echo " platform=$platform" 1>&2
  427. echo " method=$method" 1>&2
  428. exit 1
  429. ;;
  430. esac