Skip to contentSkip to conversation

kernel-arch: Add clang toolchain support

PATCH Superseded [email protected] 2 participants 6 messages Jul 7 – Jul 8

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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
index 7aea9cd3e8..6ebc0f13ea 100644
@@ -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
#1 lore raw

PATCH v2 2/4 kernel-yocto-rust: use clang toolchain for riscv64 when Rust is enabled

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
index 4e230d405a..5d047b5f01 100644
@@ -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
#2 lore raw

PATCH v2 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
index 49f2bfc1ae..1badb37476 100644
@@ -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
#3 lore raw

PATCH v2 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
index e7fdeb5d4b..cda9ffcf9c 100644
@@ -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')
+    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
Acked-by on v2 1/4
#4 lore raw
In reply to [email protected]
Quoted 2 lines · Harish Sadineni

Convert the existing GCC tool definitions to use the :toolchain-gcc override so they only apply when TOOLCHAIN = "gcc" (the default).

I went through the series with a focus on making sure it's a no-op for the common gcc / non-riscv64 kernel build, and it checks out:

- toolchain-gcc lands in OVERRIDES for a default build (base.bbclass does inherit_defer toolchain/${TOOLCHAIN}, gcc.bbclass sets TCOVERRIDE = "toolchain-gcc", and OVERRIDES carries ${TCOVERRIDE}), so KERNEL_CC:toolchain-gcc et al resolve to exactly what the unconditional assignments produced before.

- The KERNEL_CC:append:{aarch64,nios2,arc} appends in linux-yocto.inc still stack correctly on top of the :toolchain-gcc value, so the arch-specific TOOLCHAIN_OPTIONS aren't lost.

- Patches 2-4 are gated to riscv64 and/or the rust KERNEL_FEATURE (kernel-yocto-rust.bbclass is only inherited when 'rust' is set), so they're inert everywhere else.

For the whole series:

Two things worth noting, neither blocking:

- One behavioural subtlety from this conversion: a KERNEL_CC:toolchain-gcc assignment now outranks any *unconditional* KERNEL_CC = "..." a downstream BSP might set, where previously it was a plain last-assignment-wins. No in-tree machine/recipe does that today, so there's no regression, but it's a change in precedence worth being aware of if a BSP overrides these.

- In patch 4, if 'clang' in toolchain: will throw if TOOLCHAIN is ever unset (getVar returns None). It's always set for kernel recipes today so it's fine, but d.getVar('TOOLCHAIN') or '' would be a touch more robust.

Bruce

#5 lore raw
Quoted 35 lines · Bruce Ashfield

CAUTION: This email comes from a non Wind River email account!
Do not click links or open attachments unless you recognize the sender and know the content is safe.

Quoted 2 lines · Harish Sadineni

Convert the existing GCC tool definitions to use the :toolchain-gcc override so they only apply when TOOLCHAIN = "gcc" (the default).

I went through the series with a focus on making sure it's a no-op for the common gcc / non-riscv64 kernel build, and it checks out:

- toolchain-gcc lands in OVERRIDES for a default build (base.bbclass does inherit_defer toolchain/${TOOLCHAIN}, gcc.bbclass sets TCOVERRIDE = "toolchain-gcc", and OVERRIDES carries ${TCOVERRIDE}), so KERNEL_CC:toolchain-gcc et al resolve to exactly what the unconditional assignments produced before.

- The KERNEL_CC:append:{aarch64,nios2,arc} appends in linux-yocto.inc still stack correctly on top of the :toolchain-gcc value, so the arch-specific TOOLCHAIN_OPTIONS aren't lost.

- Patches 2-4 are gated to riscv64 and/or the rust KERNEL_FEATURE (kernel-yocto-rust.bbclass is only inherited when 'rust' is set), so they're inert everywhere else.

For the whole series:

Two things worth noting, neither blocking:

- One behavioural subtlety from this conversion: a KERNEL_CC:toolchain-gcc assignment now outranks any *unconditional* KERNEL_CC = "..." a downstream BSP might set, where previously it was a plain last-assignment-wins. No in-tree machine/recipe does that today, so there's no regression, but it's a change in precedence worth being aware of if a BSP overrides these.

Quoted 3 lines

- In patch 4, if 'clang' in toolchain: will throw if TOOLCHAIN is ever unset (getVar returns None). It's always set for kernel recipes today so it's fine, but d.getVar('TOOLCHAIN') or '' would be a touch more robust.

Hi Bruce,

Thanks for the ack. I've sent v3 with your suggested d.getVar('TOOLCHAIN') or '' fix
folded into patch 4 (the only change since v2), and carried your Acked-by forward on all patches.https://lists.openembedded.org/g/openembedded-core/message/240489 Thanks, Harish

Quoted 2 lines

Bruce