|
| 1 | +// Test linking using `cc` with `rust-lld`, using the unstable CLI described in MCP 510 |
| 2 | +// see https://github.com/rust-lang/compiler-team/issues/510 for more info |
| 3 | + |
| 4 | +//@ needs-rust-lld |
| 5 | +//@ ignore-msvc |
| 6 | +//@ ignore-s390x lld does not yet support s390x as target |
| 7 | + |
| 8 | +extern crate run_make_support; |
| 9 | + |
| 10 | +use run_make_support::regex::Regex; |
| 11 | +use run_make_support::rustc; |
| 12 | +use std::process::Output; |
| 13 | + |
| 14 | +fn main() { |
| 15 | + // Opt-in to lld and the self-contained linker, to link with rust-lld. We'll check that by |
| 16 | + // asking the linker to display its version number with a link-arg. |
| 17 | + let output = rustc() |
| 18 | + .env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info") |
| 19 | + .arg("-Zlinker-features=+lld") |
| 20 | + .arg("-Clink-self-contained=+linker") |
| 21 | + .arg("-Zunstable-options") |
| 22 | + .arg("-Clink-args=-Wl,-v") |
| 23 | + .input("main.rs") |
| 24 | + .run(); |
| 25 | + assert!( |
| 26 | + find_lld_version_in_logs(output), |
| 27 | + "the LLD version string should be present in the output logs" |
| 28 | + ); |
| 29 | + |
| 30 | + // It should not be used when we explictly opt-out of lld. |
| 31 | + let output = rustc() |
| 32 | + .env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info") |
| 33 | + .arg("-Clink-args=-Wl,-v") |
| 34 | + .arg("-Zlinker-features=-lld") |
| 35 | + .input("main.rs") |
| 36 | + .run(); |
| 37 | + assert!( |
| 38 | + !find_lld_version_in_logs(output), |
| 39 | + "the LLD version string should not be present in the output logs" |
| 40 | + ); |
| 41 | + |
| 42 | + // While we're here, also check that the last linker feature flag "wins" when passed multiple |
| 43 | + // times to rustc. |
| 44 | + let output = rustc() |
| 45 | + .env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info") |
| 46 | + .arg("-Clink-args=-Wl,-v") |
| 47 | + .arg("-Clink-self-contained=+linker") |
| 48 | + .arg("-Zunstable-options") |
| 49 | + .arg("-Zlinker-features=-lld") |
| 50 | + .arg("-Zlinker-features=+lld") |
| 51 | + .arg("-Zlinker-features=-lld,+lld") |
| 52 | + .input("main.rs") |
| 53 | + .run(); |
| 54 | + assert!( |
| 55 | + find_lld_version_in_logs(output), |
| 56 | + "the LLD version string should be present in the output logs" |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +fn find_lld_version_in_logs(output: Output) -> bool { |
| 61 | + let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap(); |
| 62 | + let stderr = std::str::from_utf8(&output.stderr).unwrap(); |
| 63 | + stderr.lines().any(|line| lld_version_re.is_match(line)) |
| 64 | +} |
0 commit comments