Skip to content

Commit ee45752

Browse files
committed
Move dependencies to workspace
Add "{{project-name}}-ebpf" to workspace. Update various cargo configs to match main aya repo.
1 parent e064758 commit ee45752

File tree

14 files changed

+114
-110
lines changed

14 files changed

+114
-110
lines changed

.cargo/rust-analyzer.toml

-2
This file was deleted.

Cargo.toml

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
[workspace]
22
resolver = "2"
3-
members = ["xtask", "{{project-name}}", "{{project-name}}-common"]
3+
members = ["xtask", "{{project-name}}", "{{project-name}}-common", "{{project-name}}-ebpf"]
4+
default-members = ["xtask", "{{project-name}}", "{{project-name}}-common"]
5+
6+
[workspace.dependencies]
7+
aya = { version = "0.13.0", default-features = false }
8+
aya-ebpf = { version = "0.1.1", default-features = false }
9+
aya-log = { version = "0.2.1", default-features = false }
10+
aya-log-ebpf = { version = "0.1.1", default-features = false }
11+
12+
anyhow = { version = "1", default-features = false }
13+
# `std` feature is currently required to build `clap`.
14+
#
15+
# See https://github.com/clap-rs/clap/blob/61f5ee5/clap_builder/src/lib.rs#L15.
16+
clap = { version = "4.5.20", default-features = false, features = ["std"] }
17+
env_logger = { version = "0.11.5", default-features = false }
18+
libc = { version = "0.2.159", default-features = false }
19+
log = { version = "0.4.22", default-features = false }
20+
tokio = { version = "1.40.0", default-features = false }
21+
22+
[profile.dev]
23+
opt-level = 3
24+
debug = false
25+
overflow-checks = false
26+
lto = true
27+
panic = "abort"
28+
incremental = false
29+
codegen-units = 1
30+
rpath = false
31+
32+
[profile.release]
33+
lto = true
34+
panic = "abort"
35+
codegen-units = 1

test.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ case "$PROG_TYPE" in
3434
ADDITIONAL_ARGS="-d tracepoint_name=sys_enter"
3535
;;
3636
"sk_msg")
37-
ADDITIONAL_ARGS="-d sock_map=TEST"
37+
ADDITIONAL_ARGS="-d sock_map=SOCK_MAP"
3838
;;
3939
"tp_btf")
4040
ADDITIONAL_ARGS="-d tracepoint_name=net_dev_queue"
@@ -51,7 +51,7 @@ esac
5151

5252
cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
5353
pushd test
54-
cargo xtask build-ebpf
55-
cargo build
54+
cargo xtask build
55+
cargo xtask build --release
5656
popd
5757
exit 0

xtask/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
anyhow = "1"
8-
clap = { version = "4.1", features = ["derive"] }
7+
anyhow = { workspace = true }
8+
clap = { workspace = true, default-features = true, features = ["derive"] }

xtask/src/build.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,27 @@ pub struct Options {
1515
pub release: bool,
1616
}
1717

18-
/// Build the project
19-
fn build_project(opts: &Options) -> Result<(), anyhow::Error> {
20-
let mut args = vec!["build"];
21-
if opts.release {
22-
args.push("--release")
23-
}
24-
let status = Command::new("cargo")
25-
.args(&args)
26-
.status()
27-
.expect("failed to build userspace");
28-
assert!(status.success());
29-
Ok(())
30-
}
31-
32-
/// Build our ebpf program and the project
18+
/// Build our ebpf program and the userspace program.
3319
pub fn build(opts: Options) -> Result<(), anyhow::Error> {
34-
// build our ebpf program followed by our application
20+
let Options {
21+
bpf_target,
22+
release,
23+
} = opts;
24+
25+
// Build our ebpf program.
3526
build_ebpf(BuildOptions {
36-
target: opts.bpf_target,
37-
release: opts.release,
38-
})
39-
.context("Error while building eBPF program")?;
40-
build_project(&opts).context("Error while building userspace application")?;
27+
target: bpf_target,
28+
release,
29+
})?;
30+
31+
// Build our userspace program.
32+
let mut cmd = Command::new("cargo");
33+
cmd.arg("build");
34+
if release {
35+
cmd.arg("--release");
36+
}
37+
let status = cmd.status().context("failed to build userspace")?;
38+
anyhow::ensure!(status.success(), "failed to build userspace program: {}", status);
39+
4140
Ok(())
42-
}
41+
}

xtask/src/build_ebpf.rs

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1-
use std::{path::PathBuf, process::Command};
1+
use std::process::Command;
22

3+
use anyhow::Context as _;
34
use clap::Parser;
45

5-
#[derive(Debug, Copy, Clone)]
6+
#[derive(Debug, Clone)]
67
pub enum Architecture {
78
BpfEl,
89
BpfEb,
910
}
1011

12+
impl Architecture {
13+
pub fn as_str(&self) -> &'static str {
14+
match self {
15+
Architecture::BpfEl => "bpfel-unknown-none",
16+
Architecture::BpfEb => "bpfeb-unknown-none",
17+
}
18+
}
19+
}
20+
1121
impl std::str::FromStr for Architecture {
12-
type Err = String;
22+
type Err = &'static str;
1323

1424
fn from_str(s: &str) -> Result<Self, Self::Err> {
1525
Ok(match s {
1626
"bpfel-unknown-none" => Architecture::BpfEl,
1727
"bpfeb-unknown-none" => Architecture::BpfEb,
18-
_ => return Err("invalid target".to_owned()),
28+
_ => return Err("invalid target"),
1929
})
2030
}
2131
}
2232

2333
impl std::fmt::Display for Architecture {
2434
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25-
f.write_str(match self {
26-
Architecture::BpfEl => "bpfel-unknown-none",
27-
Architecture::BpfEb => "bpfeb-unknown-none",
28-
})
35+
f.write_str(self.as_str())
2936
}
3037
}
3138

@@ -40,28 +47,22 @@ pub struct Options {
4047
}
4148

4249
pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
43-
let dir = PathBuf::from("{{project-name}}-ebpf");
44-
let target = format!("--target={}", opts.target);
45-
let mut args = vec![
46-
"build",
47-
target.as_str(),
48-
"-Z",
49-
"build-std=core",
50-
];
51-
if opts.release {
52-
args.push("--release")
50+
let Options { target, release } = opts;
51+
52+
let mut cmd = Command::new("cargo");
53+
cmd.current_dir("{{project-name}}-ebpf")
54+
// Command::new creates a child process which inherits all env variables. This means env
55+
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed so
56+
// the rust-toolchain.toml file in the -ebpf folder is honored.
57+
.env_remove("RUSTUP_TOOLCHAIN")
58+
.args(["build", "--target", target.as_str()]);
59+
60+
if release {
61+
cmd.arg("--release");
5362
}
5463

55-
// Command::new creates a child process which inherits all env variables. This means env
56-
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed
57-
// so the rust-toolchain.toml file in the -ebpf folder is honored.
64+
let status = cmd.status().context("failed to build bpf program")?;
65+
anyhow::ensure!(status.success(), "failed to build bpf program: {}", status);
5866

59-
let status = Command::new("cargo")
60-
.current_dir(dir)
61-
.env_remove("RUSTUP_TOOLCHAIN")
62-
.args(&args)
63-
.status()
64-
.expect("failed to build bpf program");
65-
assert!(status.success());
6667
Ok(())
6768
}

{{project-name}}-common/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ default = []
88
user = ["aya"]
99

1010
[dependencies]
11-
aya = { version = "0.13", optional = true }
11+
aya = { workspace = true, optional = true }
1212

1313
[lib]
1414
path = "src/lib.rs"
+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
# We have this so that one doesn't need to manually pass
2+
# --target=bpfel-unknown-none -Z build-std=core when running cargo
3+
# check/build/doc etc.
4+
#
5+
# NB: this file gets loaded only if you run cargo from this directory, it's
6+
# ignored if you run from the workspace root. See
7+
# https://doc.rust-lang.org/cargo/reference/config.html#hierarchical-structure
18
[build]
2-
target-dir = "../target"
3-
target = "bpfel-unknown-none"
9+
target = ["bpfeb-unknown-none", "bpfel-unknown-none"]
410

511
[unstable]
612
build-std = ["core"]

{{project-name}}-ebpf/.cargo/rust-analyzer.toml

-3
This file was deleted.

{{project-name}}-ebpf/.helix/config.toml

-2
This file was deleted.

{{project-name}}-ebpf/Cargo.toml

+3-21
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,11 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
aya-ebpf = "0.1.1"
8-
aya-log-ebpf = "0.1.1"
97
{{ project-name }}-common = { path = "../{{ project-name }}-common" }
108

9+
aya-ebpf = { workspace = true }
10+
aya-log-ebpf = { workspace = true }
11+
1112
[[bin]]
1213
name = "{{ project-name }}"
1314
path = "src/main.rs"
14-
15-
[profile.dev]
16-
opt-level = 3
17-
debug = false
18-
debug-assertions = false
19-
overflow-checks = false
20-
lto = true
21-
panic = "abort"
22-
incremental = false
23-
codegen-units = 1
24-
rpath = false
25-
26-
[profile.release]
27-
lto = true
28-
panic = "abort"
29-
codegen-units = 1
30-
31-
[workspace]
32-
members = []
-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,2 @@
11
[toolchain]
22
channel = "nightly"
3-
# The source code of rustc, provided by the rust-src component, is needed for
4-
# building eBPF programs.
5-
components = [
6-
"cargo",
7-
"clippy",
8-
"rust-docs",
9-
"rust-src",
10-
"rust-std",
11-
"rustc",
12-
"rustfmt",
13-
]

{{project-name}}-ebpf/src/main.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ use aya_log_ebpf::info;
138138

139139
use {{crate_name}}_common::SockKey;
140140

141-
#[map(name="{{sock_map}}")]
141+
#[map]
142142
static {{sock_map}}: SockHash<SockKey> = SockHash::<SockKey>::with_max_entries(1024, 0);
143143

144144
#[sk_msg]
@@ -353,7 +353,8 @@ fn try_{{crate_name}}(ctx: PerfEventContext) -> Result<u32, u32> {
353353
}
354354
{%- endcase %}
355355

356+
#[cfg(not(test))]
356357
#[panic_handler]
357358
fn panic(_info: &core::panic::PanicInfo) -> ! {
358-
unsafe { core::hint::unreachable_unchecked() }
359+
loop {}
359360
}

{{project-name}}/Cargo.toml

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
name = "{{project-name}}"
33
version = "0.1.0"
44
edition = "2021"
5-
publish = false
65

76
[dependencies]
8-
aya = "0.13"
9-
aya-log = "0.2"
7+
{{project-name}}-common = { path = "../{{project-name}}-common", features = ["user"] }
8+
9+
anyhow = { workspace = true }
10+
aya = { workspace = true }
11+
aya-log = { workspace = true }
12+
env_logger = {workspace = true }
13+
libc = { workspace = true }
14+
log = { workspace = true }
15+
tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "net", "signal"] }
16+
1017
{% if program_types_with_opts contains program_type -%}
11-
clap = { version = "4.1", features = ["derive"] }
18+
clap = { workspace = true, features = ["derive"] }
1219
{% endif -%}
13-
{{project-name}}-common = { path = "../{{project-name}}-common", features = ["user"] }
14-
anyhow = "1"
15-
env_logger = "0.10"
16-
libc = "0.2"
17-
log = "0.4"
18-
tokio = { version = "1.25", features = ["macros", "rt", "rt-multi-thread", "net", "signal"] }
1920

2021
[[bin]]
2122
name = "{{project-name}}"

0 commit comments

Comments
 (0)