Skip to content

Commit c1f2ed1

Browse files
authored
Rollup merge of rust-lang#123729 - jieyouxu:rmake-refactor-2, r=oli-obk
run-make: refactor out command wrappers for `clang` and `llvm-readobj` This PR is rebased on top of rust-lang#123699. This PR is a follow up to rust-lang#123612 to refactor out command wrappers into the support library for `llvm-readobj` and `clang`. r? ghost
2 parents 43852d0 + ca945e9 commit c1f2ed1

File tree

4 files changed

+140
-42
lines changed

4 files changed

+140
-42
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::env;
2+
use std::path::Path;
3+
use std::process::Command;
4+
5+
use crate::{bin_name, handle_failed_output, tmp_dir};
6+
7+
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
8+
pub fn clang() -> Clang {
9+
Clang::new()
10+
}
11+
12+
/// A `clang` invocation builder.
13+
#[derive(Debug)]
14+
pub struct Clang {
15+
cmd: Command,
16+
}
17+
18+
crate::impl_common_helpers!(Clang);
19+
20+
impl Clang {
21+
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
22+
pub fn new() -> Self {
23+
let clang =
24+
env::var("CLANG").expect("`CLANG` not specified, but this is required to find `clang`");
25+
let cmd = Command::new(clang);
26+
Self { cmd }
27+
}
28+
29+
/// Provide an input file.
30+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
31+
self.cmd.arg(path.as_ref());
32+
self
33+
}
34+
35+
/// Specify the name of the executable. The executable will be placed under `$TMPDIR`, and the
36+
/// extension will be determined by [`bin_name`].
37+
pub fn out_exe(&mut self, name: &str) -> &mut Self {
38+
self.cmd.arg("-o");
39+
self.cmd.arg(tmp_dir().join(bin_name(name)));
40+
self
41+
}
42+
43+
/// Specify which target triple clang should target.
44+
pub fn target(&mut self, target_triple: &str) -> &mut Self {
45+
self.cmd.arg("-target");
46+
self.cmd.arg(target_triple);
47+
self
48+
}
49+
50+
/// Pass `-nostdlib` to disable linking the C standard library.
51+
pub fn no_stdlib(&mut self) -> &mut Self {
52+
self.cmd.arg("-nostdlib");
53+
self
54+
}
55+
56+
/// Specify architecture.
57+
pub fn arch(&mut self, arch: &str) -> &mut Self {
58+
self.cmd.arg(format!("-march={arch}"));
59+
self
60+
}
61+
62+
/// Specify LTO settings.
63+
pub fn lto(&mut self, lto: &str) -> &mut Self {
64+
self.cmd.arg(format!("-flto={lto}"));
65+
self
66+
}
67+
68+
/// Specify which ld to use.
69+
pub fn use_ld(&mut self, ld: &str) -> &mut Self {
70+
self.cmd.arg(format!("-fuse-ld={ld}"));
71+
self
72+
}
73+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! as `object` or `wasmparser`, they can be re-exported and be made available through this library.
55
66
pub mod cc;
7+
pub mod clang;
8+
pub mod llvm_readobj;
79
pub mod run;
810
pub mod rustc;
911
pub mod rustdoc;
@@ -17,6 +19,8 @@ pub use regex;
1719
pub use wasmparser;
1820

1921
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
22+
pub use clang::{clang, Clang};
23+
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
2024
pub use run::{run, run_fail};
2125
pub use rustc::{aux_build, rustc, Rustc};
2226
pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::env;
2+
use std::path::{Path, PathBuf};
3+
use std::process::Command;
4+
5+
use crate::handle_failed_output;
6+
7+
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available
8+
/// at `$LLVM_BIN_DIR/llvm-readobj`.
9+
pub fn llvm_readobj() -> LlvmReadobj {
10+
LlvmReadobj::new()
11+
}
12+
13+
/// A `llvm-readobj` invocation builder.
14+
#[derive(Debug)]
15+
pub struct LlvmReadobj {
16+
cmd: Command,
17+
}
18+
19+
crate::impl_common_helpers!(LlvmReadobj);
20+
21+
impl LlvmReadobj {
22+
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available
23+
/// at `$LLVM_BIN_DIR/llvm-readobj`.
24+
pub fn new() -> Self {
25+
let llvm_bin_dir = env::var("LLVM_BIN_DIR")
26+
.expect("`LLVM_BIN_DIR` not specified, but this is required to find `llvm-readobj`");
27+
let llvm_bin_dir = PathBuf::from(llvm_bin_dir);
28+
let llvm_readobj = llvm_bin_dir.join("llvm-readobj");
29+
let cmd = Command::new(llvm_readobj);
30+
Self { cmd }
31+
}
32+
33+
/// Provide an input file.
34+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
35+
self.cmd.arg(path.as_ref());
36+
self
37+
}
38+
39+
/// Pass `--file-header` to display file headers.
40+
pub fn file_header(&mut self) -> &mut Self {
41+
self.cmd.arg("--file-header");
42+
self
43+
}
44+
}

tests/run-make/cross-lang-lto-riscv-abi/rmake.rs

+19-42
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
//! Make sure that cross-language LTO works on riscv targets,
2-
//! which requires extra abi metadata to be emitted.
2+
//! which requires extra `target-abi` metadata to be emitted.
33
//@ needs-matching-clang
44
//@ needs-llvm-components riscv
55
extern crate run_make_support;
66

7-
use run_make_support::{bin_name, rustc, tmp_dir};
7+
use run_make_support::{bin_name, clang, llvm_readobj, rustc, tmp_dir};
88
use std::{
99
env,
1010
path::PathBuf,
1111
process::{Command, Output},
1212
str,
1313
};
1414

15-
fn handle_failed_output(output: Output) {
16-
eprintln!("output status: `{}`", output.status);
17-
eprintln!("=== STDOUT ===\n{}\n\n", String::from_utf8(output.stdout).unwrap());
18-
eprintln!("=== STDERR ===\n{}\n\n", String::from_utf8(output.stderr).unwrap());
19-
std::process::exit(1)
20-
}
21-
2215
fn check_target(target: &str, clang_target: &str, carch: &str, is_double_float: bool) {
2316
eprintln!("Checking target {target}");
2417
// Rust part
@@ -30,40 +23,24 @@ fn check_target(target: &str, clang_target: &str, carch: &str, is_double_float:
3023
.linker_plugin_lto("on")
3124
.run();
3225
// C part
33-
let clang = env::var("CLANG").unwrap();
34-
let mut cmd = Command::new(clang);
35-
let executable = tmp_dir().join("riscv-xlto");
36-
cmd.arg("-target")
37-
.arg(clang_target)
38-
.arg(format!("-march={carch}"))
39-
.arg(format!("-flto=thin"))
40-
.arg(format!("-fuse-ld=lld"))
41-
.arg("-nostdlib")
42-
.arg("-o")
43-
.arg(&executable)
44-
.arg("cstart.c")
45-
.arg(tmp_dir().join("libriscv_xlto.rlib"));
46-
eprintln!("{cmd:?}");
47-
let output = cmd.output().unwrap();
48-
if !output.status.success() {
49-
handle_failed_output(output);
50-
}
26+
clang()
27+
.target(clang_target)
28+
.arch(carch)
29+
.lto("thin")
30+
.use_ld("lld")
31+
.no_stdlib()
32+
.out_exe("riscv-xlto")
33+
.input("cstart.c")
34+
.input(tmp_dir().join("libriscv_xlto.rlib"))
35+
.run();
36+
5137
// Check that the built binary has correct float abi
52-
let llvm_readobj =
53-
PathBuf::from(env::var("LLVM_BIN_DIR").unwrap()).join(bin_name("llvm-readobj"));
54-
let mut cmd = Command::new(llvm_readobj);
55-
cmd.arg("--file-header").arg(executable);
56-
eprintln!("{cmd:?}");
57-
let output = cmd.output().unwrap();
58-
if output.status.success() {
59-
assert!(
60-
!(is_double_float
61-
^ dbg!(str::from_utf8(&output.stdout).unwrap())
62-
.contains("EF_RISCV_FLOAT_ABI_DOUBLE"))
63-
)
64-
} else {
65-
handle_failed_output(output);
66-
}
38+
let executable = tmp_dir().join(bin_name("riscv-xlto"));
39+
let output = llvm_readobj().input(&executable).file_header().run();
40+
let stdout = String::from_utf8_lossy(&output.stdout);
41+
eprintln!("obj:\n{}", stdout);
42+
43+
assert!(!(is_double_float ^ stdout.contains("EF_RISCV_FLOAT_ABI_DOUBLE")));
6744
}
6845

6946
fn main() {

0 commit comments

Comments
 (0)