Skip to content

Commit

Permalink
wip: restore xtask run
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Oct 11, 2024
1 parent 9661423 commit 925479b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
23 changes: 23 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
47 changes: 47 additions & 0 deletions xtask/src/run.rs
Original file line number Diff line number Diff line change
@@ -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<OsString>,
}

/// 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(())
}
4 changes: 2 additions & 2 deletions {{project-name}}/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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");
Expand Down

0 comments on commit 925479b

Please sign in to comment.