From 93dcafca387e050aeb0d9017dd063a336bfefdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Fri, 5 Nov 2021 02:13:46 +0000 Subject: [PATCH] xtask: codegen: fix bindings for archs other than x86 --- xtask/src/codegen/aya.rs | 31 ++++++++++++++++----------- xtask/src/codegen/aya_bpf_bindings.rs | 25 ++++++++++++++++++--- xtask/src/codegen/mod.rs | 11 ++++++++++ 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/xtask/src/codegen/aya.rs b/xtask/src/codegen/aya.rs index 7164a246..6214fb1f 100644 --- a/xtask/src/codegen/aya.rs +++ b/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 "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); } diff --git a/xtask/src/codegen/aya_bpf_bindings.rs b/xtask/src/codegen/aya_bpf_bindings.rs index a5d74ce0..a7b6cb8c 100644 --- a/xtask/src/codegen/aya_bpf_bindings.rs +++ b/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 "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"), diff --git a/xtask/src/codegen/mod.rs b/xtask/src/codegen/mod.rs index c363b193..c42aa534 100644 --- a/xtask/src/codegen/mod.rs +++ b/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, }