jamone
jamone

Reputation: 17421

Swift Package Manager executable app, set deployment target

I'm using Swift Package Manager to create a macOS executable. When I use things that aren't available in all macOS versions I get compile errors. Two big examples are URL(fileURLWithPath: filePath, relativeTo: directoryToSearch) and url.hasDirectoryPath.

When building with swift build I get error: 'init(fileURLWithPath:relativeTo:)' is only available on OS X 10.11 or newer errors. I don't care about any old OS versions, as it's just a personal tool. How can I set the deployment target to be 10.14 so I don't have to sprinkle checks all through out my code?

I found https://hirschmann.io/swift-package-manager/ which talks about this issue. However it's solution is creating an xcconfig file with the deployment target set and using swift package generate-xcodeproj --xcconfig-overrides ./main.xcconfig to apply it to the generated Xcode project. While it does work, it only works for the Xcode project, so if I just want to do swift build to get the free standing executable to use outside of Xcode then I can't.

My package file was auto generated by swift package init --type executable and hasn't been changed.

// 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"]),
    ]
)

Upvotes: 12

Views: 7474

Answers (1)

Ole Begemann
Ole Begemann

Reputation: 135548

This may not help you right now, but the upcoming Swift 5.0 will include the ability to specify the deployment target in the package manifest, using a syntax like this:

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

(The same is true for some other common build settings.)

Until then, you can override the default deployment target via command line arguments like this:

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

You'll have to include these arguments in every call to swift build, swift run, swift test.

Upvotes: 22

Related Questions