Is there a way to define a resource so it is included only on certain platforms in Package.swift?
You can use #if os(...)
in Package.swift but IIRC this will break cross compilation. I donât think thereâs an officially supported way yet. SwiftNIO transport services (an Apple platform only package) works around this by wrapping all source files in an #if
: GitHub - apple/swift-nio-transport-services: Extensions for SwiftNIO to support Apple platforms as first-class citizens.
#if
works fine inside source files, but not for resources. So I guess I'm stuck.
The use case is a small UI library that includes a .xib
file for each platform. But of course the .xib
for Mac causes an error when building for iOS and vice versa.
More conditionalizability in the manifest sorely needed. In the meantime, the best workaround I am aware of is to query the environment in the manifest and then adjust accordingly:
if ProcessInfo.processInfo.environment["TARGETING_IOS"] == "true" {
// Make whatever changes are necessary:
package.targets.removeAll(where: { $0.name == "ImpossibleOnIOS" })
}
But youâll have to set the environment variable yourself, so the tacticâs usefulness varies. If the change needs to affect clients, and you expect those clients to be using Xcodeâs GUI, then it is probably a nonâstarter. On the other hand, if the clients are on Linux, then configuring the environment should be easy. Or if the affected targets are only tests anyway, then CI can supply the variable to its invocations of xcodebuild
and you can temporarily comment out the change or the condition while you are actively using Xcodeâ GUI.