Compilation conditions and swift packages

In the Xcode build settings you can set SWIFT_ACTIVE_COMPILATION_CONDITIONS for Debug and Release builds. And you can check them in code like this:

#if FOO 
...
#endif  

They don't seem to work for code in a Swift package. Is there a way to pass these to the Swift package code?

1 Like

That is what build settings on each target are for.

Sorry, my question left out a key detail. I'm building an iOS app that uses a Swift package. I'm the author of both the app and package. I want to customize the building of the Swift package for this app, and only this app. The Package.swift file in my app project is read-only.

1 Like

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