build.sh 6.3 KB

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