2024-04-14 12:00:32 -05:00
|
|
|
// Test linking using `cc` with `rust-lld`, using the unstable CLI described in MCP 510
|
|
|
|
// see https://github.com/rust-lang/compiler-team/issues/510 for more info
|
|
|
|
|
|
|
|
//@ needs-rust-lld
|
|
|
|
//@ ignore-s390x lld does not yet support s390x as target
|
|
|
|
|
|
|
|
use run_make_support::regex::Regex;
|
2024-08-04 11:19:00 -05:00
|
|
|
use run_make_support::{is_msvc, rustc};
|
2024-04-14 12:00:32 -05:00
|
|
|
|
|
|
|
fn main() {
|
2024-08-04 11:19:00 -05:00
|
|
|
// lld-link is used if msvc, otherwise a gnu-compatible lld is used.
|
|
|
|
let linker_version_flag = if is_msvc() { "--version" } else { "-Wl,-v" };
|
|
|
|
|
2024-04-14 12:00:32 -05:00
|
|
|
// Opt-in to lld and the self-contained linker, to link with rust-lld. We'll check that by
|
|
|
|
// asking the linker to display its version number with a link-arg.
|
|
|
|
let output = rustc()
|
|
|
|
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
|
|
|
|
.arg("-Zlinker-features=+lld")
|
|
|
|
.arg("-Clink-self-contained=+linker")
|
|
|
|
.arg("-Zunstable-options")
|
2024-08-04 11:19:00 -05:00
|
|
|
.link_arg(linker_version_flag)
|
2024-04-14 12:00:32 -05:00
|
|
|
.input("main.rs")
|
|
|
|
.run();
|
|
|
|
assert!(
|
2024-06-07 08:23:10 -05:00
|
|
|
find_lld_version_in_logs(output.stderr_utf8()),
|
2024-05-24 10:46:42 -05:00
|
|
|
"the LLD version string should be present in the output logs:\n{}",
|
2024-06-07 08:23:10 -05:00
|
|
|
output.stderr_utf8()
|
2024-04-14 12:00:32 -05:00
|
|
|
);
|
|
|
|
|
2024-08-04 11:19:00 -05:00
|
|
|
// It should not be used when we explicitly opt-out of lld.
|
2024-04-14 12:00:32 -05:00
|
|
|
let output = rustc()
|
|
|
|
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
|
2024-08-04 11:19:00 -05:00
|
|
|
.link_arg(linker_version_flag)
|
2024-04-14 12:00:32 -05:00
|
|
|
.arg("-Zlinker-features=-lld")
|
|
|
|
.input("main.rs")
|
|
|
|
.run();
|
|
|
|
assert!(
|
2024-06-07 08:23:10 -05:00
|
|
|
!find_lld_version_in_logs(output.stderr_utf8()),
|
2024-05-24 10:46:42 -05:00
|
|
|
"the LLD version string should not be present in the output logs:\n{}",
|
2024-06-07 08:23:10 -05:00
|
|
|
output.stderr_utf8()
|
2024-04-14 12:00:32 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// While we're here, also check that the last linker feature flag "wins" when passed multiple
|
|
|
|
// times to rustc.
|
|
|
|
let output = rustc()
|
|
|
|
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
|
2024-08-04 11:19:00 -05:00
|
|
|
.link_arg(linker_version_flag)
|
2024-04-14 12:00:32 -05:00
|
|
|
.arg("-Clink-self-contained=+linker")
|
|
|
|
.arg("-Zunstable-options")
|
|
|
|
.arg("-Zlinker-features=-lld")
|
|
|
|
.arg("-Zlinker-features=+lld")
|
|
|
|
.arg("-Zlinker-features=-lld,+lld")
|
|
|
|
.input("main.rs")
|
|
|
|
.run();
|
|
|
|
assert!(
|
2024-06-07 08:23:10 -05:00
|
|
|
find_lld_version_in_logs(output.stderr_utf8()),
|
2024-05-24 10:46:42 -05:00
|
|
|
"the LLD version string should be present in the output logs:\n{}",
|
2024-06-07 08:23:10 -05:00
|
|
|
output.stderr_utf8()
|
2024-04-14 12:00:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:23:10 -05:00
|
|
|
fn find_lld_version_in_logs(stderr: String) -> bool {
|
2024-04-14 12:00:32 -05:00
|
|
|
let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
|
2024-05-24 10:50:20 -05:00
|
|
|
stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
|
2024-04-14 12:00:32 -05:00
|
|
|
}
|