Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/rust: Store skeletons in $OUT_DIR #248

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions examples/rust/profile/build.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion examples/rust/profile/src/bpf/vmlinux.h

This file was deleted.

5 changes: 3 additions & 2 deletions examples/rust/profile/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
35 changes: 22 additions & 13 deletions examples/rust/tracecon/build.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion examples/rust/tracecon/src/bpf/vmlinux.h

This file was deleted.

5 changes: 3 additions & 2 deletions examples/rust/tracecon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 22 additions & 13 deletions examples/rust/xdp/build.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion examples/rust/xdp/src/bpf/vmlinux.h

This file was deleted.

5 changes: 3 additions & 2 deletions examples/rust/xdp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Loading