build.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. )
  96. # TODO(varconst): --warn-unused-vars - right now, it makes the log overflow on
  97. # Travis.
  98. cmake_options=(
  99. -Wdeprecated
  100. --warn-uninitialized
  101. )
  102. if [[ -n "${SANITIZERS:-}" ]]; then
  103. for sanitizer in $SANITIZERS; do
  104. case "$sanitizer" in
  105. asan)
  106. xcb_flags+=(
  107. -enableAddressSanitizer YES
  108. )
  109. cmake_options+=(
  110. -DWITH_ASAN=ON
  111. )
  112. ;;
  113. tsan)
  114. xcb_flags+=(
  115. -enableThreadSanitizer YES
  116. )
  117. cmake_options+=(
  118. -DWITH_TSAN=ON
  119. )
  120. ;;
  121. ubsan)
  122. xcb_flags+=(
  123. -enableUndefinedBehaviorSanitizer YES
  124. )
  125. cmake_options+=(
  126. -DWITH_UBSAN=ON
  127. )
  128. ;;
  129. *)
  130. echo "Unknown sanitizer '$sanitizer'" 1>&2
  131. exit 1
  132. ;;
  133. esac
  134. done
  135. fi
  136. case "$product-$method-$platform" in
  137. Firebase-xcodebuild-*)
  138. RunXcodebuild \
  139. -workspace 'Example/Firebase.xcworkspace' \
  140. -scheme "AllUnitTests_$platform" \
  141. "${xcb_flags[@]}" \
  142. build \
  143. test
  144. if [[ $platform == 'iOS' ]]; then
  145. RunXcodebuild \
  146. -workspace 'Functions/Example/FirebaseFunctions.xcworkspace' \
  147. -scheme "FirebaseFunctions_Tests" \
  148. "${xcb_flags[@]}" \
  149. build \
  150. test
  151. # Test iOS Objective-C static library build
  152. cd Example
  153. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  154. pod update --no-repo-update
  155. # Workarounds for https://github.com/CocoaPods/CocoaPods/issues/7592.
  156. # Remove when updating to CocoaPods 1.5.1
  157. sed -i -e 's/-l"FirebaseMessaging"//' "Pods/Target Support Files/Pods-Messaging_Tests_iOS/Pods-Messaging_Tests_iOS.debug.xcconfig"
  158. sed -i -e 's/-l"FirebaseAuth-iOS" -l"FirebaseCore-iOS"//' "Pods/Target Support Files/Pods-Auth_Tests_iOS/Pods-Auth_Tests_iOS.debug.xcconfig"
  159. cd ..
  160. RunXcodebuild \
  161. -workspace 'Example/Firebase.xcworkspace' \
  162. -scheme "AllUnitTests_$platform" \
  163. "${xcb_flags[@]}" \
  164. build \
  165. test
  166. cd Functions/Example
  167. sed -i -e 's/use_frameworks/\#use_frameworks/' Podfile
  168. pod update --no-repo-update
  169. cd ../..
  170. RunXcodebuild \
  171. -workspace 'Functions/Example/FirebaseFunctions.xcworkspace' \
  172. -scheme "FirebaseFunctions_Tests" \
  173. "${xcb_flags[@]}" \
  174. build \
  175. test
  176. fi
  177. ;;
  178. Firestore-xcodebuild-iOS)
  179. RunXcodebuild \
  180. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  181. -scheme "Firestore_Tests_$platform" \
  182. "${xcb_flags[@]}" \
  183. build \
  184. test
  185. RunXcodebuild \
  186. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  187. -scheme 'SwiftBuildTest' \
  188. "${xcb_flags[@]}" \
  189. build
  190. ;;
  191. Firestore-cmake-macOS)
  192. test -d build || mkdir build
  193. echo "Preparing cmake build ..."
  194. (cd build; cmake "${cmake_options[@]}" ..)
  195. echo "Building cmake build ..."
  196. cpus=$(sysctl -n hw.ncpu)
  197. (cd build; env CTEST_OUTPUT_ON_FAILURE=1 make -j $cpus all)
  198. ;;
  199. *)
  200. echo "Don't know how to build this product-platform-method combination" 1>&2
  201. echo " product=$product" 1>&2
  202. echo " platform=$platform" 1>&2
  203. echo " method=$method" 1>&2
  204. exit 1
  205. ;;
  206. esac