-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbuild.rs
More file actions
80 lines (71 loc) · 2.67 KB
/
build.rs
File metadata and controls
80 lines (71 loc) · 2.67 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Build utilities for BPF programs using codspeed-bpf
//!
//! This module provides helpers for building BPF programs that use codspeed-bpf headers.
//!
//! To use this in your build.rs, add codspeed-bpf with the "build" feature to your
//! [build-dependencies]:
//!
//! ```toml
//! [build-dependencies]
//! codspeed-bpf = { path = "../codspeed-bpf", features = ["build"] }
//! ```
//!
//! Then in your build.rs:
//!
//! ```ignore
//! fn main() {
//! codspeed_bpf::build::build_bpf("exectrack", "src/ebpf/c/exectrack.bpf.c");
//! codspeed_bpf::build::generate_bindings("wrapper.h");
//! }
//! ```
use std::{env, path::PathBuf};
/// Build a BPF program with codspeed-bpf headers available
///
/// # Arguments
/// * `program_name` - The name of the BPF program (e.g., "exectrack", "memtrack")
/// * `source_file` - The path to the BPF source file (e.g., "src/ebpf/c/exectrack.bpf.c")
///
/// This function will:
/// 1. Compile the BPF program using libbpf-cargo
/// 2. Include codspeed-bpf headers in the clang search path
/// 3. Generate a skeleton into OUT_DIR/{program_name}.skel.rs
pub fn build_bpf(program_name: &str, source_file: &str) {
use libbpf_cargo::SkeletonBuilder;
println!("cargo:rerun-if-changed=src/ebpf/c");
let arch = env::var("CARGO_CFG_TARGET_ARCH")
.expect("CARGO_CFG_TARGET_ARCH must be set in build script");
let output =
PathBuf::from(env::var("OUT_DIR").unwrap()).join(format!("{program_name}.skel.rs"));
// Get the path to codspeed-bpf's C headers
let codspeed_bpf_include = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.parent()
.unwrap()
.join("codspeed-bpf/src/c");
SkeletonBuilder::new()
.source(source_file)
.clang_args([
"-I",
&vmlinux::include_path_root().join(arch).to_string_lossy(),
"-I",
&codspeed_bpf_include.to_string_lossy(),
])
.build_and_generate(&output)
.unwrap_or_else(|_| panic!("Failed to build {program_name}.bpf.c"));
}
/// Generate Rust bindings for a C header file
///
/// # Arguments
/// * `header_file` - The path to the header file (e.g., "wrapper.h")
///
/// This function will:
/// 1. Use bindgen to generate Rust bindings
/// 2. Write the output to OUT_DIR/event.rs
pub fn generate_bindings(header_file: &str) {
let bindings = bindgen::Builder::default()
.header(header_file)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
let out_file = PathBuf::from(env::var("OUT_DIR").unwrap()).join("event.rs");
std::fs::write(&out_file, bindings.to_string()).expect("Couldn't write bindings!");
}