Skip to content

[BridgingHeader] Provide a deterministic unique pch file path #1567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,14 @@ public struct Driver {
let importedObjCHeader: VirtualPath.Handle?

/// The path to the pch for the imported Objective-C header.
let bridgingPrecompiledHeader: VirtualPath.Handle?
lazy var bridgingPrecompiledHeader: VirtualPath.Handle? = {
let contextHash = try? explicitDependencyBuildPlanner?.getMainModuleContextHash()
return Self.computeBridgingPrecompiledHeader(&parsedOptions,
compilerMode: compilerMode,
importedObjCHeader: importedObjCHeader,
outputFileMap: outputFileMap,
contextHash: contextHash)
}()

/// Path to the dependencies file.
let dependenciesFilePath: VirtualPath.Handle?
Expand Down Expand Up @@ -801,10 +808,6 @@ public struct Driver {
recordedInputModificationDates: recordedInputModificationDates)

self.importedObjCHeader = try Self.computeImportedObjCHeader(&parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
self.bridgingPrecompiledHeader = try Self.computeBridgingPrecompiledHeader(&parsedOptions,
compilerMode: compilerMode,
importedObjCHeader: importedObjCHeader,
outputFileMap: outputFileMap)

self.supportedFrontendFlags =
try Self.computeSupportedCompilerArgs(of: self.toolchain,
Expand All @@ -829,7 +832,7 @@ public struct Driver {
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used without explicit module build, turn off caching"),
location: nil)
self.enableCaching = false
} else if importedObjCHeader != nil && bridgingPrecompiledHeader == nil {
} else if importedObjCHeader != nil, !parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) {
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used with -disable-bridging-pch, turn off caching"),
location: nil)
self.enableCaching = false
Expand Down Expand Up @@ -1779,7 +1782,7 @@ extension Driver {
/// The swift-driver doesn't have actions, so the logic here takes the jobs and tries
/// to mimic the actions that would be created by the C++ driver and
/// prints them in *hopefully* the same order.
private func printActions(_ jobs: [Job]) {
private mutating func printActions(_ jobs: [Job]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using lazy var is mutating as I just learnt about it too.

defer {
stdoutStream.flush()
}
Expand Down Expand Up @@ -2872,23 +2875,29 @@ extension Driver {
static func computeBridgingPrecompiledHeader(_ parsedOptions: inout ParsedOptions,
compilerMode: CompilerMode,
importedObjCHeader: VirtualPath.Handle?,
outputFileMap: OutputFileMap?) throws -> VirtualPath.Handle? {
outputFileMap: OutputFileMap?,
contextHash: String?) -> VirtualPath.Handle? {
guard compilerMode.supportsBridgingPCH,
let input = importedObjCHeader,
parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) else {
return nil
}

if let outputPath = try outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
if let outputPath = try? outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
return outputPath
}

let inputFile = VirtualPath.lookup(input)
let pchFileName = inputFile.basenameWithoutExt.appendingFileTypeExtension(.pch)
let pchFile : String
let baseName = VirtualPath.lookup(input).basenameWithoutExt
if let hash = contextHash {
pchFile = baseName + "-" + hash + ".pch"
} else {
pchFile = baseName.appendingFileTypeExtension(.pch)
}
if let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
return try VirtualPath(path: outputDirectory).appending(component: pchFileName).intern()
return try? VirtualPath(path: outputDirectory).appending(component: pchFile).intern()
} else {
return try VirtualPath.createUniqueTemporaryFile(RelativePath(validating: pchFileName)).intern()
return try? VirtualPath.temporary(RelativePath(validating: pchFile)).intern()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
commandLine.append(contentsOf: mainModuleDependenciesArgs.commandLine)
}

/// Get the context hash for the main module.
public func getMainModuleContextHash() throws -> String? {
let mainModuleId: ModuleDependencyId = .swift(dependencyGraph.mainModuleName)
let mainModuleDetails = try dependencyGraph.swiftModuleDetails(of: mainModuleId)
return mainModuleDetails.contextHash
}

/// Resolve all module dependencies of the main module and add them to the lists of
/// inputs and command line flags.
public mutating func resolveBridgingHeaderDependencies(inputs: inout [TypedVirtualPath],
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftDriverTests/CachingBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ final class CachingBuildTests: XCTestCase {
let baseName = "testCachingBuildJobs"
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink") ||
matchTemporary(outputFilePath, basename: "Bridging-", fileExtension: "pch"))
matchTemporary(outputFilePath, basename: "Bridging", fileExtension: "pch"))
default:
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
let baseName = "testExplicitModuleBuildJobs"
XCTAssertTrue(matchTemporary(outputFilePath, basename: baseName, fileExtension: "o") ||
matchTemporary(outputFilePath, basename: baseName, fileExtension: "autolink") ||
matchTemporary(outputFilePath, basename: "Bridging-", fileExtension: "pch"))
matchTemporary(outputFilePath, basename: "Bridging", fileExtension: "pch"))
default:
XCTFail("Unexpected module dependency build job output: \(outputFilePath)")
}
Expand Down