Skip to content

Commit 058bee8

Browse files
committed
Add Toolchain.overrideToolPath (and remove passing swiftCompiler path everywhere). Also cache tool paths after they have been looked up.
1 parent 44ff59f commit 058bee8

16 files changed

+90
-91
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ public struct Driver {
7070
/// The set of parsed options.
7171
var parsedOptions: ParsedOptions
7272

73-
/// The Swift compiler executable.
74-
public let swiftCompiler: AbsolutePath
75-
7673
/// Extra command-line arguments to pass to the Swift compiler.
7774
public let swiftCompilerPrefixArgs: [String]
7875

@@ -222,16 +219,16 @@ public struct Driver {
222219

223220
// Find the Swift compiler executable.
224221
if let frontendPath = self.parsedOptions.getLastArgument(.driverUseFrontendPath) {
225-
let frontendCommandLine = frontendPath.asSingle.split(separator: ";").map { String($0) }
222+
var frontendCommandLine = frontendPath.asSingle.split(separator: ";").map { String($0) }
226223
if frontendCommandLine.isEmpty {
227224
self.diagnosticEngine.emit(.error_no_swift_frontend)
228-
self.swiftCompiler = try self.toolchain.getToolPath(.swiftCompiler)
225+
self.swiftCompilerPrefixArgs = []
229226
} else {
230-
self.swiftCompiler = try AbsolutePath(validating: frontendCommandLine.first!)
227+
let frontendPath = frontendCommandLine.removeFirst()
228+
self.toolchain.overrideToolPath(.swiftCompiler, path: try AbsolutePath(validating: frontendPath))
229+
self.swiftCompilerPrefixArgs = frontendCommandLine
231230
}
232-
self.swiftCompilerPrefixArgs = Array(frontendCommandLine.dropFirst())
233231
} else {
234-
self.swiftCompiler = try self.toolchain.getToolPath(.swiftCompiler)
235232
self.swiftCompilerPrefixArgs = []
236233
}
237234

@@ -287,8 +284,7 @@ public struct Driver {
287284
moduleOutput: self.moduleOutput,
288285
inputFiles: inputFiles,
289286
diagnosticEngine: diagnosticEngine,
290-
actualSwiftVersion:
291-
try? toolchain.swiftCompilerVersion(self.swiftCompiler)
287+
actualSwiftVersion: try? toolchain.swiftCompilerVersion()
292288
)
293289

294290
self.sdkPath = Self.computeSDKPath(&parsedOptions, compilerMode: compilerMode, toolchain: toolchain, diagnosticsEngine: diagnosticEngine, env: env)
@@ -299,9 +295,10 @@ public struct Driver {
299295
outputFileMap: outputFileMap)
300296

301297
self.enabledSanitizers = try Self.parseSanitizerArgValues(
302-
&parsedOptions, diagnosticEngine: diagnosticEngine,
303-
toolchain: toolchain, targetTriple: targetTriple,
304-
swiftCompiler: swiftCompiler)
298+
&parsedOptions,
299+
diagnosticEngine: diagnosticEngine,
300+
toolchain: toolchain,
301+
targetTriple: targetTriple)
305302

306303
// Supplemental outputs.
307304
self.dependenciesFilePath = try Self.computeSupplementaryOutputPath(
@@ -562,7 +559,7 @@ extension Driver {
562559
) throws {
563560
// We just need to invoke the corresponding tool if the kind isn't Swift compiler.
564561
guard driverKind.isSwiftCompiler else {
565-
return try exec(path: swiftCompiler.pathString, args: driverKind.usageArgs + parsedOptions.commandLine)
562+
return try exec(path: toolchain.getToolPath(.swiftCompiler).pathString, args: driverKind.usageArgs + parsedOptions.commandLine)
566563
}
567564

568565
if parsedOptions.contains(.help) || parsedOptions.contains(.helpHidden) {
@@ -643,7 +640,7 @@ extension Driver {
643640
}
644641

645642
private func printVersion<S: OutputByteStream>(outputStream: inout S) throws {
646-
outputStream.write(try Process.checkNonZeroExit(args: swiftCompiler.pathString, "--version"))
643+
outputStream.write(try Process.checkNonZeroExit(args: toolchain.getToolPath(.swiftCompiler).pathString, "--version"))
647644
outputStream.flush()
648645
}
649646
}
@@ -1030,8 +1027,7 @@ extension Driver {
10301027
_ parsedOptions: inout ParsedOptions,
10311028
diagnosticEngine: DiagnosticsEngine,
10321029
toolchain: Toolchain,
1033-
targetTriple: Triple,
1034-
swiftCompiler: AbsolutePath
1030+
targetTriple: Triple
10351031
) throws -> Set<Sanitizer> {
10361032

10371033
var set = Set<Sanitizer>()
@@ -1056,7 +1052,6 @@ extension Driver {
10561052
for: sanitizer,
10571053
targetTriple: targetTriple,
10581054
parsedOptions: &parsedOptions,
1059-
swiftCompiler: swiftCompiler,
10601055
isShared: sanitizer != .fuzzer
10611056
)
10621057

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ extension Driver {
154154

155155
return Job(
156156
kind: .compile,
157-
tool: .absolute(swiftCompiler),
157+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
158158
commandLine: commandLine,
159159
displayInputs: primaryInputs,
160160
inputs: inputs,

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
import TSCBasic
1313

1414
extension DarwinToolchain {
15-
private func findARCLiteLibPath(swiftCompiler: AbsolutePath) throws
16-
-> AbsolutePath? {
17-
let path = swiftCompiler
15+
private func findARCLiteLibPath() throws -> AbsolutePath? {
16+
let path = try getToolPath(.swiftCompiler)
1817
.parentDirectory // 'swift'
1918
.parentDirectory // 'bin'
2019
.appending(components: "lib", "arc")
@@ -36,7 +35,6 @@ extension DarwinToolchain {
3635
to commandLine: inout [Job.ArgTemplate],
3736
parsedOptions: inout ParsedOptions,
3837
targetTriple: Triple,
39-
swiftCompiler: AbsolutePath,
4038
darwinLibName: String
4139
) throws {
4240
// Adding the rpaths might negatively interact when other rpaths are involved,
@@ -55,7 +53,7 @@ extension DarwinToolchain {
5553

5654

5755
let clangPath = try clangLibraryPath(
58-
for: targetTriple, swiftCompiler: swiftCompiler,
56+
for: targetTriple,
5957
parsedOptions: &parsedOptions)
6058
commandLine.appendFlag("-rpath")
6159
commandLine.appendPath(clangPath)
@@ -65,7 +63,6 @@ extension DarwinToolchain {
6563
to commandLine: inout [Job.ArgTemplate],
6664
parsedOptions: inout ParsedOptions,
6765
targetTriple: Triple,
68-
swiftCompiler: AbsolutePath,
6966
sanitizer: Sanitizer,
7067
isShared: Bool
7168
) throws {
@@ -84,16 +81,14 @@ extension DarwinToolchain {
8481
named: sanitizerName,
8582
to: &commandLine,
8683
for: targetTriple,
87-
parsedOptions: &parsedOptions,
88-
swiftCompiler: swiftCompiler
84+
parsedOptions: &parsedOptions
8985
)
9086

9187
if isShared {
9288
try addLinkRuntimeLibraryRPath(
9389
to: &commandLine,
9490
parsedOptions: &parsedOptions,
9591
targetTriple: targetTriple,
96-
swiftCompiler: swiftCompiler,
9792
darwinLibName: sanitizerName
9893
)
9994
}
@@ -102,12 +97,10 @@ extension DarwinToolchain {
10297
private func addProfileGenerationArgs(
10398
to commandLine: inout [Job.ArgTemplate],
10499
parsedOptions: inout ParsedOptions,
105-
targetTriple: Triple,
106-
swiftCompiler: AbsolutePath
100+
targetTriple: Triple
107101
) throws {
108102
guard parsedOptions.hasArgument(.profileGenerate) else { return }
109103
let clangPath = try clangLibraryPath(for: targetTriple,
110-
swiftCompiler: swiftCompiler,
111104
parsedOptions: &parsedOptions)
112105

113106
let runtime = targetTriple.darwinPlatform!.libraryNameSuffix
@@ -150,7 +143,6 @@ extension DarwinToolchain {
150143
private func addArgsToLinkARCLite(
151144
to commandLine: inout [Job.ArgTemplate],
152145
parsedOptions: inout ParsedOptions,
153-
swiftCompiler: AbsolutePath,
154146
targetTriple: Triple
155147
) throws {
156148
guard parsedOptions.hasFlag(
@@ -161,8 +153,7 @@ extension DarwinToolchain {
161153
return
162154
}
163155

164-
guard let arcLiteLibPath = try findARCLiteLibPath(
165-
swiftCompiler: swiftCompiler),
156+
guard let arcLiteLibPath = try findARCLiteLibPath(),
166157
let platformName = targetTriple.platformName() else {
167158
return
168159
}
@@ -187,8 +178,7 @@ extension DarwinToolchain {
187178
outputFile: VirtualPath,
188179
sdkPath: String?,
189180
sanitizers: Set<Sanitizer>,
190-
targetTriple: Triple,
191-
swiftCompiler: AbsolutePath
181+
targetTriple: Triple
192182
) throws -> AbsolutePath {
193183

194184
// FIXME: If we used Clang as a linker instead of going straight to ld,
@@ -204,7 +194,7 @@ extension DarwinToolchain {
204194
targetTriple.darwinPlatform!.with(.device)!.libraryNameSuffix
205195
let compilerRTPath =
206196
try clangLibraryPath(
207-
for: targetTriple, swiftCompiler: swiftCompiler,
197+
for: targetTriple,
208198
parsedOptions: &parsedOptions)
209199
.appending(component: "libclang_rt.\(darwinPlatformSuffix).a")
210200
if localFileSystem.exists(compilerRTPath) {
@@ -237,7 +227,6 @@ extension DarwinToolchain {
237227
to: &commandLine,
238228
parsedOptions: &parsedOptions,
239229
targetTriple: targetTriple,
240-
swiftCompiler: swiftCompiler,
241230
sanitizer: sanitizer,
242231
isShared: sanitizer != .fuzzer
243232
)
@@ -250,8 +239,7 @@ extension DarwinToolchain {
250239
to: &commandLine,
251240
parsedOptions: &parsedOptions,
252241
sdkPath: sdkPath,
253-
targetTriple: targetTriple,
254-
swiftCompiler: swiftCompiler
242+
targetTriple: targetTriple
255243
)
256244

257245
// These custom arguments should be right before the object file at the
@@ -270,7 +258,6 @@ extension DarwinToolchain {
270258
try addArgsToLinkARCLite(
271259
to: &commandLine,
272260
parsedOptions: &parsedOptions,
273-
swiftCompiler: swiftCompiler,
274261
targetTriple: targetTriple
275262
)
276263
addDeploymentTargetArgs(
@@ -280,8 +267,7 @@ extension DarwinToolchain {
280267
try addProfileGenerationArgs(
281268
to: &commandLine,
282269
parsedOptions: &parsedOptions,
283-
targetTriple: targetTriple,
284-
swiftCompiler: swiftCompiler
270+
targetTriple: targetTriple
285271
)
286272

287273
commandLine.appendFlags(

Sources/SwiftDriver/Jobs/EmitModuleJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Driver {
7070

7171
return Job(
7272
kind: .emitModule,
73-
tool: .absolute(swiftCompiler),
73+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
7474
commandLine: commandLine,
7575
inputs: inputs,
7676
outputs: outputs

Sources/SwiftDriver/Jobs/GeneratePCHJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extension Driver {
6060

6161
return Job(
6262
kind: .generatePCH,
63-
tool: .absolute(swiftCompiler),
63+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
6464
commandLine: commandLine,
6565
displayInputs: [],
6666
inputs: inputs,

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ extension GenericUnixToolchain {
5050
outputFile: VirtualPath,
5151
sdkPath: String?,
5252
sanitizers: Set<Sanitizer>,
53-
targetTriple: Triple,
54-
swiftCompiler: AbsolutePath
53+
targetTriple: Triple
5554
) throws -> AbsolutePath {
5655
switch linkerOutputType {
5756
case .dynamicLibrary:
@@ -130,7 +129,7 @@ extension GenericUnixToolchain {
130129
let runtimePaths = try runtimeLibraryPaths(
131130
for: targetTriple,
132131
parsedOptions: &parsedOptions,
133-
sdkPath: sdkPath, swiftCompiler: swiftCompiler,
132+
sdkPath: sdkPath,
134133
isShared: hasRuntimeArgs
135134
)
136135

@@ -148,7 +147,6 @@ extension GenericUnixToolchain {
148147
let sharedResourceDirPath = try computeResourceDirPath(
149148
for: targetTriple,
150149
parsedOptions: &parsedOptions,
151-
swiftCompiler: swiftCompiler,
152150
isShared: true
153151
)
154152

@@ -194,7 +192,6 @@ extension GenericUnixToolchain {
194192
var linkFilePath: AbsolutePath? = try computeResourceDirPath(
195193
for: targetTriple,
196194
parsedOptions: &parsedOptions,
197-
swiftCompiler: swiftCompiler,
198195
isShared: false
199196
)
200197

Sources/SwiftDriver/Jobs/InterpretJob.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ extension Driver {
3838

3939
let extraEnvironment = try toolchain.platformSpecificInterpreterEnvironmentVariables(
4040
env: self.env, parsedOptions: &parsedOptions, sdkPath: self.sdkPath,
41-
targetTriple: self.targetTriple, swiftCompiler: self.swiftCompiler)
41+
targetTriple: self.targetTriple)
4242

4343
return Job(
4444
kind: .interpret,
45-
tool: .absolute(swiftCompiler),
45+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
4646
commandLine: commandLine,
4747
inputs:inputs,
4848
outputs: [],

Sources/SwiftDriver/Jobs/LinkJob.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ extension Driver {
4848
outputFile: outputFile,
4949
sdkPath: sdkPath,
5050
sanitizers: enabledSanitizers,
51-
targetTriple: targetTriple,
52-
swiftCompiler: swiftCompiler
51+
targetTriple: targetTriple
5352
)
5453

5554
return Job(

Sources/SwiftDriver/Jobs/MergeModuleJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension Driver {
5151

5252
return Job(
5353
kind: .mergeModule,
54-
tool: .absolute(swiftCompiler),
54+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
5555
commandLine: commandLine,
5656
inputs: inputs,
5757
outputs: outputs

Sources/SwiftDriver/Jobs/ReplJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension Driver {
4141
commandLine.appendFlags("-module-name", moduleName)
4242
return Job(
4343
kind: .repl,
44-
tool: .absolute(swiftCompiler),
44+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
4545
commandLine: commandLine,
4646
inputs: [],
4747
outputs: [],

Sources/SwiftDriver/Jobs/Toolchain+InterpreterSupport.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ extension DarwinToolchain {
3434
env: [String : String],
3535
parsedOptions: inout ParsedOptions,
3636
sdkPath: String?,
37-
targetTriple: Triple,
38-
swiftCompiler: AbsolutePath) throws -> [String: String] {
37+
targetTriple: Triple) throws -> [String: String] {
3938
var envVars: [String: String] = [:]
4039

4140
let runtimePaths = try runtimeLibraryPaths(
4241
for: targetTriple,
4342
parsedOptions: &parsedOptions,
44-
sdkPath: sdkPath, swiftCompiler: swiftCompiler,
43+
sdkPath: sdkPath,
4544
isShared: true
4645
).map { $0.pathString }
4746

@@ -63,15 +62,13 @@ extension GenericUnixToolchain {
6362
env: [String : String],
6463
parsedOptions: inout ParsedOptions,
6564
sdkPath: String?,
66-
targetTriple: Triple,
67-
swiftCompiler: AbsolutePath) throws -> [String: String] {
65+
targetTriple: Triple) throws -> [String: String] {
6866
var envVars: [String: String] = [:]
6967

7068
let runtimePaths = try runtimeLibraryPaths(
7169
for: targetTriple,
7270
parsedOptions: &parsedOptions,
7371
sdkPath: sdkPath,
74-
swiftCompiler: swiftCompiler,
7572
isShared: true
7673
).map { $0.pathString }
7774

0 commit comments

Comments
 (0)