Skip to content

Commit fe4a66c

Browse files
committed
Replace occurences of the deprecated 'ProcessEnv.vars' with 'ProcessEnv.block'
1 parent 762564e commit fe4a66c

29 files changed

+387
-191
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import struct TSCBasic.SHA256
2828
import var TSCBasic.localFileSystem
2929
import var TSCBasic.stderrStream
3030
import var TSCBasic.stdoutStream
31+
import typealias TSCBasic.ProcessEnvironmentBlock
32+
import struct TSCBasic.ProcessEnvironmentKey
3133

3234
extension Driver {
3335
/// Stub Error for terminating the process.
@@ -153,7 +155,7 @@ public struct Driver {
153155
/// The set of environment variables that are visible to the driver and
154156
/// processes it launches. This is a hook for testing; in actual use
155157
/// it should be identical to the real environment.
156-
public let env: [String: String]
158+
public let env: ProcessEnvironmentBlock
157159

158160
/// Whether we are using the driver as the integrated driver via libSwiftDriver
159161
public let integratedDriver: Bool
@@ -814,11 +816,44 @@ public struct Driver {
814816
)
815817
}
816818

819+
@available(*, deprecated, renamed: "init(args:envBlock:diagnosticsOutput:fileSystem:executor:integratedDriver:compilerIntegratedTooling:compilerExecutableDir:externalTargetModuleDetailsMap:interModuleDependencyOracle:)")
820+
@_disfavoredOverload
821+
public init(
822+
args: [String],
823+
env: [String: String] = ProcessEnv.vars,
824+
diagnosticsOutput: DiagnosticsOutput = .engine(DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler])),
825+
fileSystem: FileSystem = localFileSystem,
826+
executor: DriverExecutor,
827+
integratedDriver: Bool = true,
828+
compilerIntegratedTooling: Bool = false,
829+
compilerExecutableDir: AbsolutePath? = nil,
830+
externalTargetModuleDetailsMap: ExternalTargetModuleDetailsMap? = nil,
831+
interModuleDependencyOracle: InterModuleDependencyOracle? = nil
832+
) throws {
833+
let envBlock = env.reduce([:]) { (partialResult: ProcessEnvironmentBlock, tuple: (key: String, value: String)) in
834+
var result = partialResult
835+
result[ProcessEnvironmentKey(tuple.key)] = tuple.value
836+
return result
837+
}
838+
try self.init(
839+
args: args,
840+
envBlock: envBlock,
841+
diagnosticsOutput: diagnosticsOutput,
842+
fileSystem: fileSystem,
843+
executor: executor,
844+
integratedDriver: integratedDriver,
845+
compilerIntegratedTooling: false,
846+
compilerExecutableDir: compilerExecutableDir,
847+
externalTargetModuleDetailsMap: externalTargetModuleDetailsMap,
848+
interModuleDependencyOracle: interModuleDependencyOracle
849+
)
850+
}
851+
817852
/// Create the driver with the given arguments.
818853
///
819854
/// - Parameter args: The command-line arguments, including the "swift" or "swiftc"
820855
/// at the beginning.
821-
/// - Parameter env: The environment variables to use. This is a hook for testing;
856+
/// - Parameter envBlock: The environment variables to use. This is a hook for testing;
822857
/// in production, you should use the default argument, which copies the current environment.
823858
/// - Parameter diagnosticsOutput: The diagnostics output implementation used by the driver to emit errors
824859
/// and warnings.
@@ -836,7 +871,7 @@ public struct Driver {
836871
/// shared across different module builds by a build system.
837872
public init(
838873
args: [String],
839-
env: [String: String] = ProcessEnv.vars,
874+
envBlock: ProcessEnvironmentBlock = ProcessEnv.block,
840875
diagnosticsOutput: DiagnosticsOutput = .engine(DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler])),
841876
fileSystem: FileSystem = localFileSystem,
842877
executor: DriverExecutor,
@@ -845,7 +880,7 @@ public struct Driver {
845880
compilerExecutableDir: AbsolutePath? = nil,
846881
interModuleDependencyOracle: InterModuleDependencyOracle? = nil
847882
) throws {
848-
self.env = env
883+
self.env = envBlock
849884
self.fileSystem = fileSystem
850885
self.integratedDriver = integratedDriver
851886
self.compilerIntegratedTooling = compilerIntegratedTooling
@@ -2535,7 +2570,7 @@ extension Driver {
25352570
static func determineNumParallelJobs(
25362571
_ parsedOptions: inout ParsedOptions,
25372572
diagnosticsEngine: DiagnosticsEngine,
2538-
env: [String: String]
2573+
env: ProcessEnvironmentBlock
25392574
) -> Int? {
25402575
guard let numJobs = parseIntOption(&parsedOptions, option: .j, diagnosticsEngine: diagnosticsEngine) else {
25412576
return nil
@@ -3028,7 +3063,7 @@ extension Driver {
30283063
targetTriple: Triple?,
30293064
fileSystem: FileSystem,
30303065
diagnosticsEngine: DiagnosticsEngine,
3031-
env: [String: String]
3066+
env: ProcessEnvironmentBlock
30323067
) -> VirtualPath? {
30333068
var sdkPath: String?
30343069

@@ -3558,7 +3593,7 @@ extension Driver {
35583593
_ parsedOptions: inout ParsedOptions,
35593594
diagnosticsEngine: DiagnosticsEngine,
35603595
compilerMode: CompilerMode,
3561-
env: [String: String],
3596+
env: ProcessEnvironmentBlock,
35623597
executor: DriverExecutor,
35633598
fileSystem: FileSystem,
35643599
useStaticResourceDir: Bool,
@@ -3589,7 +3624,7 @@ extension Driver {
35893624
static func computeTargetInfo(_ parsedOptions: inout ParsedOptions,
35903625
diagnosticsEngine: DiagnosticsEngine,
35913626
compilerMode: CompilerMode,
3592-
env: [String: String],
3627+
env: ProcessEnvironmentBlock,
35933628
executor: DriverExecutor,
35943629
libSwiftScan: SwiftScan?,
35953630
toolchain: Toolchain,

Sources/SwiftDriver/Execution/DriverExecutor.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import struct TSCBasic.ProcessResult
1515
import struct Foundation.Data
1616
import class Foundation.JSONDecoder
1717
import var Foundation.EXIT_SUCCESS
18+
import typealias TSCBasic.ProcessEnvironmentBlock
19+
import struct TSCBasic.ProcessEnvironmentKey
1820

1921
/// A type that is capable of executing compilation jobs on some underlying
2022
/// build service.
@@ -205,3 +207,14 @@ public protocol JobExecutionDelegate {
205207
/// Called when a job is skipped.
206208
func jobSkipped(job: Job)
207209
}
210+
211+
@_spi(Testing) public extension ProcessEnvironmentBlock {
212+
var legacyVars: [String: String] {
213+
return self.reduce([:]) {
214+
(partialResult: [String: String], tuple: (key: ProcessEnvironmentKey, value: String)) in
215+
var result = partialResult
216+
result[tuple.key.value] = tuple.value
217+
return result
218+
}
219+
}
220+
}

Sources/SwiftDriver/Execution/ProcessProtocol.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import class TSCBasic.Process
1414
import struct TSCBasic.ProcessResult
15+
import typealias TSCBasic.ProcessEnvironmentBlock
1516

1617
import class Foundation.FileHandle
1718
import struct Foundation.Data
@@ -32,17 +33,18 @@ public protocol ProcessProtocol {
3233

3334
static func launchProcess(
3435
arguments: [String],
35-
env: [String: String]
36+
env: ProcessEnvironmentBlock
3637
) throws -> Self
3738

3839
static func launchProcessAndWriteInput(
3940
arguments: [String],
40-
env: [String: String],
41+
env: ProcessEnvironmentBlock,
4142
inputFileHandle: FileHandle
4243
) throws -> Self
4344
}
4445

4546
extension TSCBasic.Process: ProcessProtocol {
47+
@available(*, deprecated, message: "use launchProcess(arguments:envBlock:) instead")
4648
public static func launchProcess(
4749
arguments: [String],
4850
env: [String: String]
@@ -52,12 +54,21 @@ extension TSCBasic.Process: ProcessProtocol {
5254
return process
5355
}
5456

57+
public static func launchProcess(
58+
arguments: [String],
59+
env: ProcessEnvironmentBlock
60+
) throws -> TSCBasic.Process {
61+
let process = Process(arguments: arguments, environmentBlock: env)
62+
try process.launch()
63+
return process
64+
}
65+
5566
public static func launchProcessAndWriteInput(
5667
arguments: [String],
57-
env: [String: String],
68+
env: ProcessEnvironmentBlock,
5869
inputFileHandle: FileHandle
5970
) throws -> TSCBasic.Process {
60-
let process = Process(arguments: arguments, environment: env)
71+
let process = Process(arguments: arguments, environmentBlock: env)
6172
let processInputStream = try process.launch()
6273
var input: Data
6374
// Write out the contents of the input handle and close the input stream

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import struct TSCBasic.RelativePath
1616
import struct TSCBasic.Diagnostic
1717
import var TSCBasic.localFileSystem
1818
import var TSCBasic.stdoutStream
19+
import typealias TSCBasic.ProcessEnvironmentBlock
1920

2021
import SwiftOptions
2122
import struct Foundation.Data
@@ -334,7 +335,7 @@ public extension Driver {
334335
useResponseFiles: useResponseFiles).0.map { $0.spm_shellEscaped() }
335336
}
336337

337-
static func getRootPath(of toolchain: Toolchain, env: [String: String])
338+
static func getRootPath(of toolchain: Toolchain, env: ProcessEnvironmentBlock)
338339
throws -> AbsolutePath {
339340
return try toolchain.getToolPath(.swiftCompiler)
340341
.parentDirectory // bin

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension DarwinToolchain {
1919
internal func findXcodeClangPath() throws -> AbsolutePath? {
2020
let result = try executor.checkNonZeroExit(
2121
args: "xcrun", "-toolchain", "default", "-f", "clang",
22-
environment: env
22+
environment: env.legacyVars
2323
).trimmingCharacters(in: .whitespacesAndNewlines)
2424

2525
return result.isEmpty ? nil : try AbsolutePath(validating: result)

Sources/SwiftDriver/Jobs/EmitSupportedFeaturesJob.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import class TSCBasic.DiagnosticsEngine
1818
import protocol TSCBasic.FileSystem
1919
import struct TSCBasic.RelativePath
2020
import var TSCBasic.localFileSystem
21+
import typealias TSCBasic.ProcessEnvironmentBlock
2122

2223
/// Describes information about the compiler's supported arguments and features
2324
@_spi(Testing) public struct SupportedCompilerFeatures: Codable {
@@ -90,7 +91,7 @@ extension Driver {
9091
}
9192

9293
static func computeSupportedCompilerFeatures(of toolchain: Toolchain,
93-
env: [String: String]) throws -> Set<String> {
94+
env: ProcessEnvironmentBlock) throws -> Set<String> {
9495
struct FeatureInfo: Codable {
9596
var name: String
9697
}

Sources/SwiftDriver/Jobs/Job.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import protocol TSCBasic.DiagnosticData
1414
import protocol TSCBasic.FileSystem
15+
import typealias TSCBasic.ProcessEnvironmentBlock
16+
import struct TSCBasic.ProcessEnvironmentKey
1517

1618
/// A job represents an individual subprocess that should be invoked during compilation.
1719
public struct Job: Codable, Equatable, Hashable {
@@ -93,7 +95,7 @@ public struct Job: Codable, Equatable, Hashable {
9395
public var outputs: [TypedVirtualPath]
9496

9597
/// Any extra environment variables which should be set while running the job.
96-
public var extraEnvironment: [String: String]
98+
public var extraEnvironment: ProcessEnvironmentBlock
9799

98100
/// Whether or not the job must be executed in place, replacing the current driver process.
99101
public var requiresInPlaceExecution: Bool
@@ -118,7 +120,7 @@ public struct Job: Codable, Equatable, Hashable {
118120
outputs: [TypedVirtualPath],
119121
outputCacheKeys: [TypedVirtualPath: String] = [:],
120122
inputOutputMap: [TypedVirtualPath : [TypedVirtualPath]] = [:],
121-
extraEnvironment: [String: String] = [:],
123+
extraEnvironment: ProcessEnvironmentBlock = [:],
122124
requiresInPlaceExecution: Bool = false
123125
) {
124126
self.moduleName = moduleName
@@ -131,7 +133,8 @@ public struct Job: Codable, Equatable, Hashable {
131133
self.outputs = outputs
132134
self.outputCacheKeys = outputCacheKeys
133135
self.compileInputOutputMap = inputOutputMap
134-
self.extraEnvironment = extraEnvironment
136+
self.extraEnvironmentBlock = extraEnvironment
137+
self.extraEnvironment = self.extraEnvironmentBlock.legacyVars
135138
self.requiresInPlaceExecution = requiresInPlaceExecution
136139
self.supportsResponseFiles = tool.supportsResponseFiles
137140
}

Sources/SwiftDriver/Jobs/Toolchain+InterpreterSupport.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import typealias TSCBasic.ProcessEnvironmentBlock
14+
import struct TSCBasic.ProcessEnvironmentKey
1315
import SwiftOptions
1416

1517
extension Toolchain {
1618
func addPathEnvironmentVariableIfNeeded(
17-
_ environmentVariable: String,
18-
to env: inout [String : String],
19-
currentEnv: [String: String],
19+
_ environmentVariable: ProcessEnvironmentKey,
20+
to env: inout ProcessEnvironmentBlock,
21+
currentEnv: ProcessEnvironmentBlock,
2022
option: Option,
2123
parsedOptions: inout ParsedOptions,
2224
extraPaths: [String] = []
@@ -31,17 +33,17 @@ extension Toolchain {
3133

3234
extension DarwinToolchain {
3335
public func platformSpecificInterpreterEnvironmentVariables(
34-
env: [String : String],
36+
env: ProcessEnvironmentBlock,
3537
parsedOptions: inout ParsedOptions,
3638
sdkPath: VirtualPath.Handle?,
37-
targetInfo: FrontendTargetInfo) throws -> [String: String] {
38-
var envVars: [String: String] = [:]
39+
targetInfo: FrontendTargetInfo) throws -> ProcessEnvironmentBlock {
40+
var envVars: ProcessEnvironmentBlock = [:]
3941

40-
addPathEnvironmentVariableIfNeeded("DYLD_LIBRARY_PATH", to: &envVars,
42+
addPathEnvironmentVariableIfNeeded(ProcessEnvironmentKey("DYLD_LIBRARY_PATH"), to: &envVars,
4143
currentEnv: env, option: .L,
4244
parsedOptions: &parsedOptions)
4345

44-
addPathEnvironmentVariableIfNeeded("DYLD_FRAMEWORK_PATH", to: &envVars,
46+
addPathEnvironmentVariableIfNeeded(ProcessEnvironmentKey("DYLD_FRAMEWORK_PATH"), to: &envVars,
4547
currentEnv: env, option: .F,
4648
parsedOptions: &parsedOptions,
4749
extraPaths: ["/System/Library/Frameworks"])
@@ -52,11 +54,11 @@ extension DarwinToolchain {
5254

5355
extension GenericUnixToolchain {
5456
public func platformSpecificInterpreterEnvironmentVariables(
55-
env: [String : String],
57+
env: ProcessEnvironmentBlock,
5658
parsedOptions: inout ParsedOptions,
5759
sdkPath: VirtualPath.Handle?,
58-
targetInfo: FrontendTargetInfo) throws -> [String: String] {
59-
var envVars: [String: String] = [:]
60+
targetInfo: FrontendTargetInfo) throws -> ProcessEnvironmentBlock {
61+
var envVars: ProcessEnvironmentBlock = [:]
6062

6163
let runtimePaths = try runtimeLibraryPaths(
6264
for: targetInfo,
@@ -65,7 +67,7 @@ extension GenericUnixToolchain {
6567
isShared: true
6668
).map { $0.name }
6769

68-
addPathEnvironmentVariableIfNeeded("LD_LIBRARY_PATH", to: &envVars,
70+
addPathEnvironmentVariableIfNeeded(ProcessEnvironmentKey("LD_LIBRARY_PATH"), to: &envVars,
6971
currentEnv: env, option: .L,
7072
parsedOptions: &parsedOptions,
7173
extraPaths: runtimePaths)
@@ -76,10 +78,10 @@ extension GenericUnixToolchain {
7678

7779
extension WindowsToolchain {
7880
public func platformSpecificInterpreterEnvironmentVariables(
79-
env: [String : String],
81+
env: ProcessEnvironmentBlock,
8082
parsedOptions: inout ParsedOptions,
8183
sdkPath: VirtualPath.Handle?,
82-
targetInfo: FrontendTargetInfo) throws -> [String: String] {
84+
targetInfo: FrontendTargetInfo) throws -> ProcessEnvironmentBlock {
8385

8486
// TODO(compnerd): setting up `Path` is meaningless currently as the lldb
8587
// support required for the interpreter mode fails to load the dependencies.

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import class TSCBasic.DiagnosticsEngine
2121
import struct TSCBasic.AbsolutePath
2222
import struct TSCBasic.Diagnostic
2323
import var TSCBasic.localFileSystem
24+
import typealias TSCBasic.ProcessEnvironmentBlock
25+
import struct TSCBasic.ProcessEnvironmentKey
2426

2527
/// Toolchain for Darwin-based platforms, such as macOS and iOS.
2628
///
2729
/// FIXME: This class is not thread-safe.
2830
public final class DarwinToolchain: Toolchain {
29-
public let env: [String: String]
31+
public let env: ProcessEnvironmentBlock
3032

3133
/// Doubles as path cache and point for overriding normal lookup
3234
private var toolPaths = [Tool: AbsolutePath]()
@@ -45,7 +47,7 @@ public final class DarwinToolchain: Toolchain {
4547

4648
public let dummyForTestingObjectFormat = Triple.ObjectFormat.macho
4749

48-
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem,
50+
public init(env: ProcessEnvironmentBlock, executor: DriverExecutor, fileSystem: FileSystem = localFileSystem,
4951
compilerExecutableDir: AbsolutePath? = nil, toolDirectory: AbsolutePath? = nil) {
5052
self.env = env
5153
self.executor = executor
@@ -139,7 +141,7 @@ public final class DarwinToolchain: Toolchain {
139141
if target?.isMacOSX == true || hostIsMacOS {
140142
let result = try executor.checkNonZeroExit(
141143
args: "xcrun", "-sdk", "macosx", "--show-sdk-path",
142-
environment: env
144+
environment: env.legacyVars
143145
).trimmingCharacters(in: .whitespacesAndNewlines)
144146
return try AbsolutePath(validating: result)
145147
}

0 commit comments

Comments
 (0)