check_firestore_symbols.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if [[ $# -ne 2 ]]; then
  35. echo "Usage: ./check_firestore_symbols.sh <PATH_TO_FIREBASE_REPO> <PATH_TO_FIRESTORE_XCFRAMEWORK>"
  36. exit 1
  37. fi
  38. # Check if the given repo path is valid.
  39. FIREBASE_REPO_PATH=$1
  40. if [[ "$FIREBASE_REPO_PATH" != /* ]]; then
  41. echo "The given path should be an absolute path."
  42. exit 1
  43. fi
  44. if [[ ! -d "$FIREBASE_REPO_PATH" ]]; then
  45. echo "The given repo does not exist: $FIREBASE_REPO_PATH"
  46. exit 1
  47. fi
  48. # Check if the given xcframework path is valid.
  49. FIRESTORE_XCFRAMEWORK_PATH=$2
  50. if [ "$(basename $FIRESTORE_XCFRAMEWORK_PATH)" != 'FirebaseFirestore.xcframework' ]; then
  51. echo "The given xcframework is not a FirebaseFirestore.xcframework."
  52. exit 1
  53. fi
  54. if [[ ! -d "$FIRESTORE_XCFRAMEWORK_PATH" ]]; then
  55. echo "The given xcframework does not exist: $FIRESTORE_XCFRAMEWORK_PATH"
  56. exit 1
  57. fi
  58. # Copy the given Firestore framework to the root of the given Firebase repo.
  59. # This script uses an env var that will alter the repo's `Package.swift` to
  60. # pick up the copied Firestore framework. See
  61. # `FIREBASECI_USE_LOCAL_FIRESTORE_ZIP` in Firebase's `Package.swift` for more.
  62. cp -r "$FIRESTORE_XCFRAMEWORK_PATH" "$FIREBASE_REPO_PATH"
  63. # Create a temporary directory for the test package. The test package defines an
  64. # executable and has the following directory structure:
  65. #
  66. # TestPkg
  67. # ├── Package.swift
  68. # └── Sources
  69. #    └── TestPkg
  70. #     └── main.swift
  71. TEST_PKG_ROOT=$(mktemp -d -t TestPkg)
  72. echo "Test package root: $TEST_PKG_ROOT"
  73. # Create the package's subdirectories.
  74. mkdir -p "$TEST_PKG_ROOT/Sources/TestPkg"
  75. # Generate the package's `Package.swift`.
  76. cat > "$TEST_PKG_ROOT/Package.swift" <<- EOM
  77. // swift-tools-version: 5.6
  78. import PackageDescription
  79. let package = Package(
  80. name: "TestPkg",
  81. platforms: [.macOS(.v10_13)],
  82. dependencies: [
  83. .package(path: "${FIREBASE_REPO_PATH}")
  84. ],
  85. targets: [
  86. .executableTarget(
  87. name: "TestPkg",
  88. dependencies: [
  89. .product(
  90. name: "FirebaseFirestore",
  91. package: "firebase-ios-sdk"
  92. )
  93. ]
  94. )
  95. ]
  96. )
  97. EOM
  98. # Generate the package's `main.swift`.
  99. cat > "$TEST_PKG_ROOT/Sources/TestPkg/main.swift" <<- EOM
  100. import FirebaseFirestore
  101. let db = Firestore.firestore()
  102. EOM
  103. # Change to the test package's root directory in order to build the package.
  104. cd "$TEST_PKG_ROOT"
  105. # Build the test package *without* the `-ObjC` linker flag, and dump the
  106. # resulting executable file's Objective-C symbols into a text file.
  107. echo "Building test package without -ObjC linker flag..."
  108. # Invoke a subshell to avoid pipefail affecting the rest of the script.
  109. (
  110. set -eo pipefail && FIREBASECI_USE_LOCAL_FIRESTORE_ZIP=1 \
  111. xcodebuild -scheme 'TestPkg' -destination 'generic/platform=macOS' \
  112. -derivedDataPath "$HOME/Library/Developer/Xcode/DerivedData/TestPkg" \
  113. | xcpretty
  114. )
  115. nm ~/Library/Developer/Xcode/DerivedData/TestPkg/Build/Products/Debug/TestPkg \
  116. | grep -o "[-+]\[.*\]" > objc_symbols_without_linker_flag.txt
  117. # Build the test package *with* the -ObjC linker flag, and dump the
  118. # resulting executable file's Objective-C symbols into a text file.
  119. echo "Building test package with -ObjC linker flag..."
  120. # Invoke a subshell to avoid pipefail affecting the rest of the script.
  121. (
  122. set -eo pipefail && FIREBASECI_USE_LOCAL_FIRESTORE_ZIP=1 \
  123. xcodebuild -scheme 'TestPkg' -destination 'generic/platform=macOS' \
  124. -derivedDataPath "$HOME/Library/Developer/Xcode/DerivedData/TestPkg-ObjC" \
  125. OTHER_LDFLAGS='-ObjC' \
  126. | xcpretty
  127. )
  128. nm ~/Library/Developer/Xcode/DerivedData/TestPkg-ObjC/Build/Products/Debug/TestPkg \
  129. | grep -o "[-+]\[.*\]" > objc_symbols_with_linker_flag.txt
  130. # Compare the two text files to see if the -ObjC linker flag has any effect.
  131. DIFF=$(diff objc_symbols_without_linker_flag.txt objc_symbols_with_linker_flag.txt)
  132. if [[ -n "$DIFF" ]]; then
  133. echo "Failure: Unlinked Objective-C symbols have been detected:"
  134. echo "$DIFF"
  135. exit 1
  136. else
  137. echo "Success: No unlinked Objective-C symbols have been detected."
  138. exit 0
  139. fi