From 5d018fd6bad1b649c39182d7a2b9f18de1e15a8f Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Thu, 5 Aug 2021 10:58:07 +0100 Subject: [PATCH] Add xtask for building ebpf programs Signed-off-by: Dave Tucker --- .cargo/config.toml | 2 + Cargo.toml | 2 +- README.md | 7 +-- xtask/Cargo.toml | 10 ++++ xtask/src/build_ebpf.rs | 62 ++++++++++++++++++++++++ xtask/src/main.rs | 29 +++++++++++ {{project-name}}-ebpf/.cargo/config.toml | 9 +--- {{project-name}}-ebpf/Cargo.toml | 6 +-- 8 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/build_ebpf.rs create mode 100644 xtask/src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..f0ccbc9 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --" \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 07c0cc1..c386364 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["{{project-name}}", "{{project-name}}-common"] +members = ["{{project-name}}", "{{project-name}}-common", "xtask"] diff --git a/README.md b/README.md index 5628bf0..832c205 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,12 @@ ## Build eBPF ```bash -pushd {{project-name}}-ebpf -cargo +nightly build -popd +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 diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..3b50084 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "xtask" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +structopt = {version = "0.3", default-features = false } +anyhow = "1" \ No newline at end of file diff --git a/xtask/src/build_ebpf.rs b/xtask/src/build_ebpf.rs new file mode 100644 index 0000000..21d8220 --- /dev/null +++ b/xtask/src/build_ebpf.rs @@ -0,0 +1,62 @@ +use std::path::PathBuf; +use std::process::Command; + +use structopt::StructOpt; + +#[derive(Debug, Copy, Clone)] +pub enum Architecture { + BpfEl, + BpfEb, +} + +impl std::str::FromStr for Architecture { + type Err = String; + + fn from_str(s: &str) -> Result { + Ok(match s { + "bpfel-unknown-none" => Architecture::BpfEl, + "bpfeb-unknown-none" => Architecture::BpfEb, + _ => return Err("invalid target".to_owned()), + }) + } +} + +impl std::fmt::Display for Architecture { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Architecture::BpfEl => "bpfel-unknown-none", + Architecture::BpfEb => "bpfeb-unknown-none", + }) + } +} + +#[derive(StructOpt)] +pub struct Options { + #[structopt(default_value = "bpfel-unknown-none", long)] + target: Architecture, + #[structopt(long)] + release: bool, +} + +pub fn build(opts: Options) -> Result<(), anyhow::Error> { + let dir = PathBuf::from("{{project-name}}-ebpf"); + let target = format!("--target={}", opts.target); + let mut args = vec![ + "+nightly", + "build", + "--verbose", + target.as_str(), + "-Z", + "build-std=core", + ]; + if opts.release { + args.push("--release") + } + let status = Command::new("cargo") + .current_dir(&dir) + .args(&args) + .status() + .expect("failed to build bpf examples"); + assert!(status.success()); + Ok(()) +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..96308fd --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,29 @@ +mod build_ebpf; + +use std::process::exit; + +use structopt::StructOpt; +#[derive(StructOpt)] +pub struct Options { + #[structopt(subcommand)] + command: Command, +} + +#[derive(StructOpt)] +enum Command { + BuildEbpf(build_ebpf::Options), +} + +fn main() { + let opts = Options::from_args(); + + use Command::*; + let ret = match opts.command { + BuildEbpf(opts) => build_ebpf::build(opts), + }; + + if let Err(e) = ret { + eprintln!("{:#}", e); + exit(1); + } +} diff --git a/{{project-name}}-ebpf/.cargo/config.toml b/{{project-name}}-ebpf/.cargo/config.toml index 26cdee3..e6759b6 100644 --- a/{{project-name}}-ebpf/.cargo/config.toml +++ b/{{project-name}}-ebpf/.cargo/config.toml @@ -1,9 +1,2 @@ [build] -target-dir = "../target" -target = "bpfel-unknown-none" - -[target.bpfel-unknown-none] -rustflags = "-Z build-std=core --target bpfel-unknown-none" - -[target.bpfeb-unknown-none] -rustflags = "-Z build-std=core --target bpfel-unknown-none" \ No newline at end of file +target-dir = "../target" \ No newline at end of file diff --git a/{{project-name}}-ebpf/Cargo.toml b/{{project-name}}-ebpf/Cargo.toml index 8b47edf..ea32147 100644 --- a/{{project-name}}-ebpf/Cargo.toml +++ b/{{project-name}}-ebpf/Cargo.toml @@ -1,14 +1,14 @@ [package] -name = "{{project-name}}-ebpf" +name = "{{ project-name }}-ebpf" version = "0.1.0" edition = "2018" [dependencies] aya-bpf = { git = "http://github.com/alessandrod/aya", branch = "main" } -{{project-name}}-common = { path = "../{{project-name}}-common" } +{{ project-name }}-common = { path = "../{{ project-name }}-common" } [[bin]] -name = "{{project-name}}" +name = "{{ project-name }}" path = "src/main.rs" [profile.dev]