Skip to content

Commit 0368510

Browse files
committed
Fixup linker input handling and use -add_ast_path when needed
1 parent 54238ad commit 0368510

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
@@ -832,6 +832,30 @@ final class SwiftDriverTests: XCTestCase {
832832
XCTAssertFalse(cmd.contains(.flag("-shared")))
833833
}
834834

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

0 commit comments

Comments
 (0)