Skip to content

Commit ebfe11b

Browse files
committed
examples/rust: Store skeletons in $OUT_DIR
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 <[email protected]>
1 parent f906237 commit ebfe11b

File tree

6 files changed

+33
-49
lines changed

6 files changed

+33
-49
lines changed

examples/rust/profile/build.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
use std::fs::create_dir_all;
2-
use std::path::Path;
1+
use std::env;
2+
use std::path::PathBuf;
33

4-
extern crate libbpf_cargo;
54
use libbpf_cargo::SkeletonBuilder;
65

7-
const SRC: &str = "./src/bpf/profile.bpf.c";
6+
const SRC: &str = "src/bpf/profile.bpf.c";
87

98
fn main() {
10-
// It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton.
11-
// Reasons are because the generated skeleton contains compiler attributes
12-
// that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]`
13-
// trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside
14-
// the path attribute either (see https://github.com/rust-lang/rust/pull/83366).
15-
//
16-
// However, there is hope! When the above feature stabilizes we can clean this
17-
// all up.
18-
create_dir_all("./src/bpf/.output").unwrap();
19-
let skel = Path::new("./src/bpf/.output/profile.skel.rs");
9+
let mut out =
10+
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
11+
out.push("profile.skel.rs");
12+
2013
SkeletonBuilder::new()
2114
.source(SRC)
22-
.build_and_generate(skel)
15+
.build_and_generate(out)
2316
.expect("bpf compilation failed");
2417
println!("cargo:rerun-if-changed={}", SRC);
2518
}

examples/rust/profile/src/main.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use tracing_subscriber::fmt::format::FmtSpan;
1919
use tracing_subscriber::fmt::time::SystemTime;
2020
use tracing_subscriber::FmtSubscriber;
2121

22-
#[path = "bpf/.output/profile.skel.rs"]
23-
mod profile;
22+
mod profile {
23+
include!(concat!(env!("OUT_DIR"), "/profile.skel.rs"));
24+
}
2425
mod syscall;
2526

2627
use profile::*;

examples/rust/tracecon/build.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
use std::fs::create_dir_all;
2-
use std::path::Path;
1+
use std::env;
2+
use std::path::PathBuf;
33

44
use libbpf_cargo::SkeletonBuilder;
55

6-
const SRC: &str = "./src/bpf/tracecon.bpf.c";
6+
const SRC: &str = "src/bpf/tracecon.bpf.c";
77

88
fn main() {
9-
// It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton.
10-
// Reasons are because the generated skeleton contains compiler attributes
11-
// that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]`
12-
// trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside
13-
// the path attribute either (see https://github.com/rust-lang/rust/pull/83366).
14-
//
15-
// However, there is hope! When the above feature stabilizes we can clean this
16-
// all up.
17-
create_dir_all("./src/bpf/.output").unwrap();
18-
let skel = Path::new("./src/bpf/.output/tracecon.skel.rs");
9+
let mut out =
10+
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
11+
out.push("tracecon.skel.rs");
12+
1913
SkeletonBuilder::new()
2014
.source(SRC)
21-
.build_and_generate(&skel)
15+
.build_and_generate(&out)
2216
.expect("bpf compilation failed");
2317
println!("cargo:rerun-if-changed={}", SRC);
2418
}

examples/rust/tracecon/src/main.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use std::sync::atomic::{AtomicBool, Ordering};
1111
use std::sync::Arc;
1212
use structopt::StructOpt;
1313

14-
#[path = "bpf/.output/tracecon.skel.rs"]
15-
mod tracecon;
14+
mod tracecon {
15+
include!(concat!(env!("OUT_DIR"), "/tracecon.skel.rs"));
16+
}
1617
use tracecon::*;
1718

1819
type Event = tracecon_bss_types::event;

examples/rust/xdp/build.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
use std::fs::create_dir_all;
2-
use std::path::Path;
1+
use std::env;
2+
use std::path::PathBuf;
33

44
use libbpf_cargo::SkeletonBuilder;
55

6-
const SRC: &str = "./src/bpf/xdppass.bpf.c";
6+
const SRC: &str = "src/bpf/xdppass.bpf.c";
77

88
fn main() {
9-
// It's unfortunate we cannot use `OUT_DIR` to store the generated skeleton.
10-
// Reasons are because the generated skeleton contains compiler attributes
11-
// that cannot be `include!()`ed via macro. And we cannot use the `#[path = "..."]`
12-
// trick either because you cannot yet `concat!(env!("OUT_DIR"), "/skel.rs")` inside
13-
// the path attribute either (see https://github.com/rust-lang/rust/pull/83366).
14-
//
15-
// However, there is hope! When the above feature stabilizes we can clean this
16-
// all up.
17-
create_dir_all("./src/bpf/.output").unwrap();
18-
let skel = Path::new("./src/bpf/.output/xdppass.skel.rs");
9+
let mut out =
10+
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
11+
out.push("xdppass.skel.rs");
12+
1913
SkeletonBuilder::new()
2014
.source(SRC)
21-
.build_and_generate(&skel)
15+
.build_and_generate(out)
2216
.unwrap();
2317
println!("cargo:rerun-if-changed={}", SRC);
2418
}

examples/rust/xdp/src/main.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use std::{thread, time};
55
use anyhow::{bail, Result};
66
use structopt::StructOpt;
77

8-
#[path = "bpf/.output/xdppass.skel.rs"]
9-
mod xdppass;
8+
mod xdppass {
9+
include!(concat!(env!("OUT_DIR"), "/xdppass.skel.rs"));
10+
}
1011
use xdppass::*;
1112

1213
#[derive(Debug, StructOpt)]

0 commit comments

Comments
 (0)