swift编译_如何在Swift Package Manager中添加已编译的框架

swift编译

At last, a long-awaited feature comes to Swift. Swift 5.3 will enable you to add binary dependencies in Swift Package Manager. Why does this sound fantastic? Because before Swift 5.3, SPM (Swift Package Manager) only enabled source-code libraries as a dependency, which meant that lots of enterprise software (which often comes as closed-source) or closed-source libraries couldn’t be added as a dependency in SPM, like Firebase or Google Analytics.

最后,Swift带来了期待已久的功能。 Swift 5.3将使您能够在Swift Package Manager中添加二进制依赖项。 为什么听起来很棒? 因为在Swift 5.3之前,SPM(Swift软件包管理器)仅将源代码库作为依赖项启用,这意味着无法将许多企业软件(通常是封闭源代码)或封闭源代码库添加为依赖项在SPM中,例如Firebase或Google Analytics(分析)。

SE-0272 by Braden Scothern, Daniel Dunbar, and Franz Busch brings binary dependencies capability to Swift 5.3, which means you can add compiled frameworks to your SPM. But keep in mind that only xcframework format is supported for now. (Other types of frameworks were still under discussion when this article was being written. See more detail here).

Braden ScothernDaniel DunbarFranz Busch的 SE-0272为Swift 5.3带来了二进制依赖功能,这意味着您可以将编译后的框架添加到SPM中。 但是请记住,目前仅支持xcframework格式。 (在撰写本文时,其他类型的框架仍在讨论中。 在此处查看更多详细信息 )。

什么是xcframework? (What is xcframework ?)

xcframework is a format that Apple introduced back in WWDC 2019, where it was discussed in “Binary Frameworks in Swift.”

xcframework是Apple在WWDC 2019中引入的一种格式,在“ Swift中的Binary Frameworks ”中进行了讨论。

There are several advantages of using xcframework, like multiple architectures and platforms (including simulator type), and no slicing x86_64 before uploading to AppStore.

使用xcframework有许多优点,例如多种体系结构和平台(包括模拟器类型),并且在上传到AppStore之前无需切片x86_64。

You should know how to use xcframework because SPM only supports this. Unless you’ve already compiled one, skip this section. I suggest you watch the WWDC 2019 session above.

您应该知道如何使用xcframework因为SPM仅支持此功能。 除非您已经编译过,否则请跳过本节。 我建议您观看上面的WWDC 2019会议。

Note: Because Swift 5.3 only allows xcframework, this feature will only work on an Apple OS (iOS, macOS, tvOS, etc). I hope that a future Swift version will add this feature to Linux and Windows.

注意:由于Swift 5.3仅允许xcframework ,因此此功能仅适用于Apple操作系统(iOS,macOS,tvOS等)。 我希望以后的Swift版本可以将此功能添加到Linux和Windows中。

如何使用 (How to Use)

Start by adding a new .binaryTarget in your SPM target.

首先在您的SPM目标中添加一个新的.binaryTarget

let package = Package(
name: "AwesomeLibrary",
...
targets: [
.target(
name: "AwesomeLibrary",
dependencies: ["Logging"]
),
.binaryTarget(name: "Logging", path: "artifacts/Logging.xcframework"
),
...
]

SPM supports both zip and raw xcframework files for binary dependency. If you used zip, make sure the xcframework is located at the root of the archive.

SPM支持zip和原始xcframework文件以实现二进制依赖性。 如果使用zip ,请确保xcframework位于存档的根目录。

There are two ways to add a binary target.

有两种添加二进制目标的方法。

带URL (With URL)

You can add a binary target from a URL, such as GitHub Release, GitHub Packages, GitLab, Bitbucket, Artifactory, Nexus, etc.

您可以从URL添加二进制目标,例如GitHub Release,GitHub Packages,GitLab,Bitbucket,Artifactory,Nexus等。

.binaryTarget(
name: "Logging",
url: "https://github.com/wendyliga/compiled-framework-spm/releases/download/0.0.1/Logging.xcframework.zip",
checksum: "c246c715ac7f6fae9ef0a89e758a8514644071a164985b1e95d344a684d84621"
)

Note: With Swift 5.3, you can generate checksum by executing swift package compute-checksum <path-to-zip>. You need to make sure to download the binary first, to compute the checksum.

注意:使用Swift 5.3,您可以通过执行swift package compute-checksum <path-to-zip>生成校验和。 您需要确保首先下载二进制文件以计算校验和。

原始文件 (Raw File)

On the other hand, you can add the binary by placing it in the source:

另一方面,您可以通过将二进制文件放入源中来添加二进制文件:

.binaryTarget(name: "Crypto", path: "artifacts/Crypto.xcframework")

.binaryTarget(name: "Crypto", path: "artifacts/Crypto.xcframework")

Note: The path to xcframework starts from the root of the project (same as Package.swift).

注意: xcframework的路径从项目的根目录开始(与Package.swift)相同Package.swift)

考虑 (Consideration)

Swift doesn’t do verification of the format of the artifacts (binary dependency), which means you should make sure to provide a correct and valid artifact.

Swift不会对工件的格式(二进制依赖性)进行验证,这意味着您应确保提供正确且有效的工件。

From the security perspective, comparing the trust level between source-based vs. binary dependency, naturally, source-based dependency will be higher since you can inspect the code. However, source-based dependency can have its own security issues as well.

从安全角度来看,比较基于源的依赖关系和二进制依赖关系之间的信任级别,自然地,基于源的依赖关系会更高,因为您可以检查代码。 但是,基于源的依赖性也可能有其自身的安全性问题。

Binary dependency also has its own problem that we should mitigate, for example, binary dependency hosted on the network. There will always be a risk of tempering. Swift reduces this by comparing the checksum of the binary you set with the one from the network. The hash also stored in the package resolves to avoid the vendor changing the binary in a later version without anyone noticing.

二进制依赖关系也有其自身的问题,例如,托管在网络上的二进制依赖关系应得到缓解。 总是有tempering的危险。 Swift通过将您设置的二进制文件的校验和与网络中的二进制校验和进行比较来减少这种情况。 解决方案还存储在哈希中,以避免供应商在没有任何人注意的情况下在更高版本中更改二进制文件。

示例项目 (Example Project)

All examples in this article can be found in this example project: wendyliga/compiled-framework-spm

本文中的所有示例都可以在以下示例项目中找到: wendyliga / compiled-framework-spm

结束 (The End)

Swift 5.3 supports the compiled framework as a dependency in SPM. This opens the way for broader use cases.

Swift 5.3支持将编译后的框架作为SPM中的依赖项。 这为更广泛的用例开辟了道路。

Swift 5.3 also brings the capability to add resource files or non-compiled files to SPM, for example, JSON, mp3, or HTML. I’ve discussed this in a previous article, “How to Add Resources in Swift Package Manager.”

Swift 5.3还具有向SPM添加资源文件或非编译文件的功能,例如JSON,mp3或HTML。 我在上一篇文章“ 如何在Swift Package Manager中添加资源 ”中对此进行了讨论。

All that’s left is for me to say thanks for your time to read the article. I hope you can use this feature in your SPM Project.

剩下的就是我要感谢您阅读本文的时间。 我希望您可以在SPM项目中使用此功能。

翻译自: https://medium.com/better-programming/how-to-add-compiled-frameworks-in-swift-package-manager-f0cdbde84fc7

swift编译

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值