Skip to content

Commit 03ec160

Browse files
committed
Re-implement the build of libbpf
1 parent 32f5ad8 commit 03ec160

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

build.rs

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::env;
44
use std::ffi;
5-
use std::fs;
65
use std::fs::read_dir;
76
use std::path;
87
use std::path::Path;
@@ -376,46 +375,52 @@ fn make_elfutils(compiler: &cc::Tool, src_dir: &path::Path, _: &path::Path) {
376375
emit_rerun_directives_for_contents(&src_dir.join("elfutils").join("src"));
377376
}
378377

379-
fn make_libbpf(
380-
compiler: &cc::Tool,
381-
cflags: &ffi::OsStr,
382-
src_dir: &path::Path,
383-
out_dir: &path::Path,
384-
) {
385-
let src_dir = src_dir.join("libbpf/src");
386-
// create obj_dir if it doesn't exist
387-
let obj_dir = path::PathBuf::from(&out_dir.join("obj").into_os_string());
388-
let _ = fs::create_dir(&obj_dir);
389-
390-
let status = process::Command::new("make")
391-
.arg("install")
392-
.arg("-j")
393-
.arg(&format!("{}", num_cpus()))
394-
.env("BUILD_STATIC_ONLY", "y")
395-
.env("PREFIX", "/")
396-
.env("LIBDIR", "")
397-
.env("OBJDIR", &obj_dir)
398-
.env("DESTDIR", out_dir)
399-
.env("CC", compiler.path())
400-
.env("CFLAGS", cflags)
401-
.current_dir(&src_dir)
402-
.status()
403-
.expect("could not execute make");
378+
fn make_libbpf(compiler: &cc::Tool, _: &ffi::OsStr, src_dir: &path::Path, _: &path::Path) {
379+
let project_dir = src_dir.join("libbpf");
404380

405-
assert!(status.success(), "make failed");
381+
let project = project_dir.to_str().unwrap();
406382

407-
let status = process::Command::new("make")
408-
.arg("clean")
409-
.current_dir(&src_dir)
410-
.status()
411-
.expect("could not execute make");
383+
let mut builder = cc::Build::new();
412384

413-
assert!(status.success(), "make failed");
414-
emit_rerun_directives_for_contents(&src_dir);
415-
}
385+
builder
386+
.include(src_dir)
387+
.include(src_dir.join("zlib"))
388+
.include(src_dir.join("elfutils").join("libelf"))
389+
.include(format!("{project}/src"))
390+
.include(format!("{project}/include"))
391+
.include(format!("{project}/include/uapi"));
392+
393+
if build_android() {
394+
let cflags = ["-DCOMPAT_NEED_REALLOCARRAY"];
395+
396+
builder.flag("-includeandroid/android.h");
397+
398+
for flag in cflags {
399+
builder.flag(flag);
400+
}
401+
} else {
402+
builder.compiler(compiler.path());
403+
404+
for flag in compiler.args() {
405+
builder.flag(flag);
406+
}
407+
}
408+
409+
for entry in std::fs::read_dir(project_dir.join("src")).expect("Failed to `read_dir`") {
410+
let entry = entry.unwrap();
411+
if entry.file_type().unwrap().is_file()
412+
&& entry.file_name().to_str().unwrap().ends_with(".c")
413+
{
414+
builder.file(entry.path());
415+
}
416+
}
417+
418+
builder
419+
.flag_if_supported("-w")
420+
.warnings(false)
421+
.compile("bpf");
416422

417-
fn num_cpus() -> usize {
418-
std::thread::available_parallelism().map_or(1, |count| count.get())
423+
emit_rerun_directives_for_contents(&src_dir);
419424
}
420425

421426
fn build_android() -> bool {

0 commit comments

Comments
 (0)