Skip to content

Commit

Permalink
Add the ability to control the environment passed to the manifest and…
Browse files Browse the repository at this point in the history
… plugin compilation

It defaults to `ProcessEnv.vars` so that there is no change in semantics unless the client code customizes the environment.
  • Loading branch information
abertelrud committed Aug 18, 2021
1 parent 8d0beab commit fb32460
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/PackageLoading/ManifestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
cmd += ["-o", compiledManifestFile.pathString]

// Compile the manifest.
let compilerResult = try Process.popen(arguments: cmd)
let compilerResult = try Process.popen(arguments: cmd, environment: toolchain.swiftCompilerEnvironment)
let compilerOutput = try (compilerResult.utf8Output() + compilerResult.utf8stderrOutput()).spm_chuzzle()
result.compilerOutput = compilerOutput

Expand Down
5 changes: 5 additions & 0 deletions Sources/PackageModel/ToolchainConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public struct ToolchainConfiguration {
/// Extra arguments to pass the Swift compiler (defaults to the empty string).
public var swiftCompilerFlags: [String]

/// Environment to pass to the Swift compiler (defaults to the inherited environment).
public var swiftCompilerEnvironment: [String: String]

/// The path of the library resources.
public var libDir: AbsolutePath

Expand All @@ -47,13 +50,15 @@ public struct ToolchainConfiguration {
public init(
swiftCompiler: AbsolutePath,
swiftCompilerFlags: [String] = [],
swiftCompilerEnvironment: [String: String] = ProcessEnv.vars,
libDir: AbsolutePath? = nil,
binDir: AbsolutePath? = nil,
sdkRoot: AbsolutePath? = nil,
xctestLocation: AbsolutePath? = nil
) {
self.swiftCompiler = swiftCompiler
self.swiftCompilerFlags = swiftCompilerFlags
self.swiftCompilerEnvironment = swiftCompilerEnvironment
self.libDir = libDir ?? Self.libDir(forBinDir: swiftCompiler.parentDirectory)
self.binDir = binDir
self.sdkRoot = sdkRoot
Expand Down
2 changes: 1 addition & 1 deletion Sources/Workspace/DefaultPluginScriptRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner {
let compiledExec = cacheDir.appending(component: "compiled-plugin")
command += ["-o", compiledExec.pathString]

let result = try Process.popen(arguments: command)
let result = try Process.popen(arguments: command, environment: toolchain.swiftCompilerEnvironment)
let output = try (result.utf8Output() + result.utf8stderrOutput()).spm_chuzzle() ?? ""
if result.exitStatus != .terminated(code: 0) {
// TODO: Make this a proper error.
Expand Down
49 changes: 49 additions & 0 deletions Tests/PackageLoadingTests/PD_5_0_LoadingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,53 @@ class PackageDescription5_0LoadingTests: PackageDescriptionLoadingTests {
XCTAssertEqual(manifest.dependencies, [])
}
}

func testManifestLoaderEnvironment() throws {
try testWithTemporaryDirectory { path in
let fs = localFileSystem

let packagePath = path.appending(component: "pkg")
let manifestPath = packagePath.appending(component: "Package.swift")
try fs.writeFileContents(manifestPath) { stream in
stream <<< """
// swift-tools-version:5
import PackageDescription
let package = Package(
name: "Trivial",
targets: [
.target(
name: "foo",
dependencies: []),
]
)
"""
}

let moduleTraceFilePath = path.appending(component: "swift-module-trace")
var toolchain = ToolchainConfiguration.default
toolchain.swiftCompilerEnvironment["SWIFT_LOADED_MODULE_TRACE_FILE"] = moduleTraceFilePath.pathString
let manifestLoader = ManifestLoader(
toolchain: toolchain,
serializedDiagnostics: true,
isManifestSandboxEnabled: false,
cacheDir: nil)

let diagnostics = DiagnosticsEngine()
let manifest = try manifestLoader.load(
at: manifestPath.parentDirectory,
packageKind: .local,
packageLocation: manifestPath.pathString,
toolsVersion: .v5,
fileSystem: fs,
diagnostics: diagnostics
)

XCTAssertTrue(diagnostics.diagnostics.isEmpty)
XCTAssertEqual(manifest.name, "Trivial")

let moduleTraceJSON = try XCTUnwrap(try localFileSystem.readFileContents(moduleTraceFilePath).validDescription)
XCTAssert(moduleTraceJSON.contains("PackageDescription"))
}
}
}

0 comments on commit fb32460

Please sign in to comment.