Methods for conditionally restricting a package for Apple Silicon?

Simply put, there are SPM libraries that are not yet compatible with Apple Silicon. I'm wondering if there's any method (including workarounds or otherwise) that others have explored for conditionally including a library only if the architecture is arm64 and so on.

In CocoaPods, I simply grab the architecture directly from the environment and put an if check around the pod definition I'm looking to restrict/allow based on architecture.

I'm about to explore something similar in SPM, eg. detecting the architecture and simply stripping the dependency if it's not included, but was curious if there was any cleaner / safer / more preferred method of achieving this?

Thanks in advance!

Well, in case anyone stumbles upon this in the future, here's a workaround I came up with. (Edited to use arch(arm64) thanks to @Peter-Schorn's answer below.)

The file below is an example Package.swift:

// swift-tools-version:5.5

import PackageDescription

#if arch(arm64)

let architectureSpecificPackageDependencies: [Package.Dependency] = []
let architectureSpecificTargetDependencies: [Target.Dependency] = []

#else

let architectureSpecificPackageDependencies: [Package.Dependency] = [
    // Your Apple Silicon-incompatible Package dependencies.
    // .package(name: "IntelPackage", url: "https://github.com/example/intel-package", .upToNextMajor(from: "1.0.0")),
]

let architectureSpecificTargetDependencies: [Target.Dependency] = [
    // Your Apple Silicon-incompatible Target dependencies.
    // .product(name: "IntelModule", package: "IntelPackage")
]

#endif

let package = Package(
    name: "MainPackage",
    products: [
        .library(name: "MainModule", targets: ["MainModule"]),
    ],
    dependencies: [
        // Your universal Package dependencies.
    ] + architectureSpecificPackageDependencies,
    targets: [
        .target(
            name: "MainModule",
            dependencies: [
                // Your universal Target dependencies.
            ] + architectureSpecificTargetDependencies
        ),
    ]
)

Why not just use the #if arch(arm64) compilation condition?

1 Like

Only because I didn't know about it, this is precisely the kind of thing I was looking for.

Is there a good resource for all of these preprocessor macros?

See Statements — The Swift Programming Language (Swift 5.6)

1 Like