Skip to content

Commit 5d61c91

Browse files
authored
Merge pull request #256 from artemcm/PrintOutputMap
Add support for printing out the output file map
2 parents 2cb6dd3 + 468fbce commit 5d61c91

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ public struct Driver {
342342
self.outputFileMap = outputFileMap
343343
}
344344

345+
// If requested, print the output file map
346+
if parsedOptions.contains(.driverPrintOutputFileMap) {
347+
if let outputFileMap = self.outputFileMap {
348+
stderrStream <<< outputFileMap.description
349+
stderrStream.flush()
350+
} else {
351+
diagnosticsEngine.emit(.error_no_output_file_map_specified)
352+
}
353+
}
354+
345355
self.fileListThreshold = try Self.computeFileListThreshold(&self.parsedOptions, diagnosticsEngine: diagnosticsEngine)
346356
self.shouldUseInputFileList = inputFiles.count > fileListThreshold
347357
if shouldUseInputFileList {
@@ -818,6 +828,10 @@ extension Diagnostic.Message {
818828
static var warning_cannot_multithread_batch_mode: Diagnostic.Message {
819829
.warning("ignoring -num-threads argument; cannot multithread batch mode")
820830
}
831+
832+
static var error_no_output_file_map_specified: Diagnostic.Message {
833+
.error("no output file map specified")
834+
}
821835
}
822836

823837
extension Driver {

Sources/SwiftDriver/Driver/OutputFileMap.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ public struct OutputFileMap: Hashable, Codable {
106106
let contents = try encoder.encode(OutputFileMapJSON.fromVirtualOutputFileMap(entries).entries)
107107
try fileSystem.writeFileContents(file, bytes: ByteString(contents))
108108
}
109+
110+
/// Human-readable texual representation
111+
var description: String {
112+
var result = ""
113+
func outputPairDescription(inputPath: VirtualPath, outputPair: (FileType, VirtualPath))
114+
-> String {
115+
"\(inputPath.description) -> \(outputPair.0.description): \"\(outputPair.1.description)\"\n"
116+
}
117+
let maps = entries.map { ($0, $1) }.sorted { $0.0.description < $1.0.description }
118+
for (input, map) in maps {
119+
let pairs = map.map { ($0, $1) }.sorted { $0.0.description < $1.0.description }
120+
for (outputType, outputPath) in pairs {
121+
result += outputPairDescription(inputPath: input, outputPair: (outputType, outputPath))
122+
}
123+
}
124+
return result
125+
}
109126
}
110127

111128
/// Struct for loading the JSON file from disk.

Sources/SwiftDriver/Utilities/FileType.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ public enum FileType: String, Hashable, CaseIterable, Codable {
132132
extension FileType: CustomStringConvertible {
133133
public var description: String {
134134
switch self {
135-
case .swift, .sil, .sib, .image, .object, .dSYM, .dependencies, .autolink,
135+
case .swift, .sil, .sib, .image, .dSYM, .dependencies, .autolink,
136136
.swiftModule, .swiftDocumentation, .swiftInterface, .swiftSourceInfoFile, .assembly,
137137
.remap, .tbd, .pcm, .pch, .clangModuleMap:
138138
return rawValue
139+
case .object:
140+
return "object"
139141

140142
case .ast:
141143
return "ast-dump"

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,32 @@ final class SwiftDriverTests: XCTestCase {
23492349
}
23502350
}
23512351

2352+
func testPrintOutputFileMap() throws {
2353+
try withTemporaryDirectory { path in
2354+
// Replace the error stream with one we capture here.
2355+
let errorStream = stderrStream
2356+
let errorOutputFile = path.appending(component: "dummy_error_stream")
2357+
TSCBasic.stderrStream = try! ThreadSafeOutputByteStream(LocalFileOutputByteStream(errorOutputFile))
2358+
2359+
let dummyInput = path.appending(component: "output_file_map_test.swift")
2360+
let outputFileMap = path.appending(component: "output_file_map.json")
2361+
let fileMap = "{\"\(dummyInput.description)\": {\"object\": \"/build/basic_output_file_map.o\"}, \"\(path)/Inputs/main.swift\": {\"object\": \"/build/main.o\"}, \"\(path)/Inputs/lib.swift\": {\"object\": \"/build/lib.o\"}}"
2362+
try localFileSystem.writeFileContents(outputFileMap) { $0 <<< fileMap }
2363+
_ = try Driver(args: ["swiftc", "-driver-print-output-file-map",
2364+
"-target", "x86_64-apple-macosx10.9",
2365+
"-o", "/build/basic_output_file_map.out",
2366+
"-module-name", "OutputFileMap",
2367+
"-output-file-map", outputFileMap.description])
2368+
let invocationError = try localFileSystem.readFileContents(errorOutputFile).description
2369+
XCTAssertTrue(invocationError.contains("/Inputs/lib.swift -> object: \"/build/lib.o\""))
2370+
XCTAssertTrue(invocationError.contains("/Inputs/main.swift -> object: \"/build/main.o\""))
2371+
XCTAssertTrue(invocationError.contains("/output_file_map_test.swift -> object: \"/build/basic_output_file_map.o\""))
2372+
2373+
// Restore the error stream to what it was
2374+
TSCBasic.stderrStream = errorStream
2375+
}
2376+
}
2377+
23522378
func testDiagnosticOptions() throws {
23532379
do {
23542380
var driver = try Driver(args: ["swift", "-no-warnings-as-errors", "-warnings-as-errors", "foo.swift"])

0 commit comments

Comments
 (0)