@@ -77,6 +77,9 @@ public struct Driver {
77
77
/// it should be identical to the real environment.
78
78
public let env : [ String : String ]
79
79
80
+ /// The file system which we should interact with.
81
+ public let fileSystem : FileSystem
82
+
80
83
/// Diagnostic engine for emitting warnings, errors, etc.
81
84
public let diagnosticEngine : DiagnosticsEngine
82
85
@@ -226,17 +229,19 @@ public struct Driver {
226
229
public init (
227
230
args: [ String ] ,
228
231
env: [ String : String ] = ProcessEnv . vars,
229
- diagnosticsEngine: DiagnosticsEngine = DiagnosticsEngine ( handlers: [ Driver . stderrDiagnosticsHandler] )
232
+ diagnosticsEngine: DiagnosticsEngine = DiagnosticsEngine ( handlers: [ Driver . stderrDiagnosticsHandler] ) ,
233
+ fileSystem: FileSystem = localFileSystem
230
234
) throws {
231
235
self . env = env
236
+ self . fileSystem = fileSystem
232
237
233
238
self . diagnosticEngine = diagnosticsEngine
234
239
235
240
if case . subcommand = try Self . invocationRunMode ( forArgs: args) . mode {
236
241
throw Error . subcommandPassedToDriver
237
242
}
238
243
239
- var args = try Self . expandResponseFiles ( args, diagnosticsEngine: self . diagnosticEngine) [ ... ]
244
+ var args = try Self . expandResponseFiles ( args, fileSystem : fileSystem , diagnosticsEngine: self . diagnosticEngine) [ ... ]
240
245
241
246
self . driverKind = try Self . determineDriverKind ( args: & args)
242
247
self . optionTable = OptionTable ( )
@@ -246,7 +251,7 @@ public struct Driver {
246
251
. map {
247
252
Triple ( $0, normalizing: true )
248
253
}
249
- ( self . toolchain, self . targetTriple) = try Self . computeToolchain ( explicitTarget, diagnosticsEngine: diagnosticEngine, env: env)
254
+ ( self . toolchain, self . targetTriple) = try Self . computeToolchain ( explicitTarget, diagnosticsEngine: diagnosticEngine, env: env, fileSystem : fileSystem )
250
255
self . targetVariantTriple = self . parsedOptions. getLastArgument ( . targetVariant) . map { Triple ( $0. asSingle, normalizing: true ) }
251
256
252
257
// Find the Swift compiler executable.
@@ -266,7 +271,7 @@ public struct Driver {
266
271
267
272
// Compute the working directory.
268
273
workingDirectory = try parsedOptions. getLastArgument ( . workingDirectory) . map { workingDirectoryArg in
269
- let cwd = localFileSystem . currentWorkingDirectory
274
+ let cwd = fileSystem . currentWorkingDirectory
270
275
return try cwd. map { AbsolutePath ( workingDirectoryArg. asSingle, relativeTo: $0) } ?? AbsolutePath ( validating: workingDirectoryArg. asSingle)
271
276
}
272
277
@@ -280,7 +285,7 @@ public struct Driver {
280
285
self . inputFiles = inputFiles
281
286
self . recordedInputModificationDates = . init( uniqueKeysWithValues:
282
287
Set ( inputFiles) . compactMap {
283
- guard let modTime = try ? localFileSystem
288
+ guard let modTime = try ? fileSystem
284
289
. getFileInfo ( $0. file) . modTime else { return nil }
285
290
return ( $0, modTime)
286
291
} )
@@ -289,7 +294,7 @@ public struct Driver {
289
294
// Initialize an empty output file map, which will be populated when we start creating jobs.
290
295
if let outputFileMapArg = parsedOptions. getLastArgument ( . outputFileMap) ? . asSingle {
291
296
let path = try VirtualPath ( path: outputFileMapArg)
292
- outputFileMap = try . load( file: path, diagnosticEngine: diagnosticEngine)
297
+ outputFileMap = try . load( fileSystem : fileSystem , file: path, diagnosticEngine: diagnosticEngine)
293
298
} else {
294
299
outputFileMap = nil
295
300
}
@@ -327,13 +332,14 @@ public struct Driver {
327
332
compilerMode: compilerMode,
328
333
outputFileMap: self . outputFileMap,
329
334
compilerOutputType: self . compilerOutputType,
330
- moduleOutput: self . moduleOutput,
335
+ moduleOutput: self . moduleOutput,
336
+ fileSystem: fileSystem,
331
337
inputFiles: inputFiles,
332
338
diagnosticEngine: diagnosticEngine,
333
339
actualSwiftVersion: try ? toolchain. swiftCompilerVersion ( )
334
340
)
335
341
336
- self . sdkPath = Self . computeSDKPath ( & parsedOptions, compilerMode: compilerMode, toolchain: toolchain, diagnosticsEngine: diagnosticEngine, env: env)
342
+ self . sdkPath = Self . computeSDKPath ( & parsedOptions, compilerMode: compilerMode, toolchain: toolchain, fileSystem : fileSystem , diagnosticsEngine: diagnosticEngine, env: env)
337
343
338
344
self . importedObjCHeader = try Self . computeImportedObjCHeader ( & parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
339
345
self . bridgingPrecompiledHeader = try Self . computeBridgingPrecompiledHeader ( & parsedOptions,
@@ -531,6 +537,7 @@ extension Driver {
531
537
/// - Parameter visitedResponseFiles: Set containing visited response files to detect recursive parsing.
532
538
private static func expandResponseFiles(
533
539
_ args: [ String ] ,
540
+ fileSystem: FileSystem ,
534
541
diagnosticsEngine: DiagnosticsEngine ,
535
542
visitedResponseFiles: inout Set < AbsolutePath >
536
543
) throws -> [ String ] {
@@ -548,9 +555,9 @@ extension Driver {
548
555
visitedResponseFiles. remove ( responseFile)
549
556
}
550
557
551
- let contents = try localFileSystem . readFileContents ( responseFile) . cString
558
+ let contents = try fileSystem . readFileContents ( responseFile) . cString
552
559
let lines = tokenizeResponseFile ( contents)
553
- result. append ( contentsOf: try expandResponseFiles ( lines, diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles) )
560
+ result. append ( contentsOf: try expandResponseFiles ( lines, fileSystem : fileSystem , diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles) )
554
561
} else {
555
562
result. append ( arg)
556
563
}
@@ -562,24 +569,23 @@ extension Driver {
562
569
/// Expand response files in the input arguments and return a new argument list.
563
570
public static func expandResponseFiles(
564
571
_ args: [ String ] ,
572
+ fileSystem: FileSystem ,
565
573
diagnosticsEngine: DiagnosticsEngine
566
574
) throws -> [ String ] {
567
575
var visitedResponseFiles = Set < AbsolutePath > ( )
568
- return try expandResponseFiles ( args, diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles)
576
+ return try expandResponseFiles ( args, fileSystem : fileSystem , diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles)
569
577
}
570
578
}
571
579
572
580
extension Driver {
573
581
/// Determine the driver kind based on the command-line arguments, consuming the arguments
574
582
/// conveying this information.
575
583
public static func determineDriverKind(
576
- args: inout ArraySlice < String > ,
577
- cwd: AbsolutePath ? = localFileSystem. currentWorkingDirectory
584
+ args: inout ArraySlice < String >
578
585
) throws -> DriverKind {
579
586
// Get the basename of the driver executable.
580
587
let execRelPath = args. removeFirst ( )
581
- let execPath = try cwd. map { AbsolutePath ( execRelPath, relativeTo: $0) } ?? AbsolutePath ( validating: execRelPath)
582
- var driverName = execPath. basename
588
+ var driverName = try VirtualPath ( path: execRelPath) . basenameWithoutExt
583
589
584
590
// Determine driver kind based on the first argument.
585
591
let driverModeOption = " --driver-mode= "
@@ -670,7 +676,7 @@ extension Driver {
670
676
forceResponseFiles: forceResponseFiles,
671
677
recordedInputModificationDates: recordedInputModificationDates
672
678
)
673
- try jobExecutor. execute ( env: env)
679
+ try jobExecutor. execute ( env: env, fileSystem : fileSystem )
674
680
}
675
681
676
682
public mutating func createToolExecutionDelegate( ) -> ToolExecutionDelegate {
@@ -694,7 +700,7 @@ extension Driver {
694
700
try ProcessEnv . setVar ( envVar, value: value)
695
701
}
696
702
697
- try job. verifyInputsNotModified ( since: self . recordedInputModificationDates)
703
+ try job. verifyInputsNotModified ( since: self . recordedInputModificationDates, fileSystem : fileSystem )
698
704
699
705
return try exec ( path: arguments [ 0 ] , args: arguments)
700
706
}
@@ -1357,6 +1363,7 @@ extension Driver {
1357
1363
_ parsedOptions: inout ParsedOptions ,
1358
1364
compilerMode: CompilerMode ,
1359
1365
toolchain: Toolchain ,
1366
+ fileSystem: FileSystem ,
1360
1367
diagnosticsEngine: DiagnosticsEngine ,
1361
1368
env: [ String : String ]
1362
1369
) -> String ? {
@@ -1392,14 +1399,14 @@ extension Driver {
1392
1399
// FIXME: TSC should provide a better utility for this.
1393
1400
if let absPath = try ? AbsolutePath ( validating: sdkPath) {
1394
1401
path = absPath
1395
- } else if let cwd = localFileSystem . currentWorkingDirectory {
1402
+ } else if let cwd = fileSystem . currentWorkingDirectory {
1396
1403
path = AbsolutePath ( sdkPath, relativeTo: cwd)
1397
1404
} else {
1398
1405
diagnosticsEngine. emit ( . warning_no_such_sdk( sdkPath) )
1399
1406
return sdkPath
1400
1407
}
1401
1408
1402
- if !localFileSystem . exists ( path) {
1409
+ if !fileSystem . exists ( path) {
1403
1410
diagnosticsEngine. emit ( . warning_no_such_sdk( sdkPath) )
1404
1411
}
1405
1412
// .. else check if SDK is too old (we need target triple to diagnose that).
@@ -1505,11 +1512,12 @@ extension Driver {
1505
1512
static func computeToolchain(
1506
1513
_ explicitTarget: Triple ? ,
1507
1514
diagnosticsEngine: DiagnosticsEngine ,
1508
- env: [ String : String ]
1515
+ env: [ String : String ] ,
1516
+ fileSystem: FileSystem
1509
1517
) throws -> ( Toolchain , Triple ) {
1510
1518
let toolchainType = try explicitTarget? . toolchainType ( diagnosticsEngine) ??
1511
1519
defaultToolchainType
1512
- let toolchain = toolchainType. init ( env: env)
1520
+ let toolchain = toolchainType. init ( env: env, fileSystem : fileSystem )
1513
1521
return ( toolchain, try explicitTarget ?? toolchain. hostTargetTriple ( ) )
1514
1522
}
1515
1523
}
0 commit comments