Skip to content

Commit 643fb73

Browse files
Merge pull request #1572 from cachemeifyoucan/eng/PR-6.0-caching-support
[6.0][Caching] SwiftDriver changes for caching support
2 parents 0c8e041 + 8338576 commit 643fb73

File tree

6 files changed

+79
-35
lines changed

6 files changed

+79
-35
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,14 @@ public struct Driver {
336336
let importedObjCHeader: VirtualPath.Handle?
337337

338338
/// The path to the pch for the imported Objective-C header.
339-
let bridgingPrecompiledHeader: VirtualPath.Handle?
339+
lazy var bridgingPrecompiledHeader: VirtualPath.Handle? = {
340+
let contextHash = try? explicitDependencyBuildPlanner?.getMainModuleContextHash()
341+
return Self.computeBridgingPrecompiledHeader(&parsedOptions,
342+
compilerMode: compilerMode,
343+
importedObjCHeader: importedObjCHeader,
344+
outputFileMap: outputFileMap,
345+
contextHash: contextHash)
346+
}()
340347

341348
/// Path to the dependencies file.
342349
let dependenciesFilePath: VirtualPath.Handle?
@@ -801,10 +808,6 @@ public struct Driver {
801808
recordedInputModificationDates: recordedInputModificationDates)
802809

803810
self.importedObjCHeader = try Self.computeImportedObjCHeader(&parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
804-
self.bridgingPrecompiledHeader = try Self.computeBridgingPrecompiledHeader(&parsedOptions,
805-
compilerMode: compilerMode,
806-
importedObjCHeader: importedObjCHeader,
807-
outputFileMap: outputFileMap)
808811

809812
self.supportedFrontendFlags =
810813
try Self.computeSupportedCompilerArgs(of: self.toolchain,
@@ -829,7 +832,7 @@ public struct Driver {
829832
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used without explicit module build, turn off caching"),
830833
location: nil)
831834
self.enableCaching = false
832-
} else if importedObjCHeader != nil && bridgingPrecompiledHeader == nil {
835+
} else if importedObjCHeader != nil, !parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) {
833836
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used with -disable-bridging-pch, turn off caching"),
834837
location: nil)
835838
self.enableCaching = false
@@ -1779,7 +1782,7 @@ extension Driver {
17791782
/// The swift-driver doesn't have actions, so the logic here takes the jobs and tries
17801783
/// to mimic the actions that would be created by the C++ driver and
17811784
/// prints them in *hopefully* the same order.
1782-
private func printActions(_ jobs: [Job]) {
1785+
private mutating func printActions(_ jobs: [Job]) {
17831786
defer {
17841787
stdoutStream.flush()
17851788
}
@@ -2872,23 +2875,29 @@ extension Driver {
28722875
static func computeBridgingPrecompiledHeader(_ parsedOptions: inout ParsedOptions,
28732876
compilerMode: CompilerMode,
28742877
importedObjCHeader: VirtualPath.Handle?,
2875-
outputFileMap: OutputFileMap?) throws -> VirtualPath.Handle? {
2878+
outputFileMap: OutputFileMap?,
2879+
contextHash: String?) -> VirtualPath.Handle? {
28762880
guard compilerMode.supportsBridgingPCH,
28772881
let input = importedObjCHeader,
28782882
parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) else {
28792883
return nil
28802884
}
28812885

2882-
if let outputPath = try outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
2886+
if let outputPath = try? outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
28832887
return outputPath
28842888
}
28852889

2886-
let inputFile = VirtualPath.lookup(input)
2887-
let pchFileName = inputFile.basenameWithoutExt.appendingFileTypeExtension(.pch)
2890+
let pchFile : String
2891+
let baseName = VirtualPath.lookup(input).basenameWithoutExt
2892+
if let hash = contextHash {
2893+
pchFile = baseName + "-" + hash + ".pch"
2894+
} else {
2895+
pchFile = baseName.appendingFileTypeExtension(.pch)
2896+
}
28882897
if let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
2889-
return try VirtualPath(path: outputDirectory).appending(component: pchFileName).intern()
2898+
return try? VirtualPath(path: outputDirectory).appending(component: pchFile).intern()
28902899
} else {
2891-
return try VirtualPath.createUniqueTemporaryFile(RelativePath(validating: pchFileName)).intern()
2900+
return try? VirtualPath.temporary(RelativePath(validating: pchFile)).intern()
28922901
}
28932902
}
28942903
}

Sources/SwiftDriver/ExplicitModuleBuilds/ExplicitDependencyBuildPlanner.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
452452
commandLine.append(contentsOf: mainModuleDependenciesArgs.commandLine)
453453
}
454454

455+
/// Get the context hash for the main module.
456+
public func getMainModuleContextHash() throws -> String? {
457+
let mainModuleId: ModuleDependencyId = .swift(dependencyGraph.mainModuleName)
458+
let mainModuleDetails = try dependencyGraph.swiftModuleDetails(of: mainModuleId)
459+
return mainModuleDetails.contextHash
460+
}
461+
455462
/// Resolve all module dependencies of the main module and add them to the lists of
456463
/// inputs and command line flags.
457464
public mutating func resolveBridgingHeaderDependencies(inputs: inout [TypedVirtualPath],

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ extension Driver {
101101
jobNeedPathRemap = false
102102
}
103103

104+
// Check if dependency scanner has put the job into direct clang cc1 mode.
105+
// If dependency scanner put us into direct cc1 mode, avoid adding `-Xcc` options, since
106+
// dependency scanner already adds needed flags and -Xcc options known by swift-driver are
107+
// clang driver flags but not it requires cc1 flags.
108+
let directModuleCC1Mode = commandLine.contains(Job.ArgTemplate.flag("-direct-clang-cc1-module-build"))
109+
func appendXccFlag(_ flag: String) {
110+
guard !directModuleCC1Mode else { return }
111+
commandLine.appendFlag(.Xcc)
112+
commandLine.appendFlag(flag)
113+
}
114+
104115
if let variant = parsedOptions.getLastArgument(.targetVariant)?.asSingle {
105116
commandLine.appendFlag(.targetVariant)
106117
commandLine.appendFlag(Triple(variant, normalizing: true).triple)
@@ -125,8 +136,7 @@ extension Driver {
125136
try commandLine.appendLast(.enableExperimentalCxxInterop, from: &parsedOptions)
126137
try commandLine.appendLast(.cxxInteroperabilityMode, from: &parsedOptions)
127138
if let stdlibVariant = parsedOptions.getLastArgument(.experimentalCxxStdlib)?.asSingle {
128-
commandLine.appendFlag("-Xcc")
129-
commandLine.appendFlag("-stdlib=\(stdlibVariant)")
139+
appendXccFlag("-stdlib=\(stdlibVariant)")
130140
}
131141

132142
if isEmbeddedEnabled && parsedOptions.hasArgument(.enableLibraryEvolution) {
@@ -173,8 +183,7 @@ extension Driver {
173183
try commandLine.appendAll(.vfsoverlay, from: &parsedOptions)
174184

175185
if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
176-
commandLine.appendFlag(.Xcc)
177-
commandLine.appendFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
186+
appendXccFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
178187
}
179188

180189
try commandLine.appendLast(.AssertConfig, from: &parsedOptions)
@@ -272,6 +281,11 @@ extension Driver {
272281
if isFrontendArgSupported(.strictConcurrency) {
273282
try commandLine.appendLast(.strictConcurrency, from: &parsedOptions)
274283
}
284+
if kind == .scanDependencies,
285+
isFrontendArgSupported(.experimentalClangImporterDirectCc1Scan) {
286+
try commandLine.appendAll(
287+
.experimentalClangImporterDirectCc1Scan, from: &parsedOptions)
288+
}
275289

276290
// Expand the -experimental-hermetic-seal-at-link flag
277291
if parsedOptions.hasArgument(.experimentalHermeticSealAtLink) {
@@ -332,7 +346,7 @@ extension Driver {
332346
try commandLine.appendLast(.disableSandbox, from: &parsedOptions)
333347
}
334348

335-
if !(isCachingEnabled && useClangIncludeTree), let workingDirectory = workingDirectory {
349+
if !directModuleCC1Mode, let workingDirectory = workingDirectory {
336350
// Add -Xcc -working-directory before any other -Xcc options to ensure it is
337351
// overridden by an explicit -Xcc -working-directory, although having a
338352
// different working directory is probably incorrect.
@@ -402,8 +416,8 @@ extension Driver {
402416
// Pass through any subsystem flags.
403417
try commandLine.appendAll(.Xllvm, from: &parsedOptions)
404418

405-
// If using clang-include-tree, `-Xcc` should only be passed to scanDependencies job.
406-
if (kind == .scanDependencies) || !(isCachingEnabled && useClangIncludeTree) {
419+
// Pass through all -Xcc flags if not under directModuleCC1Mode.
420+
if !directModuleCC1Mode {
407421
try commandLine.appendAll(.Xcc, from: &parsedOptions)
408422
}
409423

0 commit comments

Comments
 (0)