From: Thomas Bertschinger <tahbertschinger@gmail.com>
To: rust-for-linux@vger.kernel.org, linux-bcachefs@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-kbuild@vger.kernel.org,
kent.overstreet@linux.dev, bfoster@redhat.com, ojeda@kernel.org,
alex.gaynor@gmail.com, wedsonaf@gmail.com, masahiroy@kernel.org
Cc: Thomas Bertschinger <tahbertschinger@gmail.com>
Subject: [PATCH RFC 1/3] kbuild: rust: move bindgen commands to top-level
Date: Tue, 6 Feb 2024 22:57:51 -0700 [thread overview]
Message-ID: <20240207055751.611644-1-tahbertschinger@gmail.com> (raw)
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@gmail.com>
---
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
reply other threads:[~2024-02-07 5:58 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240207055751.611644-1-tahbertschinger@gmail.com \
--to=tahbertschinger@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=bfoster@redhat.com \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcachefs@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).