Platform-Dependent Resources

Is there a way to define a resource so it is included only on certain platforms in Package.swift?

1 Like

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: https://github.com/apple/swift-nio-transport-services/

#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.

Terms of Service

Privacy Policy

Cookie Policy