Skip to content

Commit ec1618c

Browse files
Rollup merge of #123975 - lqd:rust-lld-tests, r=jieyouxu
Port the 2 `rust-lld` run-make tests to `rmake` In preparation for finalizing most of the `rust-lld` work, this PR ports the following tests to `rmake`: - `tests/run-make/rust-lld` - `tests/run-make/rust-lld-custom-target` As they use `$(CGREP) -e` I added `regex` as an exported dependency to the `run_make_support` library. Unfortunately, the most recent versions depend on `memchr` 2.6.0 but it's currently pinned at 2.5.0 in the workspace, and therefore had to settle for the older `regex-1.8.0`. r? `@jieyouxu`
2 parents 4885ddf + af887d3 commit ec1618c

File tree

10 files changed

+126
-23
lines changed

10 files changed

+126
-23
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3342,6 +3342,7 @@ name = "run_make_support"
33423342
version = "0.0.0"
33433343
dependencies = [
33443344
"object 0.34.0",
3345+
"regex",
33453346
"wasmparser",
33463347
]
33473348

src/tools/compiletest/src/header.rs

+1
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
842842
"needs-profiler-support",
843843
"needs-relocation-model-pic",
844844
"needs-run-enabled",
845+
"needs-rust-lld",
845846
"needs-rust-lldb",
846847
"needs-sanitizer-address",
847848
"needs-sanitizer-cfi",

src/tools/run-make-support/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
[dependencies]
77
object = "0.34.0"
88
wasmparser = "0.118.2"
9+
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace

src/tools/run-make-support/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
1313
use std::process::{Command, Output};
1414

1515
pub use object;
16+
pub use regex;
1617
pub use wasmparser;
1718

1819
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};

src/tools/run-make-support/src/rustc.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ impl Rustc {
128128
self
129129
}
130130

131-
/// Specify target triple.
131+
/// Specify the target triple, or a path to a custom target json spec file.
132132
pub fn target(&mut self, target: &str) -> &mut Self {
133-
assert!(!target.contains(char::is_whitespace), "target triple cannot contain spaces");
134133
self.cmd.arg(format!("--target={target}"));
135134
self
136135
}
@@ -149,6 +148,12 @@ impl Rustc {
149148
self
150149
}
151150

151+
/// Add an extra argument to the linker invocation, via `-Clink-arg`.
152+
pub fn link_arg(&mut self, link_arg: &str) -> &mut Self {
153+
self.cmd.arg(format!("-Clink-arg={link_arg}"));
154+
self
155+
}
156+
152157
#[track_caller]
153158
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
154159
let caller_location = std::panic::Location::caller();

src/tools/tidy/src/allowed_run_make_makefiles.txt

-2
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ run-make/rlib-format-packed-bundled-libs-2/Makefile
249249
run-make/rlib-format-packed-bundled-libs-3/Makefile
250250
run-make/rlib-format-packed-bundled-libs/Makefile
251251
run-make/rmeta-preferred/Makefile
252-
run-make/rust-lld-custom-target/Makefile
253-
run-make/rust-lld/Makefile
254252
run-make/rustc-macro-dep-files/Makefile
255253
run-make/rustdoc-determinism/Makefile
256254
run-make/rustdoc-error-lines/Makefile

tests/run-make/rust-lld-custom-target/Makefile

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
.crate_type("cdylib")
23+
.target("custom-target.json")
24+
.link_arg("-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+
.crate_type("cdylib")
36+
.target("custom-target.json")
37+
.arg("-Zlinker-features=-lld")
38+
.link_arg("-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+
}

tests/run-make/rust-lld/Makefile

-12
This file was deleted.

tests/run-make/rust-lld/rmake.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
.link_arg("-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+
.link_arg("-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+
.link_arg("-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

Comments
 (0)