Documentation/kbuild/llvm.rst

Source file repositories/reference/linux-study-clean/Documentation/kbuild/llvm.rst

File Facts

System
Linux kernel
Corpus path
Documentation/kbuild/llvm.rst
Extension
.rst
Size
7489 bytes
Lines
222
Domain
Support Tooling And Documentation
Bucket
Documentation
Inferred role
Support Tooling And Documentation: documentation
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

.. _kbuild_llvm:

==============================
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>`_, `OpenMandriva
<https://www.openmandriva.org/>`_, and `Chimera Linux
<https://chimera-linux.org/>`_ use Clang built kernels. Google's and Meta's
datacenter fleets also run kernels built with Clang.

`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."

Building with LLVM
------------------

Invoke ``make`` via::

	make LLVM=1

to compile for the host target. For cross compiling::

	make LLVM=1 ARCH=arm64

The LLVM= argument
------------------

LLVM has substitutes for GNU binutils utilities. They can be enabled
individually. The full list of supported make variables::

	make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
	  OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
	  HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld

``LLVM=1`` expands to the above.

If your LLVM tools are not available in your PATH, you can supply their
location using the LLVM variable with a trailing slash::

	make LLVM=/path/to/llvm/

which will use ``/path/to/llvm/clang``, ``/path/to/llvm/ld.lld``, etc. The
following may also be used::

	PATH=/path/to/llvm:$PATH make LLVM=1

If your LLVM tools have a version suffix and you want to test with that
explicit version rather than the unsuffixed executables like ``LLVM=1``, you
can pass the suffix using the ``LLVM`` variable::

	make LLVM=-14

which will use ``clang-14``, ``ld.lld-14``, etc.

To support combinations of out of tree paths with version suffixes, we
recommend::

Annotation

Implementation Notes