Skip to content

Commit

Permalink
improve: pre-generate BPF skeleton into source
Browse files Browse the repository at this point in the history
This removes requirment for setting up libelf and zlib on build
platform.
  • Loading branch information
EHfive committed Jun 14, 2024
1 parent 6cfe6d2 commit 6b9b935
Show file tree
Hide file tree
Showing 4 changed files with 8,838 additions and 25 deletions.
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ ipv6 = []
# Required on 32-bit platforms
bindgen = ["libbpf-sys/bindgen"]
# Link against static `libelf` and `zlib`.
# Notice libbpf is vendrored and static in any case.
static = ["libbpf-sys/static"]
#
# libbpf is vendrored and static in any case.
#
# Generate BPF skeleton instead of using pre-genrated skeleton, this is for BPF
# program development.
gen-skel = []
# Pre-generate BPF skeleton into source, this for release so downstream does not
# have to generate BPF skeleton in thier build process hence introducing
# build platform dependencies on libelf and zlib on which might be tricky to
# handle in cross-compilation.
gen-skel-source = ["gen-skel"]

[dependencies]
anyhow = "1.0.86"
Expand Down
50 changes: 28 additions & 22 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
use std::env;
use std::path::PathBuf;
fn main() {
#[cfg(feature = "gen-skel")]
{
use libbpf_cargo::SkeletonBuilder;
use std::env;
use std::path::PathBuf;

use libbpf_cargo::SkeletonBuilder;
const SRC: &str = "src/bpf/einat.bpf.c";

const SRC: &str = "src/bpf/einat.bpf.c";
let mut out = if cfg!(feature = "gen-skel-source") {
PathBuf::from("src")
} else {
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"))
};

fn main() {
let mut out =
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
out.push("einat.skel.rs");
out.push("einat.skel.rs");

let mut c_args = vec![
"-Wno-compare-distinct-pointer-types".to_string(),
"-mcpu=v3".to_string(),
];
let mut c_args = vec![
"-Wno-compare-distinct-pointer-types".to_string(),
"-mcpu=v3".to_string(),
];

if cfg!(feature = "ipv6") {
c_args.push("-DFEAT_IPV6".to_string());
}
if cfg!(feature = "ipv6") {
c_args.push("-DFEAT_IPV6".to_string());
}

SkeletonBuilder::new()
.source(SRC)
.clang_args(c_args)
.debug(true)
.build_and_generate(&out)
.unwrap();
println!("cargo:rerun-if-changed={SRC}");
SkeletonBuilder::new()
.source(SRC)
.clang_args(c_args)
.debug(true)
.build_and_generate(&out)
.unwrap();
println!("cargo:rerun-if-changed={SRC}");
}
}
Loading

0 comments on commit 6b9b935

Please sign in to comment.