Skip to content

Commit fda643c

Browse files
author
Harlan Haskins
authored
Merge pull request #35 from sjavora/there-can-be-only-one-filetype
Handle all FileTypes in OutputFileMap
2 parents 74980ca + 4ceee16 commit fda643c

File tree

4 files changed

+126
-44
lines changed

4 files changed

+126
-44
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ The goal of the new Swift driver is to provide a drop-in replacement for the exi
136136
* [x] Parseable output, as used by SwiftPM
137137
* [x] Response files
138138
* [ ] Input and primary input file lists
139-
* [ ] Complete `OutputFileMap` implementation to handle all file types uniformly
139+
* [x] Complete `OutputFileMap` implementation to handle all file types uniformly
140140
* Testing
141141
* [ ] Build stuff with SwiftPM or Xcode or your favorite build system, using `swift-driver`. Were the results identical? What changed?
142142
* [x] Shim in `swift-driver` so it can run the Swift repository's [driver test suite](https://github.com/apple/swift/tree/master/test/Driver).

Sources/SwiftDriver/Driver/OutputFileMap.swift

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,48 @@ fileprivate struct OutputFileMapJSON: Codable {
100100
/// The data associated with an input file.
101101
/// \c fileprivate so that the \c store method above can see it
102102
fileprivate struct Entry: Codable {
103-
enum CodingKeys: String, CodingKey {
104-
case dependencies
105-
case object
106-
case swiftmodule
107-
case swiftinterface
108-
case swiftDependencies = "swift-dependencies"
109-
case diagnostics
103+
104+
private struct CodingKeys: CodingKey {
105+
106+
let fileType: FileType
107+
108+
init(fileType: FileType) {
109+
self.fileType = fileType
110+
}
111+
112+
init?(stringValue: String) {
113+
guard let fileType = FileType(name: stringValue) else { return nil }
114+
self.fileType = fileType
115+
}
116+
117+
var stringValue: String { fileType.name }
118+
var intValue: Int? { nil }
119+
init?(intValue: Int) { nil }
110120
}
111121

112-
let dependencies: String?
113-
let object: String?
114-
let swiftmodule: String?
115-
let swiftinterface: String?
116-
let swiftDependencies: String?
117-
let diagnostics: String?
122+
let paths: [FileType: String]
123+
124+
fileprivate init(paths: [FileType: String]) {
125+
self.paths = paths
126+
}
127+
128+
init(from decoder: Decoder) throws {
129+
let container = try decoder.container(keyedBy: CodingKeys.self)
130+
131+
paths = try Dictionary(uniqueKeysWithValues:
132+
container.allKeys.map { key in (key.fileType, try container.decode(String.self, forKey: key)) }
133+
)
134+
}
135+
136+
func encode(to encoder: Encoder) throws {
137+
138+
var container = encoder.container(keyedBy: CodingKeys.self)
139+
140+
try paths.forEach { fileType, path in try container.encode(path, forKey: CodingKeys(fileType: fileType)) }
141+
}
118142
}
119143

120-
/// The parsed entires
144+
/// The parsed entries
121145
/// \c fileprivate so that the \c store method above can see it
122146
fileprivate let entries: [String: Entry]
123147

@@ -132,23 +156,9 @@ fileprivate struct OutputFileMapJSON: Codable {
132156

133157
/// Converts into virtual path entries.
134158
func toVirtualOutputFileMap() throws -> [VirtualPath : [FileType : VirtualPath]] {
135-
var result: [VirtualPath : [FileType : VirtualPath]] = [:]
136-
137-
for (input, entry) in entries {
138-
let input = try VirtualPath(path: input)
139-
140-
var map: [FileType: String] = [:]
141-
map[.dependencies] = entry.dependencies
142-
map[.object] = entry.object
143-
map[.swiftModule] = entry.swiftmodule
144-
map[.swiftInterface] = entry.swiftinterface
145-
map[.swiftDeps] = entry.swiftDependencies
146-
map[.diagnostics] = entry.diagnostics
147-
148-
result[input] = try map.mapValues(VirtualPath.init(path:))
149-
}
150-
151-
return result
159+
Dictionary(uniqueKeysWithValues: try entries.map { input, entry in
160+
(try VirtualPath(path: input), try entry.paths.mapValues(VirtualPath.init(path:)))
161+
})
152162
}
153163

154164
/// Converts from virtual path entries
@@ -161,13 +171,7 @@ fileprivate struct OutputFileMapJSON: Codable {
161171
return (fixedIfMaster, convert(outputs: entry.value))
162172
}
163173
func convert(outputs: [FileType: VirtualPath]) -> Entry {
164-
Entry(
165-
dependencies: outputs[.dependencies]?.name,
166-
object: outputs[.object]?.name,
167-
swiftmodule: outputs[.swiftModule]?.name,
168-
swiftinterface: outputs[.swiftInterface]?.name,
169-
swiftDependencies: outputs[.swiftDeps]?.name,
170-
diagnostics: outputs[.diagnostics]?.name)
174+
Entry(paths: outputs.mapValues({ $0.name }))
171175
}
172176
return Self(entries: Dictionary(uniqueKeysWithValues: entries.map(convert(entry:))))
173177
}

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ extension FileType {
349349

350350
case .swift, .dSYM, .autolink, .dependencies, .swiftDocumentation, .pcm,
351351
.diagnostics, .objcHeader, .image, .swiftDeps, .moduleTrace, .tbd,
352-
.optimizationRecord,.swiftInterface:
352+
.optimizationRecord, .swiftInterface, .swiftSourceInfoFile:
353353
fatalError("Output type can never be a primary output")
354354
}
355355
}

Sources/SwiftDriver/Utilities/FileType.swift

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/// Describes the kinds of files that the driver understands.
1414
///
15-
/// The raw values for these enumerations describe the default extension for]
15+
/// The raw values for these enumerations describe the default extension for
1616
/// the file type.
1717
public enum FileType: String, Hashable, CaseIterable, Codable {
1818
/// Swift source file.
@@ -45,12 +45,15 @@ public enum FileType: String, Hashable, CaseIterable, Codable {
4545
/// A compiled Swift module file.
4646
case swiftModule = "swiftmodule"
4747

48-
/// Swift documentation for amodule.
48+
/// Swift documentation for a module.
4949
case swiftDocumentation = "swiftdoc"
5050

5151
/// A textual Swift interface file.
5252
case swiftInterface = "swiftinterface"
5353

54+
/// Serialized source information.
55+
case swiftSourceInfoFile = "swiftsourceinfo"
56+
5457
/// Assembler source.
5558
case assembly = "s"
5659

@@ -110,7 +113,7 @@ extension FileType: CustomStringConvertible {
110113
public var description: String {
111114
switch self {
112115
case .swift, .sil, .sib, .image, .object, .dSYM, .dependencies, .autolink,
113-
.swiftModule, .swiftDocumentation, .swiftInterface, .assembly,
116+
.swiftModule, .swiftDocumentation, .swiftInterface, .swiftSourceInfoFile, .assembly,
114117
.remap, .tbd, .pcm, .pch:
115118
return rawValue
116119

@@ -163,8 +166,83 @@ extension FileType {
163166
case .object, .pch, .ast, .llvmIR, .llvmBitcode, .assembly, .swiftModule,
164167
.importedModules, .indexData, .remap, .dSYM, .autolink, .dependencies,
165168
.swiftDocumentation, .pcm, .diagnostics, .objcHeader, .image,
166-
.swiftDeps, .moduleTrace, .tbd, .optimizationRecord, .swiftInterface:
169+
.swiftDeps, .moduleTrace, .tbd, .optimizationRecord, .swiftInterface, .swiftSourceInfoFile:
167170
return false
168171
}
169172
}
170173
}
174+
175+
extension FileType {
176+
177+
private static let typesByName = Dictionary(uniqueKeysWithValues: FileType.allCases.map { ($0.name, $0) })
178+
179+
init?(name: String) {
180+
guard let type = Self.typesByName[name] else { return nil }
181+
182+
self = type
183+
}
184+
185+
/// The NAME values as specified in FileTypes.def
186+
var name: String {
187+
switch self {
188+
case .swift:
189+
return "swift"
190+
case .sil:
191+
return "sil"
192+
case .sib:
193+
return "sib"
194+
case .image:
195+
return "image"
196+
case .object:
197+
return "object"
198+
case .dSYM:
199+
return "dSYM"
200+
case .dependencies:
201+
return "dependencies"
202+
case .autolink:
203+
return "autolink"
204+
case .swiftModule:
205+
return "swiftmodule"
206+
case .swiftDocumentation:
207+
return "swiftdoc"
208+
case .swiftInterface:
209+
return "swiftinterface"
210+
case .swiftSourceInfoFile:
211+
return "swiftsourceinfo"
212+
case .assembly:
213+
return "assembly"
214+
case .remap:
215+
return "remap"
216+
case .tbd:
217+
return "tbd"
218+
case .pcm:
219+
return "pcm"
220+
case .pch:
221+
return "pch"
222+
case .ast:
223+
return "ast-dump"
224+
case .raw_sil:
225+
return "raw-sil"
226+
case .raw_sib:
227+
return "raw-sib"
228+
case .llvmIR:
229+
return "llvm-ir"
230+
case .llvmBitcode:
231+
return "llvm-bc"
232+
case .objcHeader:
233+
return "objc-header"
234+
case .swiftDeps:
235+
return "swift-dependencies"
236+
case .importedModules:
237+
return "imported-modules"
238+
case .moduleTrace:
239+
return "module-trace"
240+
case .indexData:
241+
return "index-data"
242+
case .optimizationRecord:
243+
return "opt-record"
244+
case .diagnostics:
245+
return "diagnostics"
246+
}
247+
}
248+
}

0 commit comments

Comments
 (0)