Skip to content

Commit ca63ee5

Browse files
committed
move exit-code to rmake
1 parent 10a7aa1 commit ca63ee5

File tree

4 files changed

+207
-13
lines changed

4 files changed

+207
-13
lines changed

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

+67-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,35 @@ use std::path::{Path, PathBuf};
33
use std::process::{Command, Output};
44

55
pub use object;
6+
// mod rustdoc;
7+
// pub use rustdoc::rustdoc;
68
pub use wasmparser;
79

10+
pub fn add_host_rpath_env(cmd: &mut Command) {
11+
let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
12+
let ld_lib_path_value = env::var(&ld_lib_path_envvar).unwrap();
13+
14+
let temp = env::var_os("TMPDIR").unwrap();
15+
let host_rpath_dir = env::var_os("HOST_RPATH_DIR").unwrap();
16+
17+
let mut paths = Vec::from([temp, host_rpath_dir]);
18+
for p in env::split_paths(&ld_lib_path_value) {
19+
paths.push(p.into_os_string());
20+
}
21+
22+
let path = std::env::join_paths(paths).unwrap();
23+
24+
cmd.env(ld_lib_path_envvar, path);
25+
}
26+
827
pub fn out_dir() -> PathBuf {
928
env::var_os("TMPDIR").unwrap().into()
1029
}
1130

1231
fn setup_common_build_cmd(command: &str) -> Command {
13-
let rustc = env::var(command).unwrap();
32+
let rustc = env::var_os(command).unwrap();
1433
let mut cmd = Command::new(rustc);
34+
add_host_rpath_env(&mut cmd);
1535
cmd.arg("--out-dir").arg(out_dir()).arg("-L").arg(out_dir());
1636
cmd
1737
}
@@ -58,6 +78,11 @@ impl RustcInvocationBuilder {
5878
self
5979
}
6080

81+
pub fn env(&mut self, key: &str, value: &str) -> &mut RustcInvocationBuilder {
82+
self.cmd.env(key, value);
83+
self
84+
}
85+
6186
#[track_caller]
6287
pub fn run(&mut self) -> Output {
6388
let caller_location = std::panic::Location::caller();
@@ -69,6 +94,30 @@ impl RustcInvocationBuilder {
6994
}
7095
output
7196
}
97+
98+
#[track_caller]
99+
pub fn run_fail(&mut self) -> Output {
100+
let caller_location = std::panic::Location::caller();
101+
let caller_line_number = caller_location.line();
102+
103+
let output = self.cmd.output().unwrap();
104+
if output.status.success() {
105+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
106+
}
107+
output
108+
}
109+
110+
#[track_caller]
111+
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
112+
let caller_location = std::panic::Location::caller();
113+
let caller_line_number = caller_location.line();
114+
115+
let output = self.cmd.output().unwrap();
116+
if output.status.code().unwrap() != code {
117+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
118+
}
119+
output
120+
}
72121
}
73122

74123
#[derive(Debug)]
@@ -117,6 +166,11 @@ impl Rustdoc {
117166
self
118167
}
119168

169+
pub fn arg_file(&mut self, arg: &Path) -> &mut Self {
170+
self.cmd.arg(arg);
171+
self
172+
}
173+
120174
#[track_caller]
121175
pub fn run(&mut self) -> Output {
122176
let caller_location = std::panic::Location::caller();
@@ -128,6 +182,18 @@ impl Rustdoc {
128182
}
129183
output
130184
}
185+
186+
#[track_caller]
187+
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
188+
let caller_location = std::panic::Location::caller();
189+
let caller_line_number = caller_location.line();
190+
191+
let output = self.cmd.output().unwrap();
192+
if output.status.code().unwrap() != code {
193+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
194+
}
195+
output
196+
}
131197
}
132198

133199
fn run_common(bin_name: &str) -> (Command, Output) {
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 mut cmd = Command::new(rustdoc);
81+
82+
add_host_rpath_env(&mut cmd);
83+
84+
cmd.arg("-L").arg(target_rpath_dir);
85+
86+
match std::env::var("RUSTC_LINKER") {
87+
Ok(rustc_linker) => {
88+
cmd.arg(&format!("-Clinker='{rustc_linker}'"));
89+
}
90+
Err(VarError::NotPresent) => {}
91+
Err(VarError::NotUnicode(s)) => {
92+
panic!("RUSTC_LINKER was found, but set to non-unicode string {s:?}");
93+
}
94+
}
95+
96+
cmd
97+
}

tests/run-make/exit-code/Makefile

-12
This file was deleted.

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

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

0 commit comments

Comments
 (0)