build.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 8 Plus'
  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. if [[ -n "${SANITIZERS:-}" ]]; then
  104. for sanitizer in $SANITIZERS; do
  105. case "$sanitizer" in
  106. asan)
  107. xcb_flags+=(
  108. -enableAddressSanitizer YES
  109. )
  110. cmake_options+=(
  111. -DWITH_ASAN=ON
  112. )
  113. ;;
  114. tsan)
  115. xcb_flags+=(
  116. -enableThreadSanitizer YES
  117. )
  118. cmake_options+=(
  119. -DWITH_TSAN=ON
  120. )
  121. ;;
  122. ubsan)
  123. xcb_flags+=(
  124. -enableUndefinedBehaviorSanitizer YES
  125. )
  126. cmake_options+=(
  127. -DWITH_UBSAN=ON
  128. )
  129. ;;
  130. *)
  131. echo "Unknown sanitizer '$sanitizer'" 1>&2
  132. exit 1
  133. ;;
  134. esac
  135. done
  136. fi
  137. case "$product-$method-$platform" in
  138. Firebase-xcodebuild-*)
  139. RunXcodebuild \
  140. -workspace 'Example/Firebase.xcworkspace' \
  141. -scheme "AllUnitTests_$platform" \
  142. "${xcb_flags[@]}" \
  143. build \
  144. test
  145. if [[ $platform == 'iOS' ]]; then
  146. RunXcodebuild \
  147. -workspace 'Functions/Example/FirebaseFunctions.xcworkspace' \
  148. -scheme "FirebaseFunctions_Tests" \
  149. "${xcb_flags[@]}" \
  150. build \
  151. test
  152. # Test iOS Objective-C static library build
  153. cd Example
  154. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  155. pod update --no-repo-update
  156. cd ..
  157. RunXcodebuild \
  158. -workspace 'Example/Firebase.xcworkspace' \
  159. -scheme "AllUnitTests_$platform" \
  160. "${xcb_flags[@]}" \
  161. build \
  162. test
  163. cd Functions/Example
  164. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  165. pod update --no-repo-update
  166. cd ../..
  167. RunXcodebuild \
  168. -workspace 'Functions/Example/FirebaseFunctions.xcworkspace' \
  169. -scheme "FirebaseFunctions_Tests" \
  170. "${xcb_flags[@]}" \
  171. build \
  172. test
  173. fi
  174. ;;
  175. Firestore-xcodebuild-iOS)
  176. RunXcodebuild \
  177. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  178. -scheme "Firestore_Tests_$platform" \
  179. "${xcb_flags[@]}" \
  180. build \
  181. test
  182. RunXcodebuild \
  183. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  184. -scheme "Firestore_IntegrationTests_$platform" \
  185. "${xcb_flags[@]}" \
  186. build
  187. RunXcodebuild \
  188. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  189. -scheme 'SwiftBuildTest' \
  190. "${xcb_flags[@]}" \
  191. build
  192. ;;
  193. Firestore-cmake-macOS)
  194. test -d build || mkdir build
  195. echo "Preparing cmake build ..."
  196. (cd build; cmake "${cmake_options[@]}" ..)
  197. echo "Building cmake build ..."
  198. cpus=$(sysctl -n hw.ncpu)
  199. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus all)
  200. ;;
  201. *)
  202. echo "Don't know how to build this product-platform-method combination" 1>&2
  203. echo " product=$product" 1>&2
  204. echo " platform=$platform" 1>&2
  205. echo " method=$method" 1>&2
  206. exit 1
  207. ;;
  208. esac