build.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. # Run integration tests (not allowed on PRs)
  160. if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
  161. RunXcodebuild \
  162. -workspace 'Example/Firebase.xcworkspace' \
  163. -scheme "Auth_ApiTests" \
  164. "${xcb_flags[@]}" \
  165. build \
  166. test
  167. RunXcodebuild \
  168. -workspace 'Example/Firebase.xcworkspace' \
  169. -scheme "Storage_IntegrationTests_iOS" \
  170. "${xcb_flags[@]}" \
  171. build \
  172. test
  173. RunXcodebuild \
  174. -workspace 'Example/Firebase.xcworkspace' \
  175. -scheme "Database_IntegrationTests_iOS" \
  176. "${xcb_flags[@]}" \
  177. build \
  178. test
  179. fi
  180. # Test iOS Objective-C static library build
  181. cd Example
  182. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  183. pod update --no-repo-update
  184. cd ..
  185. RunXcodebuild \
  186. -workspace 'Example/Firebase.xcworkspace' \
  187. -scheme "AllUnitTests_$platform" \
  188. "${xcb_flags[@]}" \
  189. build \
  190. test
  191. fi
  192. ;;
  193. InAppMessaging-xcodebuild-iOS)
  194. RunXcodebuild \
  195. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  196. -scheme 'InAppMessaging_Example_iOS' \
  197. "${xcb_flags[@]}" \
  198. build \
  199. test
  200. cd InAppMessaging/Example
  201. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  202. pod update --no-repo-update
  203. cd ../..
  204. RunXcodebuild \
  205. -workspace 'InAppMessaging/Example/InAppMessaging-Example-iOS.xcworkspace' \
  206. -scheme 'InAppMessaging_Example_iOS' \
  207. "${xcb_flags[@]}" \
  208. build \
  209. test
  210. # Run UI tests on both iPad and iPhone simulators
  211. # TODO: Running two destinations from one xcodebuild command stopped working with Xcode 10.
  212. # Consider separating static library tests to a separate job.
  213. RunXcodebuild \
  214. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  215. -scheme 'FiamDisplaySwiftExample' \
  216. "${xcb_flags[@]}" \
  217. build \
  218. test
  219. RunXcodebuild \
  220. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  221. -scheme 'FiamDisplaySwiftExample' \
  222. -sdk 'iphonesimulator' \
  223. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  224. build \
  225. test
  226. cd InAppMessagingDisplay/Example
  227. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  228. pod update --no-repo-update
  229. cd ../..
  230. # Run UI tests on both iPad and iPhone simulators
  231. RunXcodebuild \
  232. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  233. -scheme 'FiamDisplaySwiftExample' \
  234. "${xcb_flags[@]}" \
  235. build \
  236. test
  237. RunXcodebuild \
  238. -workspace 'InAppMessagingDisplay/Example/InAppMessagingDisplay-Sample.xcworkspace' \
  239. -scheme 'FiamDisplaySwiftExample' \
  240. -sdk 'iphonesimulator' \
  241. -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)' \
  242. build \
  243. test
  244. ;;
  245. Firestore-xcodebuild-iOS)
  246. RunXcodebuild \
  247. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  248. -scheme "Firestore_Tests_$platform" \
  249. "${xcb_flags[@]}" \
  250. build \
  251. test
  252. # Firestore_SwiftTests_iOS require Swift 4, which needs Xcode 9
  253. if [[ "$xcode_major" -ge 9 ]]; then
  254. RunXcodebuild \
  255. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  256. -scheme "Firestore_SwiftTests_$platform" \
  257. "${xcb_flags[@]}" \
  258. build \
  259. test
  260. fi
  261. RunXcodebuild \
  262. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  263. -scheme "Firestore_IntegrationTests_$platform" \
  264. "${xcb_flags[@]}" \
  265. build
  266. ;;
  267. Firestore-cmake-macOS)
  268. test -d build || mkdir build
  269. echo "Preparing cmake build ..."
  270. (cd build; cmake "${cmake_options[@]}" ..)
  271. echo "Building cmake build ..."
  272. cpus=$(sysctl -n hw.ncpu)
  273. (cd build; env make -j $cpus all generate_protos)
  274. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus test)
  275. ;;
  276. SymbolCollision-xcodebuild-*)
  277. RunXcodebuild \
  278. -workspace 'SymbolCollisionTest/SymbolCollisionTest.xcworkspace' \
  279. -scheme "SymbolCollisionTest" \
  280. "${xcb_flags[@]}" \
  281. build
  282. ;;
  283. *)
  284. echo "Don't know how to build this product-platform-method combination" 1>&2
  285. echo " product=$product" 1>&2
  286. echo " platform=$platform" 1>&2
  287. echo " method=$method" 1>&2
  288. exit 1
  289. ;;
  290. esac