How can I conditionally link a Swift package only for devices and not simulators?

I have a third party package that compiles a bunch of c code that is not built for simulator.
Here is the the package: mlc-llm/ios/MLCSwift/Package.swift at main · mlc-ai/mlc-llm · GitHub

The problem is that these targets do not compile on Simulator. I'd like to just exclude them entirely and I can use #if available or #if !targetEnvironment(simulator) to avoid importing them.

That would work out of the box, but it still tries to link. How do I disable linking for simulator only?

I can't use conditions because the platforms don't include simulator. Which is pretty frustrating. If simulators cannot do the same thing that iOS can do, then it should be a condition

1 Like

Swift packages have no support for environment specification? Wouldn't that line up super super well with targetEnvironment?

I'm not sure if this does a great job at directly answering your question… but one strategy I see to work around limitations in SPM is passing environment variables from shell and then building the package manifest from conditional logic on those variables. It's not as elegant as a "proper" API… but this might be a legit strategy for now given the current abilities of SPM… but YMMV and there could be new APIs on SPM I do not know about that do this in a better way.

So could that work by defining an environment variable in the build scheme or do you always need to compile using shell?

Build scheme meaning Xcode Build Scheme? I have not tried. I believe this pattern was mostly for running builds from command line and CI servers. I think it might work from Xcode… want to give it a try and see what happens?