@@ -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
@@ -229,17 +232,19 @@ public struct Driver {
229
232
public init (
230
233
args: [ String ] ,
231
234
env: [ String : String ] = ProcessEnv . vars,
232
- diagnosticsEngine: DiagnosticsEngine = DiagnosticsEngine ( handlers: [ Driver . stderrDiagnosticsHandler] )
235
+ diagnosticsEngine: DiagnosticsEngine = DiagnosticsEngine ( handlers: [ Driver . stderrDiagnosticsHandler] ) ,
236
+ fileSystem: FileSystem = localFileSystem
233
237
) throws {
234
238
self . env = env
239
+ self . fileSystem = fileSystem
235
240
236
241
self . diagnosticEngine = diagnosticsEngine
237
242
238
243
if case . subcommand = try Self . invocationRunMode ( forArgs: args) . mode {
239
244
throw Error . subcommandPassedToDriver
240
245
}
241
246
242
- var args = try Self . expandResponseFiles ( args, diagnosticsEngine: self . diagnosticEngine) [ ... ]
247
+ var args = try Self . expandResponseFiles ( args, fileSystem : fileSystem , diagnosticsEngine: self . diagnosticEngine) [ ... ]
243
248
244
249
self . driverKind = try Self . determineDriverKind ( args: & args)
245
250
self . optionTable = OptionTable ( )
@@ -249,7 +254,7 @@ public struct Driver {
249
254
. map {
250
255
Triple ( $0, normalizing: true )
251
256
}
252
- ( self . toolchain, self . targetTriple) = try Self . computeToolchain ( explicitTarget, diagnosticsEngine: diagnosticEngine, env: env)
257
+ ( self . toolchain, self . targetTriple) = try Self . computeToolchain ( explicitTarget, diagnosticsEngine: diagnosticEngine, env: env, fileSystem : fileSystem )
253
258
self . targetVariantTriple = self . parsedOptions. getLastArgument ( . targetVariant) . map { Triple ( $0. asSingle, normalizing: true ) }
254
259
255
260
// Find the Swift compiler executable.
@@ -269,7 +274,7 @@ public struct Driver {
269
274
270
275
// Compute the working directory.
271
276
workingDirectory = try parsedOptions. getLastArgument ( . workingDirectory) . map { workingDirectoryArg in
272
- let cwd = localFileSystem . currentWorkingDirectory
277
+ let cwd = fileSystem . currentWorkingDirectory
273
278
return try cwd. map { AbsolutePath ( workingDirectoryArg. asSingle, relativeTo: $0) } ?? AbsolutePath ( validating: workingDirectoryArg. asSingle)
274
279
}
275
280
@@ -283,7 +288,7 @@ public struct Driver {
283
288
self . inputFiles = inputFiles
284
289
self . recordedInputModificationDates = . init( uniqueKeysWithValues:
285
290
Set ( inputFiles) . compactMap {
286
- guard let modTime = try ? localFileSystem
291
+ guard let modTime = try ? fileSystem
287
292
. getFileInfo ( $0. file) . modTime else { return nil }
288
293
return ( $0, modTime)
289
294
} )
@@ -292,7 +297,7 @@ public struct Driver {
292
297
// Initialize an empty output file map, which will be populated when we start creating jobs.
293
298
if let outputFileMapArg = parsedOptions. getLastArgument ( . outputFileMap) ? . asSingle {
294
299
let path = try VirtualPath ( path: outputFileMapArg)
295
- outputFileMap = try . load( file: path, diagnosticEngine: diagnosticEngine)
300
+ outputFileMap = try . load( fileSystem : fileSystem , file: path, diagnosticEngine: diagnosticEngine)
296
301
} else {
297
302
outputFileMap = nil
298
303
}
@@ -330,13 +335,14 @@ public struct Driver {
330
335
compilerMode: compilerMode,
331
336
outputFileMap: self . outputFileMap,
332
337
compilerOutputType: self . compilerOutputType,
333
- moduleOutput: self . moduleOutput,
338
+ moduleOutput: self . moduleOutput,
339
+ fileSystem: fileSystem,
334
340
inputFiles: inputFiles,
335
341
diagnosticEngine: diagnosticEngine,
336
342
actualSwiftVersion: try ? toolchain. swiftCompilerVersion ( )
337
343
)
338
344
339
- self . sdkPath = Self . computeSDKPath ( & parsedOptions, compilerMode: compilerMode, toolchain: toolchain, diagnosticsEngine: diagnosticEngine, env: env)
345
+ self . sdkPath = Self . computeSDKPath ( & parsedOptions, compilerMode: compilerMode, toolchain: toolchain, fileSystem : fileSystem , diagnosticsEngine: diagnosticEngine, env: env)
340
346
341
347
self . importedObjCHeader = try Self . computeImportedObjCHeader ( & parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
342
348
self . bridgingPrecompiledHeader = try Self . computeBridgingPrecompiledHeader ( & parsedOptions,
@@ -534,6 +540,7 @@ extension Driver {
534
540
/// - Parameter visitedResponseFiles: Set containing visited response files to detect recursive parsing.
535
541
private static func expandResponseFiles(
536
542
_ args: [ String ] ,
543
+ fileSystem: FileSystem ,
537
544
diagnosticsEngine: DiagnosticsEngine ,
538
545
visitedResponseFiles: inout Set < AbsolutePath >
539
546
) throws -> [ String ] {
@@ -551,9 +558,9 @@ extension Driver {
551
558
visitedResponseFiles. remove ( responseFile)
552
559
}
553
560
554
- let contents = try localFileSystem . readFileContents ( responseFile) . cString
561
+ let contents = try fileSystem . readFileContents ( responseFile) . cString
555
562
let lines = tokenizeResponseFile ( contents)
556
- result. append ( contentsOf: try expandResponseFiles ( lines, diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles) )
563
+ result. append ( contentsOf: try expandResponseFiles ( lines, fileSystem : fileSystem , diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles) )
557
564
} else {
558
565
result. append ( arg)
559
566
}
@@ -565,24 +572,23 @@ extension Driver {
565
572
/// Expand response files in the input arguments and return a new argument list.
566
573
public static func expandResponseFiles(
567
574
_ args: [ String ] ,
575
+ fileSystem: FileSystem ,
568
576
diagnosticsEngine: DiagnosticsEngine
569
577
) throws -> [ String ] {
570
578
var visitedResponseFiles = Set < AbsolutePath > ( )
571
- return try expandResponseFiles ( args, diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles)
579
+ return try expandResponseFiles ( args, fileSystem : fileSystem , diagnosticsEngine: diagnosticsEngine, visitedResponseFiles: & visitedResponseFiles)
572
580
}
573
581
}
574
582
575
583
extension Driver {
576
584
/// Determine the driver kind based on the command-line arguments, consuming the arguments
577
585
/// conveying this information.
578
586
public static func determineDriverKind(
579
- args: inout ArraySlice < String > ,
580
- cwd: AbsolutePath ? = localFileSystem. currentWorkingDirectory
587
+ args: inout ArraySlice < String >
581
588
) throws -> DriverKind {
582
589
// Get the basename of the driver executable.
583
590
let execRelPath = args. removeFirst ( )
584
- let execPath = try cwd. map { AbsolutePath ( execRelPath, relativeTo: $0) } ?? AbsolutePath ( validating: execRelPath)
585
- var driverName = execPath. basename
591
+ var driverName = try VirtualPath ( path: execRelPath) . basenameWithoutExt
586
592
587
593
// Determine driver kind based on the first argument.
588
594
let driverModeOption = " --driver-mode= "
@@ -680,7 +686,7 @@ extension Driver {
680
686
forceResponseFiles: forceResponseFiles,
681
687
recordedInputModificationDates: recordedInputModificationDates
682
688
)
683
- try jobExecutor. execute ( env: env)
689
+ try jobExecutor. execute ( env: env, fileSystem : fileSystem )
684
690
}
685
691
686
692
public mutating func createToolExecutionDelegate( ) -> ToolExecutionDelegate {
@@ -704,7 +710,7 @@ extension Driver {
704
710
try ProcessEnv . setVar ( envVar, value: value)
705
711
}
706
712
707
- try job. verifyInputsNotModified ( since: self . recordedInputModificationDates)
713
+ try job. verifyInputsNotModified ( since: self . recordedInputModificationDates, fileSystem : fileSystem )
708
714
709
715
return try exec ( path: arguments [ 0 ] , args: arguments)
710
716
}
@@ -988,6 +994,9 @@ extension Driver {
988
994
case . interpret:
989
995
compilerOutputType = nil
990
996
997
+ case . scanDependencies:
998
+ compilerOutputType = . jsonDependencies
999
+
991
1000
default :
992
1001
fatalError ( " unhandled output mode option \( outputOption) " )
993
1002
}
@@ -1401,6 +1410,7 @@ extension Driver {
1401
1410
_ parsedOptions: inout ParsedOptions ,
1402
1411
compilerMode: CompilerMode ,
1403
1412
toolchain: Toolchain ,
1413
+ fileSystem: FileSystem ,
1404
1414
diagnosticsEngine: DiagnosticsEngine ,
1405
1415
env: [ String : String ]
1406
1416
) -> String ? {
@@ -1436,14 +1446,14 @@ extension Driver {
1436
1446
// FIXME: TSC should provide a better utility for this.
1437
1447
if let absPath = try ? AbsolutePath ( validating: sdkPath) {
1438
1448
path = absPath
1439
- } else if let cwd = localFileSystem . currentWorkingDirectory {
1449
+ } else if let cwd = fileSystem . currentWorkingDirectory {
1440
1450
path = AbsolutePath ( sdkPath, relativeTo: cwd)
1441
1451
} else {
1442
1452
diagnosticsEngine. emit ( . warning_no_such_sdk( sdkPath) )
1443
1453
return sdkPath
1444
1454
}
1445
1455
1446
- if !localFileSystem . exists ( path) {
1456
+ if !fileSystem . exists ( path) {
1447
1457
diagnosticsEngine. emit ( . warning_no_such_sdk( sdkPath) )
1448
1458
}
1449
1459
// .. else check if SDK is too old (we need target triple to diagnose that).
@@ -1549,11 +1559,12 @@ extension Driver {
1549
1559
static func computeToolchain(
1550
1560
_ explicitTarget: Triple ? ,
1551
1561
diagnosticsEngine: DiagnosticsEngine ,
1552
- env: [ String : String ]
1562
+ env: [ String : String ] ,
1563
+ fileSystem: FileSystem
1553
1564
) throws -> ( Toolchain , Triple ) {
1554
1565
let toolchainType = try explicitTarget? . toolchainType ( diagnosticsEngine) ??
1555
1566
defaultToolchainType
1556
- let toolchain = toolchainType. init ( env: env)
1567
+ let toolchain = toolchainType. init ( env: env, fileSystem : fileSystem )
1557
1568
return ( toolchain, try explicitTarget ?? toolchain. hostTargetTriple ( ) )
1558
1569
}
1559
1570
}
0 commit comments