Skip to content

Commit ceff075

Browse files
HONG Yifanojeda
HONG Yifan
authored andcommitted
kbuild: rust: add PROCMACROLDFLAGS
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ 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 <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: HONG Yifan <[email protected]> Acked-by: Masahiro Yamada <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent c80dd3f commit ceff075

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Documentation/kbuild/kbuild.rst

+11
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ HOSTRUSTFLAGS
9191
-------------
9292
Additional flags to be passed to $(HOSTRUSTC) when building host programs.
9393

94+
PROCMACROLDFLAGS
95+
----------------
96+
Flags to be passed when linking Rust proc macros. Since proc macros are loaded
97+
by rustc at build time, they must be linked in a way that is compatible with
98+
the rustc toolchain being used.
99+
100+
For instance, it can be useful when rustc uses a different C library than
101+
the one the user wants to use for host programs.
102+
103+
If unset, it defaults to the flags passed when linking host programs.
104+
94105
HOSTLDFLAGS
95106
-----------
96107
Additional flags to be passed when linking host programs.

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ KBUILD_HOSTRUSTFLAGS := $(rust_common_flags) -O -Cstrip=debuginfo \
497497
-Zallow-features= $(HOSTRUSTFLAGS)
498498
KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
499499
KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
500+
KBUILD_PROCMACROLDFLAGS := $(or $(PROCMACROLDFLAGS),$(KBUILD_HOSTLDFLAGS))
500501

501502
# Make variables (CC, etc...)
502503
CPP = $(CC) -E
@@ -621,7 +622,7 @@ export HOSTRUSTC KBUILD_HOSTRUSTFLAGS
621622
export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
622623
export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
623624
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
624-
export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
625+
export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS KBUILD_PROCMACROLDFLAGS LDFLAGS_MODULE
625626
export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
626627

627628
export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS

rust/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
361361
cmd_rustc_procmacro = \
362362
$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
363363
-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
364-
-Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
364+
-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
365365
--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
366366
--crate-type proc-macro \
367367
--crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) $<

0 commit comments

Comments
 (0)