build.sh 8.5 KB

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