Rust for linux build error due to Clang (bindgen) version

From: Yuta Hayama
Date: Tue Jul 04 2023 - 05:06:49 EST


Hi,

I am a beginner in Rust and LLVM, so sorry if this is a rudimentary question.
I tried to build with CONFIG_RUST=y on the torvalds/linux kernel and got the
following error:

$ LANG=C make O=../build_6.5rc_pre LLVM=1

... (snip)

RUSTC L rust/core.o
EXPORTS rust/exports_core_generated.h
RUSTC P rust/libmacros.so
BINDGEN rust/bindings/bindings_generated.rs
thread 'main' panicked at '"ftrace_branch_data_union_(anonymous_at_/home/lineo/kernel/git_kernel_org/torvalds_linux/linux/include/linux/compiler_types_h_146_2)" is not a valid Ident', /home/lineo/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.24/src/fallback.rs:693:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
make[2]: *** [/home/lineo/kernel/git.kernel.org/torvalds.linux/linux/rust/Makefile:310: rust/bindings/bindings_generated.rs] Error 1
make[2]: *** Deleting file 'rust/bindings/bindings_generated.rs'
make[1]: *** [/home/lineo/kernel/git.kernel.org/torvalds.linux/linux/Makefile:1293: prepare] Error 2
make[1]: Leaving directory '/home/lineo/kernel/git.kernel.org/torvalds.linux/build_6.5rc_pre'
make: *** [Makefile:226: __sub-make] Error 2

The versions of the tools and other items used are as follows:

kernel: torvalds/linux a1257b5e3b7f8a21faf462d0118067fe31e71ffb
rustc: 1.68.2
bindgen: 0.56.0
clang: 17.0.0


This issue seems similar to what is described at the following URL.
https://stackoverflow.com/questions/76443280/rust-bindgen-causes-a-is-not-a-valid-ident-error-on-build

If this explanation is correct, then a Clang earlier than 16 or bindgen 0.62.0
or later is needed to avoid this issue. And I searched the ML and found that
a patch to fix it already exists.
https://lore.kernel.org/rust-for-linux/20230612194311.24826-1-aakashsensharma@xxxxxxxxx/

But I think this patch is still in the rust-dev branch of the rust-for-linux
tree and has not been merged into the mainline. The current mainline kernel
seems to have a issue where if the Clang is too new (16 or later), the build
fails even though `make rustavailable` passes.

Does this mean we need something like max-tool-version.sh? But that might be
a bit silly... If such "error occurs because the tool is too new" is repeated
in the future, I feel that it is necessary to check not only the lower limit of
the tool version, but also the upper limit.


Regards,

Yuta Hayama