Skip to contentSkip to conversation

kernel: make the kernel toolchain switchable to clang

PATCH Superseded [email protected] 2 participants 7 messages Jul 14 – Jul 16

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover kernel: make the kernel toolchain switchable to clang

From: "mark.yang" <[email protected]>

Since v1, clang kernel toolchain support has been merged to master with 928d81916f ("kernel-arch: Add clang toolchain support") and e88ee794a3 ("kernel-yocto: Set CLANG_FLAGS for kernel config checks when using clang"), superseding two of the three v1 patches. As discussed on the v1 thread, this rework contains what remains on top of current master.

On current master a clang kernel only builds when ld-is-lld happens to be in DISTRO_FEATURES, since nothing else stages a bare ld.lld:

scripts/Kconfig.include:41: linker 'ld.lld ' not found

Full log: https://errors.yoctoproject.org/Errors/Build/242422/

Patch 1 falls back to GNU ld in that case.

kbuild wants the same toolchain for every make invocation against a build tree (Documentation/kbuild/llvm.rst: "The same value used for LLVM= should be set for each invocation of make"), and the out-of-tree module and make-mod-scripts builds run make against that same tree. Setting TOOLCHAIN on the kernel recipe alone leaves them on the gcc
defaults: kbuild spots the CC_VERSION_TEXT mismatch and silently regenerates .config. On current master, building cryptodev-module after a clang kernel succeeds without a warning, produces a gcc .ko and flips the shared .config back to CONFIG_CC_IS_GCC.

The toolchain has to be known at parse time to set up the cross toolchain dependencies, so this needs to be a configuration variable. Patch 2 moves modules and make-mod-scripts onto explicit cross toolchain dependencies and patch 3 adds a KERNEL_TOOLCHAIN switch covering all kernel-arch recipes.

Tested on qemux86-64 (master c2bd43b373, clang 22.1.8, gcc 16.1.0):
with KERNEL_TOOLCHAIN unset, core-image-minimal and cryptodev-module build with gcc as before. With KERNEL_TOOLCHAIN = "clang" the kernel, make-mod-scripts and cryptodev-module all build with clang, the shared .config keeps CONFIG_CC_IS_CLANG after the module build, and linking uses GNU ld, or LLD with ld-is-lld in DISTRO_FEATURES.

mark.yang (3):
kernel-arch: fall back to GNU ld unless ld-is-lld is in
DISTRO_FEATURES
module.bbclass/make-mod-scripts: inhibit default dependencies
kernel-arch: add KERNEL_TOOLCHAIN to switch the whole kernel toolchain

 meta/classes-recipe/kernel-arch.bbclass                      | 5 +++--
 meta/classes-recipe/module.bbclass                           | 3 +++
 meta/recipes-kernel/make-mod-scripts/make-mod-scripts_1.0.bb | 3 +++
 3 files changed, 9 insertions(+), 2 deletions(-)
#1 lore raw

PATCH v2 1/3 kernel-arch: fall back to GNU ld unless ld-is-lld is in DISTRO_FEATURES

From: "mark.yang" <[email protected]>

Nothing stages a bare ld.lld unless ld-is-lld is in DISTRO_FEATURES, so a clang kernel fails its first linker check on any other setup. Use GNU ld.bfd from binutils-cross instead.

---
v2: new patch; the merged clang support uses a bare ld.lld which
nothing stages without ld-is-lld

 meta/classes-recipe/kernel-arch.bbclass | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
meta/classes-recipe/kernel-arch.bbclass +1 -1
index 6ebc0f13ea..321c9ed577 100644
@@ -88,7 +88,7 @@ KERNEL_CC:toolchain-clang = "${CCACHE}clang ${HOST_CC_KERNEL_ARCH} \
  -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_LD:toolchain-clang = "${@bb.utils.contains('DISTRO_FEATURES', 'ld-is-lld', 'ld.lld', '${HOST_PREFIX}ld.bfd', d)} ${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"
#2 lore raw

PATCH v2 2/3 module.bbclass/make-mod-scripts: inhibit default dependencies

In reply to [email protected]

From: "mark.yang" <[email protected]>

Modules and make-mod-scripts build against the shared kernel build tree with the kernel toolchain, so the default virtual/libc and virtual/compilerlibs dependencies are unused. Inhibit them and add the cross toolchain explicitly, as kernel.bbclass does, which also keeps compiler-rt and libcxx out of module builds with clang.

---
v2: split out of the KERNEL_TOOLCHAIN patch

 meta/classes-recipe/module.bbclass                           | 3 +++
 meta/recipes-kernel/make-mod-scripts/make-mod-scripts_1.0.bb | 3 +++
 2 files changed, 6 insertions(+)
meta/classes-recipe/module.bbclass +3 -0
index 6b2c09f441..ce5898125b 100644
@@ -6,6 +6,9 @@
 
 inherit module-base kernel-module-split pkgconfig
 
+INHIBIT_DEFAULT_DEPS = "1"
+DEPENDS += "virtual/cross-cc virtual/cross-binutils"
+
 EXTRA_OEMAKE += "KERNEL_SRC=${STAGING_KERNEL_DIR}"
 
 MODULES_INSTALL_TARGET ?= "modules_install"
meta/recipes-kernel/make-mod-scripts/make-mod-scripts_1.0.bb +3 -0
index 4b0630313f..338a50de91 100644
@@ -18,6 +18,9 @@ DEV_PKG_DEPENDENCY = ""
 DEPENDS += "bc-native bison-native"
 DEPENDS += "gmp-native"
 
+INHIBIT_DEFAULT_DEPS = "1"
+DEPENDS += "virtual/cross-cc virtual/cross-binutils"
+
 EXTRA_OEMAKE = " HOSTCC="${BUILD_CC}" HOSTCFLAGS="${BUILD_CFLAGS}" HOSTLDFLAGS="${BUILD_LDFLAGS}" HOSTCPP="${BUILD_CPP}""
 EXTRA_OEMAKE += " HOSTCXX="${BUILD_CXX} ${BUILD_CXXFLAGS} ${BUILD_LDFLAGS}" CROSS_COMPILE=${TARGET_PREFIX}"
 
#3 lore raw

Re: [OE-core] [PATCH v2 2/3] module.bbclass/make-mod-scripts: inhibit default dependencies

Quoted 10 lines · mark.yang via lists.openembedded.org

From: "mark.yang" <[email protected]>

Modules and make-mod-scripts build against the shared kernel build tree with the kernel toolchain, so the default virtual/libc and virtual/compilerlibs dependencies are unused. Inhibit them and add the cross toolchain explicitly, as kernel.bbclass does, which also keeps compiler-rt and libcxx out of module builds with clang.

---

Hi Mark,

Thanks for your patch.

We get this error during selftests:

2026-07-15 07:01:52,991 - oe-selftest - INFO - bbtests.BitbakeTests.test_non_gplv3 (subunit.RemotedTestCase)
2026-07-15 07:01:52,992 - oe-selftest - INFO -  ... FAIL

...
NOTE: Resolving any missing task queue dependencies
ERROR: Nothing PROVIDES 'virtual/compilerlibs' (but /srv/pokybuild/yocto-worker/oe-selftest-fedora/build/build-st-1131783/meta-selftest/recipes-test/selftest-ed/selftest-ed_1.20.2.bb DEPENDS on or otherwise requires it) gcc-runtime PROVIDES virtual/compilerlibs but was skipped: it has incompatible license(s): GPL-3.0-or-later GPL-3.0-or-later WITH GCC-exception-3.1
ERROR: Required build target 'selftest-ed' has no buildable providers.
Missing or unbuildable dependency chain was: ['selftest-ed', 'virtual/compilerlibs']

https://autobuilder.yoctoproject.org/valkyrie/#/builders/48/builds/4104

Can you have a look at the issue?

Thanks,
Mathieu

Signature · 3 lines
Mathieu Dubois-Briand, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Re: [OE-core] [PATCH v2 2/3] module.bbclass/make-mod-scripts: inhibit default dependencies

Quoted 27 lines · Mathieu Dubois-Briand
Quoted 10 lines · mark.yang via lists.openembedded.org

From: "mark.yang" <[email protected]>

Modules and make-mod-scripts build against the shared kernel build tree with the kernel toolchain, so the default virtual/libc and virtual/compilerlibs dependencies are unused. Inhibit them and add the cross toolchain explicitly, as kernel.bbclass does, which also keeps compiler-rt and libcxx out of module builds with clang.

---

Hi Mark,

Thanks for your patch.

We get this error during selftests:

2026-07-15 07:01:52,991 - oe-selftest - INFO - bbtests.BitbakeTests.test_non_gplv3 (subunit.RemotedTestCase)
2026-07-15 07:01:52,992 - oe-selftest - INFO -  ... FAIL

...
NOTE: Resolving any missing task queue dependencies
ERROR: Nothing PROVIDES 'virtual/compilerlibs' (but /srv/pokybuild/yocto-worker/oe-selftest-fedora/build/build-st-1131783/meta-selftest/recipes-test/selftest-ed/selftest-ed_1.20.2.bb DEPENDS on or otherwise requires it) gcc-runtime PROVIDES virtual/compilerlibs but was skipped: it has incompatible license(s): GPL-3.0-or-later GPL-3.0-or-later WITH GCC-exception-3.1
ERROR: Required build target 'selftest-ed' has no buildable providers.
Missing or unbuildable dependency chain was: ['selftest-ed', 'virtual/compilerlibs']

Quoted 6 lines

https://autobuilder.yoctoproject.org/valkyrie/#/builders/48/builds/4104

Can you have a look at the issue?

Thanks,
Mathieu

Or maybe not. I suspect something else, and I will test again your past shortly.

Thanks,
Mathieu

Signature · 3 lines
Mathieu Dubois-Briand, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author
#5 lore raw

Re: [PATCH v2 2/3] module.bbclass/make-mod-scripts: inhibit default dependencies

Hi Mathieu,

Thanks for testing the series and for the follow-up.

I tried to reproduce this on current master (c2bd43b373) with the three patches applied, and the test passes here:

RESULTS - bbtests.BitbakeTests.test_non_gplv3: PASSED (184.91s)

Looking at the failure itself, I don't think patch 2/3 can trigger it.

Happy to dig further if your retest still point

Thanks,
Mark

#6 lore raw

PATCH v2 3/3 kernel-arch: add KERNEL_TOOLCHAIN to switch the whole kernel toolchain

In reply to [email protected]

From: "mark.yang" <[email protected]>

Setting TOOLCHAIN on the kernel recipe alone leaves modules and make-mod-scripts running make against the shared build tree with gcc; kbuild spots the CC_VERSION_TEXT mismatch and silently regenerates the shared .config. Add a KERNEL_TOOLCHAIN switch covering all kernel-arch recipes so they change toolchain together, gcc by default.

---
v2: rebased on the merged clang toolchain support which supersedes
the other v1 patches; dependency changes split out into patch 2

 meta/classes-recipe/kernel-arch.bbclass | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
meta/classes-recipe/kernel-arch.bbclass +2 -1
index 321c9ed577..5f8dbacc0c 100644
@@ -92,4 +92,5 @@ KERNEL_LD:toolchain-clang = "${@bb.utils.contains('DISTRO_FEATURES', 'ld-is-lld'
 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"
+KERNEL_TOOLCHAIN ?= "gcc"
+TOOLCHAIN = "${KERNEL_TOOLCHAIN}"