Skip to content

Commit

Permalink
Replace xtask with build scripts
Browse files Browse the repository at this point in the history
Adapt aya-rs/aya@3d463a3 and subsequent work
to the template. This has worked very well for us in the main project,
and our users should get the same hotness.
  • Loading branch information
tamird committed Oct 10, 2024
1 parent 4da4bf4 commit 51da029
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 244 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 }
cargo_metadata = { version = "0.18.0", 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.
Expand All @@ -18,6 +19,7 @@ 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 }
which = { version = "6.0.0", default-features = false }

[profile.dev]
opt-level = 3
Expand Down
28 changes: 3 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,7 @@

1. Install bpf-linker: `cargo install bpf-linker`

## Build eBPF
## Build, Run

```bash
cargo xtask build-ebpf
```

To perform a release build you can use the `--release` flag.
You may also change the target architecture with the `--target` flag.

## Build Userspace

```bash
cargo build
```

## Build eBPF and Userspace

```bash
cargo xtask build
```

## Run

```bash
RUST_LOG=info cargo xtask run
```
Use `cargo build`, `cargo run`, etc. as normal. Cargo build scripts are used to
automatically build the eBPF correctly and include it in the program.
9 changes: 4 additions & 5 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ esac

cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
pushd test
cargo xtask build
cargo xtask build --release
cargo build --package test
cargo build --package test --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both test
# and test-ebpf both depend on test-common and test activates test-common's aya dependency, we end
# up trying to compile the panic handler twice: once from the bpf program, and again from std via
# aya.
# and test-ebpf depend on test-common and test activates test-common's aya dependency, we end up
# trying to compile the panic handler twice: once from the bpf program, and again from std via aya.
cargo clippy --exclude test-ebpf --all-targets --workspace -- --deny warnings
cargo clippy --package test-ebpf --all-targets -- --deny warnings
popd
Expand Down
4 changes: 0 additions & 4 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@
name = "xtask"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
clap = { workspace = true, default-features = true, features = ["derive"] }
41 changes: 0 additions & 41 deletions xtask/src/build.rs

This file was deleted.

68 changes: 0 additions & 68 deletions xtask/src/build_ebpf.rs

This file was deleted.

1 change: 1 addition & 0 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const AYA_BUILD_EBPF: &str = "AYA_BUILD_EBPF";
36 changes: 0 additions & 36 deletions xtask/src/main.rs

This file was deleted.

55 changes: 0 additions & 55 deletions xtask/src/run.rs

This file was deleted.

4 changes: 4 additions & 0 deletions {{project-name}}-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ edition = "2021"
aya-ebpf = { workspace = true }
aya-log-ebpf = { workspace = true }

[build-dependencies]
which = { workspace = true }
xtask = { path = "../xtask" }

[[bin]]
name = "{{ project-name }}"
path = "src/main.rs"
30 changes: 30 additions & 0 deletions {{project-name}}-ebpf/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::env;

use which::which;
use xtask::AYA_BUILD_EBPF;

/// Building this crate has an undeclared dependency on the `bpf-linker` binary. This would be
/// better expressed by [artifact-dependencies][bindeps] but issues such as
/// https://github.com/rust-lang/cargo/issues/12385 make their use impractical for the time being.
///
/// This file implements an imperfect solution: it causes cargo to rebuild the crate whenever the
/// mtime of `which bpf-linker` changes. Note that possibility that a new bpf-linker is added to
/// $PATH ahead of the one used as the cache key still exists. Solving this in the general case
/// would require rebuild-if-changed-env=PATH *and* rebuild-if-changed={every-directory-in-PATH}
/// which would likely mean far too much cache invalidation.
///
/// [bindeps]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html?highlight=feature#artifact-dependencies
fn main() {
println!("cargo:rerun-if-env-changed={}", AYA_BUILD_EBPF);

let build_ebpf = env::var(AYA_BUILD_EBPF)
.as_deref()
.map(str::parse)
.map(Result::unwrap)
.unwrap_or_default();

if build_ebpf {
let bpf_linker = which("bpf-linker").unwrap();
println!("cargo:rerun-if-changed={}", bpf_linker.to_str().unwrap());
}
}
3 changes: 3 additions & 0 deletions {{project-name}}-ebpf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![no_std]

// This file exists to enable the library target.
17 changes: 17 additions & 0 deletions {{project-name}}/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "net"
clap = { workspace = true, features = ["derive"] }
{% endif -%}

[build-dependencies]
cargo_metadata = { workspace = true }
# TODO(https://github.com/rust-lang/cargo/issues/12375): this should be an artifact dependency, but
# it's not possible to tell cargo to use `-Z build-std` to build it. We cargo-in-cargo in the build
# script to build this, but we want to teach cargo about the dependecy so that cache invalidation
# works properly.
#
# Note also that https://github.com/rust-lang/cargo/issues/10593 occurs when `target = ...` is added
# to an artifact dependency; it seems possible to work around that by setting `resolver = "1"` in
# Cargo.toml in the workspace root.
#
# Finally note that *any* usage of `artifact = ...` in *any* Cargo.toml in the workspace breaks
# workflows with stable cargo; stable cargo outright refuses to load manifests that use unstable
# features.
{{project-name}}-ebpf = { path = "../{{project-name}}-ebpf" }
xtask = { path = "../xtask"}

[[bin]]
name = "{{project-name}}"
path = "src/main.rs"
Loading

0 comments on commit 51da029

Please sign in to comment.