Skip to content

Using Clang with OpenEmbedded/Yocto Project

Introduction

Clang is C/C++/ObjC  frontend for LLVM compiler suite,  supporting several architectures now. We have added support for clang into OpenEmbedded using a standalone layer

https://github.com/kraj/meta-clang

This layer requires OpenEmbedded-core layer as dependency

Configuring meta-clang

git clone git://github.com/openembedded/openembedded-core
cd openembedded-core
git clone git://github.com/openembedded/bitbake
git clone git://github.com/kraj/meta-clang

Setup

. ./oe-init-build-env

Edit conf/bblayer.conf to add meta-clang

# LAYER_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
LCONF_VERSION = "6"

BBPATH = "${TOPDIR}"
BBFILES ?= ""

BBLAYERS ?= " \
 /a/wheezy/home/kraj/work/openembedded-core/meta-clang \
 /a/wheezy/home/kraj/work/openembedded-core/meta \
 "

BBLAYERS_NON_REMOVABLE ?= " \
 /a/wheezy/home/kraj/work/openembedded-core/meta \
 "

 

Selecting Default system Compiler

When meta-clang is added to layers it switches the default cross compiler for the system to be clang naturally. This selection is made by a variable which is assigned to choose clang if nothing else is selected

TOOLCHAIN ??= "clang"

One could start using GCC as default system compiler by setting TOOLCHAIN variable to ‘gcc’

TOOLCHAIN = "gcc"

This selection is done in configuration metadata files like conf/local.conf

Per-recipe ( package ) Compiler selection

When using clang, not all components are compilable yet, hence we have to sometimes choose gcc for certain package recipe. In order to do it, you have to write a .bbappend in meta-clang, as an example below bbappend shows how we select gcc for building glibc

$ cat recipes-excluded/nonclangable/glibc_%.bbappend
TOOLCHAIN = "gcc"

Current Status

We can build/boot core-image-minimal for qemux86 and raspberryPi machines, of course that includes few components which are still compiled using gcc. But at this point, we are ready to port the packages to compile with clang

Compiling Kernel with Clang

For kernel we still use gcc, to start using clang for kernel we would require to override KERNEL_CC variable

kernel-arch.bbclass:KERNEL_CC = "${CCACHE}${HOST_PREFIX}gcc ${HOST_CC_KERNEL_ARCH} -fuse-ld=bfd"

To use clang

kernel-arch.bbclass:KERNEL_CC = "${CCACHE}${HOST_PREFIX}clang ${HOST_CC_KERNEL_ARCH} -fuse-ld=bfd"

This can also be done in kernel recipe too.

KERNEL_CC_forcevariable = "${CCACHE}${HOST_PREFIX}clang ${HOST_CC_KERNEL_ARCH} -fuse-ld=bfd"

Compiler runtime

We currently use gcc runtime by default with clang as well, meta-clang does have recipes for compiler-rt and libc++ which can be used to replace libgcc and libstdc++, at application level this selection can be made to depend on compiler-rt and libc++

SDK

We can generate OpenEmbedded SDK which will have both gcc and clang based cross compilers

bitbake -cpopulate_sdk core-image-minimal

Installing the SDK should be done something like this Installing the Toolchain¶

To use clang in SDK one should use CLANGCC and CLANGCXX environment variables to reference the clang compilers

 

7 thoughts on “Using Clang with OpenEmbedded/Yocto Project”

  1. Hi,

    Thank you for your blog. I am using your meta-clang recipe with yocto. however, the build does not seem to have libc++.so. How do i get one? Looking forward to your response.

    Br,
    mora_an

  2. in your local.conf add

    IMAGE_INSTALL_append = ” \
    libcxx \

    it has not received as much testing as I would have liked. If you find issues. Feel free to bring them
    or even better send fixes.

  3. Hi Khem,

    Thank you for your quick response. I tried IMAGE_INSTALL_APPEND += “libcxx” without much success. I am building it for zc706 (with meta-xilinx), i assume this should not change anything. I will try once again with tha latest poky version and see if it works. Meanwhile it would be great help, if can get a libc++.so built for arm. Any pointers?

    Br,
    mora_an

  4. Hi,

    Great Post! Is it possible to generate packages with different architectures? I have an aarch64 system, most of its sw components are generated with gcc but I need a few of them to be compiled by clang and in 32bit architecture. Is it possible to achieve this?

    Thanks a lot!
    Alejandro

    1. @Alejandro this is interesting usecase I do not yet have multilib enabled in clang which is what you would need here, All we need is to wire up the multilib location into clang driver which is then used when -m32 is used but so far this is not yet done.

  5. Hi

    I am trying to compile kernel in yocto with meta-clang.
    I modified local.conf with
    TOOLCHAIN=”clang”
    RUNTIME=”llvm”
    and
    kernel-arch.bbclass with
    KERNEL_CC = “${CCACHE}${HOST_PREFIX}clang ${HOST_CC_KERNEL_ARCH} -fuse-ld=lld”

    And I get the error that ‘x86_64-ese-linux-clang -fuse-ld=lld’ not found.
    I checked meta-clang is built and x86_64-ese-linux-clang is installed. Am I missing something?

Leave a Reply

Your email address will not be published. Required fields are marked *