Skip to content

Commit 8b7a7b0

Browse files
[BridgingHeader] Provide a deterministic unique pch file path
Teach swift driver to use a deterministic path for compiled PCH output. Also when dependency scanner is updated to return a module scanning or PCH hash from the scanning result, it will add that to the code path to have an unique PCH file path for the specific compilation.
1 parent 6bf6442 commit 8b7a7b0

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

Sources/SwiftDriver/Driver/Driver.swift

+21-13
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,13 @@ 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+
Self.computeBridgingPrecompiledHeader(&parsedOptions,
341+
compilerMode: compilerMode,
342+
importedObjCHeader: importedObjCHeader,
343+
outputFileMap: outputFileMap,
344+
planner: explicitDependencyBuildPlanner)
345+
}()
340346

341347
/// Path to the dependencies file.
342348
let dependenciesFilePath: VirtualPath.Handle?
@@ -801,10 +807,6 @@ public struct Driver {
801807
recordedInputModificationDates: recordedInputModificationDates)
802808

803809
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)
808810

809811
self.supportedFrontendFlags =
810812
try Self.computeSupportedCompilerArgs(of: self.toolchain,
@@ -829,7 +831,7 @@ public struct Driver {
829831
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used without explicit module build, turn off caching"),
830832
location: nil)
831833
self.enableCaching = false
832-
} else if importedObjCHeader != nil && bridgingPrecompiledHeader == nil {
834+
} else if importedObjCHeader != nil, !parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) {
833835
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used with -disable-bridging-pch, turn off caching"),
834836
location: nil)
835837
self.enableCaching = false
@@ -1779,7 +1781,7 @@ extension Driver {
17791781
/// The swift-driver doesn't have actions, so the logic here takes the jobs and tries
17801782
/// to mimic the actions that would be created by the C++ driver and
17811783
/// prints them in *hopefully* the same order.
1782-
private func printActions(_ jobs: [Job]) {
1784+
private mutating func printActions(_ jobs: [Job]) {
17831785
defer {
17841786
stdoutStream.flush()
17851787
}
@@ -2872,23 +2874,29 @@ extension Driver {
28722874
static func computeBridgingPrecompiledHeader(_ parsedOptions: inout ParsedOptions,
28732875
compilerMode: CompilerMode,
28742876
importedObjCHeader: VirtualPath.Handle?,
2875-
outputFileMap: OutputFileMap?) throws -> VirtualPath.Handle? {
2877+
outputFileMap: OutputFileMap?,
2878+
planner: ExplicitDependencyBuildPlanner?) -> VirtualPath.Handle? {
28762879
guard compilerMode.supportsBridgingPCH,
28772880
let input = importedObjCHeader,
28782881
parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) else {
28792882
return nil
28802883
}
28812884

2882-
if let outputPath = try outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
2885+
if let outputPath = try? outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
28832886
return outputPath
28842887
}
28852888

2886-
let inputFile = VirtualPath.lookup(input)
2887-
let pchFileName = inputFile.basenameWithoutExt.appendingFileTypeExtension(.pch)
2889+
let pchFile : String
2890+
var baseName = VirtualPath.lookup(input).basenameWithoutExt
2891+
if let contextHash = try? planner?.getMainModuleContextHash() {
2892+
pchFile = baseName + "-" + contextHash + ".pch"
2893+
} else {
2894+
pchFile = baseName.appendingFileTypeExtension(.pch)
2895+
}
28882896
if let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
2889-
return try VirtualPath(path: outputDirectory).appending(component: pchFileName).intern()
2897+
return try? VirtualPath(path: outputDirectory).appending(component: pchFile).intern()
28902898
} else {
2891-
return try VirtualPath.createUniqueTemporaryFile(RelativePath(validating: pchFileName)).intern()
2899+
return try? VirtualPath.temporary(RelativePath(validating: pchFile)).intern()
28922900
}
28932901
}
28942902
}

Sources/SwiftDriver/ExplicitModuleBuilds/ExplicitDependencyBuildPlanner.swift

+7
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
425425
commandLine.append(contentsOf: mainModuleDependenciesArgs.commandLine)
426426
}
427427

428+
/// Get the context hash for the main module.
429+
public func getMainModuleContextHash() throws -> String? {
430+
let mainModuleId: ModuleDependencyId = .swift(dependencyGraph.mainModuleName)
431+
let mainModuleDetails = try dependencyGraph.swiftModuleDetails(of: mainModuleId)
432+
return mainModuleDetails.contextHash
433+
}
434+
428435
/// Resolve all module dependencies of the main module and add them to the lists of
429436
/// inputs and command line flags.
430437
public mutating func resolveBridgingHeaderDependencies(inputs: inout [TypedVirtualPath],

Tests/SwiftDriverTests/CachingBuildTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ final class CachingBuildTests: XCTestCase {
324324
let baseName = "testCachingBuildJobs"
325325
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
326326
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink") ||
327-
matchTemporary(outputFilePath, basename: "Bridging-", fileExtension: "pch"))
327+
matchTemporary(outputFilePath, basename: "Bridging", fileExtension: "pch"))
328328
default:
329329
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
330330
}

0 commit comments

Comments
 (0)