Skip to content

[5.5] Add support for -dump-pcm compiler mode #692

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 1 commit into from
Jun 2, 2021
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
13 changes: 9 additions & 4 deletions Sources/SwiftDriver/Driver/CompilerMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

/// Compile a Clang module (.pcm).
case compilePCM

/// Dump information about a precompiled Clang module
case dumpPCM
}

/// Information about batch mode, which is used to determine how to form
Expand All @@ -43,7 +46,7 @@ extension CompilerMode {
/// Whether this compilation mode uses -primary-file to specify its inputs.
public var usesPrimaryFileInputs: Bool {
switch self {
case .immediate, .repl, .singleCompile, .compilePCM:
case .immediate, .repl, .singleCompile, .compilePCM, .dumpPCM:
return false

case .standardCompile, .batchCompile:
Expand All @@ -57,14 +60,14 @@ extension CompilerMode {
case .immediate, .repl, .standardCompile, .batchCompile:
return false

case .singleCompile, .compilePCM:
case .singleCompile, .compilePCM, .dumpPCM:
return true
}
}

public var isStandardCompilationForPlanning: Bool {
switch self {
case .immediate, .repl, .compilePCM:
case .immediate, .repl, .compilePCM, .dumpPCM:
return false
case .batchCompile, .standardCompile, .singleCompile:
return true
Expand All @@ -88,7 +91,7 @@ extension CompilerMode {
// headers.
public var supportsBridgingPCH: Bool {
switch self {
case .batchCompile, .singleCompile, .standardCompile, .compilePCM:
case .batchCompile, .singleCompile, .standardCompile, .compilePCM, .dumpPCM:
return true
case .immediate, .repl:
return false
Expand All @@ -111,6 +114,8 @@ extension CompilerMode: CustomStringConvertible {
return "immediate compilation"
case .compilePCM:
return "compile Clang module (.pcm)"
case .dumpPCM:
return "dump Clang module (.pcm)"
}
}
}
6 changes: 6 additions & 0 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,9 @@ extension Driver {
case .emitPcm:
return .compilePCM

case .dumpPcm:
return .dumpPCM

default:
// Output flag doesn't determine the compiler mode.
break
Expand Down Expand Up @@ -1488,6 +1491,9 @@ extension Driver {
case .emitPcm:
compilerOutputType = .pcm

case .dumpPcm:
compilerOutputType = nil

case .emitImportedModules:
compilerOutputType = .importedModules

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fileprivate extension CompilerMode {
var supportsIncrementalCompilation: Bool {
switch self {
case .standardCompile, .immediate, .repl, .batchCompile: return true
case .singleCompile, .compilePCM: return false
case .singleCompile, .compilePCM, .dumpPCM: return false
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extension Driver {
// Only pass -target to the REPL or immediate modes if it was explicitly
// specified on the command line.
switch compilerMode {
case .standardCompile, .singleCompile, .batchCompile, .compilePCM:
case .standardCompile, .singleCompile, .batchCompile, .compilePCM, .dumpPCM:
commandLine.appendFlag(.target)
commandLine.appendFlag(targetTriple.triple)

Expand Down
31 changes: 30 additions & 1 deletion Sources/SwiftDriver/Jobs/GeneratePCMJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Driver {
/// (https://clang.llvm.org/docs/Modules.html#module-map-language) and the
/// output is a compiled module that also includes the additional information
/// needed by Swift's Clang importer, e.g., the Swift name lookup tables.
mutating func generatePCMJob(input: TypedVirtualPath) throws -> Job {
mutating func generateEmitPCMJob(input: TypedVirtualPath) throws -> Job {
var inputs = [TypedVirtualPath]()
var outputs = [TypedVirtualPath]()

Expand Down Expand Up @@ -63,4 +63,33 @@ extension Driver {
outputs: outputs
)
}

/// Create a job that dumps information about a Clang module
///
/// The input is a Clang Pre-compiled module file (.pcm).
mutating func generateDumpPCMJob(input: TypedVirtualPath) throws -> Job {
var inputs = [TypedVirtualPath]()
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }

commandLine.appendFlag("-frontend")
commandLine.appendFlag(.dumpPcm)

// Input precompiled module.
inputs.append(input)
commandLine.appendPath(input.file)

try addCommonFrontendOptions(
commandLine: &commandLine, inputs: &inputs, bridgingHeaderHandling: .ignored)

return Job(
moduleName: moduleOutputInfo.name,
kind: .dumpPCM,
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
commandLine: commandLine,
displayInputs: [],
inputs: inputs,
primaryInputs: [],
outputs: []
)
}
}
8 changes: 6 additions & 2 deletions Sources/SwiftDriver/Jobs/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public struct Job: Codable, Equatable, Hashable {

/// Generate a compiled Clang module.
case generatePCM = "generate-pcm"
case dumpPCM = "dump-pcm"
case interpret
case repl
case verifyDebugInfo = "verify-debug-info"
Expand Down Expand Up @@ -187,6 +188,9 @@ extension Job : CustomStringConvertible {
case .generatePCM:
return "Compiling Clang module \(moduleName)"

case .dumpPCM:
return "Dump information about Clang module \(displayInputs.first?.file.name ?? "")"

case .interpret:
return "Interpreting \(displayInputs.first?.file.name ?? "")"

Expand Down Expand Up @@ -234,7 +238,7 @@ extension Job.Kind {
public var isSwiftFrontend: Bool {
switch self {
case .backend, .compile, .mergeModule, .emitModule, .generatePCH,
.generatePCM, .interpret, .repl, .printTargetInfo,
.generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo,
.versionRequest, .emitSupportedFeatures, .scanDependencies, .verifyModuleInterface:
return true

Expand All @@ -249,7 +253,7 @@ extension Job.Kind {
case .compile:
return true
case .backend, .mergeModule, .emitModule, .generatePCH,
.generatePCM, .interpret, .repl, .printTargetInfo,
.generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo,
.versionRequest, .autolinkExtract, .generateDSYM,
.help, .link, .verifyDebugInfo, .scanDependencies,
.emitSupportedFeatures, .moduleWrap, .verifyModuleInterface:
Expand Down
12 changes: 11 additions & 1 deletion Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SwiftOptions
public enum PlanningError: Error, DiagnosticData {
case replReceivedInput
case emitPCMWrongInputFiles
case dumpPCMWrongInputFiles

public var description: String {
switch self {
Expand All @@ -24,6 +25,9 @@ public enum PlanningError: Error, DiagnosticData {

case .emitPCMWrongInputFiles:
return "Clang module emission requires exactly one input file (the module map)"

case .dumpPCMWrongInputFiles:
return "Emitting information about Clang module requires exactly one input file (pre-compiled module)"
}
}
}
Expand Down Expand Up @@ -650,7 +654,13 @@ extension Driver {
if inputFiles.count != 1 {
throw PlanningError.emitPCMWrongInputFiles
}
return ([try generatePCMJob(input: inputFiles.first!)], nil)
return ([try generateEmitPCMJob(input: inputFiles.first!)], nil)

case .dumpPCM:
if inputFiles.count != 1 {
throw PlanningError.dumpPCMWrongInputFiles
}
return ([try generateDumpPCMJob(input: inputFiles.first!)], nil)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4056,6 +4056,19 @@ final class SwiftDriverTests: XCTestCase {
}
}

func testPCMDump() throws {
do {
var driver = try Driver(args: ["swiftc", "-dump-pcm", "module.pcm"])
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 1)

XCTAssertEqual(plannedJobs[0].kind, .dumpPCM)
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("module.pcm")))
XCTAssertEqual(plannedJobs[0].outputs.count, 0)
}
}

func testIndexFilePathHandling() throws {
do {
var driver = try Driver(args: ["swiftc", "-index-file", "-index-file-path",
Expand Down