Pods XXX frameworks - ShenYj/ShenYj.github.io GitHub Wiki

Pods-XXX-frameworks.sh

在使用 CocoaPods 管理类库时,配置完 Podfile 执行完 pod install,CocoaPods 配置好 Workspace后,会自动生成一个脚本

脚本名称格式为:

Pods-项目名-frameworks.sh

存在于 Pods/Targets Support Files/Pods-项目名

前提条件是开启 use_frameworks! 使用动态库依赖的方式,如果将其注释或去掉,基于静态库依赖,就不会产生此脚本

  • Countdown-background-recovery 为例

    .
    └──Pods
        └── Target Support Files
            └── Pods-RxTimerWithBackgroundRecovery
                  └── Pods-RxTimerWithBackgroundRecovery-frameworks.sh
  • Pods-RxTimerWithBackgroundRecovery-frameworks.sh

    完整脚本
      #!/bin/sh
      set -e
      set -u
      set -o pipefail
    
      function on_error {
      echo "$(realpath -mq "${0}"):$1: error: Unexpected failure"
      }
      trap 'on_error $LINENO' ERR
    
      if [ -z ${FRAMEWORKS_FOLDER_PATH+x} ]; then
      # If FRAMEWORKS_FOLDER_PATH is not set, then there's nowhere for us to copy
      # frameworks to, so exit 0 (signalling the script phase was successful).
      exit 0
      fi
    
      echo "mkdir -p ${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
      mkdir -p "${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
    
      COCOAPODS_PARALLEL_CODE_SIGN="${COCOAPODS_PARALLEL_CODE_SIGN:-false}"
      SWIFT_STDLIB_PATH="${DT_TOOLCHAIN_DIR}/usr/lib/swift/${PLATFORM_NAME}"
      BCSYMBOLMAP_DIR="BCSymbolMaps"
    
    
      # This protects against multiple targets copying the same framework dependency at the same time. The solution
      # was originally proposed here: https://lists.samba.org/archive/rsync/2008-February/020158.html
      RSYNC_PROTECT_TMP_FILES=(--filter "P .*.??????")
    
      # Copies and strips a vendored framework
      install_framework()
      {
      if [ -r "${BUILT_PRODUCTS_DIR}/$1" ]; then
          local source="${BUILT_PRODUCTS_DIR}/$1"
      elif [ -r "${BUILT_PRODUCTS_DIR}/$(basename "$1")" ]; then
          local source="${BUILT_PRODUCTS_DIR}/$(basename "$1")"
      elif [ -r "$1" ]; then
          local source="$1"
      fi
    
      local destination="${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
    
      if [ -L "${source}" ]; then
          echo "Symlinked..."
          source="$(readlink "${source}")"
      fi
    
      if [ -d "${source}/${BCSYMBOLMAP_DIR}" ]; then
          # Locate and install any .bcsymbolmaps if present, and remove them from the .framework before the framework is copied
          find "${source}/${BCSYMBOLMAP_DIR}" -name "*.bcsymbolmap"|while read f; do
          echo "Installing $f"
          install_bcsymbolmap "$f" "$destination"
          rm "$f"
          done
          rmdir "${source}/${BCSYMBOLMAP_DIR}"
      fi
    
      # Use filter instead of exclude so missing patterns don't throw errors.
      echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${source}\" \"${destination}\""
      rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${source}" "${destination}"
    
      local basename
      basename="$(basename -s .framework "$1")"
      binary="${destination}/${basename}.framework/${basename}"
    
      if ! [ -r "$binary" ]; then
          binary="${destination}/${basename}"
      elif [ -L "${binary}" ]; then
          echo "Destination binary is symlinked..."
          dirname="$(dirname "${binary}")"
          binary="${dirname}/$(readlink "${binary}")"
      fi
    
      # Strip invalid architectures so "fat" simulator / device frameworks work on device
      if [[ "$(file "$binary")" == *"dynamically linked shared library"* ]]; then
          strip_invalid_archs "$binary"
      fi
    
      # Resign the code if required by the build settings to avoid unstable apps
      code_sign_if_enabled "${destination}/$(basename "$1")"
    
      # Embed linked Swift runtime libraries. No longer necessary as of Xcode 7.
      if [ "${XCODE_VERSION_MAJOR}" -lt 7 ]; then
          local swift_runtime_libs
          swift_runtime_libs=$(xcrun otool -LX "$binary" | grep --color=never @rpath/libswift | sed -E s/@rpath\\/\(.+dylib\).*/\\1/g | uniq -u)
          for lib in $swift_runtime_libs; do
          echo "rsync -auv \"${SWIFT_STDLIB_PATH}/${lib}\" \"${destination}\""
          rsync -auv "${SWIFT_STDLIB_PATH}/${lib}" "${destination}"
          code_sign_if_enabled "${destination}/${lib}"
          done
      fi
      }
      # Copies and strips a vendored dSYM
      install_dsym() {
      local source="$1"
      warn_missing_arch=${2:-true}
      if [ -r "$source" ]; then
          # Copy the dSYM into the targets temp dir.
          echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${source}\" \"${DERIVED_FILES_DIR}\""
          rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${source}" "${DERIVED_FILES_DIR}"
    
          local basename
          basename="$(basename -s .dSYM "$source")"
          binary_name="$(ls "$source/Contents/Resources/DWARF")"
          binary="${DERIVED_FILES_DIR}/${basename}.dSYM/Contents/Resources/DWARF/${binary_name}"
    
          # Strip invalid architectures from the dSYM.
          if [[ "$(file "$binary")" == *"Mach-O "*"dSYM companion"* ]]; then
          strip_invalid_archs "$binary" "$warn_missing_arch"
          fi
          if [[ $STRIP_BINARY_RETVAL == 0 ]]; then
          # Move the stripped file into its final destination.
          echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \"- CVS/\" --filter \"- .svn/\" --filter \"- .git/\" --filter \"- .hg/\" --filter \"- Headers\" --filter \"- PrivateHeaders\" --filter \"- Modules\" \"${DERIVED_FILES_DIR}/${basename}.framework.dSYM\" \"${DWARF_DSYM_FOLDER_PATH}\""
          rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${DERIVED_FILES_DIR}/${basename}.dSYM" "${DWARF_DSYM_FOLDER_PATH}"
          else
          # The dSYM was not stripped at all, in this case touch a fake folder so the input/output paths from Xcode do not reexecute this script because the file is missing.
          touch "${DWARF_DSYM_FOLDER_PATH}/${basename}.dSYM"
          fi
      fi
      }
    
      # Used as a return value for each invocation of `strip_invalid_archs` function.
      STRIP_BINARY_RETVAL=0
    
      # Strip invalid architectures
      strip_invalid_archs() {
      binary="$1"
      warn_missing_arch=${2:-true}
      # Get architectures for current target binary
      binary_archs="$(lipo -info "$binary" | rev | cut -d ':' -f1 | awk '{$1=$1;print}' | rev)"
      # Intersect them with the architectures we are building for
      intersected_archs="$(echo ${ARCHS[@]} ${binary_archs[@]} | tr ' ' '\n' | sort | uniq -d)"
      # If there are no archs supported by this binary then warn the user
      if [[ -z "$intersected_archs" ]]; then
          if [[ "$warn_missing_arch" == "true" ]]; then
          echo "warning: [CP] Vendored binary '$binary' contains architectures ($binary_archs) none of which match the current build architectures ($ARCHS)."
          fi
          STRIP_BINARY_RETVAL=1
          return
      fi
      stripped=""
      for arch in $binary_archs; do
          if ! [[ "${ARCHS}" == *"$arch"* ]]; then
          # Strip non-valid architectures in-place
          lipo -remove "$arch" -output "$binary" "$binary"
          stripped="$stripped $arch"
          fi
      done
      if [[ "$stripped" ]]; then
          echo "Stripped $binary of architectures:$stripped"
      fi
      STRIP_BINARY_RETVAL=0
      }
    
      # Copies the bcsymbolmap files of a vendored framework
      install_bcsymbolmap() {
          local bcsymbolmap_path="$1"
          local destination="${BUILT_PRODUCTS_DIR}"
          echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${bcsymbolmap_path}" "${destination}""
          rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "${bcsymbolmap_path}" "${destination}"
      }
    
      # Signs a framework with the provided identity
      code_sign_if_enabled() {
      if [ -n "${EXPANDED_CODE_SIGN_IDENTITY:-}" -a "${CODE_SIGNING_REQUIRED:-}" != "NO" -a "${CODE_SIGNING_ALLOWED}" != "NO" ]; then
          # Use the current code_sign_identity
          echo "Code Signing $1 with Identity ${EXPANDED_CODE_SIGN_IDENTITY_NAME}"
          local code_sign_cmd="/usr/bin/codesign --force --sign ${EXPANDED_CODE_SIGN_IDENTITY} ${OTHER_CODE_SIGN_FLAGS:-} --preserve-metadata=identifier,entitlements '$1'"
    
          if [ "${COCOAPODS_PARALLEL_CODE_SIGN}" == "true" ]; then
          code_sign_cmd="$code_sign_cmd &"
          fi
          echo "$code_sign_cmd"
          eval "$code_sign_cmd"
      fi
      }
    
      if [[ "$CONFIGURATION" == "Debug" ]]; then
      install_framework "${BUILT_PRODUCTS_DIR}/Differentiator/Differentiator.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/Hue/Hue.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/NSObject+Rx/NSObject_Rx.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxAlertViewable/RxAlertViewable.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxAnimated/RxAnimated.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxAppState/RxAppState.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxBinding/RxBinding.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxCocoa/RxCocoa.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxDataSources/RxDataSources.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxFlow/RxFlow.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxGesture/RxGesture.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxKeyboard/RxKeyboard.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxOptional/RxOptional.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxRelay/RxRelay.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxSwift/RxSwift.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxSwiftExt/RxSwiftExt.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxUIAlert/RxUIAlert.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxViewController/RxViewController.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SnapKit/SnapKit.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SwiftDate/SwiftDate.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SwifterSwift/SwifterSwift.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SwiftyJSON/SwiftyJSON.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/Then/Then.framework"
      fi
      if [[ "$CONFIGURATION" == "Release" ]]; then
      install_framework "${BUILT_PRODUCTS_DIR}/Differentiator/Differentiator.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/Hue/Hue.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/NSObject+Rx/NSObject_Rx.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxAlertViewable/RxAlertViewable.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxAnimated/RxAnimated.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxAppState/RxAppState.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxBinding/RxBinding.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxCocoa/RxCocoa.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxDataSources/RxDataSources.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxFlow/RxFlow.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxGesture/RxGesture.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxKeyboard/RxKeyboard.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxOptional/RxOptional.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxRelay/RxRelay.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxSwift/RxSwift.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxSwiftExt/RxSwiftExt.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxUIAlert/RxUIAlert.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/RxViewController/RxViewController.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SnapKit/SnapKit.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SwiftDate/SwiftDate.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SwifterSwift/SwifterSwift.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/SwiftyJSON/SwiftyJSON.framework"
      install_framework "${BUILT_PRODUCTS_DIR}/Then/Then.framework"
      fi
      if [ "${COCOAPODS_PARALLEL_CODE_SIGN}" == "true" ]; then
      wait
      fi
    

在项目 build phase - [CP] Embed Pods Frameworks 配置了这个脚本

同时基于 Xcode 的 New System Build, 在运行脚本时增加的输入(input file list)输出(output file list)列表

  • input file list: ${PODS_ROOT}/Target Support Files/Pods-RxTimerWithBackgroundRecovery/Pods-RxTimerWithBackgroundRecovery-frameworks-${CONFIGURATION}-input-files.xcfilelist

    ${PODS_ROOT}/Target Support Files/Pods-SPMDemo/Pods-SPMDemo-frameworks.sh
    ${BUILT_PRODUCTS_DIR}/Alamofire/Alamofire.framework
    ${BUILT_PRODUCTS_DIR}/Differentiator/Differentiator.framework
    ${BUILT_PRODUCTS_DIR}/FloatingPanel/FloatingPanel.framework
    ${BUILT_PRODUCTS_DIR}/Moya/Moya.framework
    ${BUILT_PRODUCTS_DIR}/Moya-ObjectMapper/Moya_ObjectMapper.framework
    ${BUILT_PRODUCTS_DIR}/NSObject+Rx/NSObject_Rx.framework
    ${BUILT_PRODUCTS_DIR}/ObjectMapper/ObjectMapper.framework
    ${BUILT_PRODUCTS_DIR}/RxAnimated/RxAnimated.framework
    ${BUILT_PRODUCTS_DIR}/RxCocoa/RxCocoa.framework
    ${BUILT_PRODUCTS_DIR}/RxDataSources/RxDataSources.framework
    ${BUILT_PRODUCTS_DIR}/RxGesture/RxGesture.framework
    ${BUILT_PRODUCTS_DIR}/RxOptional/RxOptional.framework
    ${BUILT_PRODUCTS_DIR}/RxRelay/RxRelay.framework
    ${BUILT_PRODUCTS_DIR}/RxSwift/RxSwift.framework
    ${BUILT_PRODUCTS_DIR}/RxTheme/RxTheme.framework
    ${BUILT_PRODUCTS_DIR}/RxViewController/RxViewController.framework
  • output file list: ${PODS_ROOT}/Target Support Files/Pods-RxTimerWithBackgroundRecovery/Pods-RxTimerWithBackgroundRecovery-frameworks-${CONFIGURATION}-output-files.xcfilelist

    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Alamofire.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Differentiator.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FloatingPanel.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Moya.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Moya_ObjectMapper.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/NSObject_Rx.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ObjectMapper.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxAnimated.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxCocoa.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxDataSources.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxGesture.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxOptional.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxRelay.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxSwift.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxTheme.framework
    ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxViewController.framework
    • 关于 BUILT_PRODUCTS_DIRTARGET_BUILD_DIRFRAMEWORKS_FOLDER_PATH 这三个环境变量都能在 Xcode help中了解到
    • PODS_ROOT 是在xcconfig中拼接的一个路径: PODS_ROOT = ${SRCROOT}/Pods
      总之这里就是拼接路径
      inputoutput 文件可以是单个文件形式,如果文件过多可以放到格式为 xcfilelist 的文件列表里

通过执行脚本: 在编译完成后,将生成的的动态库拷贝到 Frameworks 目录下, 在通过 CocoaPods 默认生成的 xcconfig中配置的 Run Path,就能拼接处完整的动态库路径,这样App 启动时 dyld 就能找到并加载动态库

⚠️ **GitHub.com Fallback** ⚠️