build.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. RunXcodebuild \
  203. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  204. -scheme 'InAppMessaging_Example_iOS' \
  205. "${xcb_flags[@]}" \
  206. build \
  207. test
  208. # Run UI tests on both iPad and iPhone simulators
  209. # TODO: Running two destinations from one xcodebuild command stopped working with Xcode 10.
  210. # Consider separating static library tests to a separate job.
  211. RunXcodebuild \
  212. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  213. -scheme 'FiamDisplaySwiftExample' \
  214. "${xcb_flags[@]}" \
  215. build \
  216. test
  217. RunXcodebuild \
  218. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  219. -scheme 'FiamDisplaySwiftExample' \
  220. -sdk 'iphonesimulator' \
  221. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  222. build \
  223. test
  224. cd InAppMessagingDisplay/Example
  225. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  226. pod update --no-repo-update
  227. cd ../..
  228. # Run UI tests on both iPad and iPhone simulators
  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. ;;
  243. Firestore-xcodebuild-iOS)
  244. RunXcodebuild \
  245. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  246. -scheme "Firestore_Tests_$platform" \
  247. "${xcb_flags[@]}" \
  248. build \
  249. test
  250. # Firestore_SwiftTests_iOS require Swift 4, which needs Xcode 9
  251. if [[ "$xcode_major" -ge 9 ]]; then
  252. RunXcodebuild \
  253. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  254. -scheme "Firestore_SwiftTests_$platform" \
  255. "${xcb_flags[@]}" \
  256. build \
  257. test
  258. fi
  259. RunXcodebuild \
  260. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  261. -scheme "Firestore_IntegrationTests_$platform" \
  262. "${xcb_flags[@]}" \
  263. build
  264. ;;
  265. Firestore-cmake-macOS)
  266. test -d build || mkdir build
  267. echo "Preparing cmake build ..."
  268. (cd build; cmake "${cmake_options[@]}" ..)
  269. echo "Building cmake build ..."
  270. cpus=$(sysctl -n hw.ncpu)
  271. (cd build; env make -j $cpus all generate_protos)
  272. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  273. ;;
  274. SymbolCollision-xcodebuild-*)
  275. RunXcodebuild \
  276. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  277. -scheme "SymbolCollisionTest" \
  278. "${xcb_flags[@]}" \
  279. build
  280. ;;
  281. *)
  282. echo "Don't know how to build this product-platform-method combination" 1>&2
  283. echo " product=$product" 1>&2
  284. echo " platform=$platform" 1>&2
  285. echo " method=$method" 1>&2
  286. exit 1
  287. ;;
  288. esac