run_make_support/
run.rs

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