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