check_firestore_symbols.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/bin/bash
  2. # Copyright 2023 Google LLC
  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. # DESCRIPTION: This script identifies Objective-C symbols within the
  16. # `FirebaseFirestore.xcframework` that are not automatically linked when used
  17. # in a client target. Because the `FirebaseFirestore.xcframework` should
  18. # function without clients needing to pass the `-ObjC` flag, this script
  19. # catches potential regressions that break that requirement.
  20. #
  21. # DEPENDENCIES: This script depends on the given Firebase repo's `Package.swift`
  22. # using the `FIREBASECI_USE_LOCAL_FIRESTORE_ZIP` env var to swap the Firestore
  23. # target definition out to instead reference a *local* binary using the
  24. # `.binaryTarget(path:)` API.
  25. #
  26. # DESIGN: This script creates an executable package that depends on Firestore
  27. # via a local binary SPM target. The package is built twice, once with the
  28. # -ObjC flag and once without. The linked Objective-C symbols are then
  29. # stripped from each build's resulting executable. The symbols are then diffed
  30. # to determine if there exists symbols that were only linked due to the -ObjC
  31. # flag.
  32. #
  33. # USAGE: ./check_firestore_symbols.sh <PATH_TO_FIREBASE_REPO> <PATH_TO_FIRESTORE_XCFRAMEWORK>
  34. set -euo pipefail
  35. if [[ $# -ne 2 ]]; then
  36. echo "Usage: ./check_firestore_symbols.sh <PATH_TO_FIREBASE_REPO> <PATH_TO_FIRESTORE_XCFRAMEWORK>"
  37. exit 1
  38. fi
  39. # Check if the given repo path is valid.
  40. FIREBASE_REPO_PATH=$1
  41. if [[ "$FIREBASE_REPO_PATH" != /* ]]; then
  42. echo "The given path should be an absolute path."
  43. exit 1
  44. fi
  45. if [[ ! -d "$FIREBASE_REPO_PATH" ]]; then
  46. echo "The given repo does not exist: $FIREBASE_REPO_PATH"
  47. exit 1
  48. fi
  49. # Check if the given xcframework path is valid.
  50. FIRESTORE_XCFRAMEWORK_PATH=$2
  51. if [ "$(basename $FIRESTORE_XCFRAMEWORK_PATH)" != 'FirebaseFirestore.xcframework' ]; then
  52. echo "The given xcframework is not a FirebaseFirestore.xcframework."
  53. exit 1
  54. fi
  55. if [[ ! -d "$FIRESTORE_XCFRAMEWORK_PATH" ]]; then
  56. echo "The given xcframework does not exist: $FIRESTORE_XCFRAMEWORK_PATH"
  57. exit 1
  58. fi
  59. # Copy the given Firestore framework to the root of the given Firebase repo.
  60. # This script uses an env var that will alter the repo's `Package.swift` to
  61. # pick up the copied Firestore framework. See
  62. # `FIREBASECI_USE_LOCAL_FIRESTORE_ZIP` in Firebase's `Package.swift` for more.
  63. cp -r "$FIRESTORE_XCFRAMEWORK_PATH" "$FIREBASE_REPO_PATH"
  64. # Create a temporary directory for the test package. The test package defines an
  65. # executable and has the following directory structure:
  66. #
  67. # TestPkg
  68. # ├── Package.swift
  69. # └── Sources
  70. #    └── TestPkg
  71. #     └── main.swift
  72. TEST_PKG_ROOT=$(mktemp -d -t TestPkg)
  73. echo "Test package root: $TEST_PKG_ROOT"
  74. # Create the package's subdirectories.
  75. mkdir -p "$TEST_PKG_ROOT/Sources/TestPkg"
  76. # Generate the package's `Package.swift`.
  77. cat > "$TEST_PKG_ROOT/Package.swift" <<- EOM
  78. // swift-tools-version: 5.6
  79. import PackageDescription
  80. let package = Package(
  81. name: "TestPkg",
  82. platforms: [.macOS(.v10_13)],
  83. dependencies: [
  84. .package(path: "${FIREBASE_REPO_PATH}")
  85. ],
  86. targets: [
  87. .executableTarget(
  88. name: "TestPkg",
  89. dependencies: [
  90. .product(
  91. name: "FirebaseFirestore",
  92. package: "firebase-ios-sdk"
  93. )
  94. ]
  95. )
  96. ]
  97. )
  98. EOM
  99. # Generate the package's `main.swift`.
  100. cat > "$TEST_PKG_ROOT/Sources/TestPkg/main.swift" <<- EOM
  101. import FirebaseFirestore
  102. let db = Firestore.firestore()
  103. EOM
  104. # Change to the test package's root directory in order to build the package.
  105. cd "$TEST_PKG_ROOT"
  106. # Build the test package *without* the `-ObjC` linker flag, and dump the
  107. # resulting executable file's Objective-C symbols into a text file.
  108. echo "Building test package without -ObjC linker flag..."
  109. FIREBASECI_USE_LOCAL_FIRESTORE_ZIP=1 xcodebuild -scheme 'TestPkg' \
  110. -destination 'generic/platform=macOS' \
  111. -derivedDataPath "$HOME/Library/Developer/Xcode/DerivedData/TestPkg" \
  112. | xcpretty
  113. nm ~/Library/Developer/Xcode/DerivedData/TestPkg/Build/Products/Debug/TestPkg \
  114. | grep -o "[-+]\[.*\]" > objc_symbols_without_linker_flag.txt
  115. # Build the test package *with* the -ObjC linker flag, and dump the
  116. # resulting executable file's Objective-C symbols into a text file.
  117. echo "Building test package with -ObjC linker flag..."
  118. FIREBASECI_USE_LOCAL_FIRESTORE_ZIP=1 xcodebuild -scheme 'TestPkg' \
  119. -destination 'generic/platform=macOS' \
  120. -derivedDataPath "$HOME/Library/Developer/Xcode/DerivedData/TestPkg-ObjC" \
  121. OTHER_LDFLAGS='-ObjC' \
  122. | xcpretty
  123. nm ~/Library/Developer/Xcode/DerivedData/TestPkg-ObjC/Build/Products/Debug/TestPkg \
  124. | grep -o "[-+]\[.*\]" > objc_symbols_with_linker_flag.txt
  125. # Compare the two text files to see if the -ObjC linker flag caused additional
  126. # symbols to link.
  127. #
  128. # Note: In the case where the diff is non-empty, the diff command will
  129. # return exit code 1, which will cause the set pipefail to terminate execution.
  130. # To avoid this, `|| true` ensures the exit code always indicates success.
  131. DIFF=$(
  132. git diff --no-index \
  133. objc_symbols_without_linker_flag.txt \
  134. objc_symbols_with_linker_flag.txt \
  135. || true
  136. )
  137. if [[ -n "$DIFF" ]]; then
  138. echo "Failure: Unlinked Objective-C symbols have been detected:"
  139. echo "$DIFF"
  140. exit 1
  141. else
  142. echo "Success: No unlinked Objective-C symbols have been detected."
  143. exit 0
  144. fi