FirebaseAnalytics-xcframeworks.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/sh
  2. set -e
  3. set -u
  4. set -o pipefail
  5. function on_error {
  6. echo "$(realpath -mq "${0}"):$1: error: Unexpected failure"
  7. }
  8. trap 'on_error $LINENO' ERR
  9. # This protects against multiple targets copying the same framework dependency at the same time. The solution
  10. # was originally proposed here: https://lists.samba.org/archive/rsync/2008-February/020158.html
  11. RSYNC_PROTECT_TMP_FILES=(--filter "P .*.??????")
  12. copy_dir()
  13. {
  14. local source="$1"
  15. local destination="$2"
  16. # Use filter instead of exclude so missing patterns don't throw errors.
  17. echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" \"${source}\" \"${destination}\""
  18. rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" "${source}" "${destination}"
  19. }
  20. SELECT_SLICE_RETVAL=""
  21. select_slice() {
  22. local paths=("$@")
  23. # Locate the correct slice of the .xcframework for the current architectures
  24. local target_path=""
  25. # Split archs on space so we can find a slice that has all the needed archs
  26. local target_archs=$(echo $ARCHS | tr " " "\n")
  27. local target_variant=""
  28. if [[ "$PLATFORM_NAME" == *"simulator" ]]; then
  29. target_variant="simulator"
  30. fi
  31. if [[ ! -z ${EFFECTIVE_PLATFORM_NAME+x} && "$EFFECTIVE_PLATFORM_NAME" == *"maccatalyst" ]]; then
  32. target_variant="maccatalyst"
  33. fi
  34. for i in ${!paths[@]}; do
  35. local matched_all_archs="1"
  36. for target_arch in $target_archs
  37. do
  38. if ! [[ "${paths[$i]}" == *"$target_variant"* ]]; then
  39. matched_all_archs="0"
  40. break
  41. fi
  42. # Verifies that the path contains the variant string (simulator or maccatalyst) if the variant is set.
  43. if [[ -z "$target_variant" && ("${paths[$i]}" == *"simulator"* || "${paths[$i]}" == *"maccatalyst"*) ]]; then
  44. matched_all_archs="0"
  45. break
  46. fi
  47. # This regex matches all possible variants of the arch in the folder name:
  48. # Let's say the folder name is: ios-armv7_armv7s_arm64_arm64e/CoconutLib.framework
  49. # We match the following: -armv7_, _armv7s_, _arm64_ and _arm64e/.
  50. # If we have a specific variant: ios-i386_x86_64-simulator/CoconutLib.framework
  51. # We match the following: -i386_ and _x86_64-
  52. # When the .xcframework wraps a static library, the folder name does not include
  53. # any .framework. In that case, the folder name can be: ios-arm64_armv7
  54. # We also match _armv7$ to handle that case.
  55. local target_arch_regex="[_\-]${target_arch}([\/_\-]|$)"
  56. if ! [[ "${paths[$i]}" =~ $target_arch_regex ]]; then
  57. matched_all_archs="0"
  58. break
  59. fi
  60. done
  61. if [[ "$matched_all_archs" == "1" ]]; then
  62. # Found a matching slice
  63. echo "Selected xcframework slice ${paths[$i]}"
  64. SELECT_SLICE_RETVAL=${paths[$i]}
  65. break
  66. fi
  67. done
  68. }
  69. install_library() {
  70. local source="$1"
  71. local name="$2"
  72. local destination="${PODS_XCFRAMEWORKS_BUILD_DIR}/${name}"
  73. # Libraries can contain headers, module maps, and a binary, so we'll copy everything in the folder over
  74. local source="$binary"
  75. echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" \"${source}/*\" \"${destination}\""
  76. rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" "${source}/*" "${destination}"
  77. }
  78. # Copies a framework to derived data for use in later build phases
  79. install_framework()
  80. {
  81. local source="$1"
  82. local name="$2"
  83. local destination="${PODS_XCFRAMEWORKS_BUILD_DIR}/${name}"
  84. if [ ! -d "$destination" ]; then
  85. mkdir -p "$destination"
  86. fi
  87. copy_dir "$source" "$destination"
  88. echo "Copied $source to $destination"
  89. }
  90. install_xcframework_library() {
  91. local basepath="$1"
  92. local name="$2"
  93. local paths=("$@")
  94. # Locate the correct slice of the .xcframework for the current architectures
  95. select_slice "${paths[@]}"
  96. local target_path="$SELECT_SLICE_RETVAL"
  97. if [[ -z "$target_path" ]]; then
  98. echo "warning: [CP] Unable to find matching .xcframework slice in '${paths[@]}' for the current build architectures ($ARCHS)."
  99. return
  100. fi
  101. install_framework "$basepath/$target_path" "$name"
  102. }
  103. install_xcframework() {
  104. local basepath="$1"
  105. local name="$2"
  106. local package_type="$3"
  107. local paths=("$@")
  108. # Locate the correct slice of the .xcframework for the current architectures
  109. select_slice "${paths[@]}"
  110. local target_path="$SELECT_SLICE_RETVAL"
  111. if [[ -z "$target_path" ]]; then
  112. echo "warning: [CP] Unable to find matching .xcframework slice in '${paths[@]}' for the current build architectures ($ARCHS)."
  113. return
  114. fi
  115. local source="$basepath/$target_path"
  116. local destination="${PODS_XCFRAMEWORKS_BUILD_DIR}/${name}"
  117. if [ ! -d "$destination" ]; then
  118. mkdir -p "$destination"
  119. fi
  120. copy_dir "$source/" "$destination"
  121. echo "Copied $source to $destination"
  122. }
  123. install_xcframework "${PODS_ROOT}/FirebaseAnalytics/Frameworks/FirebaseAnalytics.xcframework" "FirebaseAnalytics" "framework" "ios-arm64_armv7" "ios-arm64_i386_x86_64-simulator"