Skip to content

Commit 552eb4f

Browse files
committed
move exit-code to rmake
1 parent c3b05c6 commit 552eb4f

File tree

4 files changed

+193
-16
lines changed

4 files changed

+193
-16
lines changed

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

+53-4
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

12-
fn setup_common_build_cmd() -> Command {
13-
let rustc = env::var("RUSTC").unwrap();
31+
fn setup_common_rustc_build_cmd() -> Command {
32+
let rustc = env::var_os("RUSTC").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
}
@@ -40,7 +60,7 @@ pub struct RustcInvocationBuilder {
4060

4161
impl RustcInvocationBuilder {
4262
fn new() -> Self {
43-
let cmd = setup_common_build_cmd();
63+
let cmd = setup_common_rustc_build_cmd();
4464
Self { cmd }
4565
}
4666

@@ -54,6 +74,11 @@ impl RustcInvocationBuilder {
5474
self
5575
}
5676

77+
pub fn env(&mut self, key: &str, value: &str) -> &mut RustcInvocationBuilder {
78+
self.cmd.env(key, value);
79+
self
80+
}
81+
5782
#[track_caller]
5883
pub fn run(&mut self) -> Output {
5984
let caller_location = std::panic::Location::caller();
@@ -65,6 +90,30 @@ impl RustcInvocationBuilder {
6590
}
6691
output
6792
}
93+
94+
#[track_caller]
95+
pub fn run_fail(&mut self) -> Output {
96+
let caller_location = std::panic::Location::caller();
97+
let caller_line_number = caller_location.line();
98+
99+
let output = self.cmd.output().unwrap();
100+
if output.status.success() {
101+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
102+
}
103+
output
104+
}
105+
106+
#[track_caller]
107+
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
108+
let caller_location = std::panic::Location::caller();
109+
let caller_line_number = caller_location.line();
110+
111+
let output = self.cmd.output().unwrap();
112+
if output.status.code().unwrap() != code {
113+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
114+
}
115+
output
116+
}
68117
}
69118

70119
#[derive(Debug)]
@@ -74,7 +123,7 @@ pub struct AuxBuildInvocationBuilder {
74123

75124
impl AuxBuildInvocationBuilder {
76125
fn new() -> Self {
77-
let mut cmd = setup_common_build_cmd();
126+
let mut cmd = setup_common_rustc_build_cmd();
78127
cmd.arg("--crate-type=lib");
79128
Self { cmd }
80129
}
+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+
rustdoc()
27+
.arg("-o")
28+
.arg_file(&out_dir().join("exit-code"))
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)