Skip to content

Commit cb592f5

Browse files
committed
Add the ability to control the environment passed to the manifest and plugin compilation
1 parent 417e39d commit cb592f5

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public protocol ManifestResourceProvider {
4444

4545
/// Extra flags to pass the Swift compiler.
4646
var swiftCompilerFlags: [String] { get }
47+
48+
/// Additional environment entries to pass to the Swift compiler and linker.
49+
var swiftCompilerEnvironment: [String: String] { get }
4750

4851
/// XCTest Location
4952
var xctestLocation: AbsolutePath? { get }
@@ -63,6 +66,10 @@ public extension ManifestResourceProvider {
6366
var swiftCompilerFlags: [String] {
6467
return []
6568
}
69+
70+
var swiftCompilerEnvironment: [String: String] {
71+
return [:]
72+
}
6673

6774
var xctestLocation: AbsolutePath? {
6875
return nil
@@ -786,8 +793,10 @@ public final class ManifestLoader: ManifestLoaderProtocol {
786793
let compiledManifestFile = tmpDir.appending(component: "\(packageIdentity)-manifest\(executableSuffix)")
787794
cmd += ["-o", compiledManifestFile.pathString]
788795

796+
let compilerEnv = ProcessEnv.vars.merging(resources.swiftCompilerEnvironment, uniquingKeysWith: { $1 })
797+
789798
// Compile the manifest.
790-
let compilerResult = try Process.popen(arguments: cmd)
799+
let compilerResult = try Process.popen(arguments: cmd, environment: compilerEnv)
791800
let compilerOutput = try (compilerResult.utf8Output() + compilerResult.utf8stderrOutput()).spm_chuzzle()
792801
result.compilerOutput = compilerOutput
793802

Sources/Workspace/DefaultPluginScriptRunner.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner {
114114
command += sources.paths.map { $0.pathString }
115115
let compiledExec = cacheDir.appending(component: "compiled-plugin")
116116
command += ["-o", compiledExec.pathString]
117+
118+
let environment = ProcessEnv.vars.merging(resources.swiftCompilerEnvironment, uniquingKeysWith: { $1 })
117119

118-
let result = try Process.popen(arguments: command)
120+
let result = try Process.popen(arguments: command, environment: environment)
119121
let output = try (result.utf8Output() + result.utf8stderrOutput()).spm_chuzzle() ?? ""
120122
if result.exitStatus != .terminated(code: 0) {
121123
// TODO: Make this a proper error.

Tests/PackageLoadingTests/PD_5_0_LoadingTests.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,66 @@ class PackageDescription5_0LoadingTests: PackageDescriptionLoadingTests {
547547
XCTAssertEqual(manifest.dependencies, [])
548548
}
549549
}
550+
551+
func testManifestLoaderEnvironment() throws {
552+
try testWithTemporaryDirectory { path in
553+
let fs = localFileSystem
554+
555+
struct CustomManifestResources: ManifestResourceProvider {
556+
var swiftCompiler: AbsolutePath
557+
var libDir: AbsolutePath
558+
var binDir: AbsolutePath?
559+
var sdkRoot: AbsolutePath?
560+
var swiftCompilerEnvironment: [String: String]
561+
}
562+
563+
let packagePath = path.appending(component: "pkg")
564+
let manifestPath = packagePath.appending(component: "Package.swift")
565+
try fs.writeFileContents(manifestPath) { stream in
566+
stream <<< """
567+
// swift-tools-version:5
568+
import PackageDescription
569+
570+
let package = Package(
571+
name: "Trivial",
572+
targets: [
573+
.target(
574+
name: "foo",
575+
dependencies: []),
576+
]
577+
)
578+
"""
579+
}
580+
581+
let moduleTraceFilePath = path.appending(component: "swift-module-trace")
582+
let customResources = CustomManifestResources(
583+
swiftCompiler: Resources.default.swiftCompiler,
584+
libDir: Resources.default.libDir,
585+
binDir: Resources.default.binDir,
586+
sdkRoot: Resources.default.sdkRoot,
587+
swiftCompilerEnvironment: ["SWIFT_LOADED_MODULE_TRACE_FILE": moduleTraceFilePath.pathString])
588+
let manifestLoader = ManifestLoader(
589+
manifestResources: customResources,
590+
serializedDiagnostics: true,
591+
isManifestSandboxEnabled: false,
592+
cacheDir: nil)
593+
594+
let diagnostics = DiagnosticsEngine()
595+
let manifest = try manifestLoader.load(
596+
at: manifestPath.parentDirectory,
597+
packageKind: .local,
598+
packageLocation: manifestPath.pathString,
599+
toolsVersion: .v5,
600+
fileSystem: fs,
601+
diagnostics: diagnostics
602+
)
603+
604+
XCTAssertTrue(diagnostics.diagnostics.isEmpty)
605+
XCTAssertEqual(manifest.name, "Trivial")
606+
607+
if let moduleTraceJSON = try? localFileSystem.readFileContents(moduleTraceFilePath).validDescription {
608+
XCTAssert(moduleTraceJSON.contains("PackageDescription"))
609+
}
610+
}
611+
}
550612
}

0 commit comments

Comments
 (0)