diff --git a/examples/rust/profile/build.rs b/examples/rust/profile/build.rs index 57c8f019..418caf52 100644 --- a/examples/rust/profile/build.rs +++ b/examples/rust/profile/build.rs @@ -1,25 +1,33 @@ -use std::fs::create_dir_all; +use std::env; +use std::env::consts::ARCH; use std::path::Path; +use std::path::PathBuf; -extern crate libbpf_cargo; use libbpf_cargo::SkeletonBuilder; -const SRC: &str = "./src/bpf/profile.bpf.c"; +const SRC: &str = "src/bpf/profile.bpf.c"; fn main() { - // It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton. - // Reasons are because the generated skeleton contains compiler attributes - // that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]` - // trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside - // the path attribute either (see https://github.com/rust-lang/rust/pull/83366). - // - // However, there is hope! When the above feature stabilizes we can clean this - // all up. - create_dir_all("./src/bpf/.output").unwrap(); - let skel = Path::new("./src/bpf/.output/profile.skel.rs"); + let mut out = + PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script")); + out.push("profile.skel.rs"); + SkeletonBuilder::new() .source(SRC) - .build_and_generate(skel) + .clang_args(format!( + "-I{}", + Path::new("../../../vmlinux") + .join(match ARCH { + "aarch64" => "arm64", + "loongarch64" => "loongarch", + "powerpc64" => "powerpc", + "riscv64" => "riscv", + "x86_64" => "x86", + _ => ARCH, + }) + .display() + )) + .build_and_generate(out) .expect("bpf compilation failed"); println!("cargo:rerun-if-changed={}", SRC); } diff --git a/examples/rust/profile/src/bpf/vmlinux.h b/examples/rust/profile/src/bpf/vmlinux.h deleted file mode 120000 index cab453bb..00000000 --- a/examples/rust/profile/src/bpf/vmlinux.h +++ /dev/null @@ -1 +0,0 @@ -../../../../../vmlinux/vmlinux.h \ No newline at end of file diff --git a/examples/rust/profile/src/main.rs b/examples/rust/profile/src/main.rs index b4089bf8..1a6a03ed 100644 --- a/examples/rust/profile/src/main.rs +++ b/examples/rust/profile/src/main.rs @@ -19,8 +19,9 @@ use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::fmt::time::SystemTime; use tracing_subscriber::FmtSubscriber; -#[path = "bpf/.output/profile.skel.rs"] -mod profile; +mod profile { + include!(concat!(env!("OUT_DIR"), "/profile.skel.rs")); +} mod syscall; use profile::*; diff --git a/examples/rust/tracecon/build.rs b/examples/rust/tracecon/build.rs index ce5c8941..eed39ceb 100644 --- a/examples/rust/tracecon/build.rs +++ b/examples/rust/tracecon/build.rs @@ -1,24 +1,33 @@ -use std::fs::create_dir_all; +use std::env; +use std::env::consts::ARCH; use std::path::Path; +use std::path::PathBuf; use libbpf_cargo::SkeletonBuilder; -const SRC: &str = "./src/bpf/tracecon.bpf.c"; +const SRC: &str = "src/bpf/tracecon.bpf.c"; fn main() { - // It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton. - // Reasons are because the generated skeleton contains compiler attributes - // that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]` - // trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside - // the path attribute either (see https://github.com/rust-lang/rust/pull/83366). - // - // However, there is hope! When the above feature stabilizes we can clean this - // all up. - create_dir_all("./src/bpf/.output").unwrap(); - let skel = Path::new("./src/bpf/.output/tracecon.skel.rs"); + let mut out = + PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script")); + out.push("tracecon.skel.rs"); + SkeletonBuilder::new() .source(SRC) - .build_and_generate(&skel) + .clang_args(format!( + "-I{}", + Path::new("../../../vmlinux") + .join(match ARCH { + "aarch64" => "arm64", + "loongarch64" => "loongarch", + "powerpc64" => "powerpc", + "riscv64" => "riscv", + "x86_64" => "x86", + _ => ARCH, + }) + .display() + )) + .build_and_generate(&out) .expect("bpf compilation failed"); println!("cargo:rerun-if-changed={}", SRC); } diff --git a/examples/rust/tracecon/src/bpf/vmlinux.h b/examples/rust/tracecon/src/bpf/vmlinux.h deleted file mode 120000 index cab453bb..00000000 --- a/examples/rust/tracecon/src/bpf/vmlinux.h +++ /dev/null @@ -1 +0,0 @@ -../../../../../vmlinux/vmlinux.h \ No newline at end of file diff --git a/examples/rust/tracecon/src/main.rs b/examples/rust/tracecon/src/main.rs index 3a9da0aa..8a4f9b8d 100644 --- a/examples/rust/tracecon/src/main.rs +++ b/examples/rust/tracecon/src/main.rs @@ -11,8 +11,9 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use structopt::StructOpt; -#[path = "bpf/.output/tracecon.skel.rs"] -mod tracecon; +mod tracecon { + include!(concat!(env!("OUT_DIR"), "/tracecon.skel.rs")); +} use tracecon::*; type Event = tracecon_bss_types::event; diff --git a/examples/rust/xdp/build.rs b/examples/rust/xdp/build.rs index 46df4b1c..3a6298c8 100644 --- a/examples/rust/xdp/build.rs +++ b/examples/rust/xdp/build.rs @@ -1,24 +1,33 @@ -use std::fs::create_dir_all; +use std::env; +use std::env::consts::ARCH; use std::path::Path; +use std::path::PathBuf; use libbpf_cargo::SkeletonBuilder; -const SRC: &str = "./src/bpf/xdppass.bpf.c"; +const SRC: &str = "src/bpf/xdppass.bpf.c"; fn main() { - // It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton. - // Reasons are because the generated skeleton contains compiler attributes - // that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]` - // trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside - // the path attribute either (see https://github.com/rust-lang/rust/pull/83366). - // - // However, there is hope! When the above feature stabilizes we can clean this - // all up. - create_dir_all("./src/bpf/.output").unwrap(); - let skel = Path::new("./src/bpf/.output/xdppass.skel.rs"); + let mut out = + PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script")); + out.push("xdppass.skel.rs"); + SkeletonBuilder::new() .source(SRC) - .build_and_generate(&skel) + .clang_args(format!( + "-I{}", + Path::new("../../../vmlinux") + .join(match ARCH { + "aarch64" => "arm64", + "loongarch64" => "loongarch", + "powerpc64" => "powerpc", + "riscv64" => "riscv", + "x86_64" => "x86", + _ => ARCH, + }) + .display() + )) + .build_and_generate(out) .unwrap(); println!("cargo:rerun-if-changed={}", SRC); } diff --git a/examples/rust/xdp/src/bpf/vmlinux.h b/examples/rust/xdp/src/bpf/vmlinux.h deleted file mode 120000 index cab453bb..00000000 --- a/examples/rust/xdp/src/bpf/vmlinux.h +++ /dev/null @@ -1 +0,0 @@ -../../../../../vmlinux/vmlinux.h \ No newline at end of file diff --git a/examples/rust/xdp/src/main.rs b/examples/rust/xdp/src/main.rs index 15d1e7f4..90f2f3fa 100644 --- a/examples/rust/xdp/src/main.rs +++ b/examples/rust/xdp/src/main.rs @@ -5,8 +5,9 @@ use std::{thread, time}; use anyhow::{bail, Result}; use structopt::StructOpt; -#[path = "bpf/.output/xdppass.skel.rs"] -mod xdppass; +mod xdppass { + include!(concat!(env!("OUT_DIR"), "/xdppass.skel.rs")); +} use xdppass::*; #[derive(Debug, StructOpt)]