Skip to main content

run_make_support/
util.rs

1use std::path::PathBuf;
2
3use crate::command::{Command, CompletedProcess};
4use crate::env::env_var;
5use crate::path_helpers::cwd;
6
7fn print_command_output(cmd: &Command, output: &CompletedProcess) {
8    cmd.inspect(|std_cmd| {
9        eprintln!("{std_cmd:?}");
10    });
11    eprintln!("output status: `{}`", output.status());
12    eprintln!("=== STDOUT ===\n{}\n\n", output.stdout_utf8());
13    eprintln!("=== STDERR ===\n{}\n\n", output.stderr_utf8());
14    if !cmd.get_context().is_empty() {
15        eprintln!("Context:\n{}", cmd.get_context());
16    }
17}
18
19pub(crate) fn verbose_print_command(cmd: &Command, output: &CompletedProcess) {
20    // Only prints when `--verbose-run-make-subprocess-output` is active (env var set),
21    // so that passing tests don't flood the terminal when using `--no-capture`.
22    if std::env::var_os("__RMAKE_VERBOSE_SUBPROCESS_OUTPUT").is_none() {
23        return;
24    }
25    print_command_output(cmd, output);
26}
27
28/// If a given [`Command`] failed (as indicated by its [`CompletedProcess`]), verbose print the
29/// executed command, failure location, output status and stdout/stderr, and abort the process with
30/// exit code `1`.
31pub(crate) fn handle_failed_output(
32    cmd: &Command,
33    output: CompletedProcess,
34    caller_line_number: u32,
35) -> ! {
36    if output.status().success() {
37        eprintln!("command unexpectedly succeeded at line {caller_line_number}");
38    } else {
39        eprintln!("command failed at line {caller_line_number}");
40    }
41    print_command_output(cmd, &output);
42    std::process::exit(1)
43}
44
45/// Set the runtime library paths as needed for running the host compilers (rustc/rustdoc/etc).
46pub(crate) fn set_host_compiler_dylib_path(cmd: &mut Command) {
47    let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
48    cmd.env(&ld_lib_path_envvar, {
49        let mut paths = vec![];
50        paths.push(cwd());
51        paths.push(PathBuf::from(env_var("HOST_RUSTC_DYLIB_PATH")));
52        for p in std::env::split_paths(&env_var(&ld_lib_path_envvar)) {
53            paths.push(p.to_path_buf());
54        }
55        std::env::join_paths(paths.iter()).unwrap()
56    });
57}