You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.9 KiB
Rust
72 lines
1.9 KiB
Rust
use std::{path::PathBuf, process::Command};
|
|
|
|
use clap::Parser;
|
|
|
|
#[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(Debug, Parser)]
|
|
pub struct Options {
|
|
/// Set the endianness of the BPF target
|
|
#[clap(default_value = "bpfel-unknown-none", long)]
|
|
pub target: Architecture,
|
|
/// Set the rust toolchain for the BPF program (only nightly toolchains are supported)
|
|
#[clap(default_value = "+nightly", long)]
|
|
pub toolchain: String,
|
|
/// Build the release target
|
|
#[clap(long)]
|
|
pub release: bool,
|
|
}
|
|
|
|
pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
|
|
let dir = PathBuf::from("{{project-name}}-ebpf");
|
|
let target = format!("--target={}", opts.target);
|
|
let toolchain = if opts.toolchain.starts_with('+') {
|
|
opts.toolchain
|
|
} else {
|
|
format!("+{}", opts.toolchain)
|
|
};
|
|
let mut args = vec![
|
|
toolchain.as_str(),
|
|
"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 program");
|
|
assert!(status.success());
|
|
Ok(())
|
|
}
|