Search code examples
swiftmacosswift-package-manager

Specify minimum macOS version for Swift Package Manager with Swift 5


In order to have some code compile with SwiftPM, without adding #if available, I'm building the project with the following parameters:

swift build -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.11"

Swift Package Manager also works with Xcode .xcconfig files, but only when generating an Xcode project.

Is there an easy way in Swift 5 to specify the minimum version of macOS when building from the command line with swift build?

Compiler error example:

error: 'archivedData(withRootObject:)' is only available on OS X 10.11 or newer
        let data = NSKeyedArchiver.archivedData(withRootObject: value)

Solution

  • let package = Package(
        name: "NAME",
        platforms: [
            .macOS(.v10_11)
        ],
        products: [
            .library(name: "NAME", targets: ["NAME"]),
        ],
        targets: [
            .target(name: "NAME"),
        ]
    )
    

    One way to do this is with Deployment Settings in SPM.