build_ebpf.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use std::path::PathBuf;
  2. use std::process::Command;
  3. use clap::Parser;
  4. #[derive(Debug, Copy, Clone)]
  5. pub enum Architecture {
  6. BpfEl,
  7. BpfEb,
  8. }
  9. impl std::str::FromStr for Architecture {
  10. type Err = String;
  11. fn from_str(s: &str) -> Result<Self, Self::Err> {
  12. Ok(match s {
  13. "bpfel-unknown-none" => Architecture::BpfEl,
  14. "bpfeb-unknown-none" => Architecture::BpfEb,
  15. _ => return Err("invalid target".to_owned()),
  16. })
  17. }
  18. }
  19. impl std::fmt::Display for Architecture {
  20. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  21. f.write_str(match self {
  22. Architecture::BpfEl => "bpfel-unknown-none",
  23. Architecture::BpfEb => "bpfeb-unknown-none",
  24. })
  25. }
  26. }
  27. #[derive(Debug, Parser)]
  28. pub struct Options {
  29. /// Set the endianness of the BPF target
  30. #[clap(default_value = "bpfel-unknown-none", long)]
  31. pub target: Architecture,
  32. /// Build the release target
  33. #[clap(long)]
  34. pub release: bool,
  35. }
  36. pub fn build_ebpf(opts: Options) -> Result<(), anyhow::Error> {
  37. let dir = PathBuf::from("responder-ebpf");
  38. let target = format!("--target={}", opts.target);
  39. let mut args = vec![
  40. "+nightly",
  41. "build",
  42. "--verbose",
  43. target.as_str(),
  44. "-Z",
  45. "build-std=core",
  46. ];
  47. if opts.release {
  48. args.push("--release")
  49. }
  50. let status = Command::new("cargo")
  51. .current_dir(&dir)
  52. .args(&args)
  53. .status()
  54. .expect("failed to build bpf program");
  55. assert!(status.success());
  56. Ok(())
  57. }