RISC-V: re-enable gcc + rust builds
Subsystems (5)
v4 cover RISC-V: re-enable gcc + rust builds
Previous v3 patch:
https://lore.kernel.org/linux-riscv/[email protected]/T/#u
v3 checks option availability via libclang's version. According to Vivian's suggestion, v4 checks option availability by directly running bindgen.
Compared to v3, commits are split into finer granularity, as suggested by Miguel and Vivian. Regarding checking for undefined for BINDGEN_TARGET, I've placed it in a separate commit. Since it's really small, I think it's not really necessary to send it as a new patch email.
Regarding Vivian's mention of riscv32 support, the documentation does not declare riscv32 is maintained, and I have limited experience with riscv32 and no strong motivation to test it. Therefore, this patch didn't consider riscv32.
---
Asuna Yang (4):
rust: export BINDGEN_TARGET from a separate Makefile
rust: generate a fatal error if BINDGEN_TARGET is undefined
rust: add a Kconfig function to test for support of bindgen options
RISC-V: fix gcc + rust builds
Documentation/rust/arch-support.rst | 2 +- Makefile | 2 ++ arch/riscv/Kconfig | 35 ++++++++++++++++++++++++++++++++++- rust/Makefile | 11 ++--------- scripts/Kconfig.include | 5 +++++ scripts/Makefile.rust | 14 ++++++++++++++ 6 files changed, 58 insertions(+), 11 deletions(-)
---
base-commit: f824272b6e3fe24950abc527136e8140f67b0b23
change-id: 20251124-gcc-rust-v4-1584dec7e5df
Best regards,
Signature · 1 line
Asuna Yang <[email protected]>
PATCH v4 1/4 rust: export BINDGEN_TARGET from a separate Makefile
A subsequent commit will add a new function rust-bindgen-option to scripts/Kconfig.include. The bindgen backend requires the --target option for cross compiling, but variable BINDGEN_TARGET in rust/Makefile cannot be exported to scripts/Kconfig.include.
--- Makefile | 2 ++ rust/Makefile | 8 -------- scripts/Makefile.rust | 9 +++++++++ 3 files changed, 11 insertions(+), 8 deletions(-)
Makefile +2 -0
@@ -718,6 +718,8 @@ ifneq ($(findstring clang,$(CC_VERSION_TEXT)),) include $(srctree)/scripts/Makefile.clang endif +include $(srctree)/scripts/Makefile.rust + # Include this also for config targets because some architectures need # cc-cross-prefix to determine CROSS_COMPILE. ifdef need-compiler
rust/Makefile +0 -8
@@ -300,14 +300,6 @@ bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \ -fzero-init-padding-bits=% -mno-fdpic \ --param=% --param asan-% -fno-isolate-erroneous-paths-dereference -# Derived from `scripts/Makefile.clang`. -BINDGEN_TARGET_x86 := x86_64-linux-gnu -BINDGEN_TARGET_arm64 := aarch64-linux-gnu -BINDGEN_TARGET_arm := arm-linux-gnueabi -BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf -BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH)) -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.
scripts/Makefile.rust new file +9 -0
@@ -0,0 +1,9 @@ +# Derived from `scripts/Makefile.clang`. +BINDGEN_TARGET_x86 := x86_64-linux-gnu +BINDGEN_TARGET_arm64 := aarch64-linux-gnu +BINDGEN_TARGET_arm := arm-linux-gnueabi +BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf +BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH)) +BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) + +export BINDGEN_TARGET
Signature · 1 line
2.51.1
PATCH v4 2/4 rust: generate a fatal error if BINDGEN_TARGET is undefined
In reply to Asuna YangGenerate a friendly fatal error if the target triplet is undefined for bindgen, rather than having the compiler generate obscure error messages during the build stage.
This piece of code is copied from scripts/Makefile.clang.
--- scripts/Makefile.rust | 4 ++++ 1 file changed, 4 insertions(+)
scripts/Makefile.rust +4 -0
@@ -6,4 +6,8 @@ BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH)) BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) +ifeq ($(BINDGEN_TARGET),) +$(error add '--target=' option to scripts/Makefile.rust) +else export BINDGEN_TARGET +endif
Signature · 1 line
2.51.1
PATCH v4 3/4 rust: add a Kconfig function to test for support of bindgen options
In reply to Asuna YangUse the BINDGEN_EXTRA_CLANG_ARGS environment variable to pass the --target option for cross compiling, so that the function can be used to test both bindgen options and bindgen backend options.
--- scripts/Kconfig.include | 5 +++++ 1 file changed, 5 insertions(+)
scripts/Kconfig.include +5 -0
@@ -75,3 +75,8 @@ rustc-llvm-version := $(shell,$(srctree)/scripts/rustc-llvm-version.sh $(RUSTC)) # If you are testing for unstable features, consider testing RUSTC_VERSION # instead, as features may have different completeness while available. rustc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(RUSTC) $(1) --crate-type=rlib /dev/null --out-dir=.tmp_$$ -o .tmp_$$/tmp.rlib) + +# $(rust-bindgen-option,<flag>) +# Return y if the Rust bindgen supports <flag>, n otherwise +# To test options for the bindgen backend, add -- at the beginning, for example: $(rust-bindgen-option,-- <backend-flag>) +rust-bindgen-option = $(success,trap "rm -f .tmp_$$.h" EXIT; touch .tmp_$$.h; BINDGEN_EXTRA_CLANG_ARGS+=" --target=$(BINDGEN_TARGET)" $(BINDGEN) .tmp_$$.h $(1))
Signature · 1 line
2.51.1
PATCH v4 4/4 RISC-V: fix gcc + rust builds
In reply to Asuna YangCommit 33549fcf37ec ("RISC-V: disallow gcc + rust builds") disabled GCC
+ Rust builds for RISC-V due to differences in extension handling compared to LLVM.
Add rust-bindgen-option conditions for the availability of libclang to the RISC-V extension Kconfig symbols that depend on the cc-option function.
For Zicsr/Zifencei special handling, since LLVM/Clang always enables these two extensions, either don't pass them to -march, or pass them explicitly and Rust bindgen libclang must recognize them.
Clang does not support -mno-riscv-attribute flag, filter it out to resolve error: unknown argument: '-mno-riscv-attribute'.
Define BINDGEN_TARGET_riscv to pass the target triplet to Rust bindgen libclang for RISC-V to resolve error: unsupported argument 'medany' to option '-mcmodel=' for target 'unknown'.
Update the documentation, GCC + Rust builds for RISC-V are now supported.
--- Documentation/rust/arch-support.rst | 2 +- arch/riscv/Kconfig | 35 ++++++++++++++++++++++++++++++++++- rust/Makefile | 3 ++- scripts/Makefile.rust | 1 + 4 files changed, 38 insertions(+), 3 deletions(-)
Documentation/rust/arch-support.rst +1 -1
@@ -18,7 +18,7 @@ Architecture Level of support Constraints ``arm`` Maintained ARMv7 Little Endian only. ``arm64`` Maintained Little Endian only. ``loongarch`` Maintained \- -``riscv`` Maintained ``riscv64`` and LLVM/Clang only. +``riscv`` Maintained ``riscv64`` only. ``um`` Maintained \- ``x86`` Maintained ``x86_64`` only. ============= ================ ==============================================
arch/riscv/Kconfig +34 -1
@@ -193,7 +193,7 @@ config RISCV select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RETHOOK if !XIP_KERNEL select HAVE_RSEQ - select HAVE_RUST if RUSTC_SUPPORTS_RISCV && CC_IS_CLANG + select HAVE_RUST if RUSTC_SUPPORTS_RISCV && TOOLCHAIN_MATCHES_ZICSR_ZIFENCEI select HAVE_SAMPLE_FTRACE_DIRECT select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR
@@ -617,6 +617,8 @@ config TOOLCHAIN_HAS_V depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32imv) depends on LD_IS_LLD || LD_VERSION >= 23800 depends on AS_HAS_OPTION_ARCH + depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64imv) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32imv) config RISCV_ISA_V bool "Vector extension support"
@@ -681,6 +683,8 @@ config TOOLCHAIN_HAS_ZABHA depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zabha) depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zabha) depends on AS_HAS_OPTION_ARCH + depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64ima_zabha) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32ima_zabha) config RISCV_ISA_ZABHA bool "Zabha extension support for atomic byte/halfword operations"
@@ -699,6 +703,8 @@ config TOOLCHAIN_HAS_ZACAS depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zacas) depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zacas) depends on AS_HAS_OPTION_ARCH + depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64ima_zacas) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32ima_zacas) config RISCV_ISA_ZACAS bool "Zacas extension support for atomic CAS"
@@ -717,6 +723,8 @@ config TOOLCHAIN_HAS_ZBB depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zbb) depends on LD_IS_LLD || LD_VERSION >= 23900 depends on AS_HAS_OPTION_ARCH + depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64ima_zbb) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32ima_zbb) # This symbol indicates that the toolchain supports all v1.0 vector crypto # extensions, including Zvk*, Zvbb, and Zvbc. LLVM added all of these at once.
@@ -732,6 +740,8 @@ config TOOLCHAIN_HAS_ZBA depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zba) depends on LD_IS_LLD || LD_VERSION >= 23900 depends on AS_HAS_OPTION_ARCH + depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64ima_zba) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32ima_zba) config RISCV_ISA_ZBA bool "Zba extension support for bit manipulation instructions"
@@ -767,6 +777,8 @@ config TOOLCHAIN_HAS_ZBC depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zbc) depends on LD_IS_LLD || LD_VERSION >= 23900 depends on AS_HAS_OPTION_ARCH + depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64ima_zbc) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32ima_zbc) config RISCV_ISA_ZBC bool "Zbc extension support for carry-less multiplication instructions"
@@ -790,6 +802,8 @@ config TOOLCHAIN_HAS_ZBKB depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zbkb) depends on LD_IS_LLD || LD_VERSION >= 23900 depends on AS_HAS_OPTION_ARCH + depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64ima_zbkb) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32ima_zbkb) config RISCV_ISA_ZBKB bool "Zbkb extension support for bit manipulation instructions"
@@ -877,6 +891,25 @@ config TOOLCHAIN_NEEDS_OLD_ISA_SPEC versions of clang and GCC to be passed to GAS, which has the same result as passing zicsr and zifencei to -march. +config RUST_BINDGEN_HAS_ZICSR_ZIFENCEI + def_bool y + depends on !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=rv64ima_zicsr_zifencei) + depends on !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=rv32ima_zicsr_zifencei) + +config TOOLCHAIN_MATCHES_ZICSR_ZIFENCEI + def_bool y + # https://github.com/llvm/llvm-project/commit/22e199e6afb1263c943c0c0d4498694e15bf8a16 + depends on TOOLCHAIN_NEEDS_OLD_ISA_SPEC || !TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI || RUST_BINDGEN_HAS_ZICSR_ZIFENCEI + help + LLVM/Clang >= 17.0.0 starts recognizing Zicsr/Zifencei in -march, passing + them to -march doesn't generate an error anymore, and passing them or not + doesn't have any real difference, it still follows ISA before version + 20190608 - Zicsr/Zifencei are included in base ISA. + + The current latest version of LLVM/Clang still does not require explicit + Zicsr/Zifencei to enable these two extensions, Clang just accepts them in + -march and then silently ignores them. + config FPU bool "FPU support" default y
rust/Makefile +2 -1
@@ -298,7 +298,8 @@ bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \ -fno-inline-functions-called-once -fsanitize=bounds-strict \ -fstrict-flex-arrays=% -fmin-function-alignment=% \ -fzero-init-padding-bits=% -mno-fdpic \ - --param=% --param asan-% -fno-isolate-erroneous-paths-dereference + --param=% --param asan-% -fno-isolate-erroneous-paths-dereference \ + -mno-riscv-attribute # All warnings are inhibited since GCC builds are very experimental, # many GCC warnings are not supported by Clang, they may only appear in
scripts/Makefile.rust +1 -0
@@ -3,6 +3,7 @@ BINDGEN_TARGET_x86 := x86_64-linux-gnu BINDGEN_TARGET_arm64 := aarch64-linux-gnu BINDGEN_TARGET_arm := arm-linux-gnueabi BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf +BINDGEN_TARGET_riscv := riscv64-linux-gnu BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH)) BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
Signature · 1 line
2.51.1
Re: [PATCH v4 4/4] RISC-V: fix gcc + rust builds
Quoted 4 lines · Asuna Yang
Commit
33549fcf37ec("RISC-V: disallow gcc + rust builds") disabled GCC
+ Rust builds for RISC-V due to differences in extension handling compared to LLVM.
This should mention what we are doing, i.e. re-enabling the GCC + Rust support again.
Also, I would recommend avoiding to say "fix" in the title, unless it is actually a fix, but perhaps the RISC-V maintainers consider it a fix.
Quoted 3 lines
Add
rust-bindgen-optionconditions for the availability of libclang to the RISC-V extensionKconfigsymbols that depend on thecc-optionfunction.
This is now in a different commit, right?
Quoted 2 lines
Update the documentation, GCC + Rust builds for RISC-V are now supported.
"are now maintained" may be better to avoid confusing it with the "Level of Support" in the table.
Quoted 3 lines
Documentation/rust/arch-support.rst | 2 +- rust/Makefile | 3 ++- scripts/Makefile.rust | 1 +
In any case, for the Rust bits, this seems OK (I didn't test it, though), so:
(By the way, I wouldn't mind if we started adding MAINTAINERS entries on who is maintaining particular architectures for Rust, especially if there is someone in particular involved with it etc.).
Thanks!
Cheers,
Miguel
Re: [PATCH v4 2/4] rust: generate a fatal error if BINDGEN_TARGET is undefined
In reply to Asuna YangQuoted 4 lines · Asuna Yang
Generate a friendly fatal error if the target triplet is undefined for bindgen, rather than having the compiler generate obscure error messages during the build stage.
I normally suggest putting in the log the errors in case someone searches for them and for future reference etc.
Cheers,
Miguel
Re: [PATCH v4 1/4] rust: export BINDGEN_TARGET from a separate Makefile
In reply to Asuna YangQuoted 5 lines · Asuna Yang
A subsequent commit will add a new function
rust-bindgen-optiontoscripts/Kconfig.include. The bindgen backend requires the--targetoption for cross compiling, but variableBINDGEN_TARGETinrust/Makefilecannot be exported toscripts/Kconfig.include.
Please end with another paragraph saying what the commit does, e.g. "Thus move the variable to a new Makefile that can then be imported from the main Makefile" or similar.
Also, I am not sure if this should be called Makefile.bindgen, but I guess it is fine as it is -- with the current name, the new file should be covered by the F: scripts/*rust* rule in MAINTAINERS already, so this looks OK, but please double-check it is the case.
Finally, is it possible to put this under need-compiler like Makefile.compiler? I don't think we need to evaluate those options for things like make help, but perhaps I am missing something. Please see commit 805b2e1d427a ("kbuild: include Makefile.compiler only when compiler is needed").
Thanks!
Cheers,
Miguel
Re: [PATCH v4 3/4] rust: add a Kconfig function to test for support of bindgen options
In reply to Asuna YangQuoted 4 lines · Asuna Yang
Use the
BINDGEN_EXTRA_CLANG_ARGSenvironment variable to pass the--targetoption for cross compiling, so that the function can be used to test both bindgen options and bindgen backend options.
It seems this explains part of how it works, but not what the commit adds and why, i.e. could we start with "Add rustc-bindgen-option..." etc.?
By the way, shouldn't this just be called bindgen-option? Up to the Kbuild team.
More importantly, should we just have a separate function for backend flags? That would be easier and would avoid having to use the environment variable, if I understand the commit message here correctly. And, anyway, it is actually the only function we need so far.
Quoted 1 line
+# Return y if the Rust bindgen supports <flag>, n otherwise
Maybe just "y if bindgen supports"?
Thanks!
Cheers,
Miguel
Re: [PATCH v4 4/4] RISC-V: fix gcc + rust builds
In reply to Miguel OjedaQuoted 4 lines · Miguel Ojeda
Quoted 3 lines
Add
rust-bindgen-optionconditions for the availability of libclang to the RISC-V extensionKconfigsymbols that depend on thecc-optionfunction.This is now in a different commit, right?
Sorry, I may not have made it clear. I mean all these changes.
+ depends on !RUST || !64BIT || $(rust-bindgen-option,-- -mabi=lp64 -march=...) + depends on !RUST || !32BIT || $(rust-bindgen-option,-- -mabi=ilp32 -march=...)
Quoted 3 lines
(By the way, I wouldn't mind if we started adding
MAINTAINERSentries on who is maintaining particular architectures for Rust, especially if there is someone in particular involved with it etc.).
I had some contributions in rust-lang/rust repo for riscv64 (PR #131955, FWIW), and I'd like to be added as a Rust + RISC-V Linux maintainer if that's okay for you guys.
For the suggestions about commit messages, I will improve them in the next revision.
Thanks for your reviews!
Re: [PATCH v4 4/4] RISC-V: fix gcc + rust builds
Quoted 2 lines · Asuna Yang
Sorry, I may not have made it clear. I mean all these changes.
Ah, sorry, I missed the "conditions" word by reading too quickly and misinterpreted it, my bad.
Quoted 3 lines
I had some contributions in rust-lang/rust repo for riscv64 (PR #131955, FWIW), and I'd like to be added as a Rust + RISC-V Linux maintainer if that's okay for you guys.
I think having people explicitly listed like that would be great (e.g. in an ... [RUST] sub-entry to the ARCH one or similar, like we do in other places), and it is a good way to contribute to Linux. I was thinking about it for other architectures too. But let's see what RISC-V maintainers think about it.
Thanks!
Cheers,
Miguel
Re: [PATCH v4 3/4] rust: add a Kconfig function to test for support of bindgen options
In reply to Miguel OjedaQuoted 5 lines · Miguel Ojeda
More importantly, should we just have a separate function for backend flags? That would be easier and would avoid having to use the environment variable, if I understand the commit message here correctly. And, anyway, it is actually the only function we need so far.
You are right, separated might be better.
Another reason is, in scripts/Makefile.compiler, there is a set of functions with the same name provided for use by Makefile. Since we currently do not need to check for bindgen options in Makefile, I didn't add function for bindgen there. If we add it in the future, the bindgen-option function in Kconfig.include will correspond to the bindgen-option-yn function in Makefile.compiler, and bindgen-option function in Makefile.compiler will return the selected option instead of yn.
If we don't separate them now, for consistency, in Makefile.compiler, $(call bindgen-option,-- --some-flag-a,-- --some-flag-b) will return an option prefixed with --, which makes it difficult to use.
I will separate them into two functions in the next revision, bindgen-option and bindgen-backend-option (or bindgen-cc-option? Which one do you prefer?).
Re: [PATCH v4 3/4] rust: add a Kconfig function to test for support of bindgen options
Quoted 4 lines · Asuna Yang
I will separate them into two functions in the next revision,
bindgen-optionandbindgen-backend-option(orbindgen-cc-option? Which one do you prefer?).
If we don't need bindgen-option (the normal one I mean) so far, perhaps we should skip it. On the other hand, rustc-option-yn is there and is not used either (it was added for consistency). Up to the Kbuild team, I guess.
As for the name, no strong preference. bindgen-backend-option sounds good, but bindgen-cc-option is more consistent with cc-option and probably easier to understand for people new to bindgen, and anyway the flags we pass there wouldn't make sense if the backend is not a C compiler.
Thanks!
Cheers,
Miguel
Nathan Chancellor
maintainerRe: [PATCH v4 3/4] rust: add a Kconfig function to test for support of bindgen options
Quoted 10 lines · Miguel Ojeda
Quoted 4 lines · Asuna Yang
I will separate them into two functions in the next revision,
bindgen-optionandbindgen-backend-option(orbindgen-cc-option? Which one do you prefer?).If we don't need
bindgen-option(the normal one I mean) so far, perhaps we should skip it. On the other hand,rustc-option-ynis there and is not used either (it was added for consistency). Up to theKbuildteam, I guess.
I do not have a strong preference one way or the other. These macros tend to be low maintenance and do not take up much space regardless so preemptively adding it so that it is ready for use in the future is no big deal in my opinion.
Quoted 5 lines
As for the name, no strong preference.
bindgen-backend-optionsounds good, butbindgen-cc-optionis more consistent withcc-optionand probably easier to understand for people new tobindgen, and anyway the flags we pass there wouldn't make sense if the backend is not a C compiler.
Yeah, I think either one sort of requires knowledge of bindgen to know the difference between a flag to bindgen and a flag to the C compiler behind bindgen so no strong preference from my side (Nicolas may feel differently).
Cheers,
Nathan