Swift Package Manager可执行应用程序,设置部署目标

时间:2019-02-15 15:26:03

标签: swift macos swift-package-manager

我正在使用Swift Package Manager创建macOS可执行文件。当我使用并非所有macOS版本都可用的功能时,会出现编译错误。两个大例子是URL(fileURLWithPath: filePath, relativeTo: directoryToSearch)url.hasDirectoryPath

使用swift build进行构建时,出现error: 'init(fileURLWithPath:relativeTo:)' is only available on OS X 10.11 or newer错误。我不关心任何旧的OS版本,因为它只是一个个人工具。如何将部署目标设置为10.14,这样我就不必在我的代码中花很多功夫了?

我发现https://hirschmann.io/swift-package-manager/在谈论这个问题。但是,它的解决方案是创建一个设置了部署目标的xcconfig文件,然后使用swift package generate-xcodeproj --xcconfig-overrides ./main.xcconfig将其应用于生成的Xcode项目。尽管它确实有效,但仅适用于Xcode项目,因此,如果我只想swift build来获得独立的可执行文件以在Xcode之外使用,我将无法使用。

我的软件包文件是由swift package init --type executable自动生成的,尚未更改。

// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "swift_converter",
    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 which this package depends on.
        .target(
            name: "swift_converter",
            dependencies: []),
        .testTarget(
            name: "swift_converterTests",
            dependencies: ["swift_converter"]),
    ]
)

1 个答案:

答案 0 :(得分:6)

这可能暂时对您没有帮助,但是即将推出的Swift 5.0将包括使用以下语法的specify the deployment target in the package manifest功能:

...
platforms: [
   .macOS(.v10_13), .iOS(.v12),
],
...

for some other common build settings也是如此。)

在此之前,您可以通过如下命令行参数覆盖默认部署目标:

$ swift build -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.14"

每次调用swift buildswift runswift test时,都必须包含这些参数。

相关问题