Skip to contentSkip to conversation

clang-native: Add class to use clang as native compiler

PATCH Superseded Khem Raj 2 participants 3 messages Jul 1, 2025

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Khem Raj

author
#0 lore raw

PATCH 1/2 clang-native: Add class to use clang as native compiler

Some recipes demand full clang/llvm builds e.g. chromium we need to use clang as native toolchain. This class collects all needed bits to enable OE built clang to provide the clang native toolchain

Setting

PREFERRED_TOOLCHAIN_NATIVE = "clang"

in recipe will chose clang for native toolchain

---
 meta/classes/toolchain/clang-native.bbclass | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 meta/classes/toolchain/clang-native.bbclass
meta/classes/toolchain/clang-native.bbclass new file +18 -0
new file mode 100644
index 00000000000..adb22883806
@@ -0,0 +1,18 @@
+BUILD_CC = "${CCACHE}${BUILD_PREFIX}clang ${BUILD_CC_ARCH}"
+BUILD_CXX = "${CCACHE}${BUILD_PREFIX}clang++ ${BUILD_CC_ARCH}"
+BUILD_FC = "${BUILD_PREFIX}gfortran ${BUILD_CC_ARCH}"
+BUILD_CPP = "${BUILD_PREFIX}clang ${BUILD_CC_ARCH} -E"
+BUILD_LD = "${BUILD_PREFIX}ld ${BUILD_LD_ARCH}"
+BUILD_CCLD = "${BUILD_PREFIX}clang ${BUILD_CC_ARCH}"
+BUILD_AR = "${BUILD_PREFIX}llvm-ar"
+BUILD_AS = "${BUILD_PREFIX}as ${BUILD_AS_ARCH}"
+BUILD_RANLIB = "${BUILD_PREFIX}llvm-ranlib -D"
+BUILD_STRIP = "${BUILD_PREFIX}llvm-strip"
+BUILD_OBJCOPY = "${BUILD_PREFIX}llvm-objcopy"
+BUILD_OBJDUMP = "${BUILD_PREFIX}llvm-objdump"
+BUILD_NM = "${BUILD_PREFIX}llvm-nm"
+BUILD_READELF = "${BUILD_PREFIX}llvm-readelf"
+
+DEPENDS += " clang-native compiler-rt-native libcxx-native"
+
+LDFLAGS += " --rtlib=libgcc --unwindlib=libgcc"
author
#1 lore raw

PATCH 2/2 base.bbclass: Deferred inherit native toolchain class

This helps build native recipe with clang as native compiler. when PREFERRED_TOOLCHAIN_NATIVE = "clang", otherwise it will inherit gcc-native, this is a bit redundant when gcc native is used, ideally we should defer_inherit it always but there is some code in classes assuming gcc as native compiler e.g.

oe.utils.host_gcc_version function expects BUILD_CC early during parsing.

---
 meta/classes-global/base.bbclass | 1 +
 1 file changed, 1 insertion(+)
meta/classes-global/base.bbclass +1 -0
index ac145d9fd64..1fa09b6a16b 100644
@@ -32,6 +32,7 @@ PREFERRED_TOOLCHAIN:class-nativesdk = "${PREFERRED_TOOLCHAIN_SDK}"
 TOOLCHAIN ??= "${PREFERRED_TOOLCHAIN}"
 
 inherit toolchain/gcc-native
+inherit_defer toolchain/${PREFERRED_TOOLCHAIN_NATIVE}-native
 inherit_defer toolchain/${TOOLCHAIN}
 
 def lsb_distro_identifier(d):
#2 lore raw

Re: [OE-core] [PATCH 2/2] base.bbclass: Deferred inherit native toolchain class

Quoted 20 lines · Khem Raj via lists.openembedded.org

This helps build native recipe with clang as native compiler. when PREFERRED_TOOLCHAIN_NATIVE = "clang", otherwise it will inherit gcc-native, this is a bit redundant when gcc native is used, ideally we should defer_inherit it always but there is some code in classes assuming gcc as native compiler e.g.

oe.utils.host_gcc_version function expects BUILD_CC early during parsing.

---
 meta/classes-global/base.bbclass | 1 +

 1 file changed, 1 insertion(+)
meta/classes- renamed from meta/classes-global/base.bbclass +0 -0

global/base.bbclass
index ac145d9fd64..1fa09b6a16b 100644

meta/classes-global/base.bbclass +0 -0
@@ -32,6 +32,7 @@ PREFERRED_TOOLCHAIN:class-nativesdk =
Quoted 8 lines

"${PREFERRED_TOOLCHAIN_SDK}"
 TOOLCHAIN ??= "${PREFERRED_TOOLCHAIN}"

 inherit toolchain/gcc-native
+inherit_defer toolchain/${PREFERRED_TOOLCHAIN_NATIVE}-native inherit_defer toolchain/${TOOLCHAIN}

 def lsb_distro_identifier(d):

The trouble with doing it this way is that a recipe which says TOOLCHAIN = "gcc" will end up getting compiled by clang.

We could avoid this by using:

TOOLCHAIN_NATIVE ??= "${PREFERRED_TOOLCHAIN_NATIVE}"

inherit_defer toolchain/${TOOLCHAIN_NATIVE}-native

and then having recipes set:

TOOLCHAIN = "gcc"
TOOLCHAIN_NATIVE = "gcc"

I really want to avoid inheriting both gcc-native and clang-native. If we have to remove the host_gcc_version function, that is probably ok. I think it is mostly used for the uninative gcc 4.8 and 4.9 issue but I doubt we have any distro with those older gcc versions on now?

Cheers,

Richard