Environment variables in Package.swift

With swift build I can get the externally set environment variables correctly in package.swift:
outmost script:

export AAA=1
swift build ...

package.swift:

if ProcessInfo.processInfo.environment["AAA"] == 1 {
 ...
}

but not in the xcodebuild archive.

I understand that xcodebuild re-reads the login environment when it calls swift build(or not), but why?

Does anyone have a solution that works on both sides at the same time?

Is there a reason you have to use environment variables instead of package traits?

Hi Max, Because I need to dynamically control whether to compile dynamic or static libraries.

More details:
We share a particularly complex package structure under iOS and macOS projects, it has multiple subpackages and a total of dozens of targets, each subpackage on the iOS side needs to be packaged into dynamic frameworks using the xcodebuild archive, and on the Mac side the outermost package into .a static library

Before is to use some strange third-party tools to regenerate the Xcode project from package, now is to streamline the process directly with xcodebuild and swift build to compile swift package, and then ran into this problem: if I set the subpackage into a dynamic library, the Mac's static library can not be packaged, and vice versa. If I set the subpackage to a dynamic library, the Mac can't package it, and vice versa, so I'm trying to control it with environment variables, but it doesn't work with xcodebuild.

Also it seems that xcodebuild doesn't support traits?

From my perspective, having to control behaviour based on environment variables means Swift Package Manager is missing a feature to address a need.

Could you please file an enhancement request describing the problem you are having?

1 Like