Skip to content

Commit

Permalink
examples/rust: Store skeletons in $OUT_DIR
Browse files Browse the repository at this point in the history
Store skeletons in $OUT_DIR as opposed to .output, which is kind of the
proper thing to do (though it certainly doesn't help
discoverability...) and it is more in line with how examples in
libbpf-rs work. Also remove the "there is hope!" comments, because even
with rust-lang/rust#83366 resolved we still
cannot use the concat! macro in #[path = ...] attributes. All hope is
lost.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Jan 12, 2024
1 parent a0515da commit ef652aa
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 49 deletions.
23 changes: 8 additions & 15 deletions examples/rust/profile/build.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
use std::fs::create_dir_all;
use std::path::Path;
use std::env;
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)
.build_and_generate(out)
.expect("bpf compilation failed");
println!("cargo:rerun-if-changed={}", SRC);
}
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
22 changes: 8 additions & 14 deletions examples/rust/tracecon/build.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
use std::fs::create_dir_all;
use std::path::Path;
use std::env;
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)
.build_and_generate(&out)
.expect("bpf compilation failed");
println!("cargo:rerun-if-changed={}", SRC);
}
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
22 changes: 8 additions & 14 deletions examples/rust/xdp/build.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
use std::fs::create_dir_all;
use std::path::Path;
use std::env;
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)
.build_and_generate(out)
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
}
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

0 comments on commit ef652aa

Please sign in to comment.