|
| 1 | +// Test linking using `cc` with `rust-lld`, using a custom target with features described in MCP 510 |
| 2 | +// see https://github.com/rust-lang/compiler-team/issues/510 for more info: |
| 3 | +// |
| 4 | +// Starting from the `x86_64-unknown-linux-gnu` target spec, we add the following options: |
| 5 | +// - a linker-flavor using lld via a C compiler |
| 6 | +// - the self-contained linker component is enabled |
| 7 | + |
| 8 | +//@ needs-rust-lld |
| 9 | +//@ only-x86_64-unknown-linux-gnu |
| 10 | + |
| 11 | +extern crate run_make_support; |
| 12 | + |
| 13 | +use run_make_support::regex::Regex; |
| 14 | +use run_make_support::rustc; |
| 15 | +use std::process::Output; |
| 16 | + |
| 17 | +fn main() { |
| 18 | + // Compile to a custom target spec with rust-lld enabled by default. We'll check that by asking |
| 19 | + // the linker to display its version number with a link-arg. |
| 20 | + let output = rustc() |
| 21 | + .env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info") |
| 22 | + .arg("--crate-type=cdylib") |
| 23 | + .arg("--target=custom-target.json") |
| 24 | + .arg("-Clink-args=-Wl,-v") |
| 25 | + .input("lib.rs") |
| 26 | + .run(); |
| 27 | + assert!( |
| 28 | + find_lld_version_in_logs(output), |
| 29 | + "the LLD version string should be present in the output logs" |
| 30 | + ); |
| 31 | + |
| 32 | + // But it can also be disabled via linker features. |
| 33 | + let output = rustc() |
| 34 | + .env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info") |
| 35 | + .arg("--crate-type=cdylib") |
| 36 | + .arg("--target=custom-target.json") |
| 37 | + .arg("-Zlinker-features=-lld") |
| 38 | + .arg("-Clink-args=-Wl,-v") |
| 39 | + .input("lib.rs") |
| 40 | + .run(); |
| 41 | + assert!( |
| 42 | + !find_lld_version_in_logs(output), |
| 43 | + "the LLD version string should not be present in the output logs" |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +fn find_lld_version_in_logs(output: Output) -> bool { |
| 48 | + let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap(); |
| 49 | + let stderr = std::str::from_utf8(&output.stderr).unwrap(); |
| 50 | + stderr.lines().any(|line| lld_version_re.is_match(line)) |
| 51 | +} |
0 commit comments