build.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. # Runs xcodebuild with the given flags, piping output to xcpretty
  57. # If xcodebuild fails with known error codes, retries once.
  58. function RunXcodebuild() {
  59. echo xcodebuild "$@"
  60. xcodebuild "$@" | xcpretty; result=$?
  61. if [[ $result == 65 ]]; then
  62. echo "xcodebuild exited with 65, retrying" 1>&2
  63. sleep 5
  64. xcodebuild "$@" | xcpretty; result=$?
  65. fi
  66. if [[ $result != 0 ]]; then
  67. exit $result
  68. fi
  69. }
  70. # Compute standard flags for all platforms
  71. case "$platform" in
  72. iOS)
  73. xcb_flags=(
  74. -sdk 'iphonesimulator'
  75. -destination 'platform=iOS Simulator,name=iPhone 7'
  76. )
  77. ;;
  78. macOS)
  79. xcb_flags=(
  80. -sdk 'macosx'
  81. -destination 'platform=OS X,arch=x86_64'
  82. )
  83. ;;
  84. tvOS)
  85. xcb_flags=(
  86. -sdk "appletvsimulator"
  87. -destination 'platform=tvOS Simulator,name=Apple TV'
  88. )
  89. ;;
  90. *)
  91. echo "Unknown platform '$platform'" 1>&2
  92. exit 1
  93. ;;
  94. esac
  95. xcb_flags+=(
  96. ONLY_ACTIVE_ARCH=YES
  97. CODE_SIGNING_REQUIRED=NO
  98. CODE_SIGNING_ALLOWED=YES
  99. COMPILER_INDEX_STORE_ENABLE=NO
  100. )
  101. # TODO(varconst): --warn-unused-vars - right now, it makes the log overflow on
  102. # Travis.
  103. cmake_options=(
  104. -Wdeprecated
  105. --warn-uninitialized
  106. )
  107. xcode_version=$(xcodebuild -version | head -n 1)
  108. xcode_version="${xcode_version/Xcode /}"
  109. xcode_major="${xcode_version/.*/}"
  110. if [[ -n "${SANITIZERS:-}" ]]; then
  111. for sanitizer in $SANITIZERS; do
  112. case "$sanitizer" in
  113. asan)
  114. xcb_flags+=(
  115. -enableAddressSanitizer YES
  116. )
  117. cmake_options+=(
  118. -DWITH_ASAN=ON
  119. )
  120. ;;
  121. tsan)
  122. xcb_flags+=(
  123. -enableThreadSanitizer YES
  124. )
  125. cmake_options+=(
  126. -DWITH_TSAN=ON
  127. )
  128. ;;
  129. ubsan)
  130. xcb_flags+=(
  131. -enableUndefinedBehaviorSanitizer YES
  132. )
  133. cmake_options+=(
  134. -DWITH_UBSAN=ON
  135. )
  136. ;;
  137. *)
  138. echo "Unknown sanitizer '$sanitizer'" 1>&2
  139. exit 1
  140. ;;
  141. esac
  142. done
  143. fi
  144. case "$product-$method-$platform" in
  145. Firebase-xcodebuild-*)
  146. RunXcodebuild \
  147. -workspace 'Example/Firebase.xcworkspace' \
  148. -scheme "AllUnitTests_$platform" \
  149. "${xcb_flags[@]}" \
  150. build \
  151. test
  152. RunXcodebuild \
  153. -workspace 'GoogleUtilities/Example/GoogleUtilities.xcworkspace' \
  154. -scheme "Example_$platform" \
  155. "${xcb_flags[@]}" \
  156. build \
  157. test
  158. if [[ $platform == 'iOS' ]]; then
  159. RunXcodebuild \
  160. -workspace 'Functions/Example/FirebaseFunctions.xcworkspace' \
  161. -scheme "FirebaseFunctions_Tests" \
  162. "${xcb_flags[@]}" \
  163. build \
  164. test
  165. # Run integration tests (not allowed on PRs)
  166. if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
  167. RunXcodebuild \
  168. -workspace 'Example/Firebase.xcworkspace' \
  169. -scheme "Auth_ApiTests" \
  170. "${xcb_flags[@]}" \
  171. build \
  172. test
  173. RunXcodebuild \
  174. -workspace 'Example/Firebase.xcworkspace' \
  175. -scheme "Storage_IntegrationTests_iOS" \
  176. "${xcb_flags[@]}" \
  177. build \
  178. test
  179. RunXcodebuild \
  180. -workspace 'Example/Firebase.xcworkspace' \
  181. -scheme "Database_IntegrationTests_iOS" \
  182. "${xcb_flags[@]}" \
  183. build \
  184. test
  185. fi
  186. # Test iOS Objective-C static library build
  187. cd Example
  188. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  189. pod update --no-repo-update
  190. cd ..
  191. RunXcodebuild \
  192. -workspace 'Example/Firebase.xcworkspace' \
  193. -scheme "AllUnitTests_$platform" \
  194. "${xcb_flags[@]}" \
  195. build \
  196. test
  197. cd Functions/Example
  198. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  199. pod update --no-repo-update
  200. cd ../..
  201. RunXcodebuild \
  202. -workspace 'Functions/Example/FirebaseFunctions.xcworkspace' \
  203. -scheme "FirebaseFunctions_Tests" \
  204. "${xcb_flags[@]}" \
  205. build \
  206. test
  207. fi
  208. ;;
  209. InAppMessaging-xcodebuild-iOS)
  210. RunXcodebuild \
  211. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  212. -scheme 'InAppMessaging_Example_iOS' \
  213. "${xcb_flags[@]}" \
  214. build \
  215. test
  216. cd InAppMessaging/Example
  217. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  218. pod update --no-repo-update
  219. cd ../..
  220. RunXcodebuild \
  221. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  222. -scheme 'InAppMessaging_Example_iOS' \
  223. "${xcb_flags[@]}" \
  224. build \
  225. test
  226. # Run UI tests on both iPad and iPhone simulators
  227. # TODO: Running two destinations from one xcodebuild command stopped working with Xcode 10.
  228. # Consider separating static library tests to a separate job.
  229. RunXcodebuild \
  230. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  231. -scheme 'FiamDisplaySwiftExample' \
  232. "${xcb_flags[@]}" \
  233. build \
  234. test
  235. RunXcodebuild \
  236. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  237. -scheme 'FiamDisplaySwiftExample' \
  238. -sdk 'iphonesimulator' \
  239. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  240. build \
  241. test
  242. cd InAppMessagingDisplay/Example
  243. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  244. pod update --no-repo-update
  245. cd ../..
  246. # Run UI tests on both iPad and iPhone simulators
  247. RunXcodebuild \
  248. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  249. -scheme 'FiamDisplaySwiftExample' \
  250. "${xcb_flags[@]}" \
  251. build \
  252. test
  253. RunXcodebuild \
  254. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  255. -scheme 'FiamDisplaySwiftExample' \
  256. -sdk 'iphonesimulator' \
  257. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  258. build \
  259. test
  260. ;;
  261. Firestore-xcodebuild-iOS)
  262. RunXcodebuild \
  263. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  264. -scheme "Firestore_Tests_$platform" \
  265. "${xcb_flags[@]}" \
  266. build \
  267. test
  268. # Firestore_SwiftTests_iOS require Swift 4, which needs Xcode 9
  269. if [[ "$xcode_major" -ge 9 ]]; then
  270. RunXcodebuild \
  271. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  272. -scheme "Firestore_SwiftTests_$platform" \
  273. "${xcb_flags[@]}" \
  274. build \
  275. test
  276. fi
  277. RunXcodebuild \
  278. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  279. -scheme "Firestore_IntegrationTests_$platform" \
  280. "${xcb_flags[@]}" \
  281. build
  282. ;;
  283. Firestore-cmake-macOS)
  284. test -d build || mkdir build
  285. echo "Preparing cmake build ..."
  286. (cd build; cmake "${cmake_options[@]}" ..)
  287. echo "Building cmake build ..."
  288. cpus=$(sysctl -n hw.ncpu)
  289. (cd build; env make -j $cpus all generate_protos)
  290. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  291. ;;
  292. SymbolCollision-xcodebuild-*)
  293. RunXcodebuild \
  294. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  295. -scheme "SymbolCollisionTest" \
  296. "${xcb_flags[@]}" \
  297. build
  298. ;;
  299. *)
  300. echo "Don't know how to build this product-platform-method combination" 1>&2
  301. echo " product=$product" 1>&2
  302. echo " platform=$platform" 1>&2
  303. echo " method=$method" 1>&2
  304. exit 1
  305. ;;
  306. esac