Skip to content

Commit 3ea9371

Browse files
authored
Merge pull request #230 from owenv/linker-inputs
Fixup linker input handling and use -add_ast_path when needed
2 parents 0fe1d84 + 0368510 commit 3ea9371

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,16 @@ extension DarwinToolchain {
303303
}
304304

305305
// Add inputs.
306-
commandLine.append(contentsOf: inputs.map { .path($0.file) })
306+
commandLine.append(contentsOf: inputs.flatMap {
307+
(path: TypedVirtualPath) -> [Job.ArgTemplate] in
308+
if path.type == .swiftModule {
309+
return [.flag("-add_ast_path"), .path(path.file)]
310+
} else if path.type == .object {
311+
return [.path(path.file)]
312+
} else {
313+
return []
314+
}
315+
})
307316

308317
// Add the output
309318
commandLine.appendFlag("-o")

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,15 @@ extension GenericUnixToolchain {
158158
)
159159
commandLine.appendPath(swiftrtPath)
160160

161-
let inputFiles: [Job.ArgTemplate] = inputs.map { input in
161+
let inputFiles: [Job.ArgTemplate] = inputs.compactMap { input in
162162
// Autolink inputs are handled specially
163163
if input.type == .autolink {
164164
return .responseFilePath(input.file)
165+
} else if input.type == .object {
166+
return .path(input.file)
167+
} else {
168+
return nil
165169
}
166-
return .path(input.file)
167170
}
168171
commandLine.append(contentsOf: inputFiles)
169172

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,30 @@ final class SwiftDriverTests: XCTestCase {
839839
XCTAssertFalse(cmd.contains(.flag("-shared")))
840840
}
841841

842+
#if os(macOS)
843+
// dsymutil won't be found on Linux.
844+
do {
845+
var driver = try Driver(args: commonArgs + ["-emit-executable", "-emit-module", "-g", "-target", "x86_64-apple-macosx10.15"], env: env)
846+
let plannedJobs = try driver.planBuild()
847+
XCTAssertEqual(5, plannedJobs.count)
848+
XCTAssertEqual(plannedJobs.map(\.kind), [.compile, .compile, .mergeModule, .link, .generateDSYM])
849+
850+
let linkJob = plannedJobs[3]
851+
XCTAssertEqual(linkJob.kind, .link)
852+
853+
let cmd = linkJob.commandLine
854+
XCTAssertTrue(cmd.contains(.flag("-o")))
855+
XCTAssertTrue(cmd.contains(.path(.temporary(RelativePath("foo.o")))))
856+
XCTAssertTrue(cmd.contains(.path(.temporary(RelativePath("bar.o")))))
857+
XCTAssertTrue(cmd.contains(subsequence: [.flag("-add_ast_path"), .path(.relative(.init("Test.swiftmodule")))]))
858+
XCTAssertEqual(linkJob.outputs[0].file, try VirtualPath(path: "Test"))
859+
860+
XCTAssertFalse(cmd.contains(.flag("-static")))
861+
XCTAssertFalse(cmd.contains(.flag("-dylib")))
862+
XCTAssertFalse(cmd.contains(.flag("-shared")))
863+
}
864+
#endif
865+
842866
// FIXME: This test will fail when run on macOS, because
843867
// swift-autolink-extract is not present
844868
#if os(Linux)

0 commit comments

Comments
 (0)