build.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. # Test iOS Objective-C static library build
  174. cd Example
  175. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  176. pod update --no-repo-update
  177. cd ..
  178. RunXcodebuild \
  179. -workspace 'Example/Firebase.xcworkspace' \
  180. -scheme "AllUnitTests_$platform" \
  181. "${xcb_flags[@]}" \
  182. build \
  183. test
  184. fi
  185. ;;
  186. Auth-xcodebuild-*)
  187. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  188. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  189. RunXcodebuild \
  190. -workspace 'Example/Auth/AuthSample/AuthSample.xcworkspace' \
  191. -scheme "Auth_ApiTests" \
  192. "${xcb_flags[@]}" \
  193. build \
  194. test
  195. fi
  196. ;;
  197. InAppMessaging-xcodebuild-iOS)
  198. RunXcodebuild \
  199. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  200. -scheme 'InAppMessaging_Example_iOS' \
  201. "${xcb_flags[@]}" \
  202. build \
  203. test
  204. cd InAppMessaging/Example
  205. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  206. pod update --no-repo-update
  207. cd ../..
  208. RunXcodebuild \
  209. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  210. -scheme 'InAppMessaging_Example_iOS' \
  211. "${xcb_flags[@]}" \
  212. build \
  213. test
  214. # Run UI tests on both iPad and iPhone simulators
  215. # TODO: Running two destinations from one xcodebuild command stopped working with Xcode 10.
  216. # Consider separating static library tests to a separate job.
  217. RunXcodebuild \
  218. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  219. -scheme 'FiamDisplaySwiftExample' \
  220. "${xcb_flags[@]}" \
  221. build \
  222. test
  223. RunXcodebuild \
  224. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  225. -scheme 'FiamDisplaySwiftExample' \
  226. -sdk 'iphonesimulator' \
  227. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  228. build \
  229. test
  230. cd InAppMessagingDisplay/Example
  231. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  232. pod update --no-repo-update
  233. cd ../..
  234. # Run UI tests on both iPad and iPhone simulators
  235. RunXcodebuild \
  236. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  237. -scheme 'FiamDisplaySwiftExample' \
  238. "${xcb_flags[@]}" \
  239. build \
  240. test
  241. RunXcodebuild \
  242. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  243. -scheme 'FiamDisplaySwiftExample' \
  244. -sdk 'iphonesimulator' \
  245. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  246. build \
  247. test
  248. ;;
  249. Firestore-xcodebuild-*)
  250. "${firestore_emulator}" start
  251. trap '"${firestore_emulator}" stop' ERR EXIT
  252. if [[ "$xcode_major" -lt 9 ]]; then
  253. # When building and testing for Xcode 8, only test unit tests.
  254. RunXcodebuild \
  255. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  256. -scheme "Firestore_Tests_$platform" \
  257. "${xcb_flags[@]}" \
  258. build \
  259. test
  260. else
  261. # IntegrationTests run all the tests, including Swift tests, which
  262. # require Swift 4.0 and Xcode 9+.
  263. RunXcodebuild \
  264. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  265. -scheme "Firestore_IntegrationTests_$platform" \
  266. "${xcb_flags[@]}" \
  267. build \
  268. test
  269. fi
  270. ;;
  271. Firestore-cmake-macOS)
  272. test -d build || mkdir build
  273. echo "Preparing cmake build ..."
  274. (cd build; cmake "${cmake_options[@]}" ..)
  275. echo "Building cmake build ..."
  276. cpus=$(sysctl -n hw.ncpu)
  277. (cd build; env make -j $cpus all generate_protos)
  278. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  279. ;;
  280. SymbolCollision-xcodebuild-*)
  281. RunXcodebuild \
  282. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  283. -scheme "SymbolCollisionTest" \
  284. "${xcb_flags[@]}" \
  285. build
  286. ;;
  287. GoogleDataTransport-xcodebuild-*)
  288. RunXcodebuild \
  289. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  290. -scheme "GoogleDataTransport-$platform-Unit-Tests-Unit" \
  291. "${xcb_flags[@]}" \
  292. build \
  293. test
  294. RunXcodebuild \
  295. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  296. -scheme "GoogleDataTransport-$platform-Unit-Tests-Lifecycle" \
  297. "${xcb_flags[@]}" \
  298. build \
  299. test
  300. ;;
  301. GoogleDataTransportIntegrationTest-xcodebuild-*)
  302. RunXcodebuild \
  303. -workspace 'gen/GoogleDataTransport/GoogleDataTransport.xcworkspace' \
  304. -scheme "GoogleDataTransport-$platform-Unit-Tests-Integration" \
  305. "${xcb_flags[@]}" \
  306. build \
  307. test
  308. ;;
  309. GoogleDataTransportCCTSupport-xcodebuild-*)
  310. RunXcodebuild \
  311. -workspace 'gen/GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport.xcworkspace' \
  312. -scheme "GoogleDataTransportCCTSupport-$platform-Unit-Tests-Unit" \
  313. "${xcb_flags[@]}" \
  314. build \
  315. test
  316. RunXcodebuild \
  317. -workspace 'gen/GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport.xcworkspace' \
  318. -scheme "GoogleDataTransportCCTSupport-$platform-Unit-Tests-Integration" \
  319. "${xcb_flags[@]}" \
  320. build \
  321. test
  322. ;;
  323. Database-xcodebuild-*)
  324. RunXcodebuild \
  325. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  326. -scheme "FirebaseDatabase-iOS-Unit-unit" \
  327. "${ios_flags[@]}" \
  328. "${xcb_flags[@]}" \
  329. build \
  330. test
  331. RunXcodebuild \
  332. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  333. -scheme "FirebaseDatabase-macOS-Unit-unit" \
  334. "${macos_flags[@]}" \
  335. "${xcb_flags[@]}" \
  336. build \
  337. test
  338. RunXcodebuild \
  339. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  340. -scheme "FirebaseDatabase-tvOS-Unit-unit" \
  341. "${tvos_flags[@]}" \
  342. "${xcb_flags[@]}" \
  343. build \
  344. test
  345. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  346. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  347. # Integration tests are only run on iOS to minimize flake failures.
  348. RunXcodebuild \
  349. -workspace 'gen/FirebaseDatabase/FirebaseDatabase.xcworkspace' \
  350. -scheme "FirebaseDatabase-iOS-Unit-integration" \
  351. "${ios_flags[@]}" \
  352. "${xcb_flags[@]}" \
  353. build \
  354. test
  355. fi
  356. ;;
  357. Messaging-xcodebuild-*)
  358. RunXcodebuild \
  359. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  360. -scheme "FirebaseMessaging-iOS-Unit-unit" \
  361. "${ios_flags[@]}" \
  362. "${xcb_flags[@]}" \
  363. build \
  364. test
  365. RunXcodebuild \
  366. -workspace 'gen/FirebaseMessaging/FirebaseMessaging.xcworkspace' \
  367. -scheme "FirebaseMessaging-tvOS-Unit-unit" \
  368. "${tvos_flags[@]}" \
  369. "${xcb_flags[@]}" \
  370. build \
  371. test
  372. ;;
  373. Storage-xcodebuild-*)
  374. RunXcodebuild \
  375. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  376. -scheme "FirebaseStorage-iOS-Unit-unit" \
  377. "${ios_flags[@]}" \
  378. "${xcb_flags[@]}" \
  379. build \
  380. test
  381. RunXcodebuild \
  382. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  383. -scheme "FirebaseStorage-macOS-Unit-unit" \
  384. "${macos_flags[@]}" \
  385. "${xcb_flags[@]}" \
  386. build \
  387. test
  388. RunXcodebuild \
  389. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  390. -scheme "FirebaseStorage-tvOS-Unit-unit" \
  391. "${tvos_flags[@]}" \
  392. "${xcb_flags[@]}" \
  393. build \
  394. test
  395. if [[ "$TRAVIS_PULL_REQUEST" == "false" ||
  396. "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then
  397. # Integration tests are only run on iOS to minimize flake failures.
  398. RunXcodebuild \
  399. -workspace 'gen/FirebaseStorage/FirebaseStorage.xcworkspace' \
  400. -scheme "FirebaseStorage-iOS-Unit-integration" \
  401. "${ios_flags[@]}" \
  402. "${xcb_flags[@]}" \
  403. build \
  404. test
  405. fi
  406. ;;
  407. *)
  408. echo "Don't know how to build this product-platform-method combination" 1>&2
  409. echo " product=$product" 1>&2
  410. echo " platform=$platform" 1>&2
  411. echo " method=$method" 1>&2
  412. exit 1
  413. ;;
  414. esac