Skip to content

Fix wrong -Xlinker propagation to Clang with WebAssembly #1538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,7 @@ extension DarwinToolchain {
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
// Because we invoke `clang` as the linker executable, we must still
// use `-Xlinker` for linker-specific arguments.
for linkerOpt in parsedOptions.arguments(for: .Xlinker) {
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag(linkerOpt.argument.asSingle)
}
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
try addExtraClangLinkerArgs(to: &commandLine, parsedOptions: &parsedOptions)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,7 @@ extension GenericUnixToolchain {
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
// Because we invoke `clang` as the linker executable, we must still
// use `-Xlinker` for linker-specific arguments.
for linkerOpt in parsedOptions.arguments(for: .Xlinker) {
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag(linkerOpt.argument.asSingle)
}
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
try addExtraClangLinkerArgs(to: &commandLine, parsedOptions: &parsedOptions)

// This should be the last option, for convenience in checking output.
commandLine.appendFlag(.o)
Expand Down
13 changes: 13 additions & 0 deletions Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ extension Toolchain {
commandLine.appendFlag(match.option.spelling + match.argument.asSingle)
}
}

func addExtraClangLinkerArgs(
to commandLine: inout [Job.ArgTemplate],
parsedOptions: inout ParsedOptions
) throws {
// Because we invoke `clang` as the linker executable, we must still
// use `-Xlinker` for linker-specific arguments.
for linkerOpt in parsedOptions.arguments(for: .Xlinker) {
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag(linkerOpt.argument.asSingle)
}
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
}
}

// MARK: - Common argument routines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ extension WebAssemblyToolchain {
from: &parsedOptions
)
addLinkedLibArgs(to: &commandLine, parsedOptions: &parsedOptions)
try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions)
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
try addExtraClangLinkerArgs(to: &commandLine, parsedOptions: &parsedOptions)

// This should be the last option, for convenience in checking output.
commandLine.appendFlag(.o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,7 @@ extension WindowsToolchain {
commandLine.appendFlag("-lclang_rt.profile")
}

for option in parsedOptions.arguments(for: .Xlinker) {
commandLine.appendFlag(.Xlinker)
commandLine.appendFlag(option.argument.asSingle)
}
// TODO(compnerd) is there a separate equivalent to OPT_linker_option_group?
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
try addExtraClangLinkerArgs(to: &commandLine, parsedOptions: &parsedOptions)

if parsedOptions.contains(.v) {
commandLine.appendFlag("-v")
Expand Down
23 changes: 23 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,29 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertTrue(cmd.contains(subsequence: [.flag("-Xlinker"), .flag("-rpath=$ORIGIN"), .flag("foo")]))
}

do {
// Xlinker flags
// Ensure that Xlinker flags are passed as such to the clang linker invocation.
try withTemporaryDirectory { path in
try localFileSystem.writeFileContents(path.appending(components: "wasi", "static-executable-args.lnk")) {
$0.send("garbage")
}
var driver = try Driver(args: commonArgs + ["-emit-executable", "-L", "/tmp", "-Xlinker", "--export-all",
"-Xlinker", "-E", "-Xclang-linker", "foo",
"-resource-dir", path.pathString,
"-target", "wasm32-unknown-wasi"], env: env)
let plannedJobs = try driver.planBuild()
XCTAssertEqual(plannedJobs.count, 4)
let linkJob = plannedJobs[3]
let cmd = linkJob.commandLine
XCTAssertTrue(cmd.contains(subsequence: [
.flag("-Xlinker"), .flag("--export-all"),
.flag("-Xlinker"), .flag("-E"),
.flag("foo")
]))
}
}

do {
var driver = try Driver(args: commonArgs + ["-emit-library", "-no-toolchain-stdlib-rpath",
"-target", "aarch64-unknown-linux"], env: env)
Expand Down