mod.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. mod aya;
  2. mod aya_bpf_bindings;
  3. mod helpers;
  4. use std::path::PathBuf;
  5. use clap::Parser;
  6. const SUPPORTED_ARCHS: &[Architecture] = &[
  7. Architecture::X86_64,
  8. Architecture::ARMv7,
  9. Architecture::AArch64,
  10. Architecture::RISCV64,
  11. ];
  12. #[derive(Debug, Copy, Clone)]
  13. pub enum Architecture {
  14. X86_64,
  15. ARMv7,
  16. AArch64,
  17. RISCV64,
  18. }
  19. impl Architecture {
  20. pub fn supported() -> &'static [Architecture] {
  21. SUPPORTED_ARCHS
  22. }
  23. }
  24. impl std::str::FromStr for Architecture {
  25. type Err = String;
  26. fn from_str(s: &str) -> Result<Self, Self::Err> {
  27. Ok(match s {
  28. "x86_64" => Architecture::X86_64,
  29. "armv7" => Architecture::ARMv7,
  30. "aarch64" => Architecture::AArch64,
  31. "riscv64" => Architecture::RISCV64,
  32. _ => return Err("invalid architecture".to_owned()),
  33. })
  34. }
  35. }
  36. impl std::fmt::Display for Architecture {
  37. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  38. f.write_str(match self {
  39. Architecture::X86_64 => "x86_64",
  40. Architecture::ARMv7 => "armv7",
  41. Architecture::AArch64 => "aarch64",
  42. Architecture::RISCV64 => "riscv64",
  43. })
  44. }
  45. }
  46. #[derive(Parser)]
  47. pub struct Options {
  48. #[clap(long, action)]
  49. libbpf_dir: PathBuf,
  50. // sysroot options. Default to ubuntu headers installed by the
  51. // libc6-dev-{arm64,armel}-cross packages.
  52. #[clap(long, default_value = "/usr/include/x86_64-linux-gnu", action)]
  53. x86_64_sysroot: PathBuf,
  54. #[clap(long, default_value = "/usr/aarch64-linux-gnu/include", action)]
  55. aarch64_sysroot: PathBuf,
  56. #[clap(long, default_value = "/usr/arm-linux-gnueabi/include", action)]
  57. armv7_sysroot: PathBuf,
  58. #[clap(long, default_value = "/usr/riscv64-linux-gnu/include", action)]
  59. riscv64_sysroot: PathBuf,
  60. #[clap(subcommand)]
  61. command: Option<Command>,
  62. }
  63. #[derive(Parser)]
  64. enum Command {
  65. #[clap(name = "aya")]
  66. Aya,
  67. #[clap(name = "aya-bpf-bindings")]
  68. AyaBpfBindings,
  69. }
  70. pub fn codegen(opts: Options) -> Result<(), anyhow::Error> {
  71. use Command::*;
  72. match opts.command {
  73. Some(Aya) => aya::codegen(&opts),
  74. Some(AyaBpfBindings) => aya_bpf_bindings::codegen(&opts),
  75. None => {
  76. aya::codegen(&opts)?;
  77. aya_bpf_bindings::codegen(&opts)
  78. }
  79. }
  80. }