Arch-specific build configuration in SwiftPM

I’m surprised that this doesn’t seem supported - which I suspect means I'm just missing something obvious…?

e.g. say I want to include certain source files only on certain architectures, such as those specialising an algorithm with AVX code, or NEON code?

I found the ‘when’ mechanism for defines, but I don’t think that helps me - I’m trying to define targets for existing C/C++ libraries I don’t control, that are architected around Linux and its convention of customising the build system rather than much simpler things like #if guards. :disappointed:

1 Like

Does using conditional compilation inside your Package.swift itself (here’s a simple example I knew off the top of my head: swift-distributed-actors/Package.swift at 51b7ca7a9d9f2256fffba6be3778d25e5a08c739 · apple/swift-distributed-actors · GitHub) combined with the arch platform condition not accomplish what you want? You can certainly have different targets and configurations defined per platform, though note that the Package.swift will be evaluated for the host platform, so this approach may fail spectacularly if you’re trying to cross-compile.

Ah yes, I hadn't considered that approach. Not the most elegant in some respects (because of the limitations on where #if clauses can be used) but it should work. Thanks!

You should not use #if checks in your Package.swift, those are invalid for cross-compilation. With cross-compilation so prevalent in Swift developers workflow (you're always cross-compiling when developing iOS apps), you're breaking your package by using #if for the vast majority of use cases.

IMO presence of those in Package.swift should trigger warnings and eventually errors.

2 Likes

Then what would you recommend as an alternative? Seems like there isn’t another way at the moment.

Exposing triples to plugins has a PR in progress, but we need to make sure this works well with both llbuild and the Xcode build system first: Add `hostTriple` and `targetTriples` to `PluginContext` by MaxDesiatov · Pull Request #7053 · apple/swift-package-manager · GitHub

After that one lands we could consider exposing triples to Package.swift in a similar way.