Skip to content

Commit

Permalink
Move dependencies to workspace
Browse files Browse the repository at this point in the history
Add "{{project-name}}-ebpf" to workspace. Update various cargo configs
to match main aya repo.
  • Loading branch information
tamird committed Oct 10, 2024
1 parent e064758 commit 7b74302
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 93 deletions.
2 changes: 0 additions & 2 deletions .cargo/rust-analyzer.toml

This file was deleted.

34 changes: 33 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
[workspace]
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
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ esac

cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
pushd test
cargo xtask build-ebpf
cargo build
cargo xtask build
cargo xtask build --release
popd
exit 0
4 changes: 2 additions & 2 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
clap = { version = "4.1", features = ["derive"] }
anyhow = { workspace = true }
clap = { workspace = true, default-features = true, features = ["derive"] }
30 changes: 14 additions & 16 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,26 @@ pub struct Options {
pub release: bool,
}

/// Build the project
fn build_project(opts: &Options) -> Result<(), anyhow::Error> {
/// Build our ebpf program and the userspace program.
pub fn build(opts: Options) -> Result<(), anyhow::Error> {
let Options { bpf_target, release } = opts;

// Build our ebpf program.
build_ebpf(BuildOptions {
target: bpf_target,
release: release,
})?;

// Build our userspace program.
let mut args = vec!["build"];
if opts.release {
if release {
args.push("--release")
}
let status = Command::new("cargo")
.args(&args)
.status()
.expect("failed to build userspace");
assert!(status.success());
Ok(())
}
.context("failed to build userspace")?;
anyhow::ensure!(status.success(), "failed to build userspace program: {}", status);

/// Build our ebpf program and the project
pub fn build(opts: Options) -> Result<(), anyhow::Error> {
// build our ebpf program followed by our application
build_ebpf(BuildOptions {
target: opts.bpf_target,
release: opts.release,
})
.context("Error while building eBPF program")?;
build_project(&opts).context("Error while building userspace application")?;
Ok(())
}
37 changes: 17 additions & 20 deletions xtask/src/build_ebpf.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use std::{path::PathBuf, process::Command};
use std::process::Command;

use anyhow::Context as _;
use clap::Parser;

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Clone)]
pub enum Architecture {
BpfEl,
BpfEb,
}

impl std::str::FromStr for Architecture {
type Err = String;
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"bpfel-unknown-none" => Architecture::BpfEl,
"bpfeb-unknown-none" => Architecture::BpfEb,
_ => return Err("invalid target".to_owned()),
_ => return Err("invalid target"),
})
}
}
Expand All @@ -40,28 +41,24 @@ pub struct Options {
}

pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("{{project-name}}-ebpf");
let target = format!("--target={}", opts.target);
let mut args = vec![
"build",
target.as_str(),
"-Z",
"build-std=core",
];
if opts.release {
let Options { target, release } = opts;

let target = target.to_string();
let mut args = vec!["build", "--target", target.as_str()];
if release {
args.push("--release")
}

// Command::new creates a child process which inherits all env variables. This means env
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed
// so the rust-toolchain.toml file in the -ebpf folder is honored.

let status = Command::new("cargo")
.current_dir(dir)
.current_dir("{{project-name}}-ebpf")
// Command::new creates a child process which inherits all env variables. This means env
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed so
// the rust-toolchain.toml file in the -ebpf folder is honored.
.env_remove("RUSTUP_TOOLCHAIN")
.args(&args)
.status()
.expect("failed to build bpf program");
assert!(status.success());
.context("failed to build bpf program")?;
anyhow::ensure!(status.success(), "failed to build bpf program: {}", status);

Ok(())
}
2 changes: 1 addition & 1 deletion {{project-name}}-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ default = []
user = ["aya"]

[dependencies]
aya = { version = "0.13", optional = true }
aya = { workspace = true, optional = true }

[lib]
path = "src/lib.rs"
10 changes: 8 additions & 2 deletions {{project-name}}-ebpf/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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]
target-dir = "../target"
target = "bpfel-unknown-none"
target = ["bpfeb-unknown-none", "bpfel-unknown-none"]

[unstable]
build-std = ["core"]
3 changes: 0 additions & 3 deletions {{project-name}}-ebpf/.cargo/rust-analyzer.toml

This file was deleted.

2 changes: 0 additions & 2 deletions {{project-name}}-ebpf/.helix/config.toml

This file was deleted.

24 changes: 3 additions & 21 deletions {{project-name}}-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,11 @@ version = "0.1.0"
edition = "2021"

[dependencies]
aya-ebpf = "0.1.1"
aya-log-ebpf = "0.1.1"
{{ project-name }}-common = { path = "../{{ project-name }}-common" }

aya-ebpf = { workspace = true }
aya-log-ebpf = { workspace = true }

[[bin]]
name = "{{ project-name }}"
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 = []
11 changes: 0 additions & 11 deletions {{project-name}}-ebpf/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
[toolchain]
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",
]
21 changes: 11 additions & 10 deletions {{project-name}}/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
name = "{{project-name}}"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
aya = "0.13"
aya-log = "0.2"
{{project-name}}-common = { path = "../{{project-name}}-common", features = ["user"] }

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 -%}
clap = { version = "4.1", features = ["derive"] }
clap = { workspace = true, features = ["derive"] }
{% 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]]
name = "{{project-name}}"
Expand Down

0 comments on commit 7b74302

Please sign in to comment.