From 925479bdf2c290f225d4adae17e06737cdce6d94 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 11 Oct 2024 11:16:47 -0400 Subject: [PATCH] wip: restore xtask run --- .cargo/config.toml | 2 ++ README.md | 9 +++++--- xtask/Cargo.toml | 4 ++++ xtask/src/main.rs | 23 +++++++++++++++++++ xtask/src/run.rs | 47 +++++++++++++++++++++++++++++++++++++++ {{project-name}}/build.rs | 4 ++-- 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 xtask/src/main.rs create mode 100644 xtask/src/run.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..35049cb --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --" diff --git a/README.md b/README.md index 9bc6def..c870678 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,10 @@ 1. Install bpf-linker: `cargo install bpf-linker` -## Build, Run +## Build & 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. +Use `cargo build`, `cargo check`, etc. as normal. Run your program with `xtask run`. + +Cargo build scripts are used to automatically build the eBPF correctly and include it in the +program. When not using `xtask run`, eBPF code generation is skipped for a faster developer +experience; this compromise necessitates the use of `xtask` to actually build the eBPF. diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index fe005f2..6f639a8 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -2,3 +2,7 @@ name = "xtask" version = "0.1.0" edition = "2021" + +[dependencies] +anyhow = { workspace = true, default-features = true } +clap = { workspace = true, default-features = true, features = ["derive"] } diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..b59e543 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,23 @@ +mod run; + +use anyhow::Result; +use clap::Parser; + +#[derive(Debug, Parser)] +pub struct Options { + #[clap(subcommand)] + command: Command, +} + +#[derive(Debug, Parser)] +enum Command { + Run(run::Options), +} + +fn main() -> Result<()> { + let Options { command } = Parser::parse(); + + match command { + Command::Run(opts) => run::run(opts), + } +} diff --git a/xtask/src/run.rs b/xtask/src/run.rs new file mode 100644 index 0000000..5537598 --- /dev/null +++ b/xtask/src/run.rs @@ -0,0 +1,47 @@ +use std::{ffi::OsString, process::Command}; + +use anyhow::{bail, Context as _, Result}; +use clap::Parser; +use xtask::AYA_BUILD_EBPF; + +#[derive(Debug, Parser)] +pub struct Options { + /// Build and run the release target. + #[clap(long)] + release: bool, + /// The command used to wrap your application. + #[clap(short, long, default_value = "sudo -E")] + runner: String, + /// Arguments to pass to your application. + #[clap(global = true, last = true)] + run_args: Vec, +} + +/// Build and run the project. +pub fn run(opts: Options) -> Result<()> { + let Options { + release, + runner, + run_args, + } = opts; + + let mut cmd = Command::new("cargo"); + cmd.env(AYA_BUILD_EBPF, "true"); + cmd.args(["run", "--package", "{{project-name}}-ebpf", "--config"]); + if release { + cmd.arg(format!("target.release.runner=\"{}\"", runner)); + cmd.arg("--release"); + } else { + cmd.arg(format!("target.dev.runner=\"{}\"", runner)); + } + if !run_args.is_empty() { + cmd.arg("--").args(run_args); + } + let status = cmd + .status() + .with_context(|| format!("failed to run {cmd:?}"))?; + if status.code() != Some(0) { + bail!("{cmd:?} failed: {status:?}") + } + Ok(()) +} diff --git a/{{project-name}}/build.rs b/{{project-name}}/build.rs index 4ead4f6..61caab3 100644 --- a/{{project-name}}/build.rs +++ b/{{project-name}}/build.rs @@ -40,7 +40,7 @@ use xtask::AYA_BUILD_EBPF; fn main() { println!("cargo:rerun-if-env-changed={}", AYA_BUILD_EBPF); - let build_integration_bpf = env::var(AYA_BUILD_EBPF) + let build_ebpf = env::var(AYA_BUILD_EBPF) .as_deref() .map(str::parse) .map(Result::unwrap) @@ -64,7 +64,7 @@ fn main() { panic!("unsupported endian={:?}", endian) }; - if build_integration_bpf { + if build_ebpf { let arch = env::var_os("CARGO_CFG_TARGET_ARCH").unwrap(); let target = format!("{target}-unknown-none");