2020-08-26 22:15:55 +03:00
|
|
|
.. _kbuild_llvm:
|
|
|
|
|
2020-02-27 02:23:36 +03:00
|
|
|
==============================
|
|
|
|
Building Linux with Clang/LLVM
|
|
|
|
==============================
|
|
|
|
|
|
|
|
This document covers how to build the Linux kernel with Clang and LLVM
|
|
|
|
utilities.
|
|
|
|
|
|
|
|
About
|
|
|
|
-----
|
|
|
|
|
|
|
|
The Linux kernel has always traditionally been compiled with GNU toolchains
|
|
|
|
such as GCC and binutils. Ongoing work has allowed for `Clang
|
|
|
|
<https://clang.llvm.org/>`_ and `LLVM <https://llvm.org/>`_ utilities to be
|
|
|
|
used as viable substitutes. Distributions such as `Android
|
|
|
|
<https://www.android.com/>`_, `ChromeOS
|
|
|
|
<https://www.chromium.org/chromium-os>`_, and `OpenMandriva
|
|
|
|
<https://www.openmandriva.org/>`_ use Clang built kernels. `LLVM is a
|
|
|
|
collection of toolchain components implemented in terms of C++ objects
|
|
|
|
<https://www.aosabook.org/en/llvm.html>`_. Clang is a front-end to LLVM that
|
|
|
|
supports C and the GNU C extensions required by the kernel, and is pronounced
|
|
|
|
"klang," not "see-lang."
|
|
|
|
|
|
|
|
Clang
|
|
|
|
-----
|
|
|
|
|
2020-08-26 02:14:38 +03:00
|
|
|
The compiler used can be swapped out via ``CC=`` command line argument to ``make``.
|
|
|
|
``CC=`` should be set when selecting a config and during a build. ::
|
2020-02-27 02:23:36 +03:00
|
|
|
|
|
|
|
make CC=clang defconfig
|
|
|
|
|
|
|
|
make CC=clang
|
|
|
|
|
|
|
|
Cross Compiling
|
|
|
|
---------------
|
|
|
|
|
|
|
|
A single Clang compiler binary will typically contain all supported backends,
|
2020-08-26 02:14:38 +03:00
|
|
|
which can help simplify cross compiling. ::
|
2020-02-27 02:23:36 +03:00
|
|
|
|
2021-08-02 21:39:10 +03:00
|
|
|
make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-gnu-
|
2020-02-27 02:23:36 +03:00
|
|
|
|
2020-08-26 02:14:38 +03:00
|
|
|
``CROSS_COMPILE`` is not used to prefix the Clang compiler binary, instead
|
2020-09-25 18:21:14 +03:00
|
|
|
``CROSS_COMPILE`` is used to set a command line flag: ``--target=<triple>``. For
|
2020-08-26 02:14:38 +03:00
|
|
|
example: ::
|
2020-02-27 02:23:36 +03:00
|
|
|
|
2020-09-25 18:21:14 +03:00
|
|
|
clang --target=aarch64-linux-gnu foo.c
|
2020-02-27 02:23:36 +03:00
|
|
|
|
|
|
|
LLVM Utilities
|
|
|
|
--------------
|
|
|
|
|
2020-08-26 02:14:38 +03:00
|
|
|
LLVM has substitutes for GNU binutils utilities. Kbuild supports ``LLVM=1``
|
|
|
|
to enable them. ::
|
2020-04-08 04:36:23 +03:00
|
|
|
|
|
|
|
make LLVM=1
|
|
|
|
|
2020-08-26 02:14:38 +03:00
|
|
|
They can be enabled individually. The full list of the parameters: ::
|
2020-02-27 02:23:36 +03:00
|
|
|
|
2020-08-26 02:14:38 +03:00
|
|
|
make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
|
2020-10-23 14:57:32 +03:00
|
|
|
OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
|
|
|
|
HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld
|
2020-02-27 02:23:36 +03:00
|
|
|
|
2021-08-06 20:27:01 +03:00
|
|
|
The integrated assembler is enabled by default. You can pass ``LLVM_IAS=0`` to
|
|
|
|
disable it.
|
2020-04-08 04:36:22 +03:00
|
|
|
|
2021-08-02 21:39:10 +03:00
|
|
|
Omitting CROSS_COMPILE
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
As explained above, ``CROSS_COMPILE`` is used to set ``--target=<triple>``.
|
|
|
|
|
|
|
|
If ``CROSS_COMPILE`` is not specified, the ``--target=<triple>`` is inferred
|
|
|
|
from ``ARCH``.
|
|
|
|
|
|
|
|
That means if you use only LLVM tools, ``CROSS_COMPILE`` becomes unnecessary.
|
|
|
|
|
|
|
|
For example, to cross-compile the arm64 kernel::
|
|
|
|
|
2021-08-06 20:27:01 +03:00
|
|
|
make ARCH=arm64 LLVM=1
|
|
|
|
|
|
|
|
If ``LLVM_IAS=0`` is specified, ``CROSS_COMPILE`` is also used to derive
|
|
|
|
``--prefix=<path>`` to search for the GNU assembler and linker. ::
|
|
|
|
|
|
|
|
make ARCH=arm64 LLVM=1 LLVM_IAS=0 CROSS_COMPILE=aarch64-linux-gnu-
|
2021-08-02 21:39:10 +03:00
|
|
|
|
2021-01-14 03:34:47 +03:00
|
|
|
Supported Architectures
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
LLVM does not target all of the architectures that Linux supports and
|
|
|
|
just because a target is supported in LLVM does not mean that the kernel
|
|
|
|
will build or work without any issues. Below is a general summary of
|
|
|
|
architectures that currently work with ``CC=clang`` or ``LLVM=1``. Level
|
|
|
|
of support corresponds to "S" values in the MAINTAINERS files. If an
|
|
|
|
architecture is not present, it either means that LLVM does not target
|
|
|
|
it or there are known issues. Using the latest stable version of LLVM or
|
|
|
|
even the development tree will generally yield the best results.
|
|
|
|
An architecture's ``defconfig`` is generally expected to work well,
|
|
|
|
certain configurations may have problems that have not been uncovered
|
|
|
|
yet. Bug reports are always welcome at the issue tracker below!
|
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
:widths: 10 10 10
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
* - Architecture
|
|
|
|
- Level of support
|
|
|
|
- ``make`` command
|
|
|
|
* - arm
|
|
|
|
- Supported
|
|
|
|
- ``LLVM=1``
|
|
|
|
* - arm64
|
|
|
|
- Supported
|
|
|
|
- ``LLVM=1``
|
|
|
|
* - mips
|
|
|
|
- Maintained
|
|
|
|
- ``CC=clang``
|
|
|
|
* - powerpc
|
|
|
|
- Maintained
|
|
|
|
- ``CC=clang``
|
|
|
|
* - riscv
|
|
|
|
- Maintained
|
|
|
|
- ``CC=clang``
|
|
|
|
* - s390
|
|
|
|
- Maintained
|
|
|
|
- ``CC=clang``
|
|
|
|
* - x86
|
|
|
|
- Supported
|
|
|
|
- ``LLVM=1``
|
|
|
|
|
2020-02-27 02:23:36 +03:00
|
|
|
Getting Help
|
|
|
|
------------
|
|
|
|
|
|
|
|
- `Website <https://clangbuiltlinux.github.io/>`_
|
2021-09-08 05:58:27 +03:00
|
|
|
- `Mailing List <https://lore.kernel.org/llvm/>`_: <llvm@lists.linux.dev>
|
|
|
|
- `Old Mailing List Archives <https://groups.google.com/g/clang-built-linux>`_
|
2020-02-27 02:23:36 +03:00
|
|
|
- `Issue Tracker <https://github.com/ClangBuiltLinux/linux/issues>`_
|
2021-09-08 05:58:30 +03:00
|
|
|
- IRC: #clangbuiltlinux on irc.libera.chat
|
2020-02-27 02:23:36 +03:00
|
|
|
- `Telegram <https://t.me/ClangBuiltLinux>`_: @ClangBuiltLinux
|
|
|
|
- `Wiki <https://github.com/ClangBuiltLinux/linux/wiki>`_
|
|
|
|
- `Beginner Bugs <https://github.com/ClangBuiltLinux/linux/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22>`_
|
|
|
|
|
2020-08-26 22:15:55 +03:00
|
|
|
.. _getting_llvm:
|
|
|
|
|
2020-02-27 02:23:36 +03:00
|
|
|
Getting LLVM
|
|
|
|
-------------
|
|
|
|
|
2020-07-19 22:46:02 +03:00
|
|
|
- https://releases.llvm.org/download.html
|
2020-02-27 02:23:36 +03:00
|
|
|
- https://github.com/llvm/llvm-project
|
|
|
|
- https://llvm.org/docs/GettingStarted.html
|
|
|
|
- https://llvm.org/docs/CMake.html
|
|
|
|
- https://apt.llvm.org/
|
|
|
|
- https://www.archlinux.org/packages/extra/x86_64/llvm/
|
|
|
|
- https://github.com/ClangBuiltLinux/tc-build
|
|
|
|
- https://github.com/ClangBuiltLinux/linux/wiki/Building-Clang-from-source
|
|
|
|
- https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/
|