Skip to content

Commit 1d8848d

Browse files
authored
Merge pull request #1364 from artemcm/CherryWMOFixes
[5.9 🍒] Fixes to WMO invocations without `.swift` input files
2 parents 9763820 + 35c45f7 commit 1d8848d

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,10 @@ extension Driver {
572572
// To match the legacy driver behavior, make sure we add the first input file
573573
// to the output file map if compiling without primary inputs (WMO), even
574574
// if there aren't any corresponding outputs.
575-
entries[inputFiles[0].fileHandle] = [:]
575+
guard let firstSourceInputHandle = inputFiles.first(where:{ $0.type == .swift })?.fileHandle else {
576+
fatalError("Formulating swift-frontend invocation without any input .swift files")
577+
}
578+
entries[firstSourceInputHandle] = [:]
576579
}
577580

578581
for flaggedPair in flaggedInputOutputPairs {

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ extension Driver {
328328
addJobOutputs: ([TypedVirtualPath]) -> Void,
329329
emitModuleTrace: Bool
330330
) throws -> Job? {
331-
guard case .singleCompile = compilerMode
331+
guard case .singleCompile = compilerMode,
332+
inputFiles.contains(where: { $0.type.isPartOfSwiftCompilation })
332333
else { return nil }
333334

334335
if parsedOptions.hasArgument(.embedBitcode),

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,6 +2824,27 @@ final class SwiftDriverTests: XCTestCase {
28242824

28252825
}
28262826

2827+
func testWMOWithNonSourceInput() throws {
2828+
var driver1 = try Driver(args: [
2829+
"swiftc", "-whole-module-optimization", "danger.o", "foo.swift", "bar.swift", "wibble.swift", "-module-name", "Test",
2830+
"-driver-filelist-threshold=0"
2831+
])
2832+
let plannedJobs = try driver1.planBuild().removingAutolinkExtractJobs()
2833+
XCTAssertEqual(plannedJobs.count, 2)
2834+
let compileJob = plannedJobs[0]
2835+
XCTAssertEqual(compileJob.kind, .compile)
2836+
XCTAssert(compileJob.commandLine.contains(.flag("-supplementary-output-file-map")))
2837+
let argIdx = try XCTUnwrap(compileJob.commandLine.firstIndex(where: { $0 == .flag("-supplementary-output-file-map") }))
2838+
let supplOutputs = compileJob.commandLine[argIdx+1]
2839+
guard case let .path(path) = supplOutputs,
2840+
case let .fileList(_, fileList) = path,
2841+
case let .outputFileMap(outFileMap) = fileList else {
2842+
throw StringError("Unexpected argument for output file map")
2843+
}
2844+
let firstKey: String = try VirtualPath.lookup(XCTUnwrap(outFileMap.entries.keys.first)).description
2845+
XCTAssertEqual(firstKey, "foo.swift")
2846+
}
2847+
28272848
func testDashDashPassingDownInput() throws {
28282849
do {
28292850
var driver = try Driver(args: ["swiftc", "-module-name=ThisModule", "-wmo", "-num-threads", "4", "-emit-module", "-o", "test.swiftmodule", "--", "main.swift", "multi-threaded.swift"])
@@ -2875,6 +2896,15 @@ final class SwiftDriverTests: XCTestCase {
28752896
}
28762897
}
28772898

2899+
func testWMOWithJustObjectInputs() throws {
2900+
var driver = try Driver(args: [
2901+
"swiftc", "-wmo", "foo.o", "bar.o"
2902+
])
2903+
let plannedJobs = try driver.planBuild()
2904+
XCTAssertEqual(plannedJobs.count, 1)
2905+
XCTAssertEqual(plannedJobs.first?.kind, .link)
2906+
}
2907+
28782908
func testModuleAliasingWithImplicitBuild() throws {
28792909
var driver = try Driver(args: [
28802910
"swiftc", "foo.swift", "-module-name", "Foo", "-module-alias", "Car=Bar",

0 commit comments

Comments
 (0)