|
|
|
@ -1,31 +1,38 @@
|
|
|
|
|
use std::{path::PathBuf, process::Command};
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
|
|
use anyhow::Context as _;
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum Architecture {
|
|
|
|
|
BpfEl,
|
|
|
|
|
BpfEb,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Architecture {
|
|
|
|
|
pub fn as_str(&self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
Architecture::BpfEl => "bpfel-unknown-none",
|
|
|
|
|
Architecture::BpfEb => "bpfeb-unknown-none",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::str::FromStr for Architecture {
|
|
|
|
|
type Err = String;
|
|
|
|
|
type Err = &'static str;
|
|
|
|
|
|
|
|
|
|
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()),
|
|
|
|
|
_ => return Err("invalid target"),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
})
|
|
|
|
|
f.write_str(self.as_str())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -40,28 +47,22 @@ pub struct Options {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
|
|
|
|
|
let dir = PathBuf::from("{{project-name}}-ebpf");
|
|
|
|
|
let target = format!("--target={}", opts.target);
|
|
|
|
|
let mut args = vec![
|
|
|
|
|
"build",
|
|
|
|
|
target.as_str(),
|
|
|
|
|
"-Z",
|
|
|
|
|
"build-std=core",
|
|
|
|
|
];
|
|
|
|
|
if opts.release {
|
|
|
|
|
args.push("--release")
|
|
|
|
|
let Options { target, release } = opts;
|
|
|
|
|
|
|
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
|
cmd.current_dir("{{project-name}}-ebpf")
|
|
|
|
|
// Command::new creates a child process which inherits all env variables. This means env
|
|
|
|
|
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed so
|
|
|
|
|
// the rust-toolchain.toml file in the -ebpf folder is honored.
|
|
|
|
|
.env_remove("RUSTUP_TOOLCHAIN")
|
|
|
|
|
.args(["build", "--target", target.as_str()]);
|
|
|
|
|
|
|
|
|
|
if release {
|
|
|
|
|
cmd.arg("--release");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Command::new creates a child process which inherits all env variables. This means env
|
|
|
|
|
// vars set by the cargo xtask command are also inherited. RUSTUP_TOOLCHAIN is removed
|
|
|
|
|
// so the rust-toolchain.toml file in the -ebpf folder is honored.
|
|
|
|
|
let status = cmd.status().context("failed to build bpf program")?;
|
|
|
|
|
anyhow::ensure!(status.success(), "failed to build bpf program: {}", status);
|
|
|
|
|
|
|
|
|
|
let status = Command::new("cargo")
|
|
|
|
|
.current_dir(dir)
|
|
|
|
|
.env_remove("RUSTUP_TOOLCHAIN")
|
|
|
|
|
.args(&args)
|
|
|
|
|
.status()
|
|
|
|
|
.expect("failed to build bpf program");
|
|
|
|
|
assert!(status.success());
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|