-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rs
50 lines (45 loc) · 1.78 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::env;
use std::fs::File;
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;
extern crate libbpf_cargo;
use libbpf_cargo::SkeletonBuilder;
const BPF_SRC: &str = concat!("src/bpf/", "profile.bpf.c");
fn btf_dump() {
let mut out = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
out.push("vmlinux.h");
let path = out.to_str().unwrap();
let btf = File::create(&out).expect(format!("failed to open: {}", path).as_str());
let mut cmd = Command::new("bpftool")
.args(&["btf", "dump", "file", "/sys/kernel/btf/vmlinux", "format", "c"])
.stdout(btf)
.spawn()
.expect("failed to generate vmlinux.h");
cmd.wait().expect("fail to generate vmlinux.h");
println!("cargo:cargo rerun-if-not-exists={}", path);
}
fn output() ->String {
let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script");
let out_dir_str = out_dir.to_str().unwrap().clone();
String::from_str(out_dir_str).unwrap()
}
fn build_skel() {
let mut out = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
out.push("profile.skel.rs");
let include = format!("-I{}", output());
SkeletonBuilder::new()
.debug(true)
.clang_args(include)
.source(BPF_SRC)
.build_and_generate(&out)
.unwrap();
println!("cargo:rerun-if-changed={BPF_SRC}");
println!("cargo:rerun-if-changed=src/bpf/lstate.h");
println!("cargo:rerun-if-changed=src/bpf/profile.h");
println!("cargo:rerun-if-changed=src/bpf/hash.h");
}
fn main() {
btf_dump();
build_skel();
}