Skip to content

Commit 4662bc8

Browse files
authored
Merge pull request #45 from owenv/module-output-path
Infer module output path from '-o' where applicable
2 parents 5f5ae14 + 77ce2a3 commit 4662bc8

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
@@ -1221,8 +1221,14 @@ extension Driver {
12211221
// The module path was specified.
12221222
moduleOutputPath = try VirtualPath(path: modulePathArg.asSingle)
12231223
} else if moduleOutputKind == .topLevel {
1224-
// FIXME: Logic to infer from -o, primary outputs, etc.
1225-
moduleOutputPath = try .init(path: moduleName.appendingFileTypeExtension(.swiftModule))
1224+
// FIXME: Logic to infer from primary outputs, etc.
1225+
let moduleFilename = moduleName.appendingFileTypeExtension(.swiftModule)
1226+
if let outputArg = parsedOptions.getLastArgument(.o)?.asSingle, let lastSeparatorIndex = outputArg.lastIndex(of: "/") {
1227+
// Put the module next to the top-level output.
1228+
moduleOutputPath = try .init(path: outputArg[outputArg.startIndex...lastSeparatorIndex] + moduleFilename)
1229+
} else {
1230+
moduleOutputPath = try .init(path: moduleFilename)
1231+
}
12261232
} else {
12271233
moduleOutputPath = .temporary(RelativePath(moduleName.appendingFileTypeExtension(.swiftModule)))
12281234
}

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)