Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]),
    ]
)
like image 826
jamone Avatar asked Feb 15 '19 15:02

jamone


People also ask

How do I set a deployment target in Xcode?

Click the Books target and select the General tab at the top. The General tab contains a section with name Deployment Info. The section contains a dropdown menu and three checkboxes, iPhone, iPad, and Mac. Because I am using Xcode 12, the dropdown menu is currently set to iOS 14.3.

How do I change deployment targets in Xcode 11?

Change deployment targetSelect your app target and go to General tab, under Deployment Info change target to whatever version you want to support.

How do I publish a Swift package?

Open your Swift package, select the Source Control menu, choose New Git Repositories, check the checkbox next to your package, and click Create. This initializes a Git repository, adds your package to the staging area, and commits your files.

How do I change package manager in Swift?

Open your app's Xcode project or workspace. Select the Swift package's folder in Finder and drag it into the Project navigator. This action adds your dependency's Swift package as a local package to your project. Make changes to the local package and your app, then verify them by building and running your app.


1 Answers

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.

like image 57
Ole Begemann Avatar answered Sep 21 '22 22:09

Ole Begemann