Skip to content

Commit 0851b31

Browse files
d-e-s-odanielocfb
authored andcommitted
libbpf-cargo: Add --clang-args argument to build and make sub-commands
With commit beff188 ("examples: Use vmlinux.h for current architecture") we are having a hard time compiling the examples using the cargo-libbpf command, because the vmlinux.h header file cannot be found. Introduce the --clang-args argument to allow for passing in additional options -- such as an include path -- to the clang invocation. This would also have helped with #337. Signed-off-by: Daniel Müller <[email protected]>
1 parent fac5693 commit 0851b31

File tree

5 files changed

+129
-32
lines changed

5 files changed

+129
-32
lines changed

libbpf-cargo/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Unreleased
44
feature
55
- Adjusted `SkeletonBuilder::clang_args` to accept an iterator of
66
arguments instead of a string
7+
- Added `--clang-args` argument to `make` and `build` sub-commands
78
- Updated `libbpf-sys` dependency to `1.3.0`
89
- Bumped minimum Rust version to `1.70`
910

libbpf-cargo/src/build.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,18 @@ fn compile_one(
223223
strip_dwarf_info(out).with_context(|| format!("Failed to strip object file {}", out.display()))
224224
}
225225

226-
fn compile(debug: bool, objs: &[UnprocessedObj], clang: &Path, target_dir: &Path) -> Result<()> {
226+
fn compile(
227+
debug: bool,
228+
objs: &[UnprocessedObj],
229+
clang: &Path,
230+
mut clang_args: Vec<OsString>,
231+
target_dir: &Path,
232+
) -> Result<()> {
227233
let header_dir = extract_libbpf_headers_to_disk(target_dir)?;
228-
let compiler_options = if let Some(dir) = &header_dir {
229-
vec![OsString::from(format!("-I{}", dir.to_str().unwrap()))]
230-
} else {
231-
Vec::new()
232-
};
234+
if let Some(dir) = header_dir {
235+
clang_args.push(OsString::from("-I"));
236+
clang_args.push(dir.into_os_string());
237+
}
233238

234239
for obj in objs {
235240
let stem = obj.path.file_stem().with_context(|| {
@@ -245,7 +250,7 @@ fn compile(debug: bool, objs: &[UnprocessedObj], clang: &Path, target_dir: &Path
245250
let mut dest_path = obj.out.to_path_buf();
246251
dest_path.push(&dest_name);
247252
fs::create_dir_all(&obj.out)?;
248-
compile_one(debug, &obj.path, &dest_path, clang, &compiler_options)?;
253+
compile_one(debug, &obj.path, &dest_path, clang, &clang_args)?;
249254
}
250255

251256
Ok(())
@@ -259,10 +264,12 @@ fn extract_clang_or_default(clang: Option<&PathBuf>) -> PathBuf {
259264
}
260265
}
261266

267+
#[allow(clippy::too_many_arguments)]
262268
pub fn build(
263269
debug: bool,
264270
manifest_path: Option<&PathBuf>,
265271
clang: Option<&PathBuf>,
272+
clang_args: Vec<OsString>,
266273
skip_clang_version_checks: bool,
267274
) -> Result<()> {
268275
let (target_dir, to_compile) = metadata::get(debug, manifest_path)?;
@@ -281,7 +288,8 @@ pub fn build(
281288
let clang = extract_clang_or_default(clang);
282289
check_clang(debug, &clang, skip_clang_version_checks)
283290
.with_context(|| anyhow!("{} is invalid", clang.display()))?;
284-
compile(debug, &to_compile, &clang, &target_dir).context("Failed to compile progs")?;
291+
compile(debug, &to_compile, &clang, clang_args, &target_dir)
292+
.context("Failed to compile progs")?;
285293

286294
Ok(())
287295
}

libbpf-cargo/src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::let_unit_value)]
22
#![warn(clippy::absolute_paths)]
33

4+
use std::ffi::OsString;
45
use std::path::PathBuf;
56

67
use anyhow::Result;
@@ -49,6 +50,9 @@ pub struct ClangOpts {
4950
/// Path to clang binary
5051
#[arg(long, value_parser)]
5152
clang_path: Option<PathBuf>,
53+
/// Additional arguments to pass to `clang`.
54+
#[arg(long, value_parser)]
55+
clang_args: Vec<OsString>,
5256
/// Skip clang version checks
5357
#[arg(long)]
5458
skip_clang_version_checks: bool,
@@ -112,12 +116,14 @@ fn main() -> Result<()> {
112116
clang_opts:
113117
ClangOpts {
114118
clang_path,
119+
clang_args,
115120
skip_clang_version_checks,
116121
},
117122
} => build::build(
118123
debug,
119124
manifest_path.as_ref(),
120125
clang_path.as_ref(),
126+
clang_args,
121127
skip_clang_version_checks,
122128
),
123129
Command::Gen {
@@ -135,6 +141,7 @@ fn main() -> Result<()> {
135141
clang_opts:
136142
ClangOpts {
137143
clang_path,
144+
clang_args,
138145
skip_clang_version_checks,
139146
},
140147
quiet,
@@ -144,6 +151,7 @@ fn main() -> Result<()> {
144151
debug,
145152
manifest_path.as_ref(),
146153
clang_path.as_ref(),
154+
clang_args,
147155
skip_clang_version_checks,
148156
quiet,
149157
cargo_build_args,

libbpf-cargo/src/make.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::OsString;
12
use std::path::PathBuf;
23
use std::process::Command;
34

@@ -8,10 +9,12 @@ use anyhow::Result;
89
use crate::build;
910
use crate::gen;
1011

12+
#[allow(clippy::too_many_arguments)]
1113
pub fn make(
1214
debug: bool,
1315
manifest_path: Option<&PathBuf>,
1416
clang: Option<&PathBuf>,
17+
clang_args: Vec<OsString>,
1518
skip_clang_version_checks: bool,
1619
quiet: bool,
1720
cargo_build_args: Vec<String>,
@@ -20,8 +23,14 @@ pub fn make(
2023
if !quiet {
2124
println!("Compiling BPF objects");
2225
}
23-
build::build(debug, manifest_path, clang, skip_clang_version_checks)
24-
.context("Failed to compile BPF objects")?;
26+
build::build(
27+
debug,
28+
manifest_path,
29+
clang,
30+
clang_args,
31+
skip_clang_version_checks,
32+
)
33+
.context("Failed to compile BPF objects")?;
2534

2635
if !quiet {
2736
println!("Generating skeletons");

0 commit comments

Comments
 (0)