I met with @emilio a few days ago (early February 2024), and we discussed this for a bit, including the bindgen-side solution/workaround of emitting extra code so that we can fetch the value.
The variations we discussed were "one pass per macro" (slow, but could work without user input), "one extra pass for all macros" (this would be fast, but would require the user to pass e.g. a regex of matching macros to evaluate that are "known to be constants" already); as well as the possibility of "appending the macros later on" (i.e. add them into a context or similar, which may be possible or not with the current API; we should also check e.g. what Clang-Repl/Cling does here).
So, in summary, bindgen would be happy with a workaround. But solving it in LLVM is, of course, the best long-term, so that would be great too (ideally both, so that we can use the feature before projects like the kernel have a minimum LLVM version >= 19).
For the LLVM side, @AaronBallman (the Clang maintainer) told me he would be happy to review our changes if we sent them, in order to help bindgen, so that is great if someone wants to work on it.
Note: see as well the "rust-for-linux" label in
bindgen: https://github.com/rust-lang/rust-bindgen/labels/rust-for-linux.Features that we would like to see
Required (we almost certainly want them)
Support for a GCC-based backend.
Support non-trivial object-like macros that evaluate to a numeric value when fully expanded.
#define A ((int)1).#defines using_IO*.--clang-macro-fallback, kernel patch with workarounds to testbindgenavailable): https://lore.kernel.org/rust-for-linux/CANiq72m20pom+B9EmWO+91E8fjbMEob3JmvHRQ6UaXe_JmatfA@mail.gmail.com/.--clang-macro-fallbackinteraction with-includerust-lang/rust-bindgen#3069.--clang-macro-fallbackinteraction with-Wp,-MMD,file.drust-lang/rust-bindgen#3070.--clang-macro-fallbackduplicate definitions rust-lang/rust-bindgen#3071.--clang-macro-fallback's types): Fails to generate correct code whenUINT64_Cis used rust-lang/rust-bindgen#3200.Support aligned members (including structs with bitfields) inside packed structs.
#[repr(C, align(...)]inside an#[repr(C, packed)].struct alt_instr: https://lore.kernel.org/all/202212272003.rgQDX8DQ-lkp@intel.com/#[repr(align)]type rust-lang/rust-bindgen#2179.#[repr(packed)]whenalignis needed rust-lang/rust-bindgen#2734.Support a new "enum variant" kind, similar to
rustified_enum, that provides a safe method to transform a C enum to Rust.constified_enum + rustified_enumwith conversions rust-lang/rust-bindgen#2646.enums, from Add option to generate safe and unsafe conversions for rustified enums rust-lang/rust-bindgen#2908 (comment)): [RFC] Revamp interface for enum representations rust-lang/rust-bindgen#3050.--with-derive-custom-structdoesn't apply to__BindgenBitfieldUnit.--with-derive-custom-structdoesn't apply to__BindgenBitfieldUnitrust-lang/rust-bindgen#3196.Support passing attributes on the command line to add
#[zerocopy(on_error = skip)].zerocopyissue: Support silent failure in derives google/zerocopy#1769.zerocopyPR (implements#[zerocopy(on_error = skip)]): [derive] Implement#[zerocopy(on_error = skip/fail)]google/zerocopy#2982 (0.8.39).A solution to convert headers with many groups of
#defineconstants such asinclude/uapi/linux/input-event-codes.h.include/uapi/linux/input-event-codes.h(that have many groups of#defineconstants, some with special values like*_CNT/*_MAX) and, with minimal modifications (perhaps in the form of comments), have them produce more idiomatic code (e.g. such as if each group was a properenum).enums (which we did in the past).Nice to have (not critical, we could workaround if needed, etc.)
process_commentsvia CLI.bindgendoes not generateunion+ManuallyDropfor some cases.manually_dropstyle is requested (e.g. via--default-non-copy-union-style manually_dropor--manually-drop-union '.*').unions with onlyDECLARE_FLEX_ARRAYsuch as instruct bpf_array.union+ManuallyDrop#1137.Translating inline assembly into Rust.
bindgencan translate it into Rust inline assembly." in LLVM and Clang wanted features & bugfixes #1132.Translating function-like macros into Rust.
--translate-function-macros): Add--translate-function-macrosto translate C function-like macros to Rustconst fnrust-lang/rust-bindgen#3350.const fns with a single type for input/output parameters.Low priority (we will likely not use them in the end)
Done (stabilized, fixed, not needed anymore, etc.)
Support
unsafe_op_in_unsafe_fn.unsafe_op_in_unsafe_fnrust-lang/rust-bindgen#2063.Support
_Noreturn,[[noreturn]],__attribute__((noreturn))_Noreturn,[[noreturn]],__attribute__((noreturn))rust-lang/rust-bindgen#2094.Support
typedefs shadowing types e.g.typedef struct T *T;.maple_*nodes (Build fail on linux-next (next-20220629 tag): the namemaple_enodeis defined multiple times #795).const struct *andstruct *patterns rust-lang/rust-bindgen#2304.Take advantage of
feature(core_ffi_c)andfeature(c_size_t).--ctypes-prefix core::ffi.feature(core_ffi_c)(and possiblyfeature(c_size_t)) rust-lang/rust-bindgen#2231.#[feature(core_ffi_c)]when available rust-lang/rust-bindgen#2267.A way to query the Clang version being used.
bindgenfor LLVM version being used rust-lang/rust-bindgen#2138.Compile-time layout tests.
Support
static inlinefunctions in headers (by generating a wrapper).bindgencurrently (0.64.0) has experimental support via--wrap-static-fns --experimental.skbuff.hhas 300+static inlines: https://lore.kernel.org/rust-for-linux/f28d6403-d042-4ffb-9872-044388d0f9d9@lunn.ch/ (also sub-thread at https://lore.kernel.org/rust-for-linux/CANiq72kD6HVrDTjQrTXDujyjA4Z5ELM6Nh8kFfVfNXrvpfU1wA@mail.gmail.com/).--wrap-static-fnsrust-lang/rust-bindgen#2928.Raw pointer bitfield accessors.
Rust for Linux in the
bindgenCI.CI: update to v6.12-rc5.
A way to specify a MSRV, instead of
--rust-target(which only supports a subset of versions).--rust-target, the kernel supported Rust 1.78.0+ andbindgen0.65.1+, thus we picked--rust-target 1.68, since it is the maximum stable version supported by the--rust-targetflag inbindgen0.65.1 (and it is lower than our MSRV).RustTargetrust-lang/rust-bindgen#2993.A way to specify an output Edition, e.g.
--rust-edition.--rust-editionrust-lang/rust-bindgen#3002.CI: update to v6.13-rc1.
Support for Edition 2024.
unsafe externissue: Generateunsafe externblocks (Edition 2024) rust-lang/rust-bindgen#2901.unsafe externPR: Add support for unsafe extern blocks rust-lang/rust-bindgen#3015.CI: update to v6.14 (which also fixes the
soft-floatissue since bindgen uses stable).CI: update to v6.15-rc4.
CI: update to v6.16-rc1.
CI: update to v6.17-rc2.
CI: update to v6.18-rc3.
Support
--with-attribute-custom-enum.Bugs that we would like to see fixed
Required (we almost certainly want them)
BindgenUnion can get incorrect alignment on i686.
Issues with integer types assigned to macros.
Nice to have (probably not critical, we could workaround if needed, etc.)
Low priority (we will likely not use them in the end)
--targetis not passed tobindgen.--targetin all cases, thus we should not be affected.clang_macro_fallbackdoesn't propagate implicit--targetto fallback translation unit rust-lang/rust-bindgen#3352.Done (stabilized, fixed, or not needed anymore, etc.)
Fix the
called `Result::unwrap()` on an `Err` value: FromBytesWithNulError { kind: InteriorNul(4) }issue (affects 0.66.0 and 0.66.1, fixed in 0.68.0, note that 0.67.x does not exist, patched in e.g. Debian).bindgenversions 0.66.0 and 0.66.1").Result::unwrap()on anErrvalue: FromBytesWithNulError { kind: InteriorNul(29) } rust-lang/rust-bindgen#2566.Support
--versionwithout header (affects 0.69.0, fixed in 0.69.1, regressed in 0.71.0, fixed again in 0.71.1).bindgen0.69.0 issue").bindgen --versiondoes not work (0.71.0 regression) rust-lang/rust-bindgen#3039.C javadoc comments are not Markdown-escaped, triggering
rustdocwarnings.rustdocwarnings rust-lang/rust-bindgen#2057.Attribute
btf_type_tagmakesconstness of pointers disappear.btf_type_tagmakesconstness of pointers disappear rust-lang/rust-bindgen#2244."Unable to find libclang" problems (rust compiler not found #934), possibly related to Clang 15.
clang-sysversion rust-lang/rust-bindgen#2368.--wrap-static-fnsgenerates code thatreturns a call to avoidfunction.--wrap-static-fnsgenerates code thatreturns a call to avoidfunction rust-lang/rust-bindgen#2485.--wrap-static-fnsgenerates()for empty parameter lists.--wrap-static-fnsgenerates()for empty parameter lists rust-lang/rust-bindgen#2484.Fix the support for linking
libclangstatically.Missing fields in nested class with LLVM 22.
Forward references to
enumtypes end up generating the wrong type.enumtypes end up generating the wrong type rust-lang/rust-bindgen#3179.Not a bug (the report was not considered a bug)
struct_group).