Compilation conditions and swift packages

I think packages are deliberately insulated so that that sort of thing cannot happen by accident.

Package flavours” have been considered as a feature for a long time, but I not aware of any work having been done on it yet.

If you are in control of the package, the closest I can think of to flimsily hack it together would be to do this in the manifest:

let package = Package(
  // ...
)

import Foundation
if ProcessInfo.processInfo.environment["MY_SWITCH"] == "ENABLED" {
  for target in package.targets
    where target.name == "TheOneIWantToChange" {
    target.swiftSettings.append(.define("MY_COMPILATION_CONDITION"))
  }
}

Then on the command line you can do...

export MY_SWITCH=ENABLED
swift package generate-xcodeproj

...and drag the generated project into the same workspace as the application.

But 99.9% of the time, you would be better off refactoring your package so as not to rely on custom use of #if (unless it can be derived from the standard conditions).

2 Likes