build.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/bin/bash
  2. set -o pipefail
  3. set -e
  4. usage() {
  5. cat <<EOF
  6. Usage: sh $0 command [argument]
  7. command:
  8. test-all: tests all projects in this repo.
  9. test-ios-objc-static: tests iOS Objective-C static example.
  10. test-ios-objc-dynamic: tests iOS Objective-C dynamic example.
  11. test-ios-objc-xcframework: tests iOS Objective-C xcframework example.
  12. test-ios-objc-cocoapods: tests iOS Objective-C CocoaPods example.
  13. test-ios-objc-cocoapods-dynamic: tests iOS Objective-C CocoaPods Dynamic example.
  14. test-ios-objc-carthage: tests iOS Objective-C Carthage example.
  15. test-ios-swift-dynamic: tests iOS Swift dynamic example.
  16. test-ios-swift-xcframework: tests iOS Swift xcframework example.
  17. test-ios-swift-cocoapods: tests iOS Swift CocoaPods example.
  18. test-ios-swift-carthage: tests iOS Swift Carthage example.
  19. test-ios-spm: tests iOS Swift Package Manager example.
  20. test-osx-objc-dynamic: tests macOS Objective-C dynamic example.
  21. test-osx-objc-xcframework: tests macOS Objective-C xcframework example.
  22. test-osx-objc-cocoapods: tests macOS Objective-C CocoaPods example.
  23. test-osx-objc-carthage: tests macOS Objective-C Carthage example.
  24. test-osx-swift-dynamic: tests macOS Swift dynamic example.
  25. test-osx-swift-xcframework: tests macOS Swift xcframework example.
  26. test-osx-swift-cocoapods: tests macOS Swift CocoaPods example.
  27. test-osx-swift-carthage: tests macOS Swift Carthage example.
  28. test-osx-spm: tests macOS Swift Package Manager example.
  29. test-watchos-objc-dynamic: tests watchOS Objective-C dynamic example.
  30. test-watchos-objc-xcframework: tests watchOS Objective-C xcframework example.
  31. test-watchos-objc-cocoapods: tests watchOS Objective-C CocoaPods example.
  32. test-watchos-objc-carthage: tests watchOS Objective-C Carthage example.
  33. test-watchos-swift-dynamic: tests watchOS Swift dynamic example.
  34. test-watchos-swift-xcframework: tests watchOS Swift xcframework example.
  35. test-watchos-swift-cocoapods: tests watchOS Swift CocoaPods example.
  36. test-watchos-swift-carthage: tests watchOS Swift Carthage example.
  37. EOF
  38. }
  39. COMMAND="$1"
  40. # https://github.com/CocoaPods/CocoaPods/issues/7708
  41. export EXPANDED_CODE_SIGN_IDENTITY=''
  42. download_zip_if_needed() {
  43. LANG="$1"
  44. local DIRECTORY=realm-$LANG-latest
  45. if [ ! -d "$DIRECTORY" ]; then
  46. curl -o "$DIRECTORY".zip -L https://static.realm.io/downloads/"$LANG"/latest
  47. unzip "$DIRECTORY".zip
  48. rm "$DIRECTORY".zip
  49. mv realm-"$LANG"-* "$DIRECTORY"
  50. fi
  51. }
  52. xcode_version_major() {
  53. echo "${REALM_XCODE_VERSION%%.*}"
  54. }
  55. xctest() {
  56. local PLATFORM="$1"
  57. local LANG="$2"
  58. local NAME="$3"
  59. local DIRECTORY="$PLATFORM/$LANG/$NAME"
  60. if [[ ! -d "$DIRECTORY" ]]; then
  61. DIRECTORY="${DIRECTORY/swift/swift-$REALM_SWIFT_VERSION}"
  62. fi
  63. if [[ $PLATFORM != osx ]]; then
  64. if [[ $NAME == Carthage* ]]; then
  65. # Building for Carthage requires that a simulator exist but not any
  66. # particular one, and having more than one makes xcodebuild
  67. # significantly slower and some of Carthage's operations time out.
  68. sh "$(dirname "$0")/../../scripts/reset-simulators.sh" -firstOnly
  69. else
  70. # The other installation methods depend on some specific simulators
  71. # existing so just create all of them to be safe.
  72. sh "$(dirname "$0")/../../scripts/reset-simulators.sh"
  73. fi
  74. fi
  75. if [[ $NAME == CocoaPods* ]]; then
  76. pod install --project-directory="$DIRECTORY"
  77. elif [[ $NAME == Carthage* ]]; then
  78. (
  79. cd "$DIRECTORY"
  80. if [ -n "${REALM_BUILD_USING_LATEST_RELEASE:-}" ]; then
  81. echo "github \"realm/realm-cocoa\"" > Cartfile
  82. else
  83. echo "github \"realm/realm-cocoa\" \"${sha:-master}\"" > Cartfile
  84. fi
  85. if [[ $PLATFORM == ios ]]; then
  86. carthage update --platform iOS
  87. elif [[ $PLATFORM == osx ]]; then
  88. carthage update --platform Mac
  89. elif [[ $PLATFORM == watchos ]]; then
  90. carthage update --platform watchOS
  91. fi
  92. )
  93. elif [[ $NAME == SwiftPackageManager* ]]; then
  94. if [ -n "$sha" ]; then
  95. sed -i '' 's@branch = "master"@branch = "'"$sha"'"@' "$DIRECTORY/$NAME.xcodeproj/project.pbxproj"
  96. fi
  97. elif [[ $LANG == swift* ]]; then
  98. download_zip_if_needed swift
  99. else
  100. download_zip_if_needed "$LANG"
  101. fi
  102. local destination=()
  103. if [[ $PLATFORM == ios ]]; then
  104. simulator_id="$(xcrun simctl list devices | grep -v unavailable | grep -m 1 -o '[0-9A-F\-]\{36\}')"
  105. xcrun simctl boot "$simulator_id"
  106. destination=(-destination "id=$simulator_id")
  107. elif [[ $PLATFORM == watchos ]]; then
  108. destination=(-sdk watchsimulator)
  109. fi
  110. local project=(-project "$DIRECTORY/$NAME.xcodeproj")
  111. local workspace="$DIRECTORY/$NAME.xcworkspace"
  112. if [ -d "$workspace" ]; then
  113. project=(-workspace "$workspace")
  114. fi
  115. local code_signing_flags=('CODE_SIGN_IDENTITY=' 'CODE_SIGNING_REQUIRED=NO' 'AD_HOC_CODE_SIGNING_ALLOWED=YES')
  116. local scheme=(-scheme "$NAME")
  117. # Ensure that dynamic framework tests try to use the correct version of the prebuilt libraries.
  118. sed -i '' 's@/swift-[0-9.]*@/swift-'"${REALM_XCODE_VERSION}"'@' "$DIRECTORY/$NAME.xcodeproj/project.pbxproj"
  119. xcodebuild "${project[@]}" "${scheme[@]}" clean build "${destination[@]}" "${code_signing_flags[@]}"
  120. if [[ $PLATFORM != watchos ]]; then
  121. xcodebuild "${project[@]}" "${scheme[@]}" test "${destination[@]}" "${code_signing_flags[@]}"
  122. fi
  123. if [[ $PLATFORM != osx ]]; then
  124. [[ $PLATFORM == 'ios' ]] && SDK=iphoneos || SDK=$PLATFORM
  125. if [ -d "$workspace" ]; then
  126. [[ $LANG == 'swift' ]] && scheme=(-scheme RealmSwift) || scheme=(-scheme Realm)
  127. else
  128. scheme=()
  129. fi
  130. xcodebuild "${project[@]}" "${scheme[@]}" -sdk "$SDK" build "${code_signing_flags[@]}"
  131. fi
  132. }
  133. swiftpm() {
  134. PLATFORM="$1"
  135. cd SwiftPMExample
  136. xcrun swift build
  137. }
  138. # shellcheck source=../../scripts/swift-version.sh
  139. source "$(dirname "$0")/../../scripts/swift-version.sh"
  140. set_xcode_and_swift_versions # exports REALM_SWIFT_VERSION, REALM_XCODE_VERSION, and DEVELOPER_DIR variables if not already set
  141. PLATFORM=$(echo "$COMMAND" | cut -d - -f 2)
  142. LANGUAGE=$(echo "$COMMAND" | cut -d - -f 3)
  143. case "$COMMAND" in
  144. "test-all")
  145. for target in ios-swift-dynamic ios-swift-cocoapods osx-swift-dynamic ios-swift-carthage osx-swift-carthage; do
  146. ./build.sh test-$target || exit 1
  147. done
  148. for target in ios osx; do
  149. ./build.sh test-$target-spm || exit 1
  150. done
  151. ;;
  152. test-*-*-cocoapods)
  153. xctest "$PLATFORM" "$LANGUAGE" CocoaPodsExample
  154. ;;
  155. test-*-*-cocoapods-dynamic)
  156. xctest "$PLATFORM" "$LANGUAGE" CocoaPodsDynamicExample
  157. ;;
  158. test-*-*-static)
  159. xctest "$PLATFORM" "$LANGUAGE" StaticExample
  160. ;;
  161. test-*-*-dynamic)
  162. xctest "$PLATFORM" "$LANGUAGE" DynamicExample
  163. ;;
  164. test-*-*-xcframework)
  165. xctest "$PLATFORM" "$LANGUAGE" XCFrameworkExample
  166. ;;
  167. test-*-*-carthage)
  168. xctest "$PLATFORM" "$LANGUAGE" CarthageExample
  169. ;;
  170. test-ios-spm)
  171. # We have to "hide" the spm example from carthage because otherwise
  172. # it'll fetch the example's package dependencies as part of deciding
  173. # what to build from this repo.
  174. if ! [ -L ios/swift/SwiftPackageManagerExample/SwiftPackageManagerExample.xcodeproj/project.pbxproj ]; then
  175. mkdir -p ios/swift/SwiftPackageManagerExample/SwiftPackageManagerExample.xcodeproj
  176. ln -s ../project.pbxproj ios/swift/SwiftPackageManagerExample/SwiftPackageManagerExample.xcodeproj
  177. fi
  178. xctest "$PLATFORM" swift SwiftPackageManagerExample
  179. ;;
  180. test-*-spm)
  181. swiftpm "$PLATFORM"
  182. ;;
  183. *)
  184. echo "Unknown command '$COMMAND'"
  185. usage
  186. exit 1
  187. ;;
  188. esac