Skip to content

Commit be15077

Browse files
d-e-s-odanielocfb
authored andcommitted
libbpf-cargo: Simplify build_btf_mmap() test helper
The build_btf_mmap() function is almost comically complex in what it does. In order to compile a single .bpf.c file it creates an entire Cargo Rust project and builds that, instead of simply compiling the file directly. Now that we have the BpfObjBuilder type, we can just about accomplish the latter without jumping through dozens of hoops. Do it. As a pleasant side effect of this reduction in complexity, the btf_dump test runtime more than halves: Before: $ time target/debug/deps/libbpf_cargo-37cf1fde780326f7 btf_dump > running 34 tests > [...] > ________________________________________________________ > Executed in 2.09 secs fish external After: $ time target/debug/deps/libbpf_cargo-37cf1fde780326f7 btf_dump > running 34 tests > [...] > ________________________________________________________ > Executed in 810.13 millis fish external Not the motivating factor at this point, but we take it. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent 3b30d20 commit be15077

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

libbpf-cargo/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl BpfObjBuilder {
199199
/// Build a BPF object file.
200200
pub fn build(&mut self, src: &Path, dst: &Path) -> Result<CompilationOutput> {
201201
self.build_many([src], dst).map(|vec| {
202-
// SANITY: We pass in a single file we `build_many` is
202+
// SANITY: We pass in a single file and `build_many` is
203203
// guaranteed to produce as many outputs as input
204204
// files; so there must be one.
205205
vec.into_iter().next().unwrap()

libbpf-cargo/src/test.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use tempfile::TempDir;
2121
use test_log::test;
2222

2323
use crate::build::build_project;
24+
use crate::build::BpfObjBuilder;
2425
use crate::gen::GenBtf;
2526
use crate::gen::GenStructOps;
2627
use crate::make::make;
@@ -122,19 +123,23 @@ fn get_libbpf_rs_path() -> PathBuf {
122123
.expect("failed to canonicalize libbpf-rs")
123124
}
124125

125-
/// Add vmlinux header into `project`'s src/bpf dir
126-
fn add_vmlinux_header(project: &Path) {
126+
fn write_vmlinux_header(dir: &Path) {
127127
let mut vmlinux = OpenOptions::new()
128128
.create(true)
129129
.truncate(true)
130130
.write(true)
131-
.open(project.join("src/bpf/vmlinux.h"))
131+
.open(dir.join("vmlinux.h"))
132132
.expect("failed to open vmlinux.h");
133133
let () = vmlinux
134134
.write_all(vmlinux::VMLINUX)
135135
.expect("failed to write vmlinux.h");
136136
}
137137

138+
/// Add vmlinux header into `project`'s src/bpf dir
139+
fn add_vmlinux_header(project: &Path) {
140+
write_vmlinux_header(&project.join("src/bpf"))
141+
}
142+
138143
#[test]
139144
fn test_build_default() {
140145
let (_dir, proj_dir, cargo_toml) = setup_temp_project();
@@ -1174,30 +1179,28 @@ macro_rules! find_type_in_btf {
11741179
/// returns struct Btf if able to compile
11751180
/// fails calling test if unable to compile
11761181
fn build_btf_mmap(prog_text: &str) -> Mmap {
1177-
let (_dir, proj_dir, cargo_toml) = setup_temp_project();
1178-
1179-
// Add prog dir
1180-
create_dir(proj_dir.join("src/bpf")).expect("failed to create prog dir");
1181-
1182-
// Add a prog
1183-
let mut prog = OpenOptions::new()
1182+
let dir = tempdir().expect("failed to create tempdir");
1183+
let dir = dir.path();
1184+
let bpf_c = dir.join("prog.bpf.c");
1185+
let mut file = OpenOptions::new()
11841186
.write(true)
11851187
.create(true)
11861188
.truncate(true)
1187-
.open(proj_dir.join("src/bpf/prog.bpf.c"))
1189+
.open(&bpf_c)
11881190
.expect("failed to open prog.bpf.c");
11891191

1190-
write!(prog, "{prog_text}").expect("failed to write prog.bpf.c");
1191-
1192+
let () = file
1193+
.write_all(prog_text.as_bytes())
1194+
.expect("failed to write prog.bpf.c");
11921195
// Lay down the necessary header files
1193-
add_vmlinux_header(&proj_dir);
1196+
let () = write_vmlinux_header(dir);
11941197

1195-
// Build the .bpf.o
1196-
build_project(Some(&cargo_toml), None, Vec::new()).expect("failed to compile");
1198+
let bpf_o = dir.join("prog.bpf.o");
1199+
let _output = BpfObjBuilder::default().build(&bpf_c, &bpf_o).unwrap();
11971200

11981201
let obj = OpenOptions::new()
11991202
.read(true)
1200-
.open(proj_dir.as_path().join("target/bpf/prog.bpf.o").as_path())
1203+
.open(bpf_o)
12011204
.expect("failed to open object file");
12021205
unsafe { Mmap::map(&obj) }.expect("Failed to mmap object file")
12031206
}

0 commit comments

Comments
 (0)