Realm-xcframeworks.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. variant_for_slice()
  13. {
  14. case "$1" in
  15. "realm-monorepo.xcframework/ios-arm64")
  16. echo ""
  17. ;;
  18. "realm-monorepo.xcframework/ios-arm64_x86_64-maccatalyst")
  19. echo "maccatalyst"
  20. ;;
  21. "realm-monorepo.xcframework/ios-arm64_x86_64-simulator")
  22. echo "simulator"
  23. ;;
  24. "realm-monorepo.xcframework/macos-x86_64_arm64")
  25. echo ""
  26. ;;
  27. "realm-monorepo.xcframework/tvos-arm64")
  28. echo ""
  29. ;;
  30. "realm-monorepo.xcframework/tvos-arm64_x86_64-simulator")
  31. echo "simulator"
  32. ;;
  33. "realm-monorepo.xcframework/watchos-arm64_armv7k_arm64_32")
  34. echo ""
  35. ;;
  36. "realm-monorepo.xcframework/watchos-arm64_i386_x86_64-simulator")
  37. echo "simulator"
  38. ;;
  39. "realm-monorepo.xcframework/xros-arm64")
  40. echo ""
  41. ;;
  42. "realm-monorepo.xcframework/xros-arm64_x86_64-simulator")
  43. echo "simulator"
  44. ;;
  45. esac
  46. }
  47. archs_for_slice()
  48. {
  49. case "$1" in
  50. "realm-monorepo.xcframework/ios-arm64")
  51. echo "arm64"
  52. ;;
  53. "realm-monorepo.xcframework/ios-arm64_x86_64-maccatalyst")
  54. echo "arm64 x86_64"
  55. ;;
  56. "realm-monorepo.xcframework/ios-arm64_x86_64-simulator")
  57. echo "arm64 x86_64"
  58. ;;
  59. "realm-monorepo.xcframework/macos-x86_64_arm64")
  60. echo "arm64 x86_64"
  61. ;;
  62. "realm-monorepo.xcframework/tvos-arm64")
  63. echo "arm64"
  64. ;;
  65. "realm-monorepo.xcframework/tvos-arm64_x86_64-simulator")
  66. echo "arm64 x86_64"
  67. ;;
  68. "realm-monorepo.xcframework/watchos-arm64_armv7k_arm64_32")
  69. echo "arm64 arm64_32 armv7k"
  70. ;;
  71. "realm-monorepo.xcframework/watchos-arm64_i386_x86_64-simulator")
  72. echo "arm64 i386 x86_64"
  73. ;;
  74. "realm-monorepo.xcframework/xros-arm64")
  75. echo "arm64"
  76. ;;
  77. "realm-monorepo.xcframework/xros-arm64_x86_64-simulator")
  78. echo "arm64 x86_64"
  79. ;;
  80. esac
  81. }
  82. copy_dir()
  83. {
  84. local source="$1"
  85. local destination="$2"
  86. # Use filter instead of exclude so missing patterns don't throw errors.
  87. echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" \"${source}*\" \"${destination}\""
  88. rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" "${source}"/* "${destination}"
  89. }
  90. SELECT_SLICE_RETVAL=""
  91. select_slice() {
  92. local xcframework_name="$1"
  93. xcframework_name="${xcframework_name##*/}"
  94. local paths=("${@:2}")
  95. # Locate the correct slice of the .xcframework for the current architectures
  96. local target_path=""
  97. # Split archs on space so we can find a slice that has all the needed archs
  98. local target_archs=$(echo $ARCHS | tr " " "\n")
  99. local target_variant=""
  100. if [[ "$PLATFORM_NAME" == *"simulator" ]]; then
  101. target_variant="simulator"
  102. fi
  103. if [[ ! -z ${EFFECTIVE_PLATFORM_NAME+x} && "$EFFECTIVE_PLATFORM_NAME" == *"maccatalyst" ]]; then
  104. target_variant="maccatalyst"
  105. fi
  106. for i in ${!paths[@]}; do
  107. local matched_all_archs="1"
  108. local slice_archs="$(archs_for_slice "${xcframework_name}/${paths[$i]}")"
  109. local slice_variant="$(variant_for_slice "${xcframework_name}/${paths[$i]}")"
  110. for target_arch in $target_archs; do
  111. if ! [[ "${slice_variant}" == "$target_variant" ]]; then
  112. matched_all_archs="0"
  113. break
  114. fi
  115. if ! echo "${slice_archs}" | tr " " "\n" | grep -F -q -x "$target_arch"; then
  116. matched_all_archs="0"
  117. break
  118. fi
  119. done
  120. if [[ "$matched_all_archs" == "1" ]]; then
  121. # Found a matching slice
  122. echo "Selected xcframework slice ${paths[$i]}"
  123. SELECT_SLICE_RETVAL=${paths[$i]}
  124. break
  125. fi
  126. done
  127. }
  128. install_xcframework() {
  129. local basepath="$1"
  130. local name="$2"
  131. local package_type="$3"
  132. local paths=("${@:4}")
  133. # Locate the correct slice of the .xcframework for the current architectures
  134. select_slice "${basepath}" "${paths[@]}"
  135. local target_path="$SELECT_SLICE_RETVAL"
  136. if [[ -z "$target_path" ]]; then
  137. echo "warning: [CP] $(basename ${basepath}): Unable to find matching slice in '${paths[@]}' for the current build architectures ($ARCHS) and platform (${EFFECTIVE_PLATFORM_NAME-${PLATFORM_NAME}})."
  138. return
  139. fi
  140. local source="$basepath/$target_path"
  141. local destination="${PODS_XCFRAMEWORKS_BUILD_DIR}/${name}"
  142. if [ ! -d "$destination" ]; then
  143. mkdir -p "$destination"
  144. fi
  145. copy_dir "$source/" "$destination"
  146. echo "Copied $source to $destination"
  147. }
  148. install_xcframework "${PODS_ROOT}/Realm/core/realm-monorepo.xcframework" "Realm" "library" "ios-arm64" "ios-arm64_x86_64-maccatalyst" "ios-arm64_x86_64-simulator"