run_make_support/
run.rs

1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3use std::{env, panic};
4
5use crate::command::{Command, CompletedProcess};
6use crate::util::{handle_failed_output, set_host_rpath};
7use crate::{cwd, env_var, is_windows};
8
9#[track_caller]
10fn run_common(name: &str, args: Option<&[&str]>) -> Command {
11    let mut bin_path = PathBuf::new();
12    bin_path.push(cwd());
13    bin_path.push(name);
14    let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
15    let mut cmd = Command::new(bin_path);
16    if let Some(args) = args {
17        for arg in args {
18            cmd.arg(arg);
19        }
20    }
21    cmd.env(&ld_lib_path_envvar, {
22        let mut paths = vec![];
23        paths.push(cwd());
24        for p in env::split_paths(&env_var("TARGET_RPATH_ENV")) {
25            paths.push(p.to_path_buf());
26        }
27        for p in env::split_paths(&env_var(&ld_lib_path_envvar)) {
28            paths.push(p.to_path_buf());
29        }
30        env::join_paths(paths.iter()).unwrap()
31    });
32    cmd.env("LC_ALL", "C"); // force english locale
33
34    if is_windows() {
35        let mut paths = vec![];
36        for p in env::split_paths(&std::env::var("PATH").unwrap_or(String::new())) {
37            paths.push(p.to_path_buf());
38        }
39        paths.push(Path::new(&env_var("TARGET_RPATH_DIR")).to_path_buf());
40        cmd.env("PATH", env::join_paths(paths.iter()).unwrap());
41    }
42
43    cmd
44}
45
46/// Run a built binary and make sure it succeeds.
47#[track_caller]
48pub fn run(name: &str) -> CompletedProcess {
49    let caller = panic::Location::caller();
50    let mut cmd = run_common(name, None);
51    let output = cmd.run();
52    if !output.status().success() {
53        handle_failed_output(&cmd, output, caller.line());
54    }
55    output
56}
57
58/// Run a built binary with one or more argument(s) and make sure it succeeds.
59#[track_caller]
60pub fn run_with_args(name: &str, args: &[&str]) -> CompletedProcess {
61    let caller = panic::Location::caller();
62    let mut cmd = run_common(name, Some(args));
63    let output = cmd.run();
64    if !output.status().success() {
65        handle_failed_output(&cmd, output, caller.line());
66    }
67    output
68}
69
70/// Run a built binary and make sure it fails.
71#[track_caller]
72pub fn run_fail(name: &str) -> CompletedProcess {
73    let caller = panic::Location::caller();
74    let mut cmd = run_common(name, None);
75    let output = cmd.run_fail();
76    if output.status().success() {
77        handle_failed_output(&cmd, output, caller.line());
78    }
79    output
80}
81
82/// Create a new custom [`Command`]. This should be preferred to creating [`std::process::Command`]
83/// directly.
84#[track_caller]
85pub fn cmd<S: AsRef<OsStr>>(program: S) -> Command {
86    let mut command = Command::new(program);
87    set_host_rpath(&mut command);
88    command.env("LC_ALL", "C"); // force english locale
89    command
90}