Skip to content

Commit 5981c7e

Browse files
authored
Merge pull request #1046 from apple/SeparateEmitModuleDiagnostics
2 parents a2bdd28 + 08318f7 commit 5981c7e

File tree

6 files changed

+84
-11
lines changed

6 files changed

+84
-11
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ public struct Driver {
278278
/// Path to the serialized diagnostics file.
279279
let serializedDiagnosticsFilePath: VirtualPath.Handle?
280280

281+
/// Path to the serialized diagnostics file of the emit-module task.
282+
let emitModuleSerializedDiagnosticsFilePath: VirtualPath.Handle?
283+
281284
/// Path to the Objective-C generated header.
282285
let objcGeneratedHeaderPath: VirtualPath.Handle?
283286

@@ -701,6 +704,14 @@ public struct Driver {
701704
emitModuleSeparately: emitModuleSeparately,
702705
outputFileMap: self.outputFileMap,
703706
moduleName: moduleOutputInfo.name)
707+
self.emitModuleSerializedDiagnosticsFilePath = try Self.computeSupplementaryOutputPath(
708+
&parsedOptions, type: .emitModuleDiagnostics, isOutputOptions: [.serializeDiagnostics],
709+
outputPath: .emitModuleSerializeDiagnosticsPath,
710+
compilerOutputType: compilerOutputType,
711+
compilerMode: compilerMode,
712+
emitModuleSeparately: emitModuleSeparately,
713+
outputFileMap: self.outputFileMap,
714+
moduleName: moduleOutputInfo.name)
704715
// FIXME: -fixits-output-path
705716
self.objcGeneratedHeaderPath = try Self.computeSupplementaryOutputPath(
706717
&parsedOptions, type: .objcHeader, isOutputOptions: [.emitObjcHeader],
@@ -2985,6 +2996,14 @@ extension Driver {
29852996
return singleOutputPath
29862997
}
29872998

2999+
// Emit-module serialized diagnostics are always specified as a single-output
3000+
// file
3001+
if type == .emitModuleDiagnostics,
3002+
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
3003+
outputType: type) {
3004+
return singleOutputPath
3005+
}
3006+
29883007
// If there is an output argument, derive the name from there.
29893008
if let outputPathArg = parsedOptions.getLastArgument(.o) {
29903009
let path = try VirtualPath(path: outputPathArg.asSingle)

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ extension Driver {
9090
case .swiftModule:
9191
return compilerMode.isSingleCompilation && moduleOutputInfo.output?.isTopLevel ?? false
9292
case .swift, .image, .dSYM, .dependencies, .autolink, .swiftDocumentation, .swiftInterface,
93-
.privateSwiftInterface, .swiftSourceInfoFile, .diagnostics, .objcHeader, .swiftDeps,
94-
.remap, .tbd, .moduleTrace, .yamlOptimizationRecord, .bitstreamOptimizationRecord, .pcm,
95-
.pch, .clangModuleMap, .jsonCompilerFeatures, .jsonTargetInfo, .jsonSwiftArtifacts,
96-
.indexUnitOutputPath, .modDepCache, .jsonAPIBaseline, .jsonABIBaseline, nil:
93+
.privateSwiftInterface, .swiftSourceInfoFile, .diagnostics, .emitModuleDiagnostics,
94+
.objcHeader, .swiftDeps, .remap, .tbd, .moduleTrace, .yamlOptimizationRecord,
95+
.bitstreamOptimizationRecord, .pcm, .pch, .clangModuleMap, .jsonCompilerFeatures,
96+
.jsonTargetInfo, .jsonSwiftArtifacts, .indexUnitOutputPath, .modDepCache,
97+
.jsonAPIBaseline, .jsonABIBaseline, nil:
9798
return false
9899
}
99100
}
@@ -446,7 +447,7 @@ extension FileType {
446447
return .emitSupportedFeatures
447448

448449
case .swift, .dSYM, .autolink, .dependencies, .swiftDocumentation, .pcm,
449-
.diagnostics, .objcHeader, .image, .swiftDeps, .moduleTrace, .tbd,
450+
.diagnostics, .emitModuleDiagnostics, .objcHeader, .image, .swiftDeps, .moduleTrace, .tbd,
450451
.yamlOptimizationRecord, .bitstreamOptimizationRecord, .swiftInterface,
451452
.privateSwiftInterface, .swiftSourceInfoFile, .clangModuleMap, .jsonSwiftArtifacts,
452453
.indexUnitOutputPath, .modDepCache, .jsonAPIBaseline, .jsonABIBaseline:

Sources/SwiftDriver/Jobs/EmitModuleJob.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ extension Driver {
3939
return
4040
}
4141

42+
addSupplementalOutput(path: emitModuleSerializedDiagnosticsFilePath, flag: "-serialize-diagnostics-path", type: .emitModuleDiagnostics)
43+
4244
// Skip files created by other jobs when emitting a module and building at the same time
4345
if emitModuleSeparately && compilerOutputType != .swiftModule {
4446
return
4547
}
4648

4749
// Add outputs that can't be merged
48-
addSupplementalOutput(path: serializedDiagnosticsFilePath, flag: "-serialize-diagnostics-path", type: .diagnostics)
49-
5050
// Workaround for rdar://85253406
5151
// Ensure that the separate emit-module job does not emit `.d.` outputs.
5252
// If we have both individual source files and the emit-module file emit .d files, we

Sources/SwiftDriver/Utilities/FileType.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public enum FileType: String, Hashable, CaseIterable, Codable {
7575
/// Clang/Swift serialized diagnostics
7676
case diagnostics = "dia"
7777

78+
/// Serialized diagnostics produced by module-generation
79+
case emitModuleDiagnostics = "emit-module.dia"
80+
7881
/// Objective-C header
7982
case objcHeader = "h"
8083

@@ -214,6 +217,9 @@ extension FileType: CustomStringConvertible {
214217
case .diagnostics:
215218
return "diagnostics"
216219

220+
case .emitModuleDiagnostics:
221+
return "emit-module-diagnostics"
222+
217223
case .jsonAPIBaseline:
218224
return "api-baseline-json"
219225

@@ -232,7 +238,7 @@ extension FileType {
232238
return true
233239
case .object, .pch, .ast, .llvmIR, .llvmBitcode, .assembly, .swiftModule,
234240
.importedModules, .indexData, .remap, .dSYM, .autolink, .dependencies,
235-
.swiftDocumentation, .pcm, .diagnostics, .objcHeader, .image,
241+
.swiftDocumentation, .pcm, .diagnostics, .emitModuleDiagnostics, .objcHeader, .image,
236242
.swiftDeps, .moduleTrace, .tbd, .yamlOptimizationRecord, .bitstreamOptimizationRecord,
237243
.swiftInterface, .privateSwiftInterface, .swiftSourceInfoFile, .jsonDependencies,
238244
.clangModuleMap, .jsonTargetInfo, .jsonCompilerFeatures, .jsonSwiftArtifacts,
@@ -329,6 +335,8 @@ extension FileType {
329335
return "bitstream-opt-record"
330336
case .diagnostics:
331337
return "diagnostics"
338+
case .emitModuleDiagnostics:
339+
return "emit-module-diagnostics"
332340
case .indexUnitOutputPath:
333341
return "index-unit-output-path"
334342
case .jsonAPIBaseline:
@@ -351,7 +359,7 @@ extension FileType {
351359
case .image, .object, .dSYM, .pch, .sib, .raw_sib, .swiftModule,
352360
.swiftDocumentation, .swiftSourceInfoFile, .llvmBitcode, .diagnostics,
353361
.pcm, .swiftDeps, .remap, .indexData, .bitstreamOptimizationRecord,
354-
.indexUnitOutputPath, .modDepCache:
362+
.indexUnitOutputPath, .modDepCache, .emitModuleDiagnostics:
355363
return false
356364
}
357365
}
@@ -364,7 +372,7 @@ extension FileType {
364372
return true
365373
case .swift, .sil, .sib, .ast, .image, .dSYM, .dependencies, .autolink,
366374
.swiftModule, .swiftDocumentation, .swiftInterface, .privateSwiftInterface,
367-
.swiftSourceInfoFile, .raw_sil, .raw_sib, .diagnostics, .objcHeader, .swiftDeps, .remap,
375+
.swiftSourceInfoFile, .raw_sil, .raw_sib, .diagnostics, .emitModuleDiagnostics, .objcHeader, .swiftDeps, .remap,
368376
.importedModules, .tbd, .moduleTrace, .indexData, .yamlOptimizationRecord, .modDepCache,
369377
.bitstreamOptimizationRecord, .pcm, .pch, .jsonDependencies, .clangModuleMap,
370378
.jsonCompilerFeatures, .jsonTargetInfo, .jsonSwiftArtifacts, .indexUnitOutputPath, .jsonAPIBaseline,

Sources/SwiftOptions/ExtraOptions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extension Option {
1919
public static let emitModuleSeparately: Option = Option("-experimental-emit-module-separately", .flag, attributes: [.helpHidden], helpText: "Emit module files as a distinct job")
2020
public static let emitModuleSeparatelyWMO: Option = Option("-emit-module-separately-wmo", .flag, attributes: [.helpHidden], helpText: "Emit module files as a distinct job in wmo builds")
2121
public static let noEmitModuleSeparatelyWMO: Option = Option("-no-emit-module-separately-wmo", .flag, attributes: [.helpHidden], helpText: "Emit module files as a distinct job in wmo builds")
22+
public static let emitModuleSerializeDiagnosticsPath: Option = Option("-emit-module-serialize-diagnostics-path", .separate, attributes: [.argumentIsPath, .supplementaryOutput], metaVar: "<path>", helpText: "Emit a serialized diagnostics file for the emit-module task to <path>")
2223
public static let useFrontendParseableOutput: Option = Option("-use-frontend-parseable-output", .flag, attributes: [.helpHidden], helpText: "Emit parseable-output from swift-frontend jobs instead of from the driver")
2324

2425
// API digester operations

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,50 @@ final class SwiftDriverTests: XCTestCase {
684684
XCTAssertEqual(try Driver(args: ["swiftc", "foo.swift", "-emit-library", "-o", "libBaz.dylib"]).moduleOutputInfo.name, "Baz")
685685
}
686686

687+
func testEmitModuleSeparatelyDiagnosticPath() throws {
688+
try withTemporaryFile { fileMapFile in
689+
let outputMapContents = """
690+
{
691+
"": {
692+
"diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.dia",
693+
"emit-module-diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia"
694+
},
695+
"foo.swift": {
696+
"diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.dia"
697+
}
698+
}
699+
"""
700+
try localFileSystem.writeFileContents(fileMapFile.path) { $0 <<< outputMapContents }
701+
702+
// Plain (batch/single-file) compile
703+
do {
704+
var driver = try Driver(args: ["swiftc", "foo.swift", "-emit-module", "-output-file-map", fileMapFile.path.pathString,
705+
"-emit-library", "-module-name", "Test", "-serialize-diagnostics"])
706+
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
707+
XCTAssertEqual(plannedJobs.count, 3)
708+
XCTAssertTrue(plannedJobs[0].kind == .emitModule)
709+
XCTAssertTrue(plannedJobs[1].kind == .compile)
710+
XCTAssertTrue(plannedJobs[2].kind == .link)
711+
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia")))]))
712+
XCTAssertTrue(plannedJobs[1].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.dia")))]))
713+
}
714+
715+
// WMO
716+
do {
717+
var driver = try Driver(args: ["swiftc", "foo.swift", "-whole-module-optimization", "-emit-module",
718+
"-output-file-map", fileMapFile.path.pathString, "-disable-cmo",
719+
"-emit-library", "-module-name", "Test", "-serialize-diagnostics"])
720+
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
721+
XCTAssertEqual(plannedJobs.count, 3)
722+
XCTAssertTrue(plannedJobs[0].kind == .compile)
723+
XCTAssertTrue(plannedJobs[1].kind == .emitModule)
724+
XCTAssertTrue(plannedJobs[2].kind == .link)
725+
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.dia")))]))
726+
XCTAssertTrue(plannedJobs[1].commandLine.contains(subsequence: ["-serialize-diagnostics-path", .path(.absolute(.init("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia")))]))
727+
}
728+
}
729+
}
730+
687731
func testOutputFileMapLoading() throws {
688732
let contents = """
689733
{
@@ -928,7 +972,7 @@ final class SwiftDriverTests: XCTestCase {
928972
$0 <<< """
929973
{
930974
"": {
931-
"diagnostics": "/build/Foo-test.dia"
975+
"emit-module-diagnostics": "/build/Foo-test.dia"
932976
}
933977
}
934978
"""

0 commit comments

Comments
 (0)