Skip to content

Commit 3d8dfd5

Browse files
Merge pull request #1858 from cachemeifyoucan/eng/PR-147955190
[Caching] Remap block-list files if needed
2 parents da1246a + d09fc23 commit 3d8dfd5

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

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)