Add Clang toolchain support for kernel builds
v3 cover Add Clang toolchain support for kernel builds
From: Harish Sadineni <[email protected]>
v2 -> v3:
- Guard against TOOLCHAIN being unset: use d.getVar('TOOLCHAIN') or '' instead of d.getVar('TOOLCHAIN') directly (suggested by Bruce Ashfield).
v1 -> v2:
- TOOLCHAIN:riscv64 now explicitly falls back to "gcc" instead of "" when rust is not in KERNEL_FEATURES, avoiding an empty TOOLCHAIN value.
This series adds LLVM/Clang toolchain support for kernel builds, defaulting to GCC but enabling Clang automatically for riscv64 when Rust is enabled.
-Convert existing GCC tool definitions (KERNEL_CC, KERNEL_LD, KERNEL_AR, etc.)
to use the :toolchain-gcc override, so they only apply when TOOLCHAIN = "gcc"
the existing default behavior is preserved as a no-op.
-Introduce parallel :toolchain-clang definitions using clang, ld.lld, llvm-ar, llvm-objcopy,
and llvm-strip, making it possible to build the kernel with the LLVM/Clang toolchain.
-Automatically select the Clang toolchain for riscv64 kernel builds when Rust support is enabled,
since upstream kernel Rust support on riscv64 requires LLVM/clang (per https://docs.kernel.org/rust/arch-support.html).
GCC remains the default everywhere else, including riscv64 without Rust.
-Add lld-native to DEPENDS, since the lld linker is required when building the kernel with Clang. -Fix a config-check failure under Clang: do_kernel_configcheck was erroring out with
kconfiglib.KconfigError: ... Sorry, this assembler is not supported (from scripts/Kconfig.include). Resolved by setting CLANG_FLAGS = "-fintegrated-as" whenever TOOLCHAIN contains "clang"
this flag is picked up by the kernel build system during config checks so the assembler test passes.
Harish Sadineni (4):
kernel-arch: Add clang toolchain support
kernel-yocto-rust: use clang toolchain for riscv64 when Rust is
enabled
kernel-yocto-rust: Add lld-native to DEPENDS
kernel-yocto: Set CLANG_FLAGS for kernel config checks when using
clang
meta/classes-recipe/kernel-arch.bbclass | 21 ++++++++++++++----- meta/classes-recipe/kernel-yocto-rust.bbclass | 3 +++ meta/classes-recipe/kernel-yocto.bbclass | 3 +++ meta/recipes-kernel/linux/linux-yocto.inc | 4 ++++ 4 files changed, 26 insertions(+), 5 deletions(-)
Signature · 1 line
2.49.0
PATCH v3 1/4 kernel-arch: Add clang toolchain support
From: Harish Sadineni <[email protected]>
Convert the existing GCC tool definitions to use the :toolchain-gcc override so they only apply when TOOLCHAIN = "gcc" (the default).
Introduce new definitions for :toolchain-clang that use clang, ld.lld, llvm-ar, llvm-objcopy, and llvm-strip, allowing the kernel to be built with the LLVM/Clang toolchain.
--- meta/classes-recipe/kernel-arch.bbclass | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
meta/classes-recipe/kernel-arch.bbclass +16 -5
@@ -71,14 +71,25 @@ HOST_AR_KERNEL_ARCH ?= "${TARGET_AR_KERNEL_ARCH}" TARGET_OBJCOPY_KERNEL_ARCH ?= "" HOST_OBJCOPY_KERNEL_ARCH ?= "${TARGET_OBJCOPY_KERNEL_ARCH}" -KERNEL_CC = "${CCACHE}${HOST_PREFIX}gcc ${HOST_CC_KERNEL_ARCH} \ +KERNEL_CC:toolchain-gcc = "${CCACHE}${HOST_PREFIX}gcc ${HOST_CC_KERNEL_ARCH} \ -fuse-ld=bfd ${DEBUG_PREFIX_MAP} \ -ffile-prefix-map=${STAGING_KERNEL_DIR}=${KERNEL_SRC_PATH} \ -ffile-prefix-map=${STAGING_KERNEL_BUILDDIR}=${KERNEL_SRC_PATH} \ " -KERNEL_LD = "${HOST_PREFIX}ld.bfd ${HOST_LD_KERNEL_ARCH}" -KERNEL_AR = "${HOST_PREFIX}ar ${HOST_AR_KERNEL_ARCH}" -KERNEL_OBJCOPY = "${HOST_PREFIX}objcopy ${HOST_OBJCOPY_KERNEL_ARCH}" +KERNEL_LD:toolchain-gcc = "${HOST_PREFIX}ld.bfd ${HOST_LD_KERNEL_ARCH}" +KERNEL_AR:toolchain-gcc = "${HOST_PREFIX}ar ${HOST_AR_KERNEL_ARCH}" +KERNEL_OBJCOPY:toolchain-gcc = "${HOST_PREFIX}objcopy ${HOST_OBJCOPY_KERNEL_ARCH}" # Code in package.py can't handle options on KERNEL_STRIP -KERNEL_STRIP = "${HOST_PREFIX}strip" +KERNEL_STRIP:toolchain-gcc = "${HOST_PREFIX}strip" + + +KERNEL_CC:toolchain-clang = "${CCACHE}clang ${HOST_CC_KERNEL_ARCH} \ + ${DEBUG_PREFIX_MAP} \ + -ffile-prefix-map=${STAGING_KERNEL_DIR}=${KERNEL_SRC_PATH} \ + -ffile-prefix-map=${STAGING_KERNEL_BUILDDIR}=${KERNEL_SRC_PATH} \ +" +KERNEL_LD:toolchain-clang = "ld.lld ${HOST_LD_KERNEL_ARCH}" +KERNEL_AR:toolchain-clang = "llvm-ar ${HOST_AR_KERNEL_ARCH}" +KERNEL_OBJCOPY:toolchain-clang = "llvm-objcopy ${HOST_OBJCOPY_KERNEL_ARCH}" +KERNEL_STRIP:toolchain-clang = "llvm-strip" TOOLCHAIN = "gcc"
Signature · 1 line
2.49.0
PATCH v3 2/4 kernel-yocto-rust: use clang toolchain for riscv64 when Rust is enabled
In reply to [email protected]From: Harish Sadineni <[email protected]>
Enable LLVM/Clang toolchain for riscv64 kernel builds when Rust support is enabled, as Rust is only supported with LLVM toolchains for this architecture.
This aligns with upstream kernel Rust architecture requirements as documented in:
https://docs.kernel.org/rust/arch-support.html
--- meta/recipes-kernel/linux/linux-yocto.inc | 4 ++++ 1 file changed, 4 insertions(+)
meta/recipes-kernel/linux/linux-yocto.inc +4 -0
@@ -91,3 +91,7 @@ do_devshell:prepend() { d.setVar("HOSTPKG_CONFIG", "pkg-config-native") d.appendVar("OE_TERMINAL_EXPORTS", " HOSTPKG_CONFIG") } + +# For riscv64, Rust support in kernel only works with LLVM/Clang only. +# https://docs.kernel.org/rust/arch-support.html +TOOLCHAIN:riscv64 = "${@bb.utils.contains('KERNEL_FEATURES', 'rust', 'clang', 'gcc', d)}"
Signature · 1 line
2.49.0
Re: [OE-core] [PATCH v3 2/4] kernel-yocto-rust: use clang toolchain for riscv64 when Rust is enabled
Quoted 21 lines · Harish via lists.openembedded.org
From: Harish Sadineni<[email protected]>
Enable LLVM/Clang toolchain for riscv64 kernel builds when Rust support is enabled, as Rust is only supported with LLVM toolchains for this architecture.
This aligns with upstream kernel Rust architecture requirements as documented in:
https://docs.kernel.org/rust/arch-support.htmlAcked-by Bruce Ashfield<[email protected]>Signed-off-by Harish Sadineni<[email protected]>--- meta/recipes-kernel/linux/linux-yocto.inc | 4 ++++ 1 file changed, 4 insertions(+)meta/recipes-kernel/linux/linux-yocto.inc +0
-0
meta/recipes-kernel/linux/linux-yocto.inc do_devshell:prepend() { · line 97
Quoted 7 lines
meta/recipes-kernel/linux/linux-yocto.inc +4
-0@@ -91,3 +91,7 @@ do_devshell:prepend() { d.setVar("HOSTPKG_CONFIG", "pkg-config-native") d.appendVar("OE_TERMINAL_EXPORTS", " HOSTPKG_CONFIG") } + +# For riscv64, Rust support in kernel only works with LLVM/Clang only. +#https://docs.kernel.org/rust/arch-support.html +TOOLCHAIN:riscv64 ="${@bb.utils.contains('KERNEL_FEATURES', 'rust', 'clang', 'gcc', d)}"
Rather than silently switching the kernel toolchain,
check for this situation and exit with an error to force the user to specify the toolchain if they have enable the rust KERNEL_FEATURE .
The user should be responsible for and informed about the requirement rather than having them finding out some time later that they have been silently switched from gcc to rust.
Also, if the user has already switched to using clang for the kernel for risvc, the no action is required.
This patch series was review in today's YP patch review meeting. The first and last patch will be merged.
For the middle 2 commits, in addition to the change above, merge them together. This ensures that the shortlog for the lld-native change also specifies that it's rust specific. and it enabled the build in one commit.
../Randy
Quoted 9 lines
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#240512):https://lists.openembedded.org/g/openembedded-core/message/240512 Mute This Topic:https://lists.openembedded.org/mt/120178683/3616765
Group Owner:[email protected] Unsubscribe:https://lists.openembedded.org/g/openembedded-core/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-
Signature · 2 lines
# Randy MacLeod # Wind River Linux
PATCH v3 3/4 kernel-yocto-rust: Add lld-native to DEPENDS
In reply to [email protected]From: Harish Sadineni <[email protected]>
Add lld-native to DEPENDS, As lld linker is required while building kernel with clang.
--- meta/classes-recipe/kernel-yocto-rust.bbclass | 3 +++ 1 file changed, 3 insertions(+)
meta/classes-recipe/kernel-yocto-rust.bbclass +3 -0
@@ -25,3 +25,6 @@ do_kernel_configme:append () { # More details in: https://lists.openembedded.org/g/openembedded-core/message/229336 # Disable ccache for kernel build if kernel rust support is enabled to workaround this. CCACHE_DISABLE ?= "1" + +DEPENDS:append:riscv64 = " lld-native" +RUST_KERNEL_TASK_DEPENDS:append:riscv64 = " lld-native:do_populate_sysroot"
Signature · 1 line
2.49.0
PATCH v3 4/4 kernel-yocto: Set CLANG_FLAGS for kernel config checks when using clang
In reply to [email protected]From: Harish Sadineni <[email protected]>
When building with TOOLCHAIN = "clang", the kernel's configuration check (do_kernel_configcheck) fails with:
kconfiglib.KconfigError: scripts/Kconfig.include:51: Sorry, this assembler is not supported.
Fix by setting CLANG_FLAGS = "-fintegrated-as" in the environment when TOOLCHAIN contains "clang". This variable is used by the kernel build system (via scripts/Kconfig.include) to pass additional flags to clang during config checks, ensuring the assembler test passes.
--- meta/classes-recipe/kernel-yocto.bbclass | 3 +++ 1 file changed, 3 insertions(+)
meta/classes-recipe/kernel-yocto.bbclass +3 -0
@@ -603,6 +603,9 @@ python do_kernel_configcheck() { env['STRIP'] = d.getVar('KERNEL_STRIP') env['ARCH'] = d.getVar('ARCH') env['srctree'] = s + toolchain = d.getVar('TOOLCHAIN') or '' + if 'clang' in toolchain: + env['CLANG_FLAGS'] = " -fintegrated-as" try: configs = subprocess.check_output(['scc', '--configs', '-o', s + '/.kernel-meta'], env=env).decode('utf-8')
Signature · 1 line
2.49.0