-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent: Generate vmlinux.h on the fly in build.rs
This adds a logic to generate "vmlinux.h" in build.rs, needed for compiling BPF programs. If vmlinux.h is supplied by the makefile, it will be copied to the build directory; otherwise bpftool will be used to extract it from the running kernel. Signed-off-by: Daiki Ueno <[email protected]>
- Loading branch information
Showing
5 changed files
with
69 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,41 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
use libbpf_cargo::SkeletonBuilder; | ||
use std::{env, path::PathBuf}; | ||
|
||
const SRC: &str = "src/bpf/audit.bpf.c"; | ||
use std::{ | ||
env, | ||
fs::{self, File}, | ||
path::PathBuf, | ||
process::Command, | ||
}; | ||
|
||
fn main() { | ||
let mut out = | ||
let builddir = | ||
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script")); | ||
out.push("audit.skel.rs"); | ||
let srcdir = PathBuf::from( | ||
env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set in build script"), | ||
); | ||
|
||
let vmlinux_h = srcdir.join("src").join("bpf").join("vmlinux.h"); | ||
if vmlinux_h.exists() { | ||
fs::copy(&vmlinux_h, &builddir.join("vmlinux.h")).expect("unable to copy vmlinux.h"); | ||
} else { | ||
let file = File::create(&builddir.join("vmlinux.h")).expect("unable to create vmlinux.h"); | ||
Command::new("bpftool") | ||
.arg("btf") | ||
.arg("dump") | ||
.arg("file") | ||
.arg("/sys/kernel/btf/vmlinux") | ||
.arg("format") | ||
.arg("c") | ||
.stdout(file) | ||
.status() | ||
.expect("unable to run bpftool"); | ||
} | ||
let src = srcdir.join("src").join("bpf").join("audit.bpf.c"); | ||
SkeletonBuilder::new() | ||
.source(SRC) | ||
.build_and_generate(&out) | ||
.source(&src) | ||
.clang_args(&format!("-I{}", builddir.display())) | ||
.build_and_generate(&builddir.join("audit.skel.rs")) | ||
.unwrap(); | ||
println!("cargo:rerun-if-changed={}", SRC); | ||
println!("cargo:rerun-if-changed={}", src.display()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,41 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
use libbpf_cargo::SkeletonBuilder; | ||
use std::{env, path::PathBuf}; | ||
|
||
const SRC: &str = "src/bpf/agent.bpf.c"; | ||
use std::{ | ||
env, | ||
fs::{self, File}, | ||
path::PathBuf, | ||
process::Command, | ||
}; | ||
|
||
fn main() { | ||
let mut out = | ||
let builddir = | ||
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script")); | ||
out.push("agent.skel.rs"); | ||
let srcdir = PathBuf::from( | ||
env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set in build script"), | ||
); | ||
|
||
let vmlinux_h = srcdir.join("src").join("bpf").join("vmlinux.h"); | ||
if vmlinux_h.exists() { | ||
fs::copy(&vmlinux_h, &builddir.join("vmlinux.h")).expect("unable to copy vmlinux.h"); | ||
} else { | ||
let file = File::create(&builddir.join("vmlinux.h")).expect("unable to create vmlinux.h"); | ||
Command::new("bpftool") | ||
.arg("btf") | ||
.arg("dump") | ||
.arg("file") | ||
.arg("/sys/kernel/btf/vmlinux") | ||
.arg("format") | ||
.arg("c") | ||
.stdout(file) | ||
.status() | ||
.expect("unable to run bpftool"); | ||
} | ||
let src = srcdir.join("src").join("bpf").join("agent.bpf.c"); | ||
SkeletonBuilder::new() | ||
.source(SRC) | ||
.build_and_generate(&out) | ||
.source(&src) | ||
.clang_args(&format!("-I{}", builddir.display())) | ||
.build_and_generate(&builddir.join("agent.skel.rs")) | ||
.unwrap(); | ||
println!("cargo:rerun-if-changed={}", SRC); | ||
println!("cargo:rerun-if-changed={}", src.display()); | ||
} |
This file was deleted.
Oops, something went wrong.