Переглянути джерело

Merge pull request #96 from alessandrod/cross

Fix bindings for archs other than x86
Alessandro Decina 3 роки тому
батько
коміт
8f20ead0c1

+ 5 - 0
.github/workflows/gen.yml

@@ -20,6 +20,11 @@ jobs:
 
       - uses: Swatinem/rust-cache@v1
 
+      - name: Install headers
+        run: |
+          apt -y update
+          apt -y install libc6-dev libc6-dev-{aarch64,armel}-cross
+
       - name: Run codegen
         run: |
           cargo xtask codegen --libbpf-dir ./libbpf

+ 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>,
 }