From bc9f059d539c5a033b242c7f7dab3c5d86b884d9 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 10 Jul 2023 16:36:56 -0400 Subject: [PATCH] xtask: inline build_ebpf --- xtask/src/build_ebpf.rs | 56 ----------------------------------------- xtask/src/docs/mod.rs | 16 ++++++++++-- xtask/src/main.rs | 13 ++-------- xtask/src/run.rs | 46 ++++++++++++++++++++++++++++++--- xtask/src/utils.rs | 24 ------------------ 5 files changed, 59 insertions(+), 96 deletions(-) delete mode 100644 xtask/src/build_ebpf.rs delete mode 100644 xtask/src/utils.rs diff --git a/xtask/src/build_ebpf.rs b/xtask/src/build_ebpf.rs deleted file mode 100644 index 5c974921..00000000 --- a/xtask/src/build_ebpf.rs +++ /dev/null @@ -1,56 +0,0 @@ -use std::{path::PathBuf, process::Command}; - -use anyhow::Result; -use clap::Parser; - -use crate::utils::{exec, workspace_root}; - -#[derive(Debug, Copy, Clone)] -pub enum Architecture { - BpfEl, - BpfEb, -} - -impl std::str::FromStr for Architecture { - type Err = &'static str; - - fn from_str(s: &str) -> Result { - Ok(match s { - "bpfel-unknown-none" => Architecture::BpfEl, - "bpfeb-unknown-none" => Architecture::BpfEb, - _ => 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", - }) - } -} - -#[derive(Debug, Parser)] -pub struct BuildEbpfOptions { - /// Set the endianness of the BPF target - #[clap(default_value = "bpfel-unknown-none", long)] - pub target: Architecture, -} - -pub fn build_ebpf(opts: BuildEbpfOptions) -> Result<()> { - let BuildEbpfOptions { target } = opts; - - let mut dir = PathBuf::from(workspace_root()); - dir.push("test/integration-ebpf"); - - exec( - Command::new("cargo") - .current_dir(&dir) - .args(["+nightly", "build", "--release", "--target"]) - .arg(target.to_string()) - .args(["-Z", "build-std=core"]) - .current_dir(&dir), - ) -} diff --git a/xtask/src/docs/mod.rs b/xtask/src/docs/mod.rs index d392798c..c7333d4c 100644 --- a/xtask/src/docs/mod.rs +++ b/xtask/src/docs/mod.rs @@ -1,5 +1,4 @@ -use crate::utils::exec; -use anyhow::{Context as _, Result}; +use anyhow::{anyhow, Context as _, Result}; use std::{ path::{Path, PathBuf}, process::Command, @@ -9,6 +8,19 @@ use std::{fs, io, io::Write}; use indoc::indoc; +pub fn exec(cmd: &mut Command) -> Result<()> { + let status = cmd + .status() + .with_context(|| format!("failed to run {cmd:?}"))?; + match status.code() { + Some(code) => match code { + 0 => Ok(()), + code => Err(anyhow!("{cmd:?} exited with code {code}")), + }, + None => Err(anyhow!("{cmd:?} terminated by signal")), + } +} + pub fn docs() -> Result<()> { let current_dir = PathBuf::from("."); let header_path = current_dir.join("header.html"); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c86da059..4a7736c8 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,10 +1,6 @@ -mod build_ebpf; mod codegen; mod docs; mod run; -pub(crate) mod utils; - -use std::process::exit; use clap::Parser; #[derive(Parser)] @@ -20,17 +16,12 @@ enum Command { IntegrationTest(run::Options), } -fn main() { +fn main() -> anyhow::Result<()> { let XtaskOptions { command } = Parser::parse(); - let ret = match command { + match command { Command::Codegen(opts) => codegen::codegen(opts), Command::Docs => docs::docs(), Command::IntegrationTest(opts) => run::run(opts), - }; - - if let Err(e) = ret { - eprintln!("{e:#}"); - exit(1); } } diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 7e3d4c22..fcd10aaa 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -9,7 +9,32 @@ use anyhow::{Context as _, Result}; use cargo_metadata::{Artifact, CompilerMessage, Message, Target}; use clap::Parser; -use crate::build_ebpf::{build_ebpf, Architecture, BuildEbpfOptions as BuildOptions}; +#[derive(Debug, Copy, Clone)] +pub enum Architecture { + BpfEl, + BpfEb, +} + +impl std::str::FromStr for Architecture { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + Ok(match s { + "bpfel-unknown-none" => Architecture::BpfEl, + "bpfeb-unknown-none" => Architecture::BpfEb, + _ => 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", + }) + } +} #[derive(Debug, Parser)] pub struct Options { @@ -90,8 +115,23 @@ pub fn run(opts: Options) -> Result<()> { run_args, } = opts; - // build our ebpf program followed by our application - build_ebpf(BuildOptions { target: bpf_target }).context("error while building eBPF program")?; + let metadata = cargo_metadata::MetadataCommand::new() + .exec() + .context("cargo metadata")?; + let dir = metadata + .workspace_root + .into_std_path_buf() + .join("test") + .join("integration-ebpf"); + + crate::docs::exec( + Command::new("cargo") + .current_dir(&dir) + .args(["+nightly", "build", "--release", "--target"]) + .arg(bpf_target.to_string()) + .args(["-Z", "build-std=core"]) + .current_dir(&dir), + )?; let binaries = build(release).context("error while building userspace application")?; let mut args = runner.trim().split_terminator(' '); diff --git a/xtask/src/utils.rs b/xtask/src/utils.rs deleted file mode 100644 index f2cd2471..00000000 --- a/xtask/src/utils.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::{cell::OnceCell, process::Command}; - -use anyhow::{bail, Context as _, Result}; - -pub fn workspace_root() -> &'static str { - static mut WORKSPACE_ROOT: OnceCell = OnceCell::new(); - unsafe { &mut WORKSPACE_ROOT }.get_or_init(|| { - let cmd = cargo_metadata::MetadataCommand::new(); - cmd.exec().unwrap().workspace_root.to_string() - }) -} - -pub fn exec(cmd: &mut Command) -> Result<()> { - let status = cmd - .status() - .with_context(|| format!("failed to run {cmd:?}"))?; - match status.code() { - Some(code) => match code { - 0 => Ok(()), - code => bail!("{cmd:?} exited with code {code}"), - }, - None => bail!("{cmd:?} terminated by signal"), - } -}