SPM platform specific targets

I'm trying to add a build target to my package that can only be built on macOS, yet for some reason SPM always attempts to build it on Linux too. SE-0273 seems to suggest that what I'm trying to achieve should be possible:

... but when building, the Package Manager will avoid building the disabled dependency and will link the correct library depending on the platform.

targets: [
    .target(
        name: "Frontend",
        dependencies: [
            .target(name: "PeripheryKit"),
            .target(name: "XcodeSupport", condition: .when(platforms: [.macOS])),
        ]
    ),
    .target(
        name: "PeripheryKit"
    ),
    .target(
        name: "XcodeSupport",
        dependencies: [
            .target(name: "PeripheryKit"),
            .product(name: "XcodeProj", package: "XcodeProj"),
        ]
    ),
]

SPM always attempts to build XcodeSupport on Linux despite the macOS platform condition on Frontend. No other target has XcodeSupport as a dependency. Is this a bug or am I misinterpreting SE-0273?

A client package that asks for Frontend will not attempt to build or use XcodeSupport for Linux.

But when you do swift build for your own package, it will still build every target in the package.

It is only a conditional dependency, not a conditional target (which is a separate, sorely needed feature.)

For now, to support swift build you can either put #if !os(Linux) around every file in XcodeSupport, or you can do something like I described in this post:

Oh interesting distinction, thanks @SDGGiesbrecht.

Terms of Service

Privacy Policy

Cookie Policy