Skip to content

Commit 8a4821f

Browse files
committed
Auto merge of rust-lang#138645 - Kobzol:stabilize-lld-test, r=<try>
[do not merge] Preparation for LLD stabilization This PR serves for testing test changes for stabilizing LLD. CC `@lqd` r? `@ghost` try-job: dist-x86_64-linux
2 parents 75530e9 + 80cbbeb commit 8a4821f

File tree

14 files changed

+109
-154
lines changed

14 files changed

+109
-154
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1328,9 +1328,7 @@ pub fn rustc_cargo_env(
13281328
}
13291329

13301330
// Enable rustc's env var for `rust-lld` when requested.
1331-
if builder.config.lld_enabled
1332-
&& (builder.config.channel == "dev" || builder.config.channel == "nightly")
1333-
{
1331+
if builder.config.lld_enabled {
13341332
cargo.env("CFG_USE_SELF_CONTAINED_LINKER", "1");
13351333
}
13361334

src/bootstrap/src/core/config/config.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2334,10 +2334,7 @@ impl Config {
23342334
// thus, disabled
23352335
// - similarly, lld will not be built nor used by default when explicitly asked not to, e.g.
23362336
// when the config sets `rust.lld = false`
2337-
if config.build.triple == "x86_64-unknown-linux-gnu"
2338-
&& config.hosts == [config.build]
2339-
&& (config.channel == "dev" || config.channel == "nightly")
2340-
{
2337+
if config.build.triple == "x86_64-unknown-linux-gnu" && config.hosts == [config.build] {
23412338
let no_llvm_config = config
23422339
.target_config
23432340
.get(&config.build)

src/tools/opt-dist/src/tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ llvm-config = "{llvm_config}"
104104
"tests/incremental",
105105
"tests/mir-opt",
106106
"tests/pretty",
107+
// Make sure that we don't use too new GLIBC symbols on x64
107108
"tests/run-make/glibc-symbols-x86_64-unknown-linux-gnu",
109+
// Make sure that we use LLD by default on x64
110+
"tests/run-make/rust-lld-x86_64-unknown-linux-gnu-dist",
108111
"tests/ui",
109112
"tests/crashes",
110113
];

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

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod assertion_helpers;
1717
pub mod diff;
1818
pub mod env;
1919
pub mod external_deps;
20+
pub mod linker;
2021
pub mod path_helpers;
2122
pub mod run;
2223
pub mod scoped_run;
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use regex::Regex;
2+
3+
use crate::{Rustc, is_msvc};
4+
5+
/// Asserts that `rustc` uses LLD for linking when executed.
6+
pub fn assert_rustc_uses_lld(rustc: &mut Rustc) {
7+
let stderr = get_stderr_with_linker_messages(rustc);
8+
assert!(
9+
has_lld_version_in_logs(&stderr),
10+
"LLD version should be present in rustc stderr:\n{stderr}"
11+
);
12+
}
13+
14+
/// Asserts that `rustc` doesn't use LLD for linking when executed.
15+
pub fn assert_rustc_doesnt_use_lld(rustc: &mut Rustc) {
16+
let stderr = get_stderr_with_linker_messages(rustc);
17+
assert!(
18+
!has_lld_version_in_logs(&stderr),
19+
"LLD version should NOT be present in rustc stderr:\n{stderr}"
20+
);
21+
}
22+
23+
fn get_stderr_with_linker_messages(rustc: &mut Rustc) -> String {
24+
// lld-link is used if msvc, otherwise a gnu-compatible lld is used.
25+
let linker_version_flag = if is_msvc() { "--version" } else { "-Wl,-v" };
26+
27+
let output = rustc.arg("-Wlinker-messages").link_arg(linker_version_flag).run();
28+
output.stderr_utf8()
29+
}
30+
31+
fn has_lld_version_in_logs(stderr: &str) -> bool {
32+
// Strip the `-Wlinker-messages` wrappers prefixing the linker output.
33+
let stderr = Regex::new(r"warning: linker std(out|err):").unwrap().replace_all(&stderr, "");
34+
let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
35+
stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
36+
}

tests/run-make/rust-lld-by-default-beta-stable/main.rs

-1
This file was deleted.

tests/run-make/rust-lld-by-default-beta-stable/rmake.rs

-27
This file was deleted.

tests/run-make/rust-lld-by-default-nightly/rmake.rs

-40
This file was deleted.

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

+9-30
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,22 @@
88
//@ needs-rust-lld
99
//@ only-x86_64-unknown-linux-gnu
1010

11-
use run_make_support::regex::Regex;
11+
use run_make_support::linker::{assert_rustc_doesnt_use_lld, assert_rustc_uses_lld};
1212
use run_make_support::rustc;
1313

1414
fn main() {
1515
// Compile to a custom target spec with rust-lld enabled by default. We'll check that by asking
1616
// the linker to display its version number with a link-arg.
17-
let output = rustc()
18-
.crate_type("cdylib")
19-
.arg("-Wlinker-messages")
20-
.target("custom-target.json")
21-
.link_arg("-Wl,-v")
22-
.input("lib.rs")
23-
.run();
24-
assert!(
25-
find_lld_version_in_logs(output.stderr_utf8()),
26-
"the LLD version string should be present in the output logs:\n{}",
27-
output.stderr_utf8()
17+
assert_rustc_uses_lld(
18+
rustc().crate_type("cdylib").target("custom-target.json").input("lib.rs"),
2819
);
2920

3021
// But it can also be disabled via linker features.
31-
let output = rustc()
32-
.crate_type("cdylib")
33-
.arg("-Wlinker-messages")
34-
.target("custom-target.json")
35-
.arg("-Zlinker-features=-lld")
36-
.link_arg("-Wl,-v")
37-
.input("lib.rs")
38-
.run();
39-
assert!(
40-
!find_lld_version_in_logs(output.stderr_utf8()),
41-
"the LLD version string should not be present in the output logs:\n{}",
42-
output.stderr_utf8()
22+
assert_rustc_doesnt_use_lld(
23+
rustc()
24+
.crate_type("cdylib")
25+
.target("custom-target.json")
26+
.arg("-Zlinker-features=-lld")
27+
.input("lib.rs"),
4328
);
4429
}
45-
46-
fn find_lld_version_in_logs(stderr: String) -> bool {
47-
let lld_version_re =
48-
Regex::new(r"^warning: linker stdout: LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
49-
stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
50-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Ensure that rust-lld is used as the default linker on `x86_64-unknown-linux-gnu`
2+
// dist artifacts and that it can also be turned off with a CLI flag.
3+
4+
//@ only-dist
5+
//@ only-x86_64-unknown-linux-gnu
6+
7+
use run_make_support::linker::{assert_rustc_doesnt_use_lld, assert_rustc_uses_lld};
8+
use run_make_support::rustc;
9+
10+
fn main() {
11+
// A regular compilation should use rust-lld by default.
12+
assert_rustc_uses_lld(rustc().input("main.rs"));
13+
14+
// But it can still be disabled by turning the linker feature off.
15+
assert_rustc_doesnt_use_lld(rustc().arg("-Zlinker-features=-lld").input("main.rs"));
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Test linking using `cc` with `rust-lld`, which is on by default on the x86_64-unknown-linux-gnu
2+
// target.
3+
// See https://github.com/rust-lang/compiler-team/issues/510 for more info
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Ensure that rust-lld is used as the default linker on `x86_64-unknown-linux-gnu`
2+
// and that it can also be turned off with a CLI flag.
3+
//
4+
// This version of the test checks that LLD is used by default when LLD is enabled in the
5+
// toolchain. There is a separate test that checks that LLD is used for dist artifacts
6+
// unconditionally.
7+
8+
//@ needs-rust-lld
9+
//@ only-x86_64-unknown-linux-gnu
10+
11+
use run_make_support::linker::{assert_rustc_doesnt_use_lld, assert_rustc_uses_lld};
12+
use run_make_support::rustc;
13+
14+
fn main() {
15+
// A regular compilation should use rust-lld by default.
16+
assert_rustc_uses_lld(rustc().input("main.rs"));
17+
18+
// But it can still be disabled by turning the linker feature off.
19+
assert_rustc_doesnt_use_lld(rustc().arg("-Zlinker-features=-lld").input("main.rs"));
20+
}

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

+17-49
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,32 @@
44
//@ needs-rust-lld
55
//@ ignore-s390x lld does not yet support s390x as target
66

7-
use run_make_support::regex::Regex;
8-
use run_make_support::{is_msvc, rustc};
7+
use run_make_support::linker::{assert_rustc_doesnt_use_lld, assert_rustc_uses_lld};
8+
use run_make_support::rustc;
99

1010
fn main() {
11-
// lld-link is used if msvc, otherwise a gnu-compatible lld is used.
12-
let linker_version_flag = if is_msvc() { "--version" } else { "-Wl,-v" };
13-
1411
// Opt-in to lld and the self-contained linker, to link with rust-lld. We'll check that by
1512
// asking the linker to display its version number with a link-arg.
16-
let output = rustc()
17-
.arg("-Zlinker-features=+lld")
18-
.arg("-Clink-self-contained=+linker")
19-
.arg("-Zunstable-options")
20-
.arg("-Wlinker-messages")
21-
.link_arg(linker_version_flag)
22-
.input("main.rs")
23-
.run();
24-
assert!(
25-
find_lld_version_in_logs(output.stderr_utf8()),
26-
"the LLD version string should be present in the output logs:\n{}",
27-
output.stderr_utf8()
13+
assert_rustc_uses_lld(
14+
rustc()
15+
.arg("-Zlinker-features=+lld")
16+
.arg("-Clink-self-contained=+linker")
17+
.arg("-Zunstable-options")
18+
.input("main.rs"),
2819
);
2920

3021
// It should not be used when we explicitly opt-out of lld.
31-
let output = rustc()
32-
.link_arg(linker_version_flag)
33-
.arg("-Zlinker-features=-lld")
34-
.arg("-Wlinker-messages")
35-
.input("main.rs")
36-
.run();
37-
assert!(
38-
!find_lld_version_in_logs(output.stderr_utf8()),
39-
"the LLD version string should not be present in the output logs:\n{}",
40-
output.stderr_utf8()
41-
);
22+
assert_rustc_doesnt_use_lld(rustc().arg("-Zlinker-features=-lld").input("main.rs"));
4223

4324
// While we're here, also check that the last linker feature flag "wins" when passed multiple
4425
// times to rustc.
45-
let output = rustc()
46-
.link_arg(linker_version_flag)
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-
.arg("-Wlinker-messages")
53-
.input("main.rs")
54-
.run();
55-
assert!(
56-
find_lld_version_in_logs(output.stderr_utf8()),
57-
"the LLD version string should be present in the output logs:\n{}",
58-
output.stderr_utf8()
26+
assert_rustc_uses_lld(
27+
rustc()
28+
.arg("-Clink-self-contained=+linker")
29+
.arg("-Zunstable-options")
30+
.arg("-Zlinker-features=-lld")
31+
.arg("-Zlinker-features=+lld")
32+
.arg("-Zlinker-features=-lld,+lld")
33+
.input("main.rs"),
5934
);
6035
}
61-
62-
fn find_lld_version_in_logs(stderr: String) -> bool {
63-
// Strip the `-Wlinker-messages` wrappers prefixing the linker output.
64-
let stderr = Regex::new(r"warning: linker std(out|err):").unwrap().replace_all(&stderr, "");
65-
let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
66-
stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
67-
}

0 commit comments

Comments
 (0)