Add xtask for building ebpf programs
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>pull/1/head
parent
9e1c761d8d
commit
5d018fd6ba
@ -0,0 +1,2 @@
|
|||||||
|
[alias]
|
||||||
|
xtask = "run --package xtask --"
|
@ -1,2 +1,2 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["{{project-name}}", "{{project-name}}-common"]
|
members = ["{{project-name}}", "{{project-name}}-common", "xtask"]
|
||||||
|
@ -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"
|
@ -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<Self, Self::Err> {
|
||||||
|
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(())
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,2 @@
|
|||||||
[build]
|
[build]
|
||||||
target-dir = "../target"
|
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"
|
|
Loading…
Reference in New Issue