Skip to content

Commit 12cdbba

Browse files
committed
move exit-code to rmake
1 parent 5b7343b commit 12cdbba

File tree

4 files changed

+191
-16
lines changed

4 files changed

+191
-16
lines changed

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

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
use std::env;
22
use std::path::{Path, PathBuf};
33
use std::process::{Command, Output};
4-
4+
mod rustdoc;
5+
pub use rustdoc::rustdoc;
56
pub use wasmparser;
67

8+
pub fn add_host_rpath_env(cmd: &mut Command) {
9+
let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
10+
let ld_lib_path_value = env::var(&ld_lib_path_envvar).unwrap();
11+
12+
let temp = env::var("TMPDIR").unwrap();
13+
let host_rpath_dir = env::var("HOST_RPATH_DIR").unwrap();
14+
15+
cmd.env(ld_lib_path_envvar, format!("{temp}:{host_rpath_dir}:{ld_lib_path_value}"));
16+
}
17+
718
pub fn out_dir() -> PathBuf {
819
env::var_os("TMPDIR").unwrap().into()
920
}
1021

11-
fn setup_common_build_cmd() -> Command {
22+
fn setup_common_rustc_build_cmd() -> Command {
1223
let rustc = env::var("RUSTC").unwrap();
1324
let mut cmd = Command::new(rustc);
25+
add_host_rpath_env(&mut cmd);
1426
cmd.arg("--out-dir").arg(out_dir()).arg("-L").arg(out_dir());
1527
cmd
1628
}
@@ -39,7 +51,7 @@ pub struct RustcInvocationBuilder {
3951

4052
impl RustcInvocationBuilder {
4153
fn new() -> Self {
42-
let cmd = setup_common_build_cmd();
54+
let cmd = setup_common_rustc_build_cmd();
4355
Self { cmd }
4456
}
4557

@@ -53,6 +65,11 @@ impl RustcInvocationBuilder {
5365
self
5466
}
5567

68+
pub fn env(&mut self, key: &str, value: &str) -> &mut RustcInvocationBuilder {
69+
self.cmd.env(key, value);
70+
self
71+
}
72+
5673
#[track_caller]
5774
pub fn run(&mut self) -> Output {
5875
let caller_location = std::panic::Location::caller();
@@ -64,6 +81,30 @@ impl RustcInvocationBuilder {
6481
}
6582
output
6683
}
84+
85+
#[track_caller]
86+
pub fn run_fail(&mut self) -> Output {
87+
let caller_location = std::panic::Location::caller();
88+
let caller_line_number = caller_location.line();
89+
90+
let output = self.cmd.output().unwrap();
91+
if output.status.success() {
92+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
93+
}
94+
output
95+
}
96+
97+
#[track_caller]
98+
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
99+
let caller_location = std::panic::Location::caller();
100+
let caller_line_number = caller_location.line();
101+
102+
let output = self.cmd.output().unwrap();
103+
if output.status.code().unwrap() != code {
104+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
105+
}
106+
output
107+
}
67108
}
68109

69110
#[derive(Debug)]
@@ -73,7 +114,7 @@ pub struct AuxBuildInvocationBuilder {
73114

74115
impl AuxBuildInvocationBuilder {
75116
fn new() -> Self {
76-
let mut cmd = setup_common_build_cmd();
117+
let mut cmd = setup_common_rustc_build_cmd();
77118
cmd.arg("--crate-type=lib");
78119
Self { cmd }
79120
}
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use std::env;
2+
use std::path::Path;
3+
use std::process::{Command, Output};
4+
5+
use crate::{add_host_rpath_env, handle_failed_output};
6+
7+
pub fn rustdoc() -> RustdocInvocationBuilder {
8+
RustdocInvocationBuilder::new()
9+
}
10+
11+
#[derive(Debug)]
12+
pub struct RustdocInvocationBuilder {
13+
cmd: Command,
14+
}
15+
16+
impl RustdocInvocationBuilder {
17+
fn new() -> Self {
18+
let cmd = setup_common_rustdoc_build_cmd();
19+
Self { cmd }
20+
}
21+
22+
pub fn arg(&mut self, arg: &str) -> &mut RustdocInvocationBuilder {
23+
self.cmd.arg(arg);
24+
self
25+
}
26+
27+
pub fn arg_file(&mut self, arg: &Path) -> &mut RustdocInvocationBuilder {
28+
self.cmd.arg(arg);
29+
self
30+
}
31+
32+
pub fn env(&mut self, key: &str, value: &str) -> &mut RustdocInvocationBuilder {
33+
self.cmd.env(key, value);
34+
self
35+
}
36+
37+
#[track_caller]
38+
pub fn run(&mut self) -> Output {
39+
let caller_location = std::panic::Location::caller();
40+
let caller_line_number = caller_location.line();
41+
42+
let output = self.cmd.output().unwrap();
43+
if !output.status.success() {
44+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
45+
}
46+
output
47+
}
48+
49+
#[track_caller]
50+
pub fn run_fail(&mut self) -> Output {
51+
let caller_location = std::panic::Location::caller();
52+
let caller_line_number = caller_location.line();
53+
54+
let output = self.cmd.output().unwrap();
55+
if output.status.success() {
56+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
57+
}
58+
output
59+
}
60+
61+
#[track_caller]
62+
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
63+
let caller_location = std::panic::Location::caller();
64+
let caller_line_number = caller_location.line();
65+
66+
let output = self.cmd.output().unwrap();
67+
if output.status.code().unwrap() != code {
68+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
69+
}
70+
output
71+
}
72+
}
73+
74+
fn setup_common_rustdoc_build_cmd() -> Command {
75+
use std::env::VarError;
76+
77+
let rustdoc = env::var("RUSTDOC").unwrap();
78+
let target_rpath_dir = env::var("TARGET_RPATH_DIR").unwrap();
79+
80+
//let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
81+
// let ld_lib_path_value = env::var(&ld_lib_path_envvar).unwrap();
82+
83+
//let temp = env::var("TMPDIR").unwrap();
84+
//let host_rpath_dir = env::var("HOST_RPATH_DIR").unwrap();
85+
86+
let mut cmd = Command::new(rustdoc);
87+
88+
// cmd.env(ld_lib_path_envvar, format!("{temp}:{host_rpath_dir}:{ld_lib_path_value}"));
89+
90+
add_host_rpath_env(&mut cmd);
91+
92+
cmd.arg("-L").arg(target_rpath_dir);
93+
94+
match std::env::var("RUSTC_LINKER") {
95+
Ok(rustc_linker) => {
96+
cmd.arg(&format!("-Clinker='{rustc_linker}'"));
97+
}
98+
Err(VarError::NotPresent) => {}
99+
Err(VarError::NotUnicode(s)) => {
100+
panic!("RUSTC_LINKER was found, but set to non-unicode string {s:?}");
101+
}
102+
}
103+
104+
cmd
105+
}

tests/run-make/exit-code/Makefile

-12
This file was deleted.

tests/run-make/exit-code/rmake.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{rustc, rustdoc, out_dir};
4+
5+
fn main() {
6+
rustc()
7+
.arg("success.rs")
8+
.run();
9+
10+
rustc()
11+
.arg("--invalid-arg-foo")
12+
.run_fail_assert_exit_code(1);
13+
14+
rustc()
15+
.arg("compile-error.rs")
16+
.run_fail_assert_exit_code(1);
17+
18+
rustc()
19+
.env("RUSTC_ICE", "0")
20+
.arg("-Ztreat-err-as-bug")
21+
.arg("compile-error.rs")
22+
.run_fail_assert_exit_code(101);
23+
24+
rustdoc()
25+
.arg("-o")
26+
.arg_file(&out_dir().join("exit-code"))
27+
.arg("success.rs")
28+
.run();
29+
30+
rustdoc()
31+
.arg("--invalid-arg-foo")
32+
.run_fail_assert_exit_code(1);
33+
34+
rustdoc()
35+
.arg("compile-error.rs")
36+
.run_fail_assert_exit_code(1);
37+
38+
rustdoc()
39+
.arg("lint-failure.rs")
40+
.run_fail_assert_exit_code(1);
41+
}

0 commit comments

Comments
 (0)