mod.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. mod aya;
  2. mod aya_ebpf_bindings;
  3. mod helpers;
  4. use std::path::{Path, PathBuf};
  5. use clap::Parser;
  6. const SUPPORTED_ARCHS: &[Architecture] = &[
  7. Architecture::Mips,
  8. Architecture::X86_64,
  9. Architecture::ARMv7,
  10. Architecture::AArch64,
  11. Architecture::RISCV64,
  12. Architecture::PowerPC64,
  13. Architecture::S390X,
  14. ];
  15. #[derive(Debug, Copy, Clone)]
  16. pub enum Architecture {
  17. X86_64,
  18. ARMv7,
  19. AArch64,
  20. RISCV64,
  21. PowerPC64,
  22. S390X,
  23. Mips,
  24. }
  25. impl Architecture {
  26. pub fn supported() -> &'static [Architecture] {
  27. SUPPORTED_ARCHS
  28. }
  29. }
  30. impl std::str::FromStr for Architecture {
  31. type Err = &'static str;
  32. fn from_str(s: &str) -> Result<Self, Self::Err> {
  33. Ok(match s {
  34. "mips" => Architecture::Mips,
  35. "x86_64" => Architecture::X86_64,
  36. "armv7" => Architecture::ARMv7,
  37. "aarch64" => Architecture::AArch64,
  38. "riscv64" => Architecture::RISCV64,
  39. "powerpc64" => Architecture::PowerPC64,
  40. "s390x" => Architecture::S390X,
  41. _ => return Err("invalid architecture"),
  42. })
  43. }
  44. }
  45. impl std::fmt::Display for Architecture {
  46. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  47. f.write_str(match self {
  48. Architecture::Mips => "mips",
  49. Architecture::X86_64 => "x86_64",
  50. Architecture::ARMv7 => "armv7",
  51. Architecture::AArch64 => "aarch64",
  52. Architecture::RISCV64 => "riscv64",
  53. Architecture::PowerPC64 => "powerpc64",
  54. Architecture::S390X => "s390x",
  55. })
  56. }
  57. }
  58. // sysroot options. Default to ubuntu headers installed by the
  59. // libc6-dev-{arm64,armel}-cross packages.
  60. #[derive(Parser)]
  61. pub struct SysrootOptions {
  62. #[arg(long, default_value = "/usr/include/x86_64-linux-gnu", action)]
  63. x86_64_sysroot: PathBuf,
  64. #[arg(long, default_value = "/usr/aarch64-linux-gnu/include", action)]
  65. aarch64_sysroot: PathBuf,
  66. #[arg(long, default_value = "/usr/arm-linux-gnueabi/include", action)]
  67. armv7_sysroot: PathBuf,
  68. #[arg(long, default_value = "/usr/riscv64-linux-gnu/include", action)]
  69. riscv64_sysroot: PathBuf,
  70. #[arg(long, default_value = "/usr/powerpc64le-linux-gnu/include", action)]
  71. powerpc64_sysroot: PathBuf,
  72. #[arg(long, default_value = "/usr/s390x-linux-gnu/include", action)]
  73. s390x_sysroot: PathBuf,
  74. #[arg(long, default_value = "/usr/mips-linux-gnu/include", action)]
  75. mips_sysroot: PathBuf,
  76. }
  77. #[derive(Parser)]
  78. pub struct Options {
  79. #[command(flatten)]
  80. sysroot_options: SysrootOptions,
  81. #[command(subcommand)]
  82. command: Option<Command>,
  83. }
  84. #[derive(clap::Subcommand)]
  85. enum Command {
  86. #[command(name = "aya")]
  87. Aya,
  88. #[command(name = "aya-ebpf-bindings")]
  89. AyaEbpfBindings,
  90. }
  91. pub fn codegen(opts: Options, libbpf_dir: &Path) -> Result<(), anyhow::Error> {
  92. let Options {
  93. sysroot_options,
  94. command,
  95. } = opts;
  96. match command {
  97. Some(command) => match command {
  98. Command::Aya => aya::codegen(&sysroot_options, libbpf_dir),
  99. Command::AyaEbpfBindings => aya_ebpf_bindings::codegen(&sysroot_options, libbpf_dir),
  100. },
  101. None => {
  102. aya::codegen(&sysroot_options, libbpf_dir)?;
  103. aya_ebpf_bindings::codegen(&sysroot_options, libbpf_dir)
  104. }
  105. }
  106. }