Browse Source

xtask: codegen: fix bindings for archs other than x86

Alessandro Decina 3 years ago
parent
commit
eb654d1e3e
3 changed files with 51 additions and 16 deletions
  1. 18 13
      xtask/src/codegen/aya.rs
  2. 22 3
      xtask/src/codegen/aya_bpf_bindings.rs
  3. 11 0
      xtask/src/codegen/mod.rs

+ 18 - 13
xtask/src/codegen/aya.rs

@@ -157,6 +157,24 @@ fn codegen_bindings(opts: &Options) -> Result<(), anyhow::Error> {
     for arch in Architecture::supported() {
         let mut bindgen = builder();
 
+        // Set target triple. This will set the right flags (which you can see
+        // running clang -target=X  -E - -dM </dev/null)
+        let target = match arch {
+            Architecture::X86_64 => "x86_64-unknown-linux-gnu",
+            Architecture::ARMv7 => "armv7-unknown-linux-gnu",
+            Architecture::AArch64 => "aarch64-unknown-linux-gnu",
+        };
+        bindgen = bindgen.clang_args(&["-target", target]);
+
+        // Set the sysroot. This is needed to ensure that the correct arch
+        // specific headers are imported.
+        let sysroot = match arch {
+            Architecture::X86_64 => &opts.x86_64_sysroot,
+            Architecture::ARMv7 => &opts.armv7_sysroot,
+            Architecture::AArch64 => &opts.aarch64_sysroot,
+        };
+        bindgen = bindgen.clang_args(&["-I", &*sysroot.to_string_lossy()]);
+
         for x in &types {
             bindgen = bindgen.allowlist_type(x);
         }
@@ -164,19 +182,6 @@ fn codegen_bindings(opts: &Options) -> Result<(), anyhow::Error> {
             bindgen = bindgen.allowlist_var(x).constified_enum("BTF_KIND_.*");
         }
 
-        // FIXME: this stuff is probably debian/ubuntu specific
-        match arch {
-            Architecture::X86_64 => {
-                bindgen = bindgen.clang_args(&["-I", "/usr/include/x86_64-linux-gnu"]);
-            }
-            Architecture::ARMv7 => {
-                bindgen = bindgen.clang_args(&["-I", "/usr/arm-linux-gnueabi/include"]);
-            }
-            Architecture::AArch64 => {
-                bindgen = bindgen.clang_args(&["-I", "/usr/aarch64-linux-gnu/include"]);
-            }
-        };
-
         for x in &types {
             bindgen = bindgen.allowlist_type(x);
         }

+ 22 - 3
xtask/src/codegen/aya_bpf_bindings.rs

@@ -70,9 +70,27 @@ pub fn codegen(opts: &Options) -> Result<(), anyhow::Error> {
     };
 
     for arch in Architecture::supported() {
-        let generated = dir.join("src").join(arch.to_string());
-
-        let bindings = builder()
+        let mut bindgen = builder();
+
+        // Set target triple. This will set the right flags (which you can see
+        // running clang -target=X  -E - -dM </dev/null)
+        let target = match arch {
+            Architecture::X86_64 => "x86_64-unknown-linux-gnu",
+            Architecture::ARMv7 => "armv7-unknown-linux-gnu",
+            Architecture::AArch64 => "aarch64-unknown-linux-gnu",
+        };
+        bindgen = bindgen.clang_args(&["-target", target]);
+
+        // Set the sysroot. This is needed to ensure that the correct arch
+        // specific headers are imported.
+        let sysroot = match arch {
+            Architecture::X86_64 => &opts.x86_64_sysroot,
+            Architecture::ARMv7 => &opts.armv7_sysroot,
+            Architecture::AArch64 => &opts.aarch64_sysroot,
+        };
+        bindgen = bindgen.clang_args(&["-I", &*sysroot.to_string_lossy()]);
+
+        let bindings = bindgen
             .generate()
             .map_err(|_| anyhow!("bindgen failed"))?
             .to_string();
@@ -84,6 +102,7 @@ pub fn codegen(opts: &Options) -> Result<(), anyhow::Error> {
             tree.items[index] = Item::Verbatim(TokenStream::new())
         }
 
+        let generated = dir.join("src").join(arch.to_string());
         // write the bindings, with the original helpers removed
         write_to_file_fmt(
             &generated.join("bindings.rs"),

+ 11 - 0
xtask/src/codegen/mod.rs

@@ -53,6 +53,17 @@ pub struct Options {
     #[structopt(long)]
     libbpf_dir: PathBuf,
 
+    // sysroot options. Default to ubuntu headers installed by the
+    // libc6-dev-{arm64,armel}-cross packages.
+    #[structopt(long, default_value = "/usr/include/x86_64-linux-gnu")]
+    x86_64_sysroot: PathBuf,
+
+    #[structopt(long, default_value = "/usr/aarch64-linux-gnu/include")]
+    aarch64_sysroot: PathBuf,
+
+    #[structopt(long, default_value = "/usr/arm-linux-gnueabi/include")]
+    armv7_sysroot: PathBuf,
+
     #[structopt(subcommand)]
     command: Option<Command>,
 }