tools build: Fix rust cross compilation
Subsystems (3)
PATCH 1/1 tools build: Fix rust cross compilation
Currently no target is specified to compile rust code when needed, which breaks cross compilation. Detect required target and pass it via rust_flags to the compiler.
Note that CROSS_COMPILE might be different from what rust compiler expects, since it may omit the target vendor value, e.g. "aarch64-linux-gnu" instead of "aarch64-unknown-linux-gnu". Thus explicitly map supported CROSS_COMPILE values to corresponding Rust versions, as suggested by Miguel Ojeda.
Tested using arm64 cross-compilation example from [1].
[1]: https://perfwiki.github.io/main/arm64-cross-compilation-dockerfile/
--- tools/build/Build.include | 9 +++++++++ tools/perf/Makefile.config | 14 ++++++++++++++ tools/perf/Makefile.perf | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-)
tools/build/Build.include +9 -0
@@ -98,6 +98,15 @@ c_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1)) c_flags = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2)) cxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj)) +### +# Rust flags to be used on rule definition, includes: +# - global $(RUST_FLAGS) +# - per target Rust flags +# - per object Rust flags +rust_flags_1 = $(RUST_FLAGS) $(RUST_FLAGS_$(basetarget).o) $(RUST_FLAGS_$(obj)) +rust_flags_2 = $(filter-out $(RUST_FLAGS_REMOVE_$(basetarget).o), $(rust_flags_1)) +rust_flags = $(filter-out $(RUST_FLAGS_REMOVE_$(obj)), $(rust_flags_2)) + ### ## HOSTCC C flags
tools/perf/Makefile.config +14 -0
@@ -1163,6 +1163,20 @@ ifndef NO_RUST CFLAGS += -DHAVE_RUST_SUPPORT $(call detected,CONFIG_RUST_SUPPORT) endif + + ifneq ($(CROSS_COMPILE),) + RUST_TARGET_FLAGS_arm := arm-unknown-linux-gnueabi + RUST_TARGET_FLAGS_arm64 := aarch64-unknown-linux-gnu + RUST_TARGET_FLAGS_m68k := m68k-unknown-linux-gnu + RUST_TARGET_FLAGS_mips := mipsel-unknown-linux-gnu + RUST_TARGET_FLAGS_powerpc := powerpc64le-unknown-linux-gnu + RUST_TARGET_FLAGS_riscv := riscv64-unknown-linux-gnu + RUST_TARGET_FLAGS_s390 := s390x-unknown-linux-gnu + RUST_TARGET_FLAGS_x86 := x86_64-unknown-linux-gnu + RUST_TARGET_FLAGS_x86_64 := x86_64-unknown-linux-gnu + + RUST_FLAGS += --target=$(RUST_TARGET_FLAGS_$(ARCH)) + endif endif # Among the variables below, these:
tools/perf/Makefile.perf +1 -1
@@ -271,7 +271,7 @@ ifeq ($(PYLINT),1) PYLINT := $(shell which pylint 2> /dev/null) endif -export srctree OUTPUT RM CC CXX RUSTC LD AR CFLAGS CXXFLAGS V BISON FLEX AWK +export srctree OUTPUT RM CC CXX RUSTC LD AR CFLAGS CXXFLAGS RUST_FLAGS V BISON FLEX AWK export HOSTCC HOSTLD HOSTAR HOSTCFLAGS SHELLCHECK MYPY PYLINT include $(srctree)/tools/build/Makefile.include
base-commit: 5d1ab659fb93eed85d6d8b2937013360157032f4
Signature · 1 line
2.52.0
Quoted 11 lines · Dmitrii Dolgov
Currently no target is specified to compile rust code when needed, which breaks cross compilation. Detect required target and pass it via
rust_flagsto the compiler.Note that
CROSS_COMPILEmight be different from what rust compiler expects, since it may omit the target vendor value, e.g. "aarch64-linux-gnu" instead of "aarch64-unknown-linux-gnu". Thus explicitly map supportedCROSS_COMPILEvalues to corresponding Rust versions, as suggested by Miguel Ojeda.
Quoted 5 lines
Tested using arm64 cross-compilation example from [1].
[1]: https://perfwiki.github.io/main/arm64-cross-compilation-dockerfile/
Signed-off-by Dmitrii Dolgov <[email protected]>
I think this was meant to v2.
By the way, should this have a Fixes: tag? i.e. since it sounds like this fixes an issue. Perhaps to 6a32fa5ccd33 ("tools build: Add a feature test for rust compiler"), or something else? I also usually recommend showing the error message in the commit message.
(Also, you can use "Link: ... [1]" for the link.)
Cc'ing Kbuild maintainers too.
I hope that helps!
Cheers,
Miguel
Quoted 2 lines · Miguel Ojeda
I think this was meant to v2.
Yep, I forgot to change the version (it was a bad idea to send it out Friday evenig).
Quoted 4 lines
By the way, should this have a Fixes: tag? i.e. since it sounds like this fixes an issue. Perhaps to
6a32fa5ccd33("tools build: Add a feature test for rust compiler"), or something else? I also usually recommend showing the error message in the commit message.
Right, I can add an example what happens.
Quoted 1 line
(Also, you can use "Link: ... [1]" for the link.)
Got it, thanks!