带有不安全构建标志的 Swift 包依赖项:目标完整性错误

Bor*_*ris 7 xcode ios swift dlib swiftpm

TL; 博士

尝试在 Xcode 的 iOS 项目中使用本地 Swift 包依赖项时,我收到以下错误:

The package product 'DlibWrapper' cannot be used as a dependency of this target because it uses unsafe build flags.

(我使用不安全标志来指定静态库位置)

我还尝试将该包作为基于远程分支的依赖项导入,但无论如何它都失败了。

根据Swift 论坛上的这篇文章,该问题已在不久前解决,并且已经合并了相应的拉取请求

问题出现在 Swift 5.2.4 (Xcode 11.6) 和 5.3 (Xcode 12 beta 3) 中。

任何线索可能是什么问题?


细节

我正在尝试构建一个包装dlib库并在 iOS 应用程序中使用它的 Swift 包。由于平台原因,我无法使用.systemLibrary目标链接dlib. 所以我在一个静态库中预编译它并与包装器代码打包在一起,如下所示:

DlibWrapper/
  Libraries/
    dlib/
      include/
          ...
      lib/
        arm64/
          libdlib.a

  Sources/
    CWrapper/
      include/
        module.modulemap
        cwrapper.h
      cwrapper.cpp

    SwiftWrapper/
      SwiftWrapper.swift

  Package.swift
Run Code Online (Sandbox Code Playgroud)

的简化内容DlibWrapper/Package.swift

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "DlibWrapper",
    platforms: [
        .iOS(.v13)
    ],
    products: [
        .library(
            name: "DlibWrapper",
            targets: ["CWrapper", "SwiftWrapper"])
    ],
    dependencies: [],
    targets: [
        .target(
            name: "SwiftWrapper",
            dependencies: ["CWrapper"]
        ),
        .target(
            name: "CWrapper",

            cxxSettings: [.headerSearchPath("../../Libraries/dlib/include")],

            linkerSettings: [
                .linkedLibrary("dlib"),
                .linkedFramework("Accelerate", .when(platforms: [.iOS])),

                // The error is caused by this line
                .unsafeFlags(["-LLibraries/dlib/lib/arm64"], .when(platforms: [.iOS])),
            ]
        ),
    ],
    cxxLanguageStandard: .cxx1z
)
Run Code Online (Sandbox Code Playgroud)

我试图使用link里面的属性,module.modulemap但编译器似乎忽略了它。此外,.linkedLibrary()在目标清单中提供库的绝对路径也无济于事,链接器抱怨它找不到库。

任何解决方法的想法?(作为最后的手段,我可​​能会将所有内容打包在一个框架中)

将不胜感激任何帮助。

谢谢

Bor*_*ris 2

更新

\n

事实证明下面的方法并不起作用。.linkedLibrary当包被编译为静态库时,Swift 完全忽略(使用 进行检查swift build --verbose)。该设置仅与动态库一起使用。然而,:在后一种情况下,语法没有任何作用 \xe2\x80\x94 无论如何都找不到库。

\n

(由于历史原因保留了下面的答案,也许它可以帮助某人找到解决方案)

\n
\n

旧答案

\n

解决方法是在链接器选项中使用 clang/gcc 冒号语法,即-l:(减去 el-冒号)。有关更多详细信息,请参阅此答案。

\n

以下是包清单中的相关片段:

\n
// swift-tools-version:5.3\n\nimport PackageDescription\n\nlet package = Package(\n    name: "DlibWrapper",\n    //...\n    targets: [\n        //...\n        .target(\n            name: "CWrapper",\n\n            cxxSettings: [.headerSearchPath("../../Libraries/dlib/include")],\n\n            linkerSettings: [\n                // Note the `:` in front of the relative path\n                .linkedLibrary(":Libraries/dlib/lib/arm64/libdlib.a", .when(platforms: [.iOS])),\n                //...\n            ]\n        ),\n    ],\n    //...\n)\n
Run Code Online (Sandbox Code Playgroud)\n

解释

\n

链接.linkedLibrary器设置必需-l为链接器提供标志。所以,无论你放在那里作为参数,都会被转换成这样的东西

\n
swift build -Xlinker -l<your-linkedLibrary-string>\n
Run Code Online (Sandbox Code Playgroud)\n

:语法允许人们指定一个具有非规范名称(即实际文件名)的库。最后,由于链接器的工作目录设置为包根目录,我们可以为其提供库的相对路径。

\n

我仍然认为这是一种解决方法,因为正确的方法是使用类似librarySearchPath,即-L链接器选项,类似于headerSearchPathin cxxSettings

\n