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
7/// If a given [`Command`] failed (as indicated by its [`CompletedProcess`]), verbose print the
8/// executed command, failure location, output status and stdout/stderr, and abort the process with
9/// exit code `1`.
10pub(crate) fn handle_failed_output(
11    cmd: &Command,
12    output: CompletedProcess,
13    caller_line_number: u32,
14) -> ! {
15    if output.status().success() {
16        eprintln!("command unexpectedly succeeded at line {caller_line_number}");
17    } else {
18        eprintln!("command failed at line {caller_line_number}");
19    }
20    eprintln!("{cmd:?}");
21    eprintln!("output status: `{}`", output.status());
22    eprintln!("=== STDOUT ===\n{}\n\n", output.stdout_utf8());
23    eprintln!("=== STDERR ===\n{}\n\n", output.stderr_utf8());
24    std::process::exit(1)
25}
26
27/// Set the runtime library path as needed for running the host rustc/rustdoc/etc.
28pub(crate) fn set_host_rpath(cmd: &mut Command) {
29    let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
30    cmd.env(&ld_lib_path_envvar, {
31        let mut paths = vec![];
32        paths.push(cwd());
33        paths.push(PathBuf::from(env_var("HOST_RPATH_DIR")));
34        for p in std::env::split_paths(&env_var(&ld_lib_path_envvar)) {
35            paths.push(p.to_path_buf());
36        }
37        std::env::join_paths(paths.iter()).unwrap()
38    });
39}