rust-lang / rust-lang/rust-bindgen

Loongarch cross-compilation issue

Open
#2,765 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Hi all,

While trying to update the bindings for a crate from using v0.66.1 to v0.69.4, I get a problem for loongarch64-unknown-linux-gnu.

Input C/C++ Header

The header is quite large, you can find it here.

Bindgen Invocation

This works as a cross-compilation for most architectures I need, but fails for loongarch64-unknown-linux-gnu. You can see the build script that regenerates the bindings here. To upgrade to a newer bindgen version we rely on this shell script to iterate over the architectures we target.

        let mut builder = bindgen::Builder::default();
        if make_generic {
            // only WIN32 bindings are "packed". It's easier to "unpack" for other architectures
            // __declspec is not needed and causes problems
            const GENERIC_PRELUDE: &str = "#define _WIN32 1\n#define __declspec(x)\n";
            builder = builder
                // layout tests are not generic
                .layout_tests(false)
                .header_contents("generic-prelude.h", GENERIC_PRELUDE)
        }

        builder = builder
            .header("pkcs11.h")
            .dynamic_library_name("Pkcs11")
            // The PKCS11 library works in a slightly different way to most shared libraries. We have
            // to call `C_GetFunctionList`, which returns a list of pointers to the _actual_ library
            // functions. This is the only function we need to create a binding for.
            .allowlist_function("C_GetFunctionList")
            // This is needed because no types will be generated if `allowlist_function` is used.
            // Unsure if this is a bug.
            .allowlist_type("*")
            .allowlist_file("pkcs11.h")
            // See this issue: https://github.com/parallaxsecond/rust-cryptoki/issues/12
            .blocklist_type("max_align_t")
            // Derive the `Debug` trait for the generated structs where possible.
            .derive_debug(true)
            // Derive the `Default` trait for the generated structs where possible.
            .derive_default(true)
            .parse_callbacks(Box::new(CargoCallbacks));
Actual Results
Caused by:
  process didn't exit successfully: `/data/ionmih01/rust-cryptoki/target/debug/build/cryptoki-sys-029c7783f44b6e05/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at /home/ionmih01/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.69.4/ir/context.rs:556:15:
  libclang error; possible causes include:
  - Invalid flag syntax
  - Unrecognized flags
  - Invalid flag arguments
  - File I/O errors
  - Host vs. target architecture mismatch
  If you encounter an error missing from this list, please file an issue or a PR!
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:645:5
     1: core::panicking::panic_fmt
               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:72:14
     2: core::panicking::panic_display
               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:178:5
     3: core::panicking::panic_str
               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:152:5
     4: core::option::expect_failed
               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/option.rs:1985:5
     5: core::option::Option<T>::expect
               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/option.rs:894:21
     6: bindgen::ir::context::BindgenContext::new
               at /home/ionmih01/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.69.4/ir/context.rs:550:13
     7: bindgen::Bindings::generate
               at /home/ionmih01/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.69.4/lib.rs:896:27
     8: bindgen::Builder::generate
               at /home/ionmih01/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.69.4/lib.rs:349:9
     9: build_script_build::generate::generate_bindings
               at ./build.rs:91:24
    10: build_script_build::main
               at ./build.rs:6:9
    11: core::ops::function::FnOnce::call_once
               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:250:5
Expected Results

I'd be interested to know if this is a problem with our script, our header, or some issue with bindgen.

Please let me know if I've missed any important details.

Also, many thanks for your work, it's empowered so many of our crates 🙏🏻

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with cryptoki-sys/build.rs, especially generate_bindings where the bindgen builder runs, and compare its arguments with regenerate_bindings.sh. Reproduce the failure for loongarch64-unknown-linux-gnu using pkcs11.h, then determine whether the cause is the script, header, or bindgen and document a confirmed path to successful binding generation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, shell
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.