xref: /linux/Documentation/kbuild/llvm.rst (revision b82779648dfd3814df4e381f086326ec70fd791f)
13519c4d6SNick Desaulniers.. _kbuild_llvm:
23519c4d6SNick Desaulniers
3fcf1b6a3SNick Desaulniers==============================
4fcf1b6a3SNick DesaulniersBuilding Linux with Clang/LLVM
5fcf1b6a3SNick Desaulniers==============================
6fcf1b6a3SNick Desaulniers
7fcf1b6a3SNick DesaulniersThis document covers how to build the Linux kernel with Clang and LLVM
8fcf1b6a3SNick Desaulniersutilities.
9fcf1b6a3SNick Desaulniers
10fcf1b6a3SNick DesaulniersAbout
11fcf1b6a3SNick Desaulniers-----
12fcf1b6a3SNick Desaulniers
13fcf1b6a3SNick DesaulniersThe Linux kernel has always traditionally been compiled with GNU toolchains
14fcf1b6a3SNick Desaulnierssuch as GCC and binutils. Ongoing work has allowed for `Clang
15fcf1b6a3SNick Desaulniers<https://clang.llvm.org/>`_ and `LLVM <https://llvm.org/>`_ utilities to be
16fcf1b6a3SNick Desaulniersused as viable substitutes. Distributions such as `Android
17fcf1b6a3SNick Desaulniers<https://www.android.com/>`_, `ChromeOS
180aee6becSNick Desaulniers<https://www.chromium.org/chromium-os>`_, `OpenMandriva
190aee6becSNick Desaulniers<https://www.openmandriva.org/>`_, and `Chimera Linux
200aee6becSNick Desaulniers<https://chimera-linux.org/>`_ use Clang built kernels. Google's and Meta's
210aee6becSNick Desaulniersdatacenter fleets also run kernels built with Clang.
220aee6becSNick Desaulniers
230aee6becSNick Desaulniers`LLVM is a collection of toolchain components implemented in terms of C++
240aee6becSNick Desaulniersobjects <https://www.aosabook.org/en/llvm.html>`_. Clang is a front-end to LLVM
250aee6becSNick Desaulniersthat supports C and the GNU C extensions required by the kernel, and is
260aee6becSNick Desaulnierspronounced "klang," not "see-lang."
27fcf1b6a3SNick Desaulniers
28a3c6bfbaSNick DesaulniersBuilding with LLVM
29a3c6bfbaSNick Desaulniers------------------
30fcf1b6a3SNick Desaulniers
31a3c6bfbaSNick DesaulniersInvoke ``make`` via::
32fcf1b6a3SNick Desaulniers
33a3c6bfbaSNick Desaulniers	make LLVM=1
34fcf1b6a3SNick Desaulniers
35a3c6bfbaSNick Desaulniersto compile for the host target. For cross compiling::
36fcf1b6a3SNick Desaulniers
37a3c6bfbaSNick Desaulniers	make LLVM=1 ARCH=arm64
38fcf1b6a3SNick Desaulniers
39a3c6bfbaSNick DesaulniersThe LLVM= argument
40a3c6bfbaSNick Desaulniers------------------
41fcf1b6a3SNick Desaulniers
42a3c6bfbaSNick DesaulniersLLVM has substitutes for GNU binutils utilities. They can be enabled
43a3c6bfbaSNick Desaulniersindividually. The full list of supported make variables::
44fcf1b6a3SNick Desaulniers
4591a9d502SNathan Chancellor	make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
46d9b5665fSVasily Gorbik	  OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
47d9b5665fSVasily Gorbik	  HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld
48fcf1b6a3SNick Desaulniers
49a3c6bfbaSNick Desaulniers``LLVM=1`` expands to the above.
50e9c28192SNathan Chancellor
51e9c28192SNathan ChancellorIf your LLVM tools are not available in your PATH, you can supply their
52e9c28192SNathan Chancellorlocation using the LLVM variable with a trailing slash::
53e9c28192SNathan Chancellor
54e9c28192SNathan Chancellor	make LLVM=/path/to/llvm/
55e9c28192SNathan Chancellor
56a3c6bfbaSNick Desaulnierswhich will use ``/path/to/llvm/clang``, ``/path/to/llvm/ld.lld``, etc. The
57a3c6bfbaSNick Desaulniersfollowing may also be used::
58a3c6bfbaSNick Desaulniers
59a3c6bfbaSNick Desaulniers	PATH=/path/to/llvm:$PATH make LLVM=1
60e9c28192SNathan Chancellor
61e9c28192SNathan ChancellorIf your LLVM tools have a version suffix and you want to test with that
62e9c28192SNathan Chancellorexplicit version rather than the unsuffixed executables like ``LLVM=1``, you
63e9c28192SNathan Chancellorcan pass the suffix using the ``LLVM`` variable::
64e9c28192SNathan Chancellor
65e9c28192SNathan Chancellor	make LLVM=-14
66e9c28192SNathan Chancellor
67e9c28192SNathan Chancellorwhich will use ``clang-14``, ``ld.lld-14``, etc.
68e9c28192SNathan Chancellor
69a3c6bfbaSNick DesaulniersTo support combinations of out of tree paths with version suffixes, we
70a3c6bfbaSNick Desaulniersrecommend::
71a3c6bfbaSNick Desaulniers
72a3c6bfbaSNick Desaulniers	PATH=/path/to/llvm/:$PATH make LLVM=-14
73a3c6bfbaSNick Desaulniers
74e9c28192SNathan Chancellor``LLVM=0`` is not the same as omitting ``LLVM`` altogether, it will behave like
75a3c6bfbaSNick Desaulniers``LLVM=1``. If you only wish to use certain LLVM utilities, use their
76a3c6bfbaSNick Desaulniersrespective make variables.
77e9c28192SNathan Chancellor
78a3c6bfbaSNick DesaulniersThe same value used for ``LLVM=`` should be set for each invocation of ``make``
79a3c6bfbaSNick Desaulniersif configuring and building via distinct commands. ``LLVM=`` should also be set
80a3c6bfbaSNick Desaulniersas an environment variable when running scripts that will eventually run
81a3c6bfbaSNick Desaulniers``make``.
827e20e47cSMasahiro Yamada
83a3c6bfbaSNick DesaulniersCross Compiling
84a3c6bfbaSNick Desaulniers---------------
85a3c6bfbaSNick Desaulniers
86a3c6bfbaSNick DesaulniersA single Clang compiler binary (and corresponding LLVM utilities) will
87a3c6bfbaSNick Desaulnierstypically contain all supported back ends, which can help simplify cross
88a3c6bfbaSNick Desaulnierscompiling especially when ``LLVM=1`` is used. If you use only LLVM tools,
89a3c6bfbaSNick Desaulniers``CROSS_COMPILE`` or target-triple-prefixes become unnecessary. Example::
90a3c6bfbaSNick Desaulniers
91a3c6bfbaSNick Desaulniers	make LLVM=1 ARCH=arm64
92a3c6bfbaSNick Desaulniers
93a3c6bfbaSNick DesaulniersAs an example of mixing LLVM and GNU utilities, for a target like ``ARCH=s390``
94a3c6bfbaSNick Desaulnierswhich does not yet have ``ld.lld`` or ``llvm-objcopy`` support, you could
95a3c6bfbaSNick Desaulniersinvoke ``make`` via::
96a3c6bfbaSNick Desaulniers
97a3c6bfbaSNick Desaulniers	make LLVM=1 ARCH=s390 LD=s390x-linux-gnu-ld.bfd \
98a3c6bfbaSNick Desaulniers	  OBJCOPY=s390x-linux-gnu-objcopy
99a3c6bfbaSNick Desaulniers
100a3c6bfbaSNick DesaulniersThis example will invoke ``s390x-linux-gnu-ld.bfd`` as the linker and
101a3c6bfbaSNick Desaulniers``s390x-linux-gnu-objcopy``, so ensure those are reachable in your ``$PATH``.
102a3c6bfbaSNick Desaulniers
103a3c6bfbaSNick Desaulniers``CROSS_COMPILE`` is not used to prefix the Clang compiler binary (or
104a3c6bfbaSNick Desaulnierscorresponding LLVM utilities) as is the case for GNU utilities when ``LLVM=1``
105a3c6bfbaSNick Desaulniersis not set.
106a3c6bfbaSNick Desaulniers
107a3c6bfbaSNick DesaulniersThe LLVM_IAS= argument
108e08831baSNick Desaulniers----------------------
109e08831baSNick Desaulniers
110a3c6bfbaSNick DesaulniersClang can assemble assembler code. You can pass ``LLVM_IAS=0`` to disable this
111a3c6bfbaSNick Desaulniersbehavior and have Clang invoke the corresponding non-integrated assembler
112a3c6bfbaSNick Desaulniersinstead. Example::
113e08831baSNick Desaulniers
114a3c6bfbaSNick Desaulniers	make LLVM=1 LLVM_IAS=0
115e08831baSNick Desaulniers
116a3c6bfbaSNick Desaulniers``CROSS_COMPILE`` is necessary when cross compiling and ``LLVM_IAS=0``
117a3c6bfbaSNick Desaulniersis used in order to set ``--prefix=`` for the compiler to find the
118a3c6bfbaSNick Desaulnierscorresponding non-integrated assembler (typically, you don't want to use the
119a3c6bfbaSNick Desaulnierssystem assembler when targeting another architecture). Example::
120e08831baSNick Desaulniers
121a3c6bfbaSNick Desaulniers	make LLVM=1 ARCH=arm LLVM_IAS=0 CROSS_COMPILE=arm-linux-gnueabi-
122e08831baSNick Desaulniers
123f12b034aSNick Desaulniers
124a3c6bfbaSNick DesaulniersCcache
125a3c6bfbaSNick Desaulniers------
126f12b034aSNick Desaulniers
127a3c6bfbaSNick Desaulniers``ccache`` can be used with ``clang`` to improve subsequent builds, (though
128a3c6bfbaSNick DesaulniersKBUILD_BUILD_TIMESTAMP_ should be set to a deterministic value between builds
129a3c6bfbaSNick Desaulniersin order to avoid 100% cache misses, see Reproducible_builds_ for more info):
130a3c6bfbaSNick Desaulniers
131a3c6bfbaSNick Desaulniers	KBUILD_BUILD_TIMESTAMP='' make LLVM=1 CC="ccache clang"
132a3c6bfbaSNick Desaulniers
133a3c6bfbaSNick Desaulniers.. _KBUILD_BUILD_TIMESTAMP: kbuild.html#kbuild-build-timestamp
134a3c6bfbaSNick Desaulniers.. _Reproducible_builds: reproducible-builds.html#timestamps
135e08831baSNick Desaulniers
136ed4e9e61SNathan ChancellorSupported Architectures
137ed4e9e61SNathan Chancellor-----------------------
138ed4e9e61SNathan Chancellor
139ed4e9e61SNathan ChancellorLLVM does not target all of the architectures that Linux supports and
140ed4e9e61SNathan Chancellorjust because a target is supported in LLVM does not mean that the kernel
141ed4e9e61SNathan Chancellorwill build or work without any issues. Below is a general summary of
142ed4e9e61SNathan Chancellorarchitectures that currently work with ``CC=clang`` or ``LLVM=1``. Level
143ed4e9e61SNathan Chancellorof support corresponds to "S" values in the MAINTAINERS files. If an
144ed4e9e61SNathan Chancellorarchitecture is not present, it either means that LLVM does not target
145ed4e9e61SNathan Chancellorit or there are known issues. Using the latest stable version of LLVM or
146ed4e9e61SNathan Chancelloreven the development tree will generally yield the best results.
147ed4e9e61SNathan ChancellorAn architecture's ``defconfig`` is generally expected to work well,
148ed4e9e61SNathan Chancellorcertain configurations may have problems that have not been uncovered
149ed4e9e61SNathan Chancelloryet. Bug reports are always welcome at the issue tracker below!
150ed4e9e61SNathan Chancellor
151ed4e9e61SNathan Chancellor.. list-table::
152ed4e9e61SNathan Chancellor   :widths: 10 10 10
153ed4e9e61SNathan Chancellor   :header-rows: 1
154ed4e9e61SNathan Chancellor
155ed4e9e61SNathan Chancellor   * - Architecture
156ed4e9e61SNathan Chancellor     - Level of support
157ed4e9e61SNathan Chancellor     - ``make`` command
158ed4e9e61SNathan Chancellor   * - arm
159ed4e9e61SNathan Chancellor     - Supported
160ed4e9e61SNathan Chancellor     - ``LLVM=1``
161ed4e9e61SNathan Chancellor   * - arm64
162ed4e9e61SNathan Chancellor     - Supported
163ed4e9e61SNathan Chancellor     - ``LLVM=1``
164291810beSNick Desaulniers   * - hexagon
165291810beSNick Desaulniers     - Maintained
166291810beSNick Desaulniers     - ``LLVM=1``
167a3c6bfbaSNick Desaulniers   * - loongarch
168a3c6bfbaSNick Desaulniers     - Maintained
169a3c6bfbaSNick Desaulniers     - ``LLVM=1``
170ed4e9e61SNathan Chancellor   * - mips
171ed4e9e61SNathan Chancellor     - Maintained
172291810beSNick Desaulniers     - ``LLVM=1``
173ed4e9e61SNathan Chancellor   * - powerpc
174ed4e9e61SNathan Chancellor     - Maintained
175a3c6bfbaSNick Desaulniers     - ``LLVM=1``
176ed4e9e61SNathan Chancellor   * - riscv
177a3c6bfbaSNick Desaulniers     - Supported
178291810beSNick Desaulniers     - ``LLVM=1``
179ed4e9e61SNathan Chancellor   * - s390
180ed4e9e61SNathan Chancellor     - Maintained
181*978fa00eSNathan Chancellor     - ``LLVM=1`` (LLVM >= 18.1.0), ``CC=clang`` (LLVM < 18.1.0)
182291810beSNick Desaulniers   * - um (User Mode)
183291810beSNick Desaulniers     - Maintained
184291810beSNick Desaulniers     - ``LLVM=1``
185ed4e9e61SNathan Chancellor   * - x86
186ed4e9e61SNathan Chancellor     - Supported
187ed4e9e61SNathan Chancellor     - ``LLVM=1``
188ed4e9e61SNathan Chancellor
189fcf1b6a3SNick DesaulniersGetting Help
190fcf1b6a3SNick Desaulniers------------
191fcf1b6a3SNick Desaulniers
192fcf1b6a3SNick Desaulniers- `Website <https://clangbuiltlinux.github.io/>`_
19328f8fc19SNathan Chancellor- `Mailing List <https://lore.kernel.org/llvm/>`_: <llvm@lists.linux.dev>
19428f8fc19SNathan Chancellor- `Old Mailing List Archives <https://groups.google.com/g/clang-built-linux>`_
195fcf1b6a3SNick Desaulniers- `Issue Tracker <https://github.com/ClangBuiltLinux/linux/issues>`_
1961c3493bbSNathan Chancellor- IRC: #clangbuiltlinux on irc.libera.chat
197fcf1b6a3SNick Desaulniers- `Telegram <https://t.me/ClangBuiltLinux>`_: @ClangBuiltLinux
198fcf1b6a3SNick Desaulniers- `Wiki <https://github.com/ClangBuiltLinux/linux/wiki>`_
199fcf1b6a3SNick Desaulniers- `Beginner Bugs <https://github.com/ClangBuiltLinux/linux/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22>`_
200fcf1b6a3SNick Desaulniers
2013519c4d6SNick Desaulniers.. _getting_llvm:
2023519c4d6SNick Desaulniers
203fcf1b6a3SNick DesaulniersGetting LLVM
204fcf1b6a3SNick Desaulniers-------------
205fcf1b6a3SNick Desaulniers
206a3c6bfbaSNick DesaulniersWe provide prebuilt stable versions of LLVM on `kernel.org
207a3c6bfbaSNick Desaulniers<https://kernel.org/pub/tools/llvm/>`_. These have been optimized with profile
208a3c6bfbaSNick Desaulniersdata for building Linux kernels, which should improve kernel build times
209a3c6bfbaSNick Desaulniersrelative to other distributions of LLVM.
210a3c6bfbaSNick Desaulniers
211dcc11ac9SNathan ChancellorBelow are links that may be useful for building LLVM from source or procuring
212dcc11ac9SNathan Chancellorit through a distribution's package manager.
213dcc11ac9SNathan Chancellor
21416a122c7SAlexander A. Klimov- https://releases.llvm.org/download.html
215fcf1b6a3SNick Desaulniers- https://github.com/llvm/llvm-project
216fcf1b6a3SNick Desaulniers- https://llvm.org/docs/GettingStarted.html
217fcf1b6a3SNick Desaulniers- https://llvm.org/docs/CMake.html
218fcf1b6a3SNick Desaulniers- https://apt.llvm.org/
219fcf1b6a3SNick Desaulniers- https://www.archlinux.org/packages/extra/x86_64/llvm/
220fcf1b6a3SNick Desaulniers- https://github.com/ClangBuiltLinux/tc-build
221fcf1b6a3SNick Desaulniers- https://github.com/ClangBuiltLinux/linux/wiki/Building-Clang-from-source
222fcf1b6a3SNick Desaulniers- https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/
223