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
7pub(crate) fn verbose_print_command(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
19/// If a given [`Command`] failed (as indicated by its [`CompletedProcess`]), verbose print the
20/// executed command, failure location, output status and stdout/stderr, and abort the process with
21/// exit code `1`.
22pub(crate) fn handle_failed_output(
23    cmd: &Command,
24    output: CompletedProcess,
25    caller_line_number: u32,
26) -> ! {
27    if output.status().success() {
28        eprintln!("command unexpectedly succeeded at line {caller_line_number}");
29    } else {
30        eprintln!("command failed at line {caller_line_number}");
31    }
32    verbose_print_command(cmd, &output);
33    std::process::exit(1)
34}
35
36/// Set the runtime library paths as needed for running the host compilers (rustc/rustdoc/etc).
37pub(crate) fn set_host_compiler_dylib_path(cmd: &mut Command) {
38    let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
39    cmd.env(&ld_lib_path_envvar, {
40        let mut paths = vec![];
41        paths.push(cwd());
42        paths.push(PathBuf::from(env_var("HOST_RUSTC_DYLIB_PATH")));
43        for p in std::env::split_paths(&env_var(&ld_lib_path_envvar)) {
44            paths.push(p.to_path_buf());
45        }
46        std::env::join_paths(paths.iter()).unwrap()
47    });
48}