Restrict macOS builds to x86_64 with SwiftPM

Is there a way to restrict macOS SPM builds to only compile/link/test for x86_64?

I develop frameworks with binary dependencies which haven't yet supported arm64. When targeting macOS 11, I am getting linking errors. Xcode is building a x86_64 and arm64 version of the framework, but it can only link to a x86_64 binary dependency.


There's always the brute-force solution:

#if !arch(x86_64)
fatalError("This package only supports x86_64; see <github issue> for further information")
1 Like

I read your comment and I was already dismissing your suggestion; but the more I think about it, the more sense it makes :sweat_smile: The build would finish, I could test my framework, and in case of needing to release it, I can always lipo -remove the "non-working" arm64 version.

I was hoping to find something more "final", such as setting a unsafe flag in the SPM manifest, or the like. Building/testing for debug works flawlessly (since it is using ONLY_NATIVE_ARCH), but it doesn't seam to work on release (or at least I don't know how to properly set unsafe flags).

1 Like

Hopefully one of the SPM developers can weigh in here with another suggestion (but Apple is shutdown this week for the American thanksgiving holiday, so you may not get a response until folks are back at their [remote, because of COVID] desks).

Oh, I wasn't aware of that. Thank you for the heads-up.

Maybe @NeoNacho or @Aciid could shed some light into this?

There is currently no way to restrict platforms or architectures of a package, so the brute force solution @scanon suggested is the only way to communicate this to your users (apart from documentation, of course).

For the question of how to build for a single architecture in Xcode, you should get a build for only the host architecture if you pick "My Mac" but for both architectures if you pick "Any Mac" as your run destination in the UI.

When using xcodebuild, the destination platform=macOS will build for the host and generic/platform=macOS will build for both architectures.

1 Like

Thank you for the confirmation, @NeoNacho.

Building with target "My Mac" with build or release configuration does indeed only build for the host architecture (x86_64 for me). However, building for testing (⌘+⇧+U) on target "My Mac" with release configuration builds for all architectures, which makes my tests fail (since my binary dependencies don't support arm64 yet). Build for testing with debug configuration does the right thing, though.

To sum up: Build for testing seems to ignore the "My Mac" target when using release configuration on SPM projects.

You can quickly replicate this on my open-source package:

  • Open it and set the IG scheme with "My Mac" target.
  • Edit the scheme > Test > Build configuration Release.
  • ⌘+⇧+U to build for testing.

Can you give me pointers on how to run tests on release in Xcode for hosting architecture only?

1 Like

I think there is currently no solution for this, since the release configuration implies ONLY_ACTIVE_ARCH set to false for packages which means they will always build for all supported architectures.

Thanks for elaborating on this. Would you say this is a problem with XCode or the build system behind it?

I'm having similar problem with the Release configuration on XCode/XCode Server, whereas i still can produce an archive (Release configuration) with xcodebuild command:

xcodebuild archive -scheme "ProjectScheme" -arch "x86_64"

When using xcodebuild command, all packages are built only for a single, in this case native arch x86_64.

It has been 1 year, so I'll ask this question slightly different...

Is it possible at this stage to setup SPM in a project with ARM64 architecture excluded, to run in a silicon Mac without using Rosetta?


+1 It would really be nice to be able to exclude arm64 when building a swift package

Could the be achieved by passing swiftSettings .unsafeFlags?

swiftSettings: [.unsafeFlags([
    "--target x86_64-apple-ios11.0-simulator",
    "--sdk /Applications/"
], .when(configuration: .debug))]

end of 2022, any updates on this?

SPM continues to ignore ARHC/EXCLUDE ARCH settings from the project file and targets..

so I can never build dependencies for Debug for projects configured to x86_64.

p.s did anyone find a solution how to specify arch target for SPM?

1 Like

We're running into this issue too; trying to integrate our first extracted Swift package and we really don't like the performance hit from running in Rosetta on M1 Macs. Anyone know if there's an update on this?

The only way I found so far to force SPM building for x86 is changing Build Configuration name in xcode. Debug doesn't respect "Excluded archs" and it seems like SPM switches to debug whenever it spots "debug" in xcode configuration name.
I found it in Tomasz Lizer Brighter Inventions blog post. I hope we will get something cleaner in the future, for now it is frustrating.

1 Like

Have you found a solution for that ?