Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port the 2 rust-lld run-make tests to rmake #123975

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3342,6 +3342,7 @@ name = "run_make_support"
version = "0.0.0"
dependencies = [
"object 0.34.0",
"regex",
"wasmparser",
]

Expand Down
1 change: 1 addition & 0 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
"needs-profiler-support",
"needs-relocation-model-pic",
"needs-run-enabled",
"needs-rust-lld",
"needs-rust-lldb",
"needs-sanitizer-address",
"needs-sanitizer-cfi",
Expand Down
1 change: 1 addition & 0 deletions src/tools/run-make-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
object = "0.34.0"
wasmparser = "0.118.2"
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
1 change: 1 addition & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Output};

pub use object;
pub use regex;
pub use wasmparser;

pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
Expand Down
9 changes: 7 additions & 2 deletions src/tools/run-make-support/src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ impl Rustc {
self
}

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

/// Add an extra argument to the linker invocation, via `-Clink-arg`.
pub fn link_arg(&mut self, link_arg: &str) -> &mut Self {
self.cmd.arg(format!("-Clink-arg={link_arg}"));
self
}

#[track_caller]
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
let caller_location = std::panic::Location::caller();
Expand Down
2 changes: 0 additions & 2 deletions src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ run-make/rlib-format-packed-bundled-libs-2/Makefile
run-make/rlib-format-packed-bundled-libs-3/Makefile
run-make/rlib-format-packed-bundled-libs/Makefile
run-make/rmeta-preferred/Makefile
run-make/rust-lld-custom-target/Makefile
run-make/rust-lld/Makefile
run-make/rustc-macro-dep-files/Makefile
run-make/rustdoc-determinism/Makefile
run-make/rustdoc-error-lines/Makefile
Expand Down
7 changes: 0 additions & 7 deletions tests/run-make/rust-lld-custom-target/Makefile

This file was deleted.

51 changes: 51 additions & 0 deletions tests/run-make/rust-lld-custom-target/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Test linking using `cc` with `rust-lld`, using a custom target with features described in MCP 510
// see https://github.com/rust-lang/compiler-team/issues/510 for more info:
//
// Starting from the `x86_64-unknown-linux-gnu` target spec, we add the following options:
// - a linker-flavor using lld via a C compiler
// - the self-contained linker component is enabled

//@ needs-rust-lld
//@ only-x86_64-unknown-linux-gnu

extern crate run_make_support;

use run_make_support::regex::Regex;
use run_make_support::rustc;
use std::process::Output;

fn main() {
// Compile to a custom target spec with rust-lld enabled by default. We'll check that by asking
// the linker to display its version number with a link-arg.
let output = rustc()
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
.crate_type("cdylib")
.target("custom-target.json")
.link_arg("-Wl,-v")
.input("lib.rs")
.run();
assert!(
find_lld_version_in_logs(output),
"the LLD version string should be present in the output logs"
);

// But it can also be disabled via linker features.
let output = rustc()
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
.crate_type("cdylib")
.target("custom-target.json")
.arg("-Zlinker-features=-lld")
.link_arg("-Wl,-v")
.input("lib.rs")
.run();
assert!(
!find_lld_version_in_logs(output),
"the LLD version string should not be present in the output logs"
);
}

fn find_lld_version_in_logs(output: Output) -> bool {
let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
let stderr = std::str::from_utf8(&output.stderr).unwrap();
stderr.lines().any(|line| lld_version_re.is_match(line))
}
12 changes: 0 additions & 12 deletions tests/run-make/rust-lld/Makefile

This file was deleted.

64 changes: 64 additions & 0 deletions tests/run-make/rust-lld/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Test linking using `cc` with `rust-lld`, using the unstable CLI described in MCP 510
// see https://github.com/rust-lang/compiler-team/issues/510 for more info

//@ needs-rust-lld
//@ ignore-msvc
//@ ignore-s390x lld does not yet support s390x as target

extern crate run_make_support;

use run_make_support::regex::Regex;
use run_make_support::rustc;
use std::process::Output;

fn main() {
// Opt-in to lld and the self-contained linker, to link with rust-lld. We'll check that by
// asking the linker to display its version number with a link-arg.
let output = rustc()
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
.arg("-Zlinker-features=+lld")
.arg("-Clink-self-contained=+linker")
.arg("-Zunstable-options")
.link_arg("-Wl,-v")
.input("main.rs")
.run();
assert!(
find_lld_version_in_logs(output),
"the LLD version string should be present in the output logs"
);

// It should not be used when we explictly opt-out of lld.
let output = rustc()
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
.link_arg("-Wl,-v")
.arg("-Zlinker-features=-lld")
.input("main.rs")
.run();
assert!(
!find_lld_version_in_logs(output),
"the LLD version string should not be present in the output logs"
);

// While we're here, also check that the last linker feature flag "wins" when passed multiple
// times to rustc.
let output = rustc()
.env("RUSTC_LOG", "rustc_codegen_ssa::back::link=info")
.link_arg("-Wl,-v")
.arg("-Clink-self-contained=+linker")
.arg("-Zunstable-options")
.arg("-Zlinker-features=-lld")
.arg("-Zlinker-features=+lld")
.arg("-Zlinker-features=-lld,+lld")
.input("main.rs")
.run();
assert!(
find_lld_version_in_logs(output),
"the LLD version string should be present in the output logs"
);
}

fn find_lld_version_in_logs(output: Output) -> bool {
let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
let stderr = std::str::from_utf8(&output.stderr).unwrap();
stderr.lines().any(|line| lld_version_re.is_match(line))
}
Loading