The bindgen commands are currently defined in rust/Makefile which means they are only accessible to be used in the rust/ tree. This is fine as long as all bindgen-generated Rust bindings live under that tree, but there will be a need to run bindgen in other areas of the kernel source. This moves the bindgen commands into scripts/Makefile.build so that they are available to be used by Makefiles anywhere in the kernel source. Signed-off-by: Thomas Bertschinger <tahbertschinger@xxxxxxxxx> --- rust/Makefile | 67 --------------------------------------- scripts/Makefile.build | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 67 deletions(-) diff --git a/rust/Makefile b/rust/Makefile index 9d2a16cc91cb..25b68933750f 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -270,73 +270,6 @@ rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \ $(call if_changed,rustc_test) $(call if_changed,rustc_test_library) -ifdef CONFIG_CC_IS_CLANG -bindgen_c_flags = $(c_flags) -else -# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC -# plugin backend and/or the Clang driver would be perfectly compatible with GCC. -# -# For the moment, here we are tweaking the flags on the fly. This is a hack, -# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT` -# if we end up using one of those structs). -bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \ - -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \ - -mindirect-branch=thunk-extern -mindirect-branch-register \ - -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \ - -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \ - -mno-pointers-to-nested-functions -mno-string \ - -mno-strict-align -mstrict-align \ - -fconserve-stack -falign-jumps=% -falign-loops=% \ - -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \ - -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \ - -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \ - -fzero-call-used-regs=% -fno-stack-clash-protection \ - -fno-inline-functions-called-once -fsanitize=bounds-strict \ - -fstrict-flex-arrays=% \ - --param=% --param asan-% - -# Derived from `scripts/Makefile.clang`. -BINDGEN_TARGET_x86 := x86_64-linux-gnu -BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) - -# All warnings are inhibited since GCC builds are very experimental, -# many GCC warnings are not supported by Clang, they may only appear in -# some configurations, with new GCC versions, etc. -bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) - -# Auto variable zero-initialization requires an additional special option with -# clang that is going to be removed sometime in the future (likely in -# clang-18), so make sure to pass this option only if clang supports it -# (libclang major version < 16). -# -# https://github.com/llvm/llvm-project/issues/44842 -# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags -ifdef CONFIG_INIT_STACK_ALL_ZERO -libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p') -ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1) -bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -endif -endif - -bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \ - $(bindgen_extra_c_flags) -endif - -ifdef CONFIG_LTO -bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags)) -else -bindgen_c_flags_lto = $(bindgen_c_flags) -endif - -bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__ - -quiet_cmd_bindgen = BINDGEN $@ - cmd_bindgen = \ - $(BINDGEN) $< $(bindgen_target_flags) \ - --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \ - --no-debug '.*' \ - -o $@ -- $(bindgen_c_flags_final) -DMODULE \ - $(bindgen_target_cflags) $(bindgen_target_extra) $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ $(shell grep -Ev '^#|^$$' $(srctree)/$(src)/bindgen_parameters) diff --git a/scripts/Makefile.build b/scripts/Makefile.build index dae447a1ad30..a6dd502f3b0c 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -312,6 +312,77 @@ quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ $(obj)/%.ll: $(src)/%.rs FORCE $(call if_changed_dep,rustc_ll_rs) +# Generate Rust bindings (.h -> .rs) +# --------------------------------------------------------------------------- + +ifdef CONFIG_CC_IS_CLANG +bindgen_c_flags = $(c_flags) +else +# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC +# plugin backend and/or the Clang driver would be perfectly compatible with GCC. +# +# For the moment, here we are tweaking the flags on the fly. This is a hack, +# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT` +# if we end up using one of those structs). +bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \ + -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \ + -mindirect-branch=thunk-extern -mindirect-branch-register \ + -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \ + -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \ + -mno-pointers-to-nested-functions -mno-string \ + -mno-strict-align -mstrict-align \ + -fconserve-stack -falign-jumps=% -falign-loops=% \ + -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \ + -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \ + -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \ + -fzero-call-used-regs=% -fno-stack-clash-protection \ + -fno-inline-functions-called-once -fsanitize=bounds-strict \ + -fstrict-flex-arrays=% \ + --param=% --param asan-% + +# Derived from `scripts/Makefile.clang`. +BINDGEN_TARGET_x86 := x86_64-linux-gnu +BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) + +# All warnings are inhibited since GCC builds are very experimental, +# many GCC warnings are not supported by Clang, they may only appear in +# some configurations, with new GCC versions, etc. +bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) + +# Auto variable zero-initialization requires an additional special option with +# clang that is going to be removed sometime in the future (likely in +# clang-18), so make sure to pass this option only if clang supports it +# (libclang major version < 16). +# +# https://github.com/llvm/llvm-project/issues/44842 +# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags +ifdef CONFIG_INIT_STACK_ALL_ZERO +libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p') +ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1) +bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang +endif +endif + +bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \ + $(bindgen_extra_c_flags) +endif + +ifdef CONFIG_LTO +bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags)) +else +bindgen_c_flags_lto = $(bindgen_c_flags) +endif + +bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__ + +export quiet_cmd_bindgen = BINDGEN $@ +export cmd_bindgen = \ + $(BINDGEN) $< $(bindgen_target_flags) \ + --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \ + --no-debug '.*' \ + -o $@ -- $(bindgen_c_flags_final) -DMODULE \ + $(bindgen_target_cflags) $(bindgen_target_extra) + # Compile assembler sources (.S) # --------------------------------------------------------------------------- -- 2.43.0