Skip to content

Commit 77ce2a3

Browse files
committed
Infer module output path from '-o' where applicable
1 parent 4efb328 commit 77ce2a3

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,14 @@ extension Driver {
12031203
// The module path was specified.
12041204
moduleOutputPath = try VirtualPath(path: modulePathArg.asSingle)
12051205
} else if moduleOutputKind == .topLevel {
1206-
// FIXME: Logic to infer from -o, primary outputs, etc.
1207-
moduleOutputPath = try .init(path: moduleName.appendingFileTypeExtension(.swiftModule))
1206+
// FIXME: Logic to infer from primary outputs, etc.
1207+
let moduleFilename = moduleName.appendingFileTypeExtension(.swiftModule)
1208+
if let outputArg = parsedOptions.getLastArgument(.o)?.asSingle, let lastSeparatorIndex = outputArg.lastIndex(of: "/") {
1209+
// Put the module next to the top-level output.
1210+
moduleOutputPath = try .init(path: outputArg[outputArg.startIndex...lastSeparatorIndex] + moduleFilename)
1211+
} else {
1212+
moduleOutputPath = try .init(path: moduleFilename)
1213+
}
12081214
} else {
12091215
moduleOutputPath = .temporary(RelativePath(moduleName.appendingFileTypeExtension(.swiftModule)))
12101216
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ final class SwiftDriverTests: XCTestCase {
309309
try assertDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-library", "-o", "libWibble.so", "-module-name", "Swift") {
310310
$1.expect(.error("module name \"Swift\" is reserved for the standard library"))
311311
}
312+
313+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-module", "-emit-library", "-o", "some/dir/libFoo.so", "-module-name", "MyModule") { driver in
314+
XCTAssertEqual(driver.moduleOutput, ModuleOutput.topLevel(try VirtualPath(path: "some/dir/MyModule.swiftmodule")))
315+
}
316+
317+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-module", "-emit-library", "-o", "/", "-module-name", "MyModule") { driver in
318+
XCTAssertEqual(driver.moduleOutput, ModuleOutput.topLevel(try VirtualPath(path: "/MyModule.swiftmodule")))
319+
}
320+
321+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "bar.swift", "-emit-module", "-emit-library", "-o", "../../some/other/dir/libFoo.so", "-module-name", "MyModule") { driver in
322+
XCTAssertEqual(driver.moduleOutput, ModuleOutput.topLevel(try VirtualPath(path: "../../some/other/dir/MyModule.swiftmodule")))
323+
}
312324
}
313325

314326
func testModuleNameFallbacks() throws {

0 commit comments

Comments
 (0)