diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 419b04231b572..346d63b1bffff 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -3,15 +3,35 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Output}; pub use object; +// mod rustdoc; +// pub use rustdoc::rustdoc; pub use wasmparser; +pub fn add_host_rpath_env(cmd: &mut Command) { + let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap(); + let ld_lib_path_value = env::var(&ld_lib_path_envvar).unwrap(); + + let temp = env::var_os("TMPDIR").unwrap(); + let host_rpath_dir = env::var_os("HOST_RPATH_DIR").unwrap(); + + let mut paths = Vec::from([temp, host_rpath_dir]); + for p in env::split_paths(&ld_lib_path_value) { + paths.push(p.into_os_string()); + } + + let path = std::env::join_paths(paths).unwrap(); + + cmd.env(ld_lib_path_envvar, path); +} + pub fn out_dir() -> PathBuf { env::var_os("TMPDIR").unwrap().into() } fn setup_common_build_cmd(command: &str) -> Command { - let rustc = env::var(command).unwrap(); + let rustc = env::var_os(command).unwrap(); let mut cmd = Command::new(rustc); + add_host_rpath_env(&mut cmd); cmd.arg("--out-dir").arg(out_dir()).arg("-L").arg(out_dir()); cmd } @@ -58,6 +78,11 @@ impl RustcInvocationBuilder { self } + pub fn env(&mut self, key: &str, value: &str) -> &mut RustcInvocationBuilder { + self.cmd.env(key, value); + self + } + #[track_caller] pub fn run(&mut self) -> Output { let caller_location = std::panic::Location::caller(); @@ -69,6 +94,30 @@ impl RustcInvocationBuilder { } output } + + #[track_caller] + pub fn run_fail(&mut self) -> Output { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.cmd.output().unwrap(); + if output.status.success() { + handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); + } + output + } + + #[track_caller] + pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.cmd.output().unwrap(); + if output.status.code().unwrap() != code { + handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); + } + output + } } #[derive(Debug)] @@ -117,6 +166,11 @@ impl Rustdoc { self } + pub fn arg_file(&mut self, arg: &Path) -> &mut Self { + self.cmd.arg(arg); + self + } + #[track_caller] pub fn run(&mut self) -> Output { let caller_location = std::panic::Location::caller(); @@ -128,6 +182,18 @@ impl Rustdoc { } output } + + #[track_caller] + pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.cmd.output().unwrap(); + if output.status.code().unwrap() != code { + handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); + } + output + } } fn run_common(bin_name: &str) -> (Command, Output) { diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs new file mode 100644 index 0000000000000..7f3ca66ee3158 --- /dev/null +++ b/src/tools/run-make-support/src/rustdoc.rs @@ -0,0 +1,97 @@ +use std::env; +use std::path::Path; +use std::process::{Command, Output}; + +use crate::{add_host_rpath_env, handle_failed_output}; + +pub fn rustdoc() -> RustdocInvocationBuilder { + RustdocInvocationBuilder::new() +} + +#[derive(Debug)] +pub struct RustdocInvocationBuilder { + cmd: Command, +} + +impl RustdocInvocationBuilder { + fn new() -> Self { + let cmd = setup_common_rustdoc_build_cmd(); + Self { cmd } + } + + pub fn arg(&mut self, arg: &str) -> &mut RustdocInvocationBuilder { + self.cmd.arg(arg); + self + } + + pub fn arg_file(&mut self, arg: &Path) -> &mut RustdocInvocationBuilder { + self.cmd.arg(arg); + self + } + + pub fn env(&mut self, key: &str, value: &str) -> &mut RustdocInvocationBuilder { + self.cmd.env(key, value); + self + } + + #[track_caller] + pub fn run(&mut self) -> Output { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.cmd.output().unwrap(); + if !output.status.success() { + handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); + } + output + } + + #[track_caller] + pub fn run_fail(&mut self) -> Output { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.cmd.output().unwrap(); + if output.status.success() { + handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); + } + output + } + + #[track_caller] + pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let output = self.cmd.output().unwrap(); + if output.status.code().unwrap() != code { + handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); + } + output + } +} + +fn setup_common_rustdoc_build_cmd() -> Command { + use std::env::VarError; + + let rustdoc = env::var("RUSTDOC").unwrap(); + let target_rpath_dir = env::var("TARGET_RPATH_DIR").unwrap(); + + let mut cmd = Command::new(rustdoc); + + add_host_rpath_env(&mut cmd); + + cmd.arg("-L").arg(target_rpath_dir); + + match std::env::var("RUSTC_LINKER") { + Ok(rustc_linker) => { + cmd.arg(&format!("-Clinker='{rustc_linker}'")); + } + Err(VarError::NotPresent) => {} + Err(VarError::NotUnicode(s)) => { + panic!("RUSTC_LINKER was found, but set to non-unicode string {s:?}"); + } + } + + cmd +} diff --git a/tests/run-make/exit-code/Makefile b/tests/run-make/exit-code/Makefile deleted file mode 100644 index 155e5cd112344..0000000000000 --- a/tests/run-make/exit-code/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -all: - $(RUSTC) success.rs; [ $$? -eq 0 ] - $(RUSTC) --invalid-arg-foo; [ $$? -eq 1 ] - $(RUSTC) compile-error.rs; [ $$? -eq 1 ] - RUSTC_ICE=0 $(RUSTC) -Ztreat-err-as-bug compile-error.rs; [ $$? -eq 101 ] - $(RUSTDOC) -o $(TMPDIR)/exit-code success.rs; [ $$? -eq 0 ] - $(RUSTDOC) --invalid-arg-foo; [ $$? -eq 1 ] - $(RUSTDOC) compile-error.rs; [ $$? -eq 1 ] - $(RUSTDOC) lint-failure.rs; [ $$? -eq 1 ] diff --git a/tests/run-make/exit-code/rmake.rs b/tests/run-make/exit-code/rmake.rs new file mode 100644 index 0000000000000..e5b112e4449b4 --- /dev/null +++ b/tests/run-make/exit-code/rmake.rs @@ -0,0 +1,43 @@ +// Test that we exit with the correct exit code for successful / unsuccessful / ICE compilations + +extern crate run_make_support; + +use run_make_support::{rustc, rustdoc, out_dir}; + +fn main() { + rustc() + .arg("success.rs") + .run(); + + rustc() + .arg("--invalid-arg-foo") + .run_fail_assert_exit_code(1); + + rustc() + .arg("compile-error.rs") + .run_fail_assert_exit_code(1); + + rustc() + .env("RUSTC_ICE", "0") + .arg("-Ztreat-err-as-bug") + .arg("compile-error.rs") + .run_fail_assert_exit_code(101); + + std::fs::remove_file(out_dir().join("success")).unwrap(); + + rustdoc() + .arg("success.rs") + .run(); + + rustdoc() + .arg("--invalid-arg-foo") + .run_fail_assert_exit_code(1); + + rustdoc() + .arg("compile-error.rs") + .run_fail_assert_exit_code(1); + + rustdoc() + .arg("lint-failure.rs") + .run_fail_assert_exit_code(1); +}