xref: /OK3568_Linux_fs/yocto/meta-clang/README.md (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1[![Yoe Distro CI](https://github.com/kraj/meta-clang/workflows/Yoe%20Distro%20CI/badge.svg?branch=master)](https://github.com/kraj/meta-clang/actions/workflows/yoe.yml?query=workflow%3AYoe+branch%3Amaster)
2
3# meta-clang (C/C++ frontend and LLVM compiler backend)
4
5This layer provides [clang/llvm](http://clang.llvm.org/) as alternative to system
6C/C++ compiler for OpenEmbedded/Yocto Project based distributions. This can cohabit
7with GNU compiler and can be used for specific recipes or full system compiler.
8
9# Getting Started
10
11```shell
12git clone git://github.com/openembedded/openembedded-core.git
13cd openembedded-core
14git clone git://github.com/openembedded/bitbake.git
15git clone git://github.com/kraj/meta-clang.git
16
17$ . ./oe-init-build-env
18```
19If using poky ( Yocto project reference Distribution )
20
21```shell
22git clone https://git.yoctoproject.org/git/poky
23cd poky
24git clone git://github.com/kraj/meta-clang.git
25
26$ . ./oe-init-build-env
27```
28
29Add meta-clang overlay
30```
31bitbake-layers add-layer ../meta-clang
32```
33
34Check `conf/bblayers.conf` to see that meta-clang is added to layer mix e.g.
35
36# Default Compiler
37
38Note that by default gcc will remain the system compiler, however if you wish
39clang to be the default compiler then set
40
41```shell
42TOOLCHAIN ?= "clang"
43```
44
45in `local.conf`, this would now switch default cross-compiler to be clang
46you can select clang per recipe too by writing bbappends for them containing
47
48```shell
49TOOLCHAIN = "clang"
50```
51also look at `conf/nonclangable.conf` for list of recipes which do not yet fully
52build with clang.
53
54# Default Compiler Runtime
55
56Default is to use GNU runtime `RUNTIME = "gnu"` which consists of libgcc, libstdc++ to provide C/C++
57runtime support. However it's possible to use LLVM runtime to replace it where
58compile-rt, llvm libunwind, and libc++ are used to provide C/C++ runtime, while
59GNU runtime works with both GCC and Clang, LLVM runtime is only tested with Clang
60compiler, switching to use LLVM runtime is done via a config metadata knob
61
62```shell
63RUNTIME = "llvm"
64```
65
66RUNTIME variable influences individual runtime elements and can be set explicitly as well
67e.g. `LIBCPLUSPLUS` `COMPILER_RT` and `UNWINDLIB`.
68
69Please note that this will still use crt files from GNU compiler always, while llvm now
70do provide crt files, they have not been yet integrated into the toolchain.
71
72# Default C++ Standard Library Switch
73
74Using RUNTIME variable will select which C++ runtime is used, however it can be overridden
75if needed to by modifying `LIBCPLUSPLUS` variable, usually defaults used by `RUNTIME` are
76best fit. e.g. below we select LLVM C++ as default C++ runtime.
77
78```shell
79LIBCPLUSPLUS = "-stdlib=libc++"
80```
81
82in `local.conf`.
83You can select libstdc++ per package too by writing bbappends for them containing
84
85```shell
86LIBCPLUSPLUS:toolchain-clang:pn-<recipe> = "-stdlibc=libc++"
87```
88Defaults are chosen to be GNU for maximum compatibility with existing GNU systems. It's always
89good to use single runtime on a system, mixing runtimes can cause complications during
90compilation as well as runtime. However, it's up to distribution policies to decide which runtime
91to use.
92
93# Adding clang in generated SDK toolchain
94
95Clang based cross compiler is not included into the generated SDK using `bitbake meta-toolchain` or
96`bitbake -cpopulate_sdk <image>` if clang is expected to be part of SDK, add `CLANGSDK = "1"`
97in `local.conf`
98
99```shell
100CLANGSDK = "1"
101```
102
103# Building
104
105Below we build for qemuarm machine as an example
106
107```shell
108$ MACHINE=qemuarm bitbake core-image-full-cmdline
109```
110# Running
111
112```shell
113$ runqemu nographic
114```
115
116# Limitations
117
118Few components do not build with clang, if you have a component to add to that list
119simply add it to `conf/nonclangable.inc` e.g.
120
121```shell
122TOOLCHAIN:pn-<recipe> = "gcc"
123```
124
125and OE will start using gcc to cross compile that recipe.
126
127If a component does not build with libc++, you can add it to `conf/nonclangable.inc` e.g.
128
129```shell
130CXX:remove:pn-<recipe>:toolchain-clang = " -stdlib=libc++ "
131```
132
133# compiler-rt failing in do_configure with custom TARGET_VENDOR
134
135If your DISTRO sets own value of TARGET_VENDOR, then it's need to be added in
136CLANG_EXTRA_OE_VENDORS, it should be done automatically, but if compiler-rt fails
137like bellow, then check the end of work-shared/llvm-project-source-12.0.0-r0/temp/log.do_patch
138is should have line like:
139NOTE: Adding support following TARGET_VENDOR values: foo in
140  /OE/build/oe-core/tmp-glibc/work-shared/llvm-project-source-12.0.0-r0/git/llvm/lib/Support/Triple.cpp and
141  /OE/build/oe-core/tmp-glibc/work-shared/llvm-project-source-12.0.0-r0/git/clang/lib/Driver/ToolChains/Gnu.cpp
142and check these files if //CLANG_EXTRA_OE_VENDORS* strings were replaced correctly.
143Read add_more_target_vendors function in recipes-devtools/clang/llvm-project-source.inc for more details.
144
145http://errors.yoctoproject.org/Errors/Details/574365/
146```shell
147-- Found assembler: TOPDIR/tmp-glibc/work/core2-64-foo-linux/compiler-rt/12.0.0-r0/recipe-sysroot-native/usr/bin/x86_64-foo-linux/x86_64-foo-linux-clang
148-- Detecting C compiler ABI info
149-- Detecting C compiler ABI info - failed
150-- Check for working C compiler: TOPDIR/tmp-glibc/work/core2-64-foo-linux/compiler-rt/12.0.0-r0/recipe-sysroot-native/usr/bin/x86_64-foo-linux/x86_64-foo-linux-clang
151-- Check for working C compiler: TOPDIR/tmp-glibc/work/core2-64-foo-linux/compiler-rt/12.0.0-r0/recipe-sysroot-native/usr/bin/x86_64-foo-linux/x86_64-foo-linux-clang - broken
152CMake Error at TOPDIR/tmp-glibc/work/core2-64-foo-linux/compiler-rt/12.0.0-r0/recipe-sysroot-native/usr/share/cmake-3.19/Modules/CMakeTestCCompiler.cmake:66 (message):
153  The C compiler
154
155    "TOPDIR/tmp-glibc/work/core2-64-foo-linux/compiler-rt/12.0.0-r0/recipe-sysroot-native/usr/bin/x86_64-foo-linux/x86_64-foo-linux-clang"
156
157  is not able to compile a simple test program.
158
159  It fails with the following output:
160
161    Change Dir: TOPDIR/tmp-glibc/work/core2-64-foo-linux/compiler-rt/12.0.0-r0/build/CMakeFiles/CMakeTmp
162
163    Run Build Command(s):ninja cmTC_928f4 && [1/2] Building C object CMakeFiles/cmTC_928f4.dir/testCCompiler.c.o
164    [2/2] Linking C executable cmTC_928f4
165```
166
167# Dependencies
168
169```shell
170URI: git://github.com/openembedded/openembedded-core.git
171branch: master
172revision: HEAD
173
174URI: git://github.com/openembedded/bitbake.git
175branch: master
176revision: HEAD
177```
178
179# Contributing
180
181You are encouraged to follow Github Pull request workflow
182to share changes and following commit message guidelines are recommended: [OE patch guidelines](https://www.openembedded.org/wiki/Commit_Patch_Message_Guidelines).
183
184Layer Maintainer: [Khem Raj](<mailto:raj.khem@gmail.com>)
185