Browse Source

xtask: inline build_ebpf

Tamir Duberstein 1 year ago
parent
commit
bc9f059d53
5 changed files with 59 additions and 96 deletions
  1. 0 56
      xtask/src/build_ebpf.rs
  2. 14 2
      xtask/src/docs/mod.rs
  3. 2 11
      xtask/src/main.rs
  4. 43 3
      xtask/src/run.rs
  5. 0 24
      xtask/src/utils.rs

+ 0 - 56
xtask/src/build_ebpf.rs

@@ -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<Self, Self::Err> {
-        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),
-    )
-}

+ 14 - 2
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");

+ 2 - 11
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);
     }
 }

+ 43 - 3
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<Self, Self::Err> {
+        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(' ');

+ 0 - 24
xtask/src/utils.rs

@@ -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<String> = 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"),
-    }
-}