Skip to content

Commit

Permalink
move exit-code to rmake
Browse files Browse the repository at this point in the history
  • Loading branch information
5225225 committed Mar 23, 2024
1 parent c3b05c6 commit 552eb4f
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 16 deletions.
57 changes: 53 additions & 4 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
let rustc = env::var("RUSTC").unwrap();
fn setup_common_rustc_build_cmd() -> Command {
let rustc = env::var_os("RUSTC").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
}
Expand Down Expand Up @@ -40,7 +60,7 @@ pub struct RustcInvocationBuilder {

impl RustcInvocationBuilder {
fn new() -> Self {
let cmd = setup_common_build_cmd();
let cmd = setup_common_rustc_build_cmd();
Self { cmd }
}

Expand All @@ -54,6 +74,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();
Expand All @@ -65,6 +90,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)]
Expand All @@ -74,7 +123,7 @@ pub struct AuxBuildInvocationBuilder {

impl AuxBuildInvocationBuilder {
fn new() -> Self {
let mut cmd = setup_common_build_cmd();
let mut cmd = setup_common_rustc_build_cmd();
cmd.arg("--crate-type=lib");
Self { cmd }
}
Expand Down
97 changes: 97 additions & 0 deletions src/tools/run-make-support/src/rustdoc.rs
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 0 additions & 12 deletions tests/run-make/exit-code/Makefile

This file was deleted.

43 changes: 43 additions & 0 deletions tests/run-make/exit-code/rmake.rs
Original file line number Diff line number Diff line change
@@ -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);

rustdoc()
.arg("-o")
.arg_file(&out_dir().join("exit-code"))
.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);
}

0 comments on commit 552eb4f

Please sign in to comment.