diff --git a/libbpf-cargo/src/build.rs b/libbpf-cargo/src/build.rs index 006698bc..ef901bd9 100644 --- a/libbpf-cargo/src/build.rs +++ b/libbpf-cargo/src/build.rs @@ -107,8 +107,10 @@ impl BpfObjBuilder { }) } - /// Build a BPF object file. - pub fn build(&mut self, src: &Path, dst: &Path) -> Result { + fn with_compiler_args(&self, f: F) -> Result + where + F: FnOnce(&[OsString]) -> Result, + { let mut compiler_args = self.compiler_args.clone(); let header_parent_dir = tempdir().context("failed to create temporary directory")?; @@ -146,8 +148,17 @@ impl BpfObjBuilder { compiler_args.push(format!("-D__TARGET_ARCH_{arch}").into()); } - let output = Self::compile_single(src, dst, &self.compiler, &compiler_args) - .with_context(|| format!("failed to compile `{}`", src.display()))?; + f(&compiler_args) + } + + /// Build a BPF object file. + pub fn build(&mut self, src: &Path, dst: &Path) -> Result { + let output = self.with_compiler_args(|compiler_args| { + let output = Self::compile_single(src, dst, &self.compiler, compiler_args) + .with_context(|| format!("failed to compile `{}`", src.display()))?; + + Ok(output) + })?; // Compilation with clang may contain DWARF information that references // system specific and temporary paths. That can render our generated