build.sh 8.3 KB

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