Move dependencies to workspace

Add "{{project-name}}-ebpf" to workspace. Update various cargo configs
to match main aya repo.
reviewable/pr124/r1
Tamir Duberstein 3 months ago
parent e064758d59
commit ee45752006

@ -1,2 +0,0 @@
[rust-analyzer]
linkedProjects = ["Cargo.toml", "{{project-name}}-ebpf/Cargo.toml"]

@ -1,3 +1,35 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = ["xtask", "{{project-name}}", "{{project-name}}-common"] members = ["xtask", "{{project-name}}", "{{project-name}}-common", "{{project-name}}-ebpf"]
default-members = ["xtask", "{{project-name}}", "{{project-name}}-common"]
[workspace.dependencies]
aya = { version = "0.13.0", default-features = false }
aya-ebpf = { version = "0.1.1", default-features = false }
aya-log = { version = "0.2.1", default-features = false }
aya-log-ebpf = { version = "0.1.1", default-features = false }
anyhow = { version = "1", default-features = false }
# `std` feature is currently required to build `clap`.
#
# See https://github.com/clap-rs/clap/blob/61f5ee5/clap_builder/src/lib.rs#L15.
clap = { version = "4.5.20", default-features = false, features = ["std"] }
env_logger = { version = "0.11.5", default-features = false }
libc = { version = "0.2.159", default-features = false }
log = { version = "0.4.22", default-features = false }
tokio = { version = "1.40.0", default-features = false }
[profile.dev]
opt-level = 3
debug = false
overflow-checks = false
lto = true
panic = "abort"
incremental = false
codegen-units = 1
rpath = false
[profile.release]
lto = true
panic = "abort"
codegen-units = 1

@ -34,7 +34,7 @@ case "$PROG_TYPE" in
ADDITIONAL_ARGS="-d tracepoint_name=sys_enter" ADDITIONAL_ARGS="-d tracepoint_name=sys_enter"
;; ;;
"sk_msg") "sk_msg")
ADDITIONAL_ARGS="-d sock_map=TEST" ADDITIONAL_ARGS="-d sock_map=SOCK_MAP"
;; ;;
"tp_btf") "tp_btf")
ADDITIONAL_ARGS="-d tracepoint_name=net_dev_queue" ADDITIONAL_ARGS="-d tracepoint_name=net_dev_queue"
@ -51,7 +51,7 @@ esac
cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS} cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
pushd test pushd test
cargo xtask build-ebpf cargo xtask build
cargo build cargo xtask build --release
popd popd
exit 0 exit 0

@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
anyhow = "1" anyhow = { workspace = true }
clap = { version = "4.1", features = ["derive"] } clap = { workspace = true, default-features = true, features = ["derive"] }

@ -15,28 +15,27 @@ pub struct Options {
pub release: bool, pub release: bool,
} }
/// Build the project /// Build our ebpf program and the userspace program.
fn build_project(opts: &Options) -> Result<(), anyhow::Error> {
let mut args = vec!["build"];
if opts.release {
args.push("--release")
}
let status = Command::new("cargo")
.args(&args)
.status()
.expect("failed to build userspace");
assert!(status.success());
Ok(())
}
/// Build our ebpf program and the project
pub fn build(opts: Options) -> Result<(), anyhow::Error> { pub fn build(opts: Options) -> Result<(), anyhow::Error> {
// build our ebpf program followed by our application let Options {
bpf_target,
release,
} = opts;
// Build our ebpf program.
build_ebpf(BuildOptions { build_ebpf(BuildOptions {
target: opts.bpf_target, target: bpf_target,
release: opts.release, release,
}) })?;
.context("Error while building eBPF program")?;
build_project(&opts).context("Error while building userspace application")?; // Build our userspace program.
let mut cmd = Command::new("cargo");
cmd.arg("build");
if release {
cmd.arg("--release");
}
let status = cmd.status().context("failed to build userspace")?;
anyhow::ensure!(status.success(), "failed to build userspace program: {}", status);
Ok(()) Ok(())
} }

@ -1,31 +1,38 @@
use std::{path::PathBuf, process::Command}; use std::process::Command;
use anyhow::Context as _;
use clap::Parser; use clap::Parser;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Clone)]
pub enum Architecture { pub enum Architecture {
BpfEl, BpfEl,
BpfEb, BpfEb,
} }
impl Architecture {
pub fn as_str(&self) -> &'static str {
match self {
Architecture::BpfEl => "bpfel-unknown-none",
Architecture::BpfEb => "bpfeb-unknown-none",
}
}
}
impl std::str::FromStr for Architecture { impl std::str::FromStr for Architecture {
type Err = String; type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s { Ok(match s {
"bpfel-unknown-none" => Architecture::BpfEl, "bpfel-unknown-none" => Architecture::BpfEl,
"bpfeb-unknown-none" => Architecture::BpfEb, "bpfeb-unknown-none" => Architecture::BpfEb,
_ => return Err("invalid target".to_owned()), _ => return Err("invalid target"),
}) })
} }
} }
impl std::fmt::Display for Architecture { impl std::fmt::Display for Architecture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self { f.write_str(self.as_str())
Architecture::BpfEl => "bpfel-unknown-none",
Architecture::BpfEb => "bpfeb-unknown-none",
})
} }
} }
@ -40,28 +47,22 @@ pub struct Options {
} }
pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> { pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("{{project-name}}-ebpf"); let Options { target, release } = opts;
let target = format!("--target={}", opts.target);
let mut args = vec![ let mut cmd = Command::new("cargo");
"build", cmd.current_dir("{{project-name}}-ebpf")
target.as_str(), // Command::new creates a child process which inherits all env variables. This means env
"-Z", // vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed so
"build-std=core", // the rust-toolchain.toml file in the -ebpf folder is honored.
]; .env_remove("RUSTUP_TOOLCHAIN")
if opts.release { .args(["build", "--target", target.as_str()]);
args.push("--release")
if release {
cmd.arg("--release");
} }
// Command::new creates a child process which inherits all env variables. This means env let status = cmd.status().context("failed to build bpf program")?;
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed anyhow::ensure!(status.success(), "failed to build bpf program: {}", status);
// so the rust-toolchain.toml file in the -ebpf folder is honored.
let status = Command::new("cargo")
.current_dir(dir)
.env_remove("RUSTUP_TOOLCHAIN")
.args(&args)
.status()
.expect("failed to build bpf program");
assert!(status.success());
Ok(()) Ok(())
} }

@ -8,7 +8,7 @@ default = []
user = ["aya"] user = ["aya"]
[dependencies] [dependencies]
aya = { version = "0.13", optional = true } aya = { workspace = true, optional = true }
[lib] [lib]
path = "src/lib.rs" path = "src/lib.rs"

@ -1,6 +1,12 @@
# We have this so that one doesn't need to manually pass
# --target=bpfel-unknown-none -Z build-std=core when running cargo
# check/build/doc etc.
#
# NB: this file gets loaded only if you run cargo from this directory, it's
# ignored if you run from the workspace root. See
# https://doc.rust-lang.org/cargo/reference/config.html#hierarchical-structure
[build] [build]
target-dir = "../target" target = ["bpfeb-unknown-none", "bpfel-unknown-none"]
target = "bpfel-unknown-none"
[unstable] [unstable]
build-std = ["core"] build-std = ["core"]

@ -1,3 +0,0 @@
[rust-analyzer]
cargo.target = "bpfel-unknown-none"
checkOnSave.allTargets = false

@ -1,2 +0,0 @@
[editor]
workspace-lsp-roots = []

@ -4,29 +4,11 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
aya-ebpf = "0.1.1"
aya-log-ebpf = "0.1.1"
{{ project-name }}-common = { path = "../{{ project-name }}-common" } {{ project-name }}-common = { path = "../{{ project-name }}-common" }
aya-ebpf = { workspace = true }
aya-log-ebpf = { workspace = true }
[[bin]] [[bin]]
name = "{{ project-name }}" name = "{{ project-name }}"
path = "src/main.rs" path = "src/main.rs"
[profile.dev]
opt-level = 3
debug = false
debug-assertions = false
overflow-checks = false
lto = true
panic = "abort"
incremental = false
codegen-units = 1
rpath = false
[profile.release]
lto = true
panic = "abort"
codegen-units = 1
[workspace]
members = []

@ -1,13 +1,2 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
# The source code of rustc, provided by the rust-src component, is needed for
# building eBPF programs.
components = [
"cargo",
"clippy",
"rust-docs",
"rust-src",
"rust-std",
"rustc",
"rustfmt",
]

@ -138,7 +138,7 @@ use aya_log_ebpf::info;
use {{crate_name}}_common::SockKey; use {{crate_name}}_common::SockKey;
#[map(name="{{sock_map}}")] #[map]
static {{sock_map}}: SockHash<SockKey> = SockHash::<SockKey>::with_max_entries(1024, 0); static {{sock_map}}: SockHash<SockKey> = SockHash::<SockKey>::with_max_entries(1024, 0);
#[sk_msg] #[sk_msg]
@ -353,7 +353,8 @@ fn try_{{crate_name}}(ctx: PerfEventContext) -> Result<u32, u32> {
} }
{%- endcase %} {%- endcase %}
#[cfg(not(test))]
#[panic_handler] #[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
unsafe { core::hint::unreachable_unchecked() } loop {}
} }

@ -2,20 +2,21 @@
name = "{{project-name}}" name = "{{project-name}}"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
publish = false
[dependencies] [dependencies]
aya = "0.13" {{project-name}}-common = { path = "../{{project-name}}-common", features = ["user"] }
aya-log = "0.2"
anyhow = { workspace = true }
aya = { workspace = true }
aya-log = { workspace = true }
env_logger = {workspace = true }
libc = { workspace = true }
log = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "net", "signal"] }
{% if program_types_with_opts contains program_type -%} {% if program_types_with_opts contains program_type -%}
clap = { version = "4.1", features = ["derive"] } clap = { workspace = true, features = ["derive"] }
{% endif -%} {% endif -%}
{{project-name}}-common = { path = "../{{project-name}}-common", features = ["user"] }
anyhow = "1"
env_logger = "0.10"
libc = "0.2"
log = "0.4"
tokio = { version = "1.25", features = ["macros", "rt", "rt-multi-thread", "net", "signal"] }
[[bin]] [[bin]]
name = "{{project-name}}" name = "{{project-name}}"

Loading…
Cancel
Save