Skip to content

Commit be9abed

Browse files
authored
Unrolled build for rust-lang#139322
Rollup merge of rust-lang#139322 - Kobzol:run-make-lld-refactor, r=jieyouxu Add helper function for checking LLD usage to `run-make-support` Extracted out of rust-lang#138645, should be a simple refactoring. r? ``@jieyouxu``
2 parents f174fd7 + 9ec11c2 commit be9abed

File tree

9 files changed

+69
-119
lines changed

9 files changed

+69
-119
lines changed

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/rmake.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,11 @@
44
//@ ignore-nightly
55
//@ only-x86_64-unknown-linux-gnu
66

7-
use std::process::Output;
8-
9-
use run_make_support::regex::Regex;
7+
use run_make_support::linker::assert_rustc_doesnt_use_lld;
108
use run_make_support::rustc;
119

1210
fn main() {
1311
// A regular compilation should not use rust-lld by default. We'll check that by asking the
1412
// linker to display its version number with a link-arg.
15-
let output = rustc().arg("-Wlinker-messages").link_arg("-Wl,-v").input("main.rs").run();
16-
assert!(
17-
!find_lld_version_in_logs(output.stderr_utf8()),
18-
"the LLD version string should not be present in the output logs:\n{}",
19-
output.stderr_utf8()
20-
);
21-
}
22-
23-
fn find_lld_version_in_logs(stderr: String) -> bool {
24-
let lld_version_re =
25-
Regex::new(r"^warning: linker stdout: LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
26-
stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
13+
assert_rustc_doesnt_use_lld(rustc().input("main.rs"));
2714
}

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

+3-24
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,14 @@
66
//@ ignore-stable
77
//@ only-x86_64-unknown-linux-gnu
88

9-
use run_make_support::regex::Regex;
9+
use run_make_support::linker::{assert_rustc_doesnt_use_lld, assert_rustc_uses_lld};
1010
use run_make_support::rustc;
1111

1212
fn main() {
1313
// A regular compilation should use rust-lld by default. We'll check that by asking the linker
1414
// to display its version number with a link-arg.
15-
let output = rustc().arg("-Wlinker-messages").link_arg("-Wl,-v").input("main.rs").run();
16-
assert!(
17-
find_lld_version_in_logs(output.stderr_utf8()),
18-
"the LLD version string should be present in the output logs:\n{}",
19-
output.stderr_utf8()
20-
);
15+
assert_rustc_uses_lld(rustc().input("main.rs"));
2116

2217
// But it can still be disabled by turning the linker feature off.
23-
let output = rustc()
24-
.arg("-Wlinker-messages")
25-
.link_arg("-Wl,-v")
26-
.arg("-Zlinker-features=-lld")
27-
.input("main.rs")
28-
.run();
29-
assert!(
30-
!find_lld_version_in_logs(output.stderr_utf8()),
31-
"the LLD version string should not be present in the output logs:\n{}",
32-
output.stderr_utf8()
33-
);
34-
}
35-
36-
fn find_lld_version_in_logs(stderr: String) -> bool {
37-
let lld_version_re =
38-
Regex::new(r"^warning: linker stdout: LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
39-
stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
18+
assert_rustc_doesnt_use_lld(rustc().arg("-Zlinker-features=-lld").input("main.rs"));
4019
}

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-
}

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

+18-50
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

30-
// 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-
);
21+
// It should not be used when we explicitly opt out of lld.
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)