Skip to content

Commit d09fc23

Browse files
[Caching] Remap block-list files if needed
Fix few remaining paths that are not re-mapped on the command-line when caching/prefix-mapping is enabled. rdar://147955190
1 parent 0b7129e commit d09fc23

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public struct Driver {
314314
/// Code & data for incremental compilation. Nil if not running in incremental mode.
315315
/// Set during planning because needs the jobs to look at outputs.
316316
@_spi(Testing) public private(set) var incrementalCompilationState: IncrementalCompilationState? = nil
317-
317+
318318
/// The graph of explicit module dependencies of this module, if the driver has planned an explicit module build.
319319
public private(set) var intermoduleDependencyGraph: InterModuleDependencyGraph? = nil
320320

@@ -703,6 +703,14 @@ public struct Driver {
703703
return try toolchain.lookupSwiftScanLib()
704704
}
705705

706+
func findBlocklists() throws -> [AbsolutePath] {
707+
if let mockBlocklistDir = env["_SWIFT_DRIVER_MOCK_BLOCK_LIST_DIR"] {
708+
// Use testing block-list directory.
709+
return try Driver.findBlocklists(RelativeTo: try AbsolutePath(validating: mockBlocklistDir))
710+
}
711+
return try Driver.findBlocklists(RelativeTo: try toolchain.executableDir)
712+
}
713+
706714
@_spi(Testing)
707715
public static func findBlocklists(RelativeTo execDir: AbsolutePath) throws -> [AbsolutePath] {
708716
// Expect to find all blocklists in such dir:
@@ -890,7 +898,7 @@ public struct Driver {
890898
let cwd = fileSystem.currentWorkingDirectory
891899
return try cwd.map{ try AbsolutePath(validating: workingDirectoryArg.asSingle, relativeTo: $0) } ?? AbsolutePath(validating: workingDirectoryArg.asSingle)
892900
}
893-
901+
894902
if let specifiedWorkingDir = self.workingDirectory {
895903
// Apply the working directory to the parsed options if passed explicitly.
896904
try Self.applyWorkingDirectory(specifiedWorkingDir, to: &self.parsedOptions)
@@ -987,8 +995,8 @@ public struct Driver {
987995

988996
self.lto = Self.ltoKind(&parsedOptions, diagnosticsEngine: diagnosticsEngine)
989997
// Figure out the primary outputs from the driver.
990-
(self.compilerOutputType, self.linkerOutputType) =
991-
Self.determinePrimaryOutputs(&parsedOptions, targetTriple: self.frontendTargetInfo.target.triple,
998+
(self.compilerOutputType, self.linkerOutputType) =
999+
Self.determinePrimaryOutputs(&parsedOptions, targetTriple: self.frontendTargetInfo.target.triple,
9921000
driverKind: driverKind, diagnosticsEngine: diagnosticEngine)
9931001

9941002
// Multithreading.
@@ -1980,12 +1988,12 @@ extension Driver {
19801988
guard let buildRecordInfo = self.buildRecordInfo, let incrementalCompilationState = self.incrementalCompilationState else {
19811989
return
19821990
}
1983-
1991+
19841992
let buildRecord = buildRecordInfo.buildRecord(
19851993
jobs, self.incrementalCompilationState?.blockingConcurrentMutationToProtectedState{
19861994
$0.skippedCompilationInputs
19871995
})
1988-
1996+
19891997
do {
19901998
try incrementalCompilationState.writeDependencyGraph(to: buildRecordInfo.dependencyGraphPath, buildRecord)
19911999
} catch {

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ extension Driver {
188188
}
189189
}
190190

191-
try commandLine.appendAll(.I, from: &parsedOptions)
192-
try commandLine.appendAll(.F, .Fsystem, from: &parsedOptions)
193-
try commandLine.appendAll(.vfsoverlay, from: &parsedOptions)
191+
// TODO: Can we drop all search paths for compile jobs for explicit module build?
192+
try addAllArgumentsWithPath(.I, to: &commandLine, remap: jobNeedPathRemap)
193+
try addAllArgumentsWithPath(.F, .Fsystem, to: &commandLine, remap: jobNeedPathRemap)
194+
try addAllArgumentsWithPath(.vfsoverlay, to: &commandLine, remap: jobNeedPathRemap)
194195

195196
if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
196197
appendXccFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
@@ -247,8 +248,8 @@ extension Driver {
247248
commandLine.appendFlag("-sample-profile-use-profi")
248249
}
249250
try commandLine.appendAllExcept(
250-
includeList: [.warningTreating],
251-
excludeList: [],
251+
includeList: [.warningTreating],
252+
excludeList: [],
252253
from: &parsedOptions
253254
)
254255
try commandLine.appendLast(.sanitizeEQ, from: &parsedOptions)
@@ -350,9 +351,9 @@ extension Driver {
350351
}
351352

352353
if isFrontendArgSupported(.blockListFile) {
353-
try Driver.findBlocklists(RelativeTo: try toolchain.executableDir).forEach {
354+
try findBlocklists().forEach {
354355
commandLine.appendFlag(.blockListFile)
355-
commandLine.appendPath($0)
356+
try addPathArgument(VirtualPath.absolute($0), to: &commandLine)
356357
}
357358
}
358359

@@ -1012,15 +1013,20 @@ extension Driver {
10121013
}
10131014

10141015
public mutating func addPathOption(option: Option, path: VirtualPath, to commandLine: inout [Job.ArgTemplate], remap: Bool = true) throws {
1015-
commandLine.appendFlag(option)
1016-
let needRemap = remap && option.attributes.contains(.argumentIsPath) &&
1016+
let needRemap = remap && isCachingEnabled && option.attributes.contains(.argumentIsPath) &&
10171017
!option.attributes.contains(.cacheInvariant)
1018-
try addPathArgument(path, to: &commandLine, remap: needRemap)
1018+
let commandPath = needRemap ? remapPath(path) : path
1019+
if option.kind == .joined {
1020+
commandLine.append(.joinedOptionAndPath(option.spelling, commandPath))
1021+
} else {
1022+
// All other kinds that involves a path can be added as separated args.
1023+
commandLine.appendFlag(option)
1024+
commandLine.appendPath(commandPath)
1025+
}
10191026
}
10201027

10211028
/// Helper function to add last argument with path to command-line.
10221029
public mutating func addLastArgumentWithPath(_ options: Option...,
1023-
from parsedOptions: inout ParsedOptions,
10241030
to commandLine: inout [Job.ArgTemplate],
10251031
remap: Bool = true) throws {
10261032
guard let parsedOption = parsedOptions.last(for: options) else {
@@ -1031,7 +1037,6 @@ extension Driver {
10311037

10321038
/// Helper function to add all arguments with path to command-line.
10331039
public mutating func addAllArgumentsWithPath(_ options: Option...,
1034-
from parsedOptions: inout ParsedOptions,
10351040
to commandLine: inout [Job.ArgTemplate],
10361041
remap: Bool) throws {
10371042
for matching in parsedOptions.arguments(for: options) {

Tests/SwiftDriverTests/CachingBuildTests.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,9 @@ final class CachingBuildTests: XCTestCase {
895895
.appending(component: "Swift")
896896
let casPath = path.appending(component: "cas")
897897
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
898+
let mockBlocklistDir = try testInputsPath.appending(components: "Dummy.xctoolchain", "usr", "bin")
899+
var env = ProcessEnv.vars
900+
env["_SWIFT_DRIVER_MOCK_BLOCK_LIST_DIR"] = mockBlocklistDir.nativePathString(escaped: true)
898901
var driver = try Driver(args: ["swiftc",
899902
"-I", cHeadersPath.nativePathString(escaped: true),
900903
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
@@ -907,6 +910,7 @@ final class CachingBuildTests: XCTestCase {
907910
"-scanner-prefix-map", testInputsPath.description + "=/^src",
908911
"-scanner-prefix-map", path.description + "=/^tmp",
909912
main.nativePathString(escaped: true)] + sdkArgumentsForTesting,
913+
env: env,
910914
interModuleDependencyOracle: dependencyOracle)
911915
guard driver.isFrontendArgSupported(.scannerPrefixMap) else {
912916
throw XCTSkip("frontend doesn't support prefix map")
@@ -929,7 +933,11 @@ final class CachingBuildTests: XCTestCase {
929933
// The only one that is not remapped should be the `-cas-path` that points to
930934
// `casPath`.
931935
XCTAssertFalse(command.contains {
932-
return $0.starts(with: path.description) && $0 != casPath.description
936+
$0.starts(with: path.description) && $0 != casPath.description
937+
})
938+
/// All source location path should be remapped as well.
939+
XCTAssertFalse(try command.contains {
940+
$0.starts(with: try testInputsPath.description)
933941
})
934942
}
935943
}

0 commit comments

Comments
 (0)