Skip to content

Commit

Permalink
libbpf-cargo: Introduce BpfObjBuilder::with_compiler_args() helper
Browse files Browse the repository at this point in the history
Introduce the BpfObjBuilder::with_compiler_args() helper for invoking a
block of code with the final set of compiler arguments.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Jan 16, 2025
1 parent 3466998 commit c85cb42
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions libbpf-cargo/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ impl BpfObjBuilder {
})
}

/// Build a BPF object file.
pub fn build(&mut self, src: &Path, dst: &Path) -> Result<CompilationOutput> {
fn with_compiler_args<F, R>(&self, f: F) -> Result<R>
where
F: FnOnce(&[OsString]) -> Result<R>,
{
let mut compiler_args = self.compiler_args.clone();

let header_parent_dir = tempdir().context("failed to create temporary directory")?;
Expand Down Expand Up @@ -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<CompilationOutput> {
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
Expand Down

0 comments on commit c85cb42

Please sign in to comment.