Skip to content

Commit 8338576

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. (cherry picked from commit bb3eed3)
1 parent 8c5ac13 commit 8338576

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
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
@@ -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

Lines changed: 1 addition & 1 deletion
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
}

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
388388
let baseName = "testExplicitModuleBuildJobs"
389389
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
390390
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink") ||
391-
matchTemporary(outputFilePath, basename: "Bridging-", fileExtension: "pch"))
391+
matchTemporary(outputFilePath, basename: "Bridging", fileExtension: "pch"))
392392
default:
393393
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
394394
}

0 commit comments

Comments
 (0)