Skip to content

Commit 8ae390f

Browse files
d-e-s-odanielocfb
authored andcommitted
Introduce evaluate() helper function in build.rs
Introduce the evaluate() helper function to our build script, which will help make the code more flexible down the line. Signed-off-by: Daniel Müller <[email protected]>
1 parent b29f80c commit 8ae390f

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

libbpf-rs/build.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::io::Result;
1010
use std::ops::Deref as _;
1111
use std::path::Path;
1212
use std::process::Command;
13+
use std::process::Output;
1314
use std::process::Stdio;
1415

1516

@@ -30,38 +31,21 @@ where
3031
)
3132
}
3233

33-
/// Run a command with the provided arguments.
34-
fn run<C, A, S>(command: C, args: A) -> Result<()>
34+
35+
fn evaluate<C, A, S>(output: &Output, command: C, args: A) -> Result<()>
3536
where
3637
C: AsRef<OsStr>,
37-
A: IntoIterator<Item = S> + Clone,
38+
A: IntoIterator<Item = S>,
3839
S: AsRef<OsStr>,
3940
{
40-
let instance = Command::new(command.as_ref())
41-
.stdin(Stdio::null())
42-
.stdout(Stdio::null())
43-
.env_clear()
44-
.envs(env::vars().filter(|(k, _)| k == "PATH"))
45-
.args(args.clone())
46-
.output()
47-
.map_err(|err| {
48-
Error::new(
49-
ErrorKind::Other,
50-
format!(
51-
"failed to run `{}`: {err}",
52-
format_command(command.as_ref(), args.clone())
53-
),
54-
)
55-
})?;
56-
57-
if !instance.status.success() {
58-
let code = if let Some(code) = instance.status.code() {
41+
if !output.status.success() {
42+
let code = if let Some(code) = output.status.code() {
5943
format!(" ({code})")
6044
} else {
6145
" (terminated by signal)".to_string()
6246
};
6347

64-
let stderr = String::from_utf8_lossy(&instance.stderr);
48+
let stderr = String::from_utf8_lossy(&output.stderr);
6549
let stderr = stderr.trim_end();
6650
let stderr = if !stderr.is_empty() {
6751
format!(": {stderr}")
@@ -81,6 +65,35 @@ where
8165
}
8266
}
8367

68+
69+
/// Run a command with the provided arguments.
70+
fn run<C, A, S>(command: C, args: A) -> Result<()>
71+
where
72+
C: AsRef<OsStr>,
73+
A: IntoIterator<Item = S> + Clone,
74+
S: AsRef<OsStr>,
75+
{
76+
let output = Command::new(command.as_ref())
77+
.stdin(Stdio::null())
78+
.stdout(Stdio::null())
79+
.env_clear()
80+
.envs(env::vars().filter(|(k, _)| k == "PATH"))
81+
.args(args.clone())
82+
.output()
83+
.map_err(|err| {
84+
Error::new(
85+
ErrorKind::Other,
86+
format!(
87+
"failed to run `{}`: {err}",
88+
format_command(command.as_ref(), args.clone())
89+
),
90+
)
91+
})?;
92+
93+
let () = evaluate(&output, command, args)?;
94+
Ok(())
95+
}
96+
8497
fn adjust_mtime(path: &Path) -> Result<()> {
8598
// Note that `OUT_DIR` is only present at runtime.
8699
let out_dir = env::var("OUT_DIR").unwrap();

0 commit comments

Comments
 (0)