使用 SwiftPM 导出 public C headers

Exporting public C headers with SwiftPM

我有一个开源 Objective C 框架,我也想使用 Swift 包管理器提供它。

通常,我在Xcode项目中设置public headers,在构建框架时,将其复制到框架包下,并在链接到时发现通过 Xcode.

我不能,但是让它与 SwiftPM 一起工作。

我为我的框架创建了一个模块映射:

framework module LNPopupController {
    umbrella header "LNPopupController.h"
    export *
    module * { export * }
}

我在 Package.swift:

中这样定义库
let package = Package(
    name: "LNPopupController",
    platforms: [
        .iOS(.v12),
        .macOS(.v10_15)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "LNPopupController",
            type: .dynamic,
            targets: ["LNPopupController"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "LNPopupController",
            dependencies: [],
            path: "LNPopupController",
            publicHeadersPath: ".",
            cSettings: [
                .headerSearchPath("."),
                .headerSearchPath("Private"),
            ]),
    ]
)

当在 Xcode 中作为项目依赖项添加时,框架编译得很好,但是当依赖目标尝试 import LNPopupController 时,会抛出错误:umbrella header 'LNPopupController.h' not found

查看构建文件夹,确实,我看到 Xcode 构建了一个二进制文件,但没有复制 public headers.

有什么方法可以指定哪些 headers 是 public,并让构建系统复制它们以便导入?

我终于明白了。这就是我为 LNPopupController:

所做的

我创建了一个 include/LNPopupController/ sub-directory 树,并在其中放置了指向所有 public headers 的软链接。

在打包文件中,我添加了publicHeadersPath: "include"

你可以在这里看到最终结果: https://github.com/LeoNatan/LNPopupController/blob/master/Package.swift

这可能不是最佳方式,但它适用于我尝试过此方法的所有 ObjC 项目。