Skip to content

Commit 02dd592

Browse files
authored
Merge pull request #691 from artemcm/DumpPCM
Add support for `-dump-pcm` compiler mode
2 parents 9efdc63 + 31b3fc3 commit 02dd592

File tree

8 files changed

+77
-10
lines changed

8 files changed

+77
-10
lines changed

Sources/SwiftDriver/Driver/CompilerMode.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
/// Compile a Clang module (.pcm).
3131
case compilePCM
32+
33+
/// Dump information about a precompiled Clang module
34+
case dumpPCM
3235
}
3336

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

4952
case .standardCompile, .batchCompile:
@@ -57,14 +60,14 @@ extension CompilerMode {
5760
case .immediate, .repl, .standardCompile, .batchCompile:
5861
return false
5962

60-
case .singleCompile, .compilePCM:
63+
case .singleCompile, .compilePCM, .dumpPCM:
6164
return true
6265
}
6366
}
6467

6568
public var isStandardCompilationForPlanning: Bool {
6669
switch self {
67-
case .immediate, .repl, .compilePCM:
70+
case .immediate, .repl, .compilePCM, .dumpPCM:
6871
return false
6972
case .batchCompile, .standardCompile, .singleCompile:
7073
return true
@@ -88,7 +91,7 @@ extension CompilerMode {
8891
// headers.
8992
public var supportsBridgingPCH: Bool {
9093
switch self {
91-
case .batchCompile, .singleCompile, .standardCompile, .compilePCM:
94+
case .batchCompile, .singleCompile, .standardCompile, .compilePCM, .dumpPCM:
9295
return true
9396
case .immediate, .repl:
9497
return false
@@ -111,6 +114,8 @@ extension CompilerMode: CustomStringConvertible {
111114
return "immediate compilation"
112115
case .compilePCM:
113116
return "compile Clang module (.pcm)"
117+
case .dumpPCM:
118+
return "dump Clang module (.pcm)"
114119
}
115120
}
116121
}

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,9 @@ extension Driver {
13091309
case .emitPcm:
13101310
return .compilePCM
13111311

1312+
case .dumpPcm:
1313+
return .dumpPCM
1314+
13121315
default:
13131316
// Output flag doesn't determine the compiler mode.
13141317
break
@@ -1501,6 +1504,9 @@ extension Driver {
15011504
case .emitPcm:
15021505
compilerOutputType = .pcm
15031506

1507+
case .dumpPcm:
1508+
compilerOutputType = nil
1509+
15041510
case .emitImportedModules:
15051511
compilerOutputType = .importedModules
15061512

Sources/SwiftDriver/IncrementalCompilation/IncrementalCompilationState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fileprivate extension CompilerMode {
180180
var supportsIncrementalCompilation: Bool {
181181
switch self {
182182
case .standardCompile, .immediate, .repl, .batchCompile: return true
183-
case .singleCompile, .compilePCM: return false
183+
case .singleCompile, .compilePCM, .dumpPCM: return false
184184
}
185185
}
186186
}

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension Driver {
5555
// Only pass -target to the REPL or immediate modes if it was explicitly
5656
// specified on the command line.
5757
switch compilerMode {
58-
case .standardCompile, .singleCompile, .batchCompile, .compilePCM:
58+
case .standardCompile, .singleCompile, .batchCompile, .compilePCM, .dumpPCM:
5959
commandLine.appendFlag(.target)
6060
commandLine.appendFlag(targetTriple.triple)
6161

Sources/SwiftDriver/Jobs/GeneratePCMJob.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension Driver {
1818
/// (https://clang.llvm.org/docs/Modules.html#module-map-language) and the
1919
/// output is a compiled module that also includes the additional information
2020
/// needed by Swift's Clang importer, e.g., the Swift name lookup tables.
21-
mutating func generatePCMJob(input: TypedVirtualPath) throws -> Job {
21+
mutating func generateEmitPCMJob(input: TypedVirtualPath) throws -> Job {
2222
var inputs = [TypedVirtualPath]()
2323
var outputs = [TypedVirtualPath]()
2424

@@ -63,4 +63,33 @@ extension Driver {
6363
outputs: outputs
6464
)
6565
}
66+
67+
/// Create a job that dumps information about a Clang module
68+
///
69+
/// The input is a Clang Pre-compiled module file (.pcm).
70+
mutating func generateDumpPCMJob(input: TypedVirtualPath) throws -> Job {
71+
var inputs = [TypedVirtualPath]()
72+
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
73+
74+
commandLine.appendFlag("-frontend")
75+
commandLine.appendFlag(.dumpPcm)
76+
77+
// Input precompiled module.
78+
inputs.append(input)
79+
commandLine.appendPath(input.file)
80+
81+
try addCommonFrontendOptions(
82+
commandLine: &commandLine, inputs: &inputs, bridgingHeaderHandling: .ignored)
83+
84+
return Job(
85+
moduleName: moduleOutputInfo.name,
86+
kind: .dumpPCM,
87+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
88+
commandLine: commandLine,
89+
displayInputs: [],
90+
inputs: inputs,
91+
primaryInputs: [],
92+
outputs: []
93+
)
94+
}
6695
}

Sources/SwiftDriver/Jobs/Job.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct Job: Codable, Equatable, Hashable {
2727

2828
/// Generate a compiled Clang module.
2929
case generatePCM = "generate-pcm"
30+
case dumpPCM = "dump-pcm"
3031
case interpret
3132
case repl
3233
case verifyDebugInfo = "verify-debug-info"
@@ -187,6 +188,9 @@ extension Job : CustomStringConvertible {
187188
case .generatePCM:
188189
return "Compiling Clang module \(moduleName)"
189190

191+
case .dumpPCM:
192+
return "Dump information about Clang module \(displayInputs.first?.file.name ?? "")"
193+
190194
case .interpret:
191195
return "Interpreting \(displayInputs.first?.file.name ?? "")"
192196

@@ -234,7 +238,7 @@ extension Job.Kind {
234238
public var isSwiftFrontend: Bool {
235239
switch self {
236240
case .backend, .compile, .mergeModule, .emitModule, .generatePCH,
237-
.generatePCM, .interpret, .repl, .printTargetInfo,
241+
.generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo,
238242
.versionRequest, .emitSupportedFeatures, .scanDependencies, .verifyModuleInterface:
239243
return true
240244

@@ -249,7 +253,7 @@ extension Job.Kind {
249253
case .compile:
250254
return true
251255
case .backend, .mergeModule, .emitModule, .generatePCH,
252-
.generatePCM, .interpret, .repl, .printTargetInfo,
256+
.generatePCM, .dumpPCM, .interpret, .repl, .printTargetInfo,
253257
.versionRequest, .autolinkExtract, .generateDSYM,
254258
.help, .link, .verifyDebugInfo, .scanDependencies,
255259
.emitSupportedFeatures, .moduleWrap, .verifyModuleInterface:

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import SwiftOptions
1616
public enum PlanningError: Error, DiagnosticData {
1717
case replReceivedInput
1818
case emitPCMWrongInputFiles
19+
case dumpPCMWrongInputFiles
1920

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

2526
case .emitPCMWrongInputFiles:
2627
return "Clang module emission requires exactly one input file (the module map)"
28+
29+
case .dumpPCMWrongInputFiles:
30+
return "Emitting information about Clang module requires exactly one input file (pre-compiled module)"
2731
}
2832
}
2933
}
@@ -650,7 +654,13 @@ extension Driver {
650654
if inputFiles.count != 1 {
651655
throw PlanningError.emitPCMWrongInputFiles
652656
}
653-
return ([try generatePCMJob(input: inputFiles.first!)], nil)
657+
return ([try generateEmitPCMJob(input: inputFiles.first!)], nil)
658+
659+
case .dumpPCM:
660+
if inputFiles.count != 1 {
661+
throw PlanningError.dumpPCMWrongInputFiles
662+
}
663+
return ([try generateDumpPCMJob(input: inputFiles.first!)], nil)
654664
}
655665
}
656666
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4139,6 +4139,19 @@ final class SwiftDriverTests: XCTestCase {
41394139
}
41404140
}
41414141

4142+
func testPCMDump() throws {
4143+
do {
4144+
var driver = try Driver(args: ["swiftc", "-dump-pcm", "module.pcm"])
4145+
let plannedJobs = try driver.planBuild()
4146+
XCTAssertEqual(plannedJobs.count, 1)
4147+
4148+
XCTAssertEqual(plannedJobs[0].kind, .dumpPCM)
4149+
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
4150+
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("module.pcm")))
4151+
XCTAssertEqual(plannedJobs[0].outputs.count, 0)
4152+
}
4153+
}
4154+
41424155
func testIndexFilePathHandling() throws {
41434156
do {
41444157
var driver = try Driver(args: ["swiftc", "-index-file", "-index-file-path",

0 commit comments

Comments
 (0)