]> www.infradead.org Git - nvme.git/commitdiff
kbuild: rust: add PROCMACROLDFLAGS
authorHONG Yifan <elsk@google.com>
Tue, 12 Nov 2024 18:44:55 +0000 (19:44 +0100)
committerMiguel Ojeda <ojeda@kernel.org>
Wed, 15 Jan 2025 08:53:54 +0000 (09:53 +0100)
These are flags to be passed when linking proc macros for the Rust
toolchain. If unset, it defaults to $(KBUILD_HOSTLDFLAGS).

This is needed because the list of flags to link hostprogs is not
necessarily the same as the list of flags used to link libmacros.so.
When we build proc macros, we need the latter, not the former (e.g. when
using a Rust compiler binary linked to a different C library than host
programs).

To distinguish between the two, introduce this new variable to stand
out from KBUILD_HOSTLDFLAGS used to link other host progs.

Signed-off-by: HONG Yifan <elsk@google.com>
Link: https://lore.kernel.org/r/20241017210430.2401398-2-elsk@google.com
[ v3:

  - `export`ed the variable. Otherwise it would not be visible in
    `rust/Makefile`.

  - Removed "additional" from the documentation and commit message,
    since this actually replaces the other flags, unlike other cases.

  - Added example of use case to documentation and commit message.
    Thanks Alice for the details on what Google needs!

  - Instead of `HOSTLDFLAGS`, used `KBUILD_HOSTLDFLAGS` as the fallback
    to preserve the previous behavior as much as possible, as discussed
    with Alice/Yifan. Thus moved the variable down too (currently we
    do not modify `KBUILD_HOSTLDFLAGS` elsewhere) and avoided
    mentioning `HOSTLDFLAGS` directly in the documentation.

  - Fixed documentation header formatting.

  - Reworded slightly.

         - Miguel ]
Tested-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: HONG Yifan <elsk@google.com>
Acked-by: Masahiro Yamada <masahiroy@kernel.org>
Link: https://lore.kernel.org/r/20241112184455.855133-1-ojeda@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Documentation/kbuild/kbuild.rst
Makefile
rust/Makefile

index 17c9f920f03da6b341525895533ffb4c100c4837..3388a10f2dcc8930d73bac9d5a8f79ac79fe8d0f 100644 (file)
@@ -91,6 +91,17 @@ HOSTRUSTFLAGS
 -------------
 Additional flags to be passed to $(HOSTRUSTC) when building host programs.
 
+PROCMACROLDFLAGS
+----------------
+Flags to be passed when linking Rust proc macros. Since proc macros are loaded
+by rustc at build time, they must be linked in a way that is compatible with
+the rustc toolchain being used.
+
+For instance, it can be useful when rustc uses a different C library than
+the one the user wants to use for host programs.
+
+If unset, it defaults to the flags passed when linking host programs.
+
 HOSTLDFLAGS
 -----------
 Additional flags to be passed when linking host programs.
index 80d9943d9744f8288d3fc88bed8213988b0d15c2..4e4c2b446ccfb4ef552a57fc1d820f9a904a0407 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -497,6 +497,7 @@ KBUILD_HOSTRUSTFLAGS := $(rust_common_flags) -O -Cstrip=debuginfo \
                        -Zallow-features= $(HOSTRUSTFLAGS)
 KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
 KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
+KBUILD_PROCMACROLDFLAGS := $(or $(PROCMACROLDFLAGS),$(KBUILD_HOSTLDFLAGS))
 
 # Make variables (CC, etc...)
 CPP            = $(CC) -E
@@ -621,7 +622,7 @@ export HOSTRUSTC KBUILD_HOSTRUSTFLAGS
 export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
 export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
 export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
-export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
+export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS KBUILD_PROCMACROLDFLAGS LDFLAGS_MODULE
 export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
 
 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
index 5c8f35f5ada278b7293f6949073fe460caa15663..71a05a3c895a0cb87a8eac571064140bdac0b30d 100644 (file)
@@ -361,7 +361,7 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
       cmd_rustc_procmacro = \
        $(RUSTC_OR_CLIPPY) $(rust_common_flags) \
                -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
-               -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
+               -Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
                --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
                --crate-type proc-macro \
                --crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) $<