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"]),
]
)
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.
Change deployment targetSelect your app target and go to General tab, under Deployment Info change target to whatever version you want to support.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With