将package.swift文件转换为二进制XCFRAMEWORK

发布于 2025-02-14 02:08:40 字数 1035 浏览 0 评论 0 原文

我公司中的某人创建了一个 swift package sdk ,现在我被任命以 binary 方式将其发布给客户,以便最终客户这将使用 SDK 将无法看到它的源代码。这就是SDK的构建方式: xcode in xcode (ps构建文件夹是空的),

我从我的阅读中读取了我了解的主题 )我需要将文件导出到 xcframework 文件中。 指南都解释了如何从框架而不是从我的情况下从 framework

但是,我遇到的绝大多数 发现这似乎完全像我需要的是 this 一个,但是我在第一个相关终端命令上出现错误=“ ./构建” 。这是我得到的主要错误行: XcodeBuild:错误:构建Swift软件包要求使用“ - 终止”选项提供目标。 “ -showdestinations”选项可用于列出可用目的地。。为什么我需要指定目的地?我希望SDK适用于所有设备(iOS 13+)。我在网上搜索的论坛的非论坛帮助我解决了这个问题。

我还阅读了苹果的说明在这里,但对终端的困惑非常困惑在我的情况下,步骤2中的命令应该看起来像。有些领域是强制性的,有些不是吗?

任何帮助将不胜感激!!

Someone in my company created a Swift package SDK and now I was tasked to publish it for the customer in a binary way so that the end customers that will use the SDK will not be able to see the source code of it. This is how the SDK is built:
the SDK in xcode (p.s. the build folder is empty)

From my reading on the subject I understand that I need to export the files into an XCFramework file. However, the vast majority of guides I've encountered explain how to make this progress from a framework, and not from a package like in my case..

The only guide I found that seems exactly like what I need is this one, however I get an error right on the first relevant terminal command of xcodebuild -scheme [my scheme name] -sdk iphoneos -configuration Release ARCHS="arm64" BUILD_DIR="./Build". This is the main error line I get: xcodebuild: error: Building a Swift package requires that a destination is provided using the "-destination" option. The "-showdestinations" option can be used to list the available destinations.. Why would I need to specify a destination? I want the SDK to work for all the devices (ios 13+). Non of the forums I searched in online helped me solve this.

I also read Apple's instructions here but got very confused about how the terminal command in step 2 is supposed to look like in my case. Are some of the fields mandatory and some are not?

Any help would be much appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2025-02-21 02:08:40

假设您仅使用iOS,并且需要用于设备和模拟器架构的XCFramework,以便从Swift软件包中生成XCFramework,则需要:

  1. 将软件包标记为 .dynamic (即 .library(名称:“ foo”,type:.dynamic,targets:[“ foo”])))
  2. 对模拟器和设备的项目归档。这将为每个体系结构生成一个.framework文件。
  3. 复制模块文件夹(如果有),将.xcarchive文件
  4. 复制到.xCartive文件中,将束(如果有)复制到.xccrarkive文件中
  5. 创建xcframework,则使用步骤2中创建的框架

此处是基于一个bash脚本,基于一个bash脚本来自这个swift论坛post 从Swift软件包中创建XCFramework(就我而言,我的软件包在XCWorkspace中。我没有尝试作为独立软件包,不确定是否可以完成的):

根据需要更改输入参数:),但最重要的是替换<您的项目名称> and < workspace>

#!/bin/bash

PROJECT_NAME="<Your project name>"
PROJECT_DIR="./Packages/${PROJECT_NAME}" # Relative path to the directory containing the `Package.swift` file
BUILD_FOLDER="./build"
OUTPUT_DIR="${PROJECT_DIR}/Output"
SIMULATOR_ARCHIVE="${OUTPUT_DIR}/${PROJECT_NAME}-iphonesimulator.xcarchive"
DEVICE_ARCHIVE="${OUTPUT_DIR}/${PROJECT_NAME}-iphoneos.xcarchive"

rm -rf "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"

# 2 iterations: 1 for device arch and another for simulator arch
for PLATFORM in "iOS" "iOS Simulator"; do
    case $PLATFORM in
      "iOS")
        ARCHIVE=$DEVICE_ARCHIVE
        SDK=iphoneos
        RELEASE_FOLDER="Release-iphoneos"
      ;;
      "iOS Simulator")
        ARCHIVE=$SIMULATOR_ARCHIVE
        SDK=iphonesimulator
        RELEASE_FOLDER="Release-iphonesimulator"
      ;;
    esac

    # Step 2
    xcodebuild archive \
      -workspace <your workspace>.xcworkspace \
      -scheme $PROJECT_NAME \
      -destination="generic/platform=${PLATFORM}" \
      -archivePath $ARCHIVE \
      -sdk $SDK \
      -derivedDataPath $BUILD_FOLDER \
      SKIP_INSTALL=NO \
      BUILD_LIBRARY_FOR_DISTRIBUTION=YES

    FRAMEWORK_PATH="${ARCHIVE}/Products/Library/Frameworks/${PROJECT_NAME}.framework"
    MODULES_PATH="$FRAMEWORK_PATH/Modules"
    mkdir -p $MODULES_PATH

    BUILD_PRODUCTS_PATH="${BUILD_FOLDER}/Build/Intermediates.noindex/ArchiveIntermediates/${PROJECT_NAME}/BuildProductsPath"
    RELEASE_PATH="${BUILD_PRODUCTS_PATH}/${RELEASE_FOLDER}"
    SWIFT_MODULE_PATH="${RELEASE_PATH}/${PROJECT_NAME}.swiftmodule"
    RESOURCES_BUNDLE_PATH="${RELEASE_PATH}/${PROJECT_NAME}_${PROJECT_NAME}.bundle"

    # Step 3
    if [ -d $SWIFT_MODULE_PATH ] 
    then
      cp -r $SWIFT_MODULE_PATH $MODULES_PATH
    fi

    # Step 4
    if [ -e $RESOURCES_BUNDLE_PATH ] 
    then
      cp -r $RESOURCES_BUNDLE_PATH $FRAMEWORK_PATH
    fi

done

# Step 5
xcodebuild -create-xcframework \
 -framework "${DEVICE_ARCHIVE}/Products/Library/Frameworks/${PROJECT_NAME}.framework" \
 -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/${PROJECT_NAME}.framework" \
 -output "${OUTPUT_DIR}/${PROJECT_NAME}.xcframework"

当脚本结束时,您将在输出文件夹中看到3个文件:

  • 设备XCarchive
  • Simulator XCarchive
  • XCFrameWork

您可以删除XCarchives,因为您不再需要它们(您还可以更新脚本以为您完成此操作)。

Assuming you are working with iOS only and you need an xcframework for both device and simulator architectures, in order to generate an XCFramework from a swift package you need to:

  1. Mark your Package as .dynamic (i.e. .library(name: "Foo", type: .dynamic, targets: ["Foo"]))
  2. Archive the project for both simulator and device. This will generate a .framework file for each architecture.
  3. Copy Modules folders (if any) into the .xcarchive files
  4. Copy the bundles (if any) into the .xcarchive files
  5. Create the xcframework with the frameworks created in step 2

Here's a bash script, based on the one from this swift forums post, to create an XCFramework from a swift package (in my case, I have my package inside an xcworkspace. I didn't try it as a standalone package, not sure if that can be done):

Change the input parameters as needed :), but most importantly replace <Your project name> and <your workspace>.

#!/bin/bash

PROJECT_NAME="<Your project name>"
PROJECT_DIR="./Packages/${PROJECT_NAME}" # Relative path to the directory containing the `Package.swift` file
BUILD_FOLDER="./build"
OUTPUT_DIR="${PROJECT_DIR}/Output"
SIMULATOR_ARCHIVE="${OUTPUT_DIR}/${PROJECT_NAME}-iphonesimulator.xcarchive"
DEVICE_ARCHIVE="${OUTPUT_DIR}/${PROJECT_NAME}-iphoneos.xcarchive"

rm -rf "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"

# 2 iterations: 1 for device arch and another for simulator arch
for PLATFORM in "iOS" "iOS Simulator"; do
    case $PLATFORM in
      "iOS")
        ARCHIVE=$DEVICE_ARCHIVE
        SDK=iphoneos
        RELEASE_FOLDER="Release-iphoneos"
      ;;
      "iOS Simulator")
        ARCHIVE=$SIMULATOR_ARCHIVE
        SDK=iphonesimulator
        RELEASE_FOLDER="Release-iphonesimulator"
      ;;
    esac

    # Step 2
    xcodebuild archive \
      -workspace <your workspace>.xcworkspace \
      -scheme $PROJECT_NAME \
      -destination="generic/platform=${PLATFORM}" \
      -archivePath $ARCHIVE \
      -sdk $SDK \
      -derivedDataPath $BUILD_FOLDER \
      SKIP_INSTALL=NO \
      BUILD_LIBRARY_FOR_DISTRIBUTION=YES

    FRAMEWORK_PATH="${ARCHIVE}/Products/Library/Frameworks/${PROJECT_NAME}.framework"
    MODULES_PATH="$FRAMEWORK_PATH/Modules"
    mkdir -p $MODULES_PATH

    BUILD_PRODUCTS_PATH="${BUILD_FOLDER}/Build/Intermediates.noindex/ArchiveIntermediates/${PROJECT_NAME}/BuildProductsPath"
    RELEASE_PATH="${BUILD_PRODUCTS_PATH}/${RELEASE_FOLDER}"
    SWIFT_MODULE_PATH="${RELEASE_PATH}/${PROJECT_NAME}.swiftmodule"
    RESOURCES_BUNDLE_PATH="${RELEASE_PATH}/${PROJECT_NAME}_${PROJECT_NAME}.bundle"

    # Step 3
    if [ -d $SWIFT_MODULE_PATH ] 
    then
      cp -r $SWIFT_MODULE_PATH $MODULES_PATH
    fi

    # Step 4
    if [ -e $RESOURCES_BUNDLE_PATH ] 
    then
      cp -r $RESOURCES_BUNDLE_PATH $FRAMEWORK_PATH
    fi

done

# Step 5
xcodebuild -create-xcframework \
 -framework "${DEVICE_ARCHIVE}/Products/Library/Frameworks/${PROJECT_NAME}.framework" \
 -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/${PROJECT_NAME}.framework" \
 -output "${OUTPUT_DIR}/${PROJECT_NAME}.xcframework"

When the script ends, you will see 3 files in the output folder:

  • Device xcarchive
  • Simulator xcarchive
  • XCFramework

You can remove the xcarchives, as you won't need them anymore (you can also update the script to do that for you).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文