mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.37k stars 305 forks source link

Other calling conventions #979

Open novafacing opened 3 months ago

novafacing commented 3 months ago

This PR adds support for other calling conventions as discussed in #55 and #718. It adds an ABI field (instead of just an extern field) to Functions and adds a concept of Predefines which right now only contains the compiler/platform-specific #defines needed to make the calling conventions work nicely but may be useful in the future for other cases where something needs to look at all the code, then conditionally emit some lines at the top of the file.

This use case is very important to my work to adopt Rust for UEFI/firmware programming, since cbindgen is needed to integrate Rust into existing C codebases easily. Most of these environments define an explicit calling convention, which we need to be able to export from Rust code for interoperability.

novafacing commented 3 months ago

Here's a godbolt with the generated headers (test cconv.rs): https://godbolt.org/z/MqPhhzqcf

There are a few compile errors which I intentionally left in (like requesting sysv64 on clang/gcc non-x86_64). I can change that behavior so a compile error is never emitted and a warning is instead if that's preferred.

novafacing commented 3 months ago

Realized I had a misunderstanding about what the extern_decl flag was doing (toggling extern keyword in output). I put it back to how it was, so now that plays nice with the ABI detection and dispatching.

novafacing commented 3 months ago

Documented the configuration option added for this feature. Should be ready to merge :)

novafacing commented 3 months ago

Calling convention predefines are now emitted in a defined order, should actually be ready to merge.