1 ====================================
2 Getting Started with the LLVM System
3 ====================================
11 Welcome to the LLVM project!
13 The LLVM project has multiple components. The core of the project is
14 itself called "LLVM". This contains all of the tools, libraries, and header
15 files needed to process intermediate representations and converts it into
16 object files. Tools include an assembler, disassembler, bitcode analyzer, and
17 bitcode optimizer. It also contains basic regression tests.
19 C-like languages use the `Clang <https://clang.llvm.org/>`_ front end. This
20 component compiles C, C++, Objective C, and Objective C++ code into LLVM bitcode
21 -- and from there into object files, using LLVM.
23 Other components include:
24 the `libc++ C++ standard library <https://libcxx.llvm.org>`_,
25 the `LLD linker <https://lld.llvm.org>`_, and more.
27 Getting the Source Code and Building LLVM
28 =========================================
30 #. Check out LLVM (including subprojects like Clang):
32 * ``git clone https://github.com/llvm/llvm-project.git``
35 ``git clone --config core.autocrlf=false
36 https://github.com/llvm/llvm-project.git``
37 * To save storage and speed-up the checkout time, you may want to do a
38 `shallow clone <https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---depthltdepthgt>`_.
39 For example, to get the latest revision of the LLVM project, use
41 ``git clone --depth 1 https://github.com/llvm/llvm-project.git``
43 #. Configure and build LLVM and Clang:
46 * ``cmake -S llvm -B build -G <generator> [options]``
48 Some common build system generators are:
50 * ``Ninja`` --- for generating `Ninja <https://ninja-build.org>`_
51 build files. Most llvm developers use Ninja.
52 * ``Unix Makefiles`` --- for generating make-compatible parallel makefiles.
53 * ``Visual Studio`` --- for generating Visual Studio projects and
55 * ``Xcode`` --- for generating Xcode projects.
58 <https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html>`_
59 for a more comprehensive list.
63 * ``-DLLVM_ENABLE_PROJECTS='...'`` --- semicolon-separated list of the LLVM
64 subprojects you'd like to additionally build. Can include any of: clang,
65 clang-tools-extra, lldb, lld, polly, or cross-project-tests.
67 For example, to build LLVM, Clang, and LLD, use
68 ``-DLLVM_ENABLE_PROJECTS="clang;lld"``.
70 * ``-DCMAKE_INSTALL_PREFIX=directory`` --- Specify for *directory* the full
71 pathname of where you want the LLVM tools and libraries to be installed
72 (default ``/usr/local``).
74 * ``-DCMAKE_BUILD_TYPE=type`` --- Controls optimization level and debug
75 information of the build. Valid options for *type* are ``Debug``,
76 ``Release``, ``RelWithDebInfo``, and ``MinSizeRel``. For more detailed
77 information see :ref:`CMAKE_BUILD_TYPE <cmake_build_type>`.
79 * ``-DLLVM_ENABLE_ASSERTIONS=ON`` --- Compile with assertion checks enabled
80 (default is ON for Debug builds, OFF for all other build types).
82 * ``-DLLVM_USE_LINKER=lld`` --- Link with the `lld linker`_, assuming it
83 is installed on your system. This can dramatically speed up link times
84 if the default linker is slow.
86 * ``-DLLVM_PARALLEL_{COMPILE,LINK}_JOBS=N`` --- Limit the number of
87 compile/link jobs running in parallel at the same time. This is
88 especially important for linking since linking can use lots of memory. If
89 you run into memory issues building LLVM, try setting this to limit the
90 maximum number of compile/link jobs running at the same time.
92 * ``cmake --build build [--target <target>]`` or the build system specified
95 * The default target (i.e. ``cmake --build build`` or ``make -C build``)
96 will build all of LLVM.
98 * The ``check-all`` target (i.e. ``ninja check-all``) will run the
99 regression tests to ensure everything is in working order.
101 * CMake will generate build targets for each tool and library, and most
102 LLVM sub-projects generate their own ``check-<project>`` target.
104 * Running a serial build will be **slow**. To improve speed, try running a
105 parallel build. That's done by default in Ninja; for ``make``, use the
106 option ``-j NN``, where ``NN`` is the number of parallel jobs, e.g. the
107 number of available CPUs.
109 * A basic CMake and build/test invocation which only builds LLVM and no other
112 ``cmake -S llvm -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug``
114 ``ninja -C build check-llvm``
116 This will setup an LLVM build with debugging info, then compile LLVM and
119 * For more detailed information on CMake options, see `CMake <CMake.html>`__
121 * If you get build or test failures, see `below`_.
123 Consult the `Getting Started with LLVM`_ section for detailed information on
124 configuring and compiling LLVM. Go to `Directory Layout`_ to learn about the
125 layout of the source code tree.
130 Stand-alone builds allow you to build a sub-project against a pre-built
131 version of the clang or llvm libraries that is already present on your
134 You can use the source code from a standard checkout of the llvm-project
135 (as described above) to do stand-alone builds, but you may also build
136 from a :ref:`sparse checkout<workflow-multicheckout-nocommit>` or from the
137 tarballs available on the `releases <https://github.com/llvm/llvm-project/releases/>`_
140 For stand-alone builds, you must have an llvm install that is configured
141 properly to be consumable by stand-alone builds of the other projects.
142 This could be a distro provided LLVM install, or you can build it yourself,
145 .. code-block:: console
147 cmake -G Ninja -S path/to/llvm-project/llvm -B $builddir \
148 -DLLVM_INSTALL_UTILS=ON \
149 -DCMAKE_INSTALL_PREFIX=/path/to/llvm/install/prefix \
152 ninja -C $builddir install
154 Once llvm is installed, to configure a project for a stand-alone build, invoke CMake like this:
156 .. code-block:: console
158 cmake -G Ninja -S path/to/llvm-project/$subproj \
159 -B $buildir_subproj \
160 -DLLVM_EXTERNAL_LIT=/path/to/lit \
161 -DLLVM_ROOT=/path/to/llvm/install/prefix
165 * The stand-alone build needs to happen in a folder that is not the
166 original folder where LLVMN was built
167 (`$builddir!=$builddir_subproj`).
168 * ``LLVM_ROOT`` should point to the prefix of your llvm installation,
169 so for example, if llvm is installed into ``/usr/bin`` and
170 ``/usr/lib64``, then you should pass ``-DLLVM_ROOT=/usr/``.
171 * Both the ``LLVM_ROOT`` and ``LLVM_EXTERNAL_LIT`` options are
172 required to do stand-alone builds for all sub-projects. Additional
173 required options for each sub-project can be found in the table
176 The ``check-$subproj`` and ``install`` build targets are supported for the
177 sub-projects listed in the table below.
179 ============ ======================== ======================
180 Sub-Project Required Sub-Directories Required CMake Options
181 ============ ======================== ======================
182 llvm llvm, cmake, third-party LLVM_INSTALL_UTILS=ON
183 clang clang, cmake CLANG_INCLUDE_TESTS=ON (Required for check-clang only)
185 ============ ======================== ======================
187 Example for building stand-alone `clang`:
189 .. code-block:: console
193 build_llvm=`pwd`/build-llvm
194 build_clang=`pwd`/build-clang
195 installprefix=`pwd`/install
196 llvm=`pwd`/llvm-project
198 mkdir -p $installprefix
200 cmake -G Ninja -S $llvm/llvm -B $build_llvm \
201 -DLLVM_INSTALL_UTILS=ON \
202 -DCMAKE_INSTALL_PREFIX=$installprefix \
203 -DCMAKE_BUILD_TYPE=Release
205 ninja -C $build_llvm install
207 cmake -G Ninja -S $llvm/clang -B $build_clang \
208 -DLLVM_EXTERNAL_LIT=$build_llvm/utils/lit \
209 -DLLVM_ROOT=$installprefix
211 ninja -C $build_clang
216 Before you begin to use the LLVM system, review the requirements given below.
217 This may save you some trouble by knowing ahead of time what hardware and
218 software you will need.
223 LLVM is known to work on the following host platforms:
225 ================== ===================== =============
227 ================== ===================== =============
228 Linux x86\ :sup:`1` GCC, Clang
229 Linux amd64 GCC, Clang
231 Linux Mips GCC, Clang
232 Linux PowerPC GCC, Clang
233 Linux SystemZ GCC, Clang
234 Solaris V9 (Ultrasparc) GCC
235 DragonFlyBSD amd64 GCC, Clang
236 FreeBSD x86\ :sup:`1` GCC, Clang
237 FreeBSD amd64 GCC, Clang
238 NetBSD x86\ :sup:`1` GCC, Clang
239 NetBSD amd64 GCC, Clang
240 OpenBSD x86\ :sup:`1` GCC, Clang
241 OpenBSD amd64 GCC, Clang
242 macOS\ :sup:`2` PowerPC GCC
244 Cygwin/Win32 x86\ :sup:`1, 3` GCC
245 Windows x86\ :sup:`1` Visual Studio
246 Windows x64 x86-64 Visual Studio
247 ================== ===================== =============
251 #. Code generation supported for Pentium processors and up
252 #. Code generation supported for 32-bit ABI only
253 #. To use LLVM modules on Win32-based system, you may configure LLVM
254 with ``-DBUILD_SHARED_LIBS=On``.
256 Note that Debug builds require a lot of time and disk space. An LLVM-only build
257 will need about 1-3 GB of space. A full build of LLVM and Clang will need around
258 15-20 GB of disk space. The exact space requirements will vary by system. (It
259 is so large because of all the debugging information and the fact that the
260 libraries are statically linked into multiple tools).
262 If you are space-constrained, you can build only selected tools or only
263 selected targets. The Release build requires considerably less space.
265 The LLVM suite *may* compile on other platforms, but it is not guaranteed to do
266 so. If compilation is successful, the LLVM utilities should be able to
267 assemble, disassemble, analyze, and optimize LLVM bitcode. Code generation
268 should work as well, although the generated native code may not work on your
274 Compiling LLVM requires that you have several software packages installed. The
275 table below lists those required packages. The Package column is the usual name
276 for the software package that LLVM depends on. The Version column provides
277 "known to work" versions of the package. The Notes column describes how LLVM
278 uses the package and provides other details.
280 =========================================================== ============ ==========================================
281 Package Version Notes
282 =========================================================== ============ ==========================================
283 `CMake <http://cmake.org/>`__ >=3.20.0 Makefile/workspace generator
284 `python <http://www.python.org/>`_ >=3.6 Automated test suite\ :sup:`1`
285 `zlib <http://zlib.net>`_ >=1.2.3.4 Compression library\ :sup:`2`
286 `GNU Make <http://savannah.gnu.org/projects/make>`_ 3.79, 3.79.1 Makefile/build processor\ :sup:`3`
287 =========================================================== ============ ==========================================
291 #. Only needed if you want to run the automated test suite in the
292 ``llvm/test`` directory.
293 #. Optional, adds compression / uncompression capabilities to selected LLVM
295 #. Optional, you can use any other build tool supported by CMake.
297 Additionally, your compilation host is expected to have the usual plethora of
298 Unix utilities. Specifically:
300 * **ar** --- archive library builder
301 * **bzip2** --- bzip2 command for distribution generation
302 * **bunzip2** --- bunzip2 command for distribution checking
303 * **chmod** --- change permissions on a file
304 * **cat** --- output concatenation utility
305 * **cp** --- copy files
306 * **date** --- print the current date/time
307 * **echo** --- print to standard output
308 * **egrep** --- extended regular expression search utility
309 * **find** --- find files/dirs in a file system
310 * **grep** --- regular expression search utility
311 * **gzip** --- gzip command for distribution generation
312 * **gunzip** --- gunzip command for distribution checking
313 * **install** --- install directories/files
314 * **mkdir** --- create a directory
315 * **mv** --- move (rename) files
316 * **ranlib** --- symbol table builder for archive libraries
317 * **rm** --- remove (delete) files and directories
318 * **sed** --- stream editor for transforming output
319 * **sh** --- Bourne shell for make build scripts
320 * **tar** --- tape archive for distribution generation
321 * **test** --- test things in file system
322 * **unzip** --- unzip command for distribution checking
323 * **zip** --- zip command for distribution generation
328 Host C++ Toolchain, both Compiler and Standard Library
329 ------------------------------------------------------
331 LLVM is very demanding of the host C++ compiler, and as such tends to expose
332 bugs in the compiler. We also attempt to follow improvements and developments in
333 the C++ language and library reasonably closely. As such, we require a modern
334 host C++ toolchain, both compiler and standard library, in order to build LLVM.
336 LLVM is written using the subset of C++ documented in :doc:`coding
337 standards<CodingStandards>`. To enforce this language version, we check the most
338 popular host toolchains for specific minimum versions in our build systems:
343 * Visual Studio 2019 16.7
345 Anything older than these toolchains *may* work, but will require forcing the
346 build system with a special option and is not really a supported host platform.
347 Also note that older versions of these compilers have often crashed or
350 For less widely used host toolchains such as ICC or xlC, be aware that a very
351 recent version may be required to support all of the C++ features used in LLVM.
353 We track certain versions of software that are *known* to fail when used as
354 part of the host toolchain. These even include linkers at times.
356 **GNU ld 2.16.X**. Some 2.16.X versions of the ld linker will produce very long
357 warning messages complaining that some "``.gnu.linkonce.t.*``" symbol was
358 defined in a discarded section. You can safely ignore these messages as they are
359 erroneous and the linkage is correct. These messages disappear using ld 2.17.
361 **GNU binutils 2.17**: Binutils 2.17 contains `a bug
362 <http://sourceware.org/bugzilla/show_bug.cgi?id=3111>`__ which causes huge link
363 times (minutes instead of seconds) when building LLVM. We recommend upgrading
364 to a newer version (2.17.50.0.4 or later).
366 **GNU Binutils 2.19.1 Gold**: This version of Gold contained `a bug
367 <http://sourceware.org/bugzilla/show_bug.cgi?id=9836>`__ which causes
368 intermittent failures when building LLVM with position independent code. The
369 symptom is an error about cyclic dependencies. We recommend upgrading to a
370 newer version of Gold.
372 Getting a Modern Host C++ Toolchain
373 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
375 This section mostly applies to Linux and older BSDs. On macOS, you should
376 have a sufficiently modern Xcode, or you will likely need to upgrade until you
377 do. Windows does not have a "system compiler", so you must install either Visual
378 Studio 2019 (or later), or a recent version of mingw64. FreeBSD 10.0 and newer
379 have a modern Clang as the system compiler.
381 However, some Linux distributions and some other or older BSDs sometimes have
382 extremely old versions of GCC. These steps attempt to help you upgrade you
383 compiler even on such a system. However, if at all possible, we encourage you
384 to use a recent version of a distribution with a modern system compiler that
385 meets these requirements. Note that it is tempting to install a prior
386 version of Clang and libc++ to be the host compiler, however libc++ was not
387 well tested or set up to build on Linux until relatively recently. As
388 a consequence, this guide suggests just using libstdc++ and a modern GCC as the
389 initial host in a bootstrap, and then using Clang (and potentially libc++).
391 The first step is to get a recent GCC toolchain installed. The most common
392 distribution on which users have struggled with the version requirements is
393 Ubuntu Precise, 12.04 LTS. For this distribution, one easy option is to install
394 the `toolchain testing PPA`_ and use it to install a modern GCC. There is
395 a really nice discussions of this on the `ask ubuntu stack exchange`_ and a
396 `github gist`_ with updated commands. However, not all users can use PPAs and
397 there are many other distributions, so it may be necessary (or just useful, if
398 you're here you *are* doing compiler development after all) to build and install
399 GCC from source. It is also quite easy to do these days.
401 .. _toolchain testing PPA:
402 https://launchpad.net/~ubuntu-toolchain-r/+archive/test
403 .. _ask ubuntu stack exchange:
404 https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu/581497#58149
406 https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91
408 Easy steps for installing a specific version of GCC:
410 .. code-block:: console
413 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2
414 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2.sig
415 % wget https://ftp.gnu.org/gnu/gnu-keyring.gpg
416 % signature_invalid=`gpg --verify --no-default-keyring --keyring ./gnu-keyring.gpg gcc-${gcc_version}.tar.bz2.sig`
417 % if [ $signature_invalid ]; then echo "Invalid signature" ; exit 1 ; fi
418 % tar -xvjf gcc-${gcc_version}.tar.bz2
419 % cd gcc-${gcc_version}
420 % ./contrib/download_prerequisites
422 % mkdir gcc-${gcc_version}-build
423 % cd gcc-${gcc_version}-build
424 % $PWD/../gcc-${gcc_version}/configure --prefix=$HOME/toolchains --enable-languages=c,c++
428 For more details, check out the excellent `GCC wiki entry`_, where I got most
429 of this information from.
432 https://gcc.gnu.org/wiki/InstallingGCC
434 Once you have a GCC toolchain, configure your build of LLVM to use the new
435 toolchain for your host compiler and C++ standard library. Because the new
436 version of libstdc++ is not on the system library search path, you need to pass
437 extra linker flags so that it can be found at link time (``-L``) and at runtime
438 (``-rpath``). If you are using CMake, this invocation should produce working
441 .. code-block:: console
445 % CC=$HOME/toolchains/bin/gcc CXX=$HOME/toolchains/bin/g++ \
446 cmake .. -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$HOME/toolchains/lib64 -L$HOME/toolchains/lib64"
448 If you fail to set rpath, most LLVM binaries will fail on startup with a message
449 from the loader similar to ``libstdc++.so.6: version `GLIBCXX_3.4.20' not
450 found``. This means you need to tweak the -rpath linker flag.
452 This method will add an absolute path to the rpath of all executables. That's
453 fine for local development. If you want to distribute the binaries you build
454 so that they can run on older systems, copy ``libstdc++.so.6`` into the
455 ``lib/`` directory. All of LLVM's shipping binaries have an rpath pointing at
456 ``$ORIGIN/../lib``, so they will find ``libstdc++.so.6`` there. Non-distributed
457 binaries don't have an rpath set and won't find ``libstdc++.so.6``. Pass
458 ``-DLLVM_LOCAL_RPATH="$HOME/toolchains/lib64"`` to cmake to add an absolute
459 path to ``libstdc++.so.6`` as above. Since these binaries are not distributed,
460 having an absolute local path is fine for them.
462 When you build Clang, you will need to give *it* access to modern C++
463 standard library in order to use it as your new host in part of a bootstrap.
464 There are two easy ways to do this, either build (and install) libc++ along
465 with Clang and then use it with the ``-stdlib=libc++`` compile and link flag,
466 or install Clang into the same prefix (``$HOME/toolchains`` above) as GCC.
467 Clang will look within its own prefix for libstdc++ and use it if found. You
468 can also add an explicit prefix for Clang to look in for a GCC toolchain with
469 the ``--gcc-toolchain=/opt/my/gcc/prefix`` flag, passing it to both compile and
470 link commands when using your just-built-Clang to bootstrap.
472 .. _Getting Started with LLVM:
474 Getting Started with LLVM
475 =========================
477 The remainder of this guide is meant to get you up and running with LLVM and to
478 give you some basic information about the LLVM environment.
480 The later sections of this guide describe the `general layout`_ of the LLVM
481 source tree, a `simple example`_ using the LLVM tool chain, and `links`_ to find
482 more information about LLVM or to get help via e-mail.
484 Terminology and Notation
485 ------------------------
487 Throughout this manual, the following names are used to denote paths specific to
488 the local system and working environment. *These are not environment variables
489 you need to set but just strings used in the rest of this document below*. In
490 any of the examples below, simply replace each of these names with the
491 appropriate pathname on your local system. All these paths are absolute:
495 This is the top level directory of the LLVM source tree.
499 This is the top level directory of the LLVM object tree (i.e. the tree where
500 object files and compiled programs will be placed. It can be the same as
503 Unpacking the LLVM Archives
504 ---------------------------
506 If you have the LLVM distribution, you will need to unpack it before you can
507 begin to compile it. LLVM is distributed as a number of different
508 subprojects. Each one has its own download which is a TAR archive that is
509 compressed with the gzip program.
511 The files are as follows, with *x.y* marking the version number:
515 Source release for the LLVM libraries and tools.
519 Source release for the Clang frontend.
523 Checkout LLVM from Git
524 ----------------------
526 You can also checkout the source code for LLVM from Git.
530 Passing ``--config core.autocrlf=false`` should not be required in
531 the future after we adjust the .gitattribute settings correctly, but
532 is required for Windows users at the time of this writing.
536 .. code-block:: console
538 % git clone https://github.com/llvm/llvm-project.git
542 .. code-block:: console
544 % git clone --config core.autocrlf=false https://github.com/llvm/llvm-project.git
546 This will create an '``llvm-project``' directory in the current directory and
547 fully populate it with all of the source code, test directories, and local
548 copies of documentation files for LLVM and all the related subprojects. Note
549 that unlike the tarballs, which contain each subproject in a separate file, the
550 git repository contains all of the projects together.
552 If you want to get a specific release (as opposed to the most recent revision),
553 you can check out a tag after cloning the repository. E.g., `git checkout
554 llvmorg-6.0.1` inside the ``llvm-project`` directory created by the above
555 command. Use `git tag -l` to list all of them.
560 See :ref:`Contributing <submit_patch>`.
565 See `Bisecting LLVM code <GitBisecting.html>`_ for how to use ``git bisect``
571 When reverting changes using git, the default message will say "This reverts
572 commit XYZ". Leave this at the end of the commit message, but add some details
573 before it as to why the commit is being reverted. A brief explanation and/or
574 links to bots that demonstrate the problem are sufficient.
576 Local LLVM Configuration
577 ------------------------
579 Once checked out repository, the LLVM suite source code must be configured
580 before being built. This process uses CMake. Unlinke the normal ``configure``
581 script, CMake generates the build files in whatever format you request as well
582 as various ``*.inc`` files, and ``llvm/include/llvm/Config/config.h.cmake``.
584 Variables are passed to ``cmake`` on the command line using the format
585 ``-D<variable name>=<value>``. The following variables are some common options
586 used by people developing LLVM.
588 +-------------------------+----------------------------------------------------+
589 | Variable | Purpose |
590 +=========================+====================================================+
591 | CMAKE_C_COMPILER | Tells ``cmake`` which C compiler to use. By |
592 | | default, this will be /usr/bin/cc. |
593 +-------------------------+----------------------------------------------------+
594 | CMAKE_CXX_COMPILER | Tells ``cmake`` which C++ compiler to use. By |
595 | | default, this will be /usr/bin/c++. |
596 +-------------------------+----------------------------------------------------+
597 | CMAKE_BUILD_TYPE | Tells ``cmake`` what type of build you are trying |
598 | | to generate files for. Valid options are Debug, |
599 | | Release, RelWithDebInfo, and MinSizeRel. Default |
601 +-------------------------+----------------------------------------------------+
602 | CMAKE_INSTALL_PREFIX | Specifies the install directory to target when |
603 | | running the install action of the build files. |
604 +-------------------------+----------------------------------------------------+
605 | Python3_EXECUTABLE | Forces CMake to use a specific Python version by |
606 | | passing a path to a Python interpreter. By default |
607 | | the Python version of the interpreter in your PATH |
609 +-------------------------+----------------------------------------------------+
610 | LLVM_TARGETS_TO_BUILD | A semicolon delimited list controlling which |
611 | | targets will be built and linked into llvm. |
612 | | The default list is defined as |
613 | | ``LLVM_ALL_TARGETS``, and can be set to include |
614 | | out-of-tree targets. The default value includes: |
615 | | ``AArch64, AMDGPU, ARM, AVR, BPF, Hexagon, Lanai, |
616 | | Mips, MSP430, NVPTX, PowerPC, RISCV, Sparc, |
617 | | SystemZ, WebAssembly, X86, XCore``. Setting this |
618 | | to ``"host"`` will only compile the host |
619 | | architecture (e.g. equivalent to specifying ``X86``|
620 | | on an x86 host machine) can |
621 | | significantly speed up compile and test times. |
622 +-------------------------+----------------------------------------------------+
623 | LLVM_ENABLE_DOXYGEN | Build doxygen-based documentation from the source |
624 | | code This is disabled by default because it is |
625 | | slow and generates a lot of output. |
626 +-------------------------+----------------------------------------------------+
627 | LLVM_ENABLE_PROJECTS | A semicolon-delimited list selecting which of the |
628 | | other LLVM subprojects to additionally build. (Only|
629 | | effective when using a side-by-side project layout |
630 | | e.g. via git). The default list is empty. Can |
631 | | include: clang, clang-tools-extra, |
632 | | cross-project-tests, flang, libc, libclc, lld, |
633 | | lldb, mlir, openmp, polly, or pstl. |
634 +-------------------------+----------------------------------------------------+
635 | LLVM_ENABLE_RUNTIMES | A semicolon-delimited list selecting which of the |
636 | | runtimes to build. (Only effective when using the |
637 | | full monorepo layout). The default list is empty. |
638 | | Can include: compiler-rt, libc, libcxx, libcxxabi, |
639 | | libunwind, or openmp. |
640 +-------------------------+----------------------------------------------------+
641 | LLVM_ENABLE_SPHINX | Build sphinx-based documentation from the source |
642 | | code. This is disabled by default because it is |
643 | | slow and generates a lot of output. Sphinx version |
644 | | 1.5 or later recommended. |
645 +-------------------------+----------------------------------------------------+
646 | LLVM_BUILD_LLVM_DYLIB | Generate libLLVM.so. This library contains a |
647 | | default set of LLVM components that can be |
648 | | overridden with ``LLVM_DYLIB_COMPONENTS``. The |
649 | | default contains most of LLVM and is defined in |
650 | | ``tools/llvm-shlib/CMakelists.txt``. This option is|
651 | | not available on Windows. |
652 +-------------------------+----------------------------------------------------+
653 | LLVM_OPTIMIZED_TABLEGEN | Builds a release tablegen that gets used during |
654 | | the LLVM build. This can dramatically speed up |
656 +-------------------------+----------------------------------------------------+
658 To configure LLVM, follow these steps:
660 #. Change directory into the object root directory:
662 .. code-block:: console
666 #. Run the ``cmake``:
668 .. code-block:: console
670 % cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=<type> -DCMAKE_INSTALL_PREFIX=/install/path
671 [other options] SRC_ROOT
673 Compiling the LLVM Suite Source Code
674 ------------------------------------
676 Unlike with autotools, with CMake your build type is defined at configuration.
677 If you want to change your build type, you can re-run cmake with the following
680 .. code-block:: console
682 % cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=<type> SRC_ROOT
684 Between runs, CMake preserves the values set for all options. CMake has the
685 following build types defined:
689 These builds are the default. The build system will compile the tools and
690 libraries unoptimized, with debugging information, and asserts enabled.
694 For these builds, the build system will compile the tools and libraries
695 with optimizations enabled and not generate debug info. CMakes default
696 optimization level is -O3. This can be configured by setting the
697 ``CMAKE_CXX_FLAGS_RELEASE`` variable on the CMake command line.
701 These builds are useful when debugging. They generate optimized binaries with
702 debug information. CMakes default optimization level is -O2. This can be
703 configured by setting the ``CMAKE_CXX_FLAGS_RELWITHDEBINFO`` variable on the
706 Once you have LLVM configured, you can build it by entering the *OBJ_ROOT*
707 directory and issuing the following command:
709 .. code-block:: console
713 If the build fails, please `check here`_ to see if you are using a version of
714 GCC that is known not to compile LLVM.
716 If you have multiple processors in your machine, you may wish to use some of the
717 parallel build options provided by GNU Make. For example, you could use the
720 .. code-block:: console
724 There are several special targets which are useful when working with the LLVM
729 Removes all files generated by the build. This includes object files,
730 generated C/C++ files, libraries, and executables.
734 Installs LLVM header files, libraries, tools, and documentation in a hierarchy
735 under ``$PREFIX``, specified with ``CMAKE_INSTALL_PREFIX``, which
736 defaults to ``/usr/local``.
738 ``make docs-llvm-html``
740 If configured with ``-DLLVM_ENABLE_SPHINX=On``, this will generate a directory
741 at ``OBJ_ROOT/docs/html`` which contains the HTML formatted documentation.
746 It is possible to cross-compile LLVM itself. That is, you can create LLVM
747 executables and libraries to be hosted on a platform different from the platform
748 where they are built (a Canadian Cross build). To generate build files for
749 cross-compiling CMake provides a variable ``CMAKE_TOOLCHAIN_FILE`` which can
750 define compiler flags and variables used during the CMake test operations.
752 The result of such a build is executables that are not runnable on the build
753 host but can be executed on the target. As an example the following CMake
754 invocation can generate build files targeting iOS. This will work on macOS
755 with the latest Xcode:
757 .. code-block:: console
759 % cmake -G "Ninja" -DCMAKE_OSX_ARCHITECTURES="armv7;armv7s;arm64"
760 -DCMAKE_TOOLCHAIN_FILE=<PATH_TO_LLVM>/cmake/platforms/iOS.cmake
761 -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_RUNTIME=Off -DLLVM_INCLUDE_TESTS=Off
762 -DLLVM_INCLUDE_EXAMPLES=Off -DLLVM_ENABLE_BACKTRACES=Off [options]
765 Note: There are some additional flags that need to be passed when building for
766 iOS due to limitations in the iOS SDK.
768 Check :doc:`HowToCrossCompileLLVM` and `Clang docs on how to cross-compile in general
769 <https://clang.llvm.org/docs/CrossCompilation.html>`_ for more information
770 about cross-compiling.
772 The Location of LLVM Object Files
773 ---------------------------------
775 The LLVM build system is capable of sharing a single LLVM source tree among
776 several LLVM builds. Hence, it is possible to build LLVM for several different
777 platforms or configurations using the same source tree.
779 * Change directory to where the LLVM object files should live:
781 .. code-block:: console
787 .. code-block:: console
789 % cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release SRC_ROOT
791 The LLVM build will create a structure underneath *OBJ_ROOT* that matches the
792 LLVM source tree. At each level where source files are present in the source
793 tree there will be a corresponding ``CMakeFiles`` directory in the *OBJ_ROOT*.
794 Underneath that directory there is another directory with a name ending in
795 ``.dir`` under which you'll find object files for each source.
799 .. code-block:: console
802 % find lib/Support/ -name APFloat*
803 lib/Support/CMakeFiles/LLVMSupport.dir/APFloat.cpp.o
805 Optional Configuration Items
806 ----------------------------
808 If you're running on a Linux system that supports the `binfmt_misc
809 <http://en.wikipedia.org/wiki/binfmt_misc>`_
810 module, and you have root access on the system, you can set your system up to
811 execute LLVM bitcode files directly. To do this, use commands like this (the
812 first command may not be required if you are already using the module):
814 .. code-block:: console
816 % mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
817 % echo ':llvm:M::BC::/path/to/lli:' > /proc/sys/fs/binfmt_misc/register
818 % chmod u+x hello.bc (if needed)
821 This allows you to execute LLVM bitcode files directly. On Debian, you can also
822 use this command instead of the 'echo' command above:
824 .. code-block:: console
826 % sudo update-binfmts --install llvm /path/to/lli --magic 'BC'
834 One useful source of information about the LLVM source base is the LLVM `doxygen
835 <http://www.doxygen.org/>`_ documentation available at
836 `<https://llvm.org/doxygen/>`_. The following is a brief introduction to code
841 Generates system build files.
843 ``llvm/cmake/modules``
844 Build configuration for llvm user defined options. Checks compiler version and
847 ``llvm/cmake/platforms``
848 Toolchain configuration for Android NDK, iOS systems and non-Windows hosts to
854 - Some simple examples showing how to use LLVM as a compiler for a custom
855 language - including lowering, optimization, and code generation.
857 - Kaleidoscope Tutorial: Kaleidoscope language tutorial run through the
858 implementation of a nice little compiler for a non-trivial language
859 including a hand-written lexer, parser, AST, as well as code generation
860 support using LLVM- both static (ahead of time) and various approaches to
861 Just In Time (JIT) compilation.
862 `Kaleidoscope Tutorial for complete beginner
863 <https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html>`_.
865 - BuildingAJIT: Examples of the `BuildingAJIT tutorial
866 <https://llvm.org/docs/tutorial/BuildingAJIT1.html>`_ that shows how LLVM’s
867 ORC JIT APIs interact with other parts of LLVM. It also, teaches how to
868 recombine them to build a custom JIT that is suited to your use-case.
873 Public header files exported from the LLVM library. The three main subdirectories:
875 ``llvm/include/llvm``
877 All LLVM-specific header files, and subdirectories for different portions of
878 LLVM: ``Analysis``, ``CodeGen``, ``Target``, ``Transforms``, etc...
880 ``llvm/include/llvm/Support``
882 Generic support libraries provided with LLVM but not necessarily specific to
883 LLVM. For example, some C++ STL utilities and a Command Line option processing
884 library store header files here.
886 ``llvm/include/llvm/Config``
888 Header files configured by ``cmake``. They wrap "standard" UNIX and
889 C header files. Source code can include these header files which
890 automatically take care of the conditional #includes that ``cmake``
896 Most source files are here. By putting code in libraries, LLVM makes it easy to
897 share code among the `tools`_.
901 Core LLVM source files that implement core classes like Instruction and
904 ``llvm/lib/AsmParser/``
906 Source code for the LLVM assembly language parser library.
908 ``llvm/lib/Bitcode/``
910 Code for reading and writing bitcode.
912 ``llvm/lib/Analysis/``
914 A variety of program analyses, such as Call Graphs, Induction Variables,
915 Natural Loop Identification, etc.
917 ``llvm/lib/Transforms/``
919 IR-to-IR program transformations, such as Aggressive Dead Code Elimination,
920 Sparse Conditional Constant Propagation, Inlining, Loop Invariant Code Motion,
921 Dead Global Elimination, and many others.
925 Files describing target architectures for code generation. For example,
926 ``llvm/lib/Target/X86`` holds the X86 machine description.
928 ``llvm/lib/CodeGen/``
930 The major parts of the code generator: Instruction Selector, Instruction
931 Scheduling, and Register Allocation.
935 The libraries represent and process code at machine code level. Handles
936 assembly and object-file emission.
938 ``llvm/lib/ExecutionEngine/``
940 Libraries for directly executing bitcode at runtime in interpreted and
941 JIT-compiled scenarios.
943 ``llvm/lib/Support/``
945 Source code that corresponding to the header files in ``llvm/include/ADT/``
946 and ``llvm/include/Support/``.
949 ----------------------
951 Contains bindings for the LLVM compiler infrastructure to allow
952 programs written in languages other than C or C++ to take advantage of the LLVM
954 LLVM project provides language bindings for OCaml and Python.
959 Projects not strictly part of LLVM but shipped with LLVM. This is also the
960 directory for creating your own LLVM-based projects which leverage the LLVM
966 Feature and regression tests and other sanity checks on LLVM infrastructure. These
967 are intended to run quickly and cover a lot of territory without being exhaustive.
972 A comprehensive correctness, performance, and benchmarking test suite
973 for LLVM. This comes in a ``separate git repository
974 <https://github.com/llvm/llvm-test-suite>``, because it contains a
975 large amount of third-party code under a variety of licenses. For
976 details see the :doc:`Testing Guide <TestingGuide>` document.
983 Executables built out of the libraries
984 above, which form the main part of the user interface. You can always get help
985 for a tool by typing ``tool_name -help``. The following is a brief introduction
986 to the most important tools. More detailed information is in
987 the `Command Guide <CommandGuide/index.html>`_.
991 ``bugpoint`` is used to debug optimization passes or code generation backends
992 by narrowing down the given test case to the minimum number of passes and/or
993 instructions that still cause a problem, whether it is a crash or
994 miscompilation. See `<HowToSubmitABug.html>`_ for more information on using
999 The archiver produces an archive containing the given LLVM bitcode files,
1000 optionally with an index for faster lookup.
1004 The assembler transforms the human readable LLVM assembly to LLVM bitcode.
1008 The disassembler transforms the LLVM bitcode to human readable LLVM assembly.
1012 ``llvm-link``, not surprisingly, links multiple LLVM modules into a single
1017 ``lli`` is the LLVM interpreter, which can directly execute LLVM bitcode
1018 (although very slowly...). For architectures that support it (currently x86,
1019 Sparc, and PowerPC), by default, ``lli`` will function as a Just-In-Time
1020 compiler (if the functionality was compiled in), and will execute the code
1021 *much* faster than the interpreter.
1025 ``llc`` is the LLVM backend compiler, which translates LLVM bitcode to a
1026 native code assembly file.
1030 ``opt`` reads LLVM bitcode, applies a series of LLVM to LLVM transformations
1031 (which are specified on the command line), and outputs the resultant
1032 bitcode. '``opt -help``' is a good way to get a list of the
1033 program transformations available in LLVM.
1035 ``opt`` can also run a specific analysis on an input LLVM bitcode
1036 file and print the results. Primarily useful for debugging
1037 analyses, or familiarizing yourself with what an analysis does.
1042 Utilities for working with LLVM source code; some are part of the build process
1043 because they are code generators for parts of the infrastructure.
1048 ``codegen-diff`` finds differences between code that LLC
1049 generates and code that LLI generates. This is useful if you are
1050 debugging one of them, assuming that the other generates correct output. For
1051 the full user manual, run ```perldoc codegen-diff'``.
1055 Emacs and XEmacs syntax highlighting for LLVM assembly files and TableGen
1056 description files. See the ``README`` for information on using them.
1060 Finds and outputs all non-generated source files,
1061 useful if one wishes to do a lot of development across directories
1062 and does not want to find each file. One way to use it is to run,
1063 for example: ``xemacs `utils/getsources.sh``` from the top of the LLVM source
1068 Performs an ``egrep -H -n`` on each source file in LLVM and
1069 passes to it a regular expression provided on ``llvmgrep``'s command
1070 line. This is an efficient way of searching the source base for a
1071 particular regular expression.
1075 Contains the tool used to generate register
1076 descriptions, instruction set descriptions, and even assemblers from common
1077 TableGen description files.
1081 vim syntax-highlighting for LLVM assembly files
1082 and TableGen description files. See the ``README`` for how to use them.
1086 An Example Using the LLVM Tool Chain
1087 ====================================
1089 This section gives an example of using LLVM with the Clang front end.
1094 #. First, create a simple C file, name it 'hello.c':
1101 printf("hello world\n");
1105 #. Next, compile the C file into a native executable:
1107 .. code-block:: console
1109 % clang hello.c -o hello
1113 Clang works just like GCC by default. The standard -S and -c arguments
1114 work as usual (producing a native .s or .o file, respectively).
1116 #. Next, compile the C file into an LLVM bitcode file:
1118 .. code-block:: console
1120 % clang -O3 -emit-llvm hello.c -c -o hello.bc
1122 The -emit-llvm option can be used with the -S or -c options to emit an LLVM
1123 ``.ll`` or ``.bc`` file (respectively) for the code. This allows you to use
1124 the `standard LLVM tools <CommandGuide/index.html>`_ on the bitcode file.
1126 #. Run the program in both forms. To run the program, use:
1128 .. code-block:: console
1134 .. code-block:: console
1138 The second examples shows how to invoke the LLVM JIT, :doc:`lli
1139 <CommandGuide/lli>`.
1141 #. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code:
1143 .. code-block:: console
1145 % llvm-dis < hello.bc | less
1147 #. Compile the program to native assembly using the LLC code generator:
1149 .. code-block:: console
1151 % llc hello.bc -o hello.s
1153 #. Assemble the native assembly language file into a program:
1155 .. code-block:: console
1157 % /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.native # On Solaris
1159 % gcc hello.s -o hello.native # On others
1161 #. Execute the native code program:
1163 .. code-block:: console
1167 Note that using clang to compile directly to native code (i.e. when the
1168 ``-emit-llvm`` option is not present) does steps 6/7/8 for you.
1173 If you are having problems building or using LLVM, or if you have any other
1174 general questions about LLVM, please consult the `Frequently Asked
1175 Questions <FAQ.html>`_ page.
1177 If you are having problems with limited memory and build time, please try
1178 building with ninja instead of make. Please consider configuring the
1179 following options with cmake:
1182 Setting this option will allow you to build with ninja instead of make.
1183 Building with ninja significantly improves your build time, especially with
1184 incremental builds, and improves your memory usage.
1187 Setting this option to lld will significantly reduce linking time for LLVM
1188 executables on ELF-based platforms, such as Linux. If you are building LLVM
1189 for the first time and lld is not available to you as a binary package, then
1190 you may want to use the gold linker as a faster alternative to GNU ld.
1192 * -DCMAKE_BUILD_TYPE
1193 Controls optimization level and debug information of the build. This setting
1194 can affect RAM and disk usage, see :ref:`CMAKE_BUILD_TYPE <cmake_build_type>`
1195 for more information.
1197 * -DLLVM_ENABLE_ASSERTIONS
1198 This option defaults to ON for Debug builds and defaults to OFF for Release
1199 builds. As mentioned in the previous option, using the Release build type and
1200 enabling assertions may be a good alternative to using the Debug build type.
1202 * -DLLVM_PARALLEL_LINK_JOBS
1203 Set this equal to number of jobs you wish to run simultaneously. This is
1204 similar to the -j option used with make, but only for link jobs. This option
1205 can only be used with ninja. You may wish to use a very low number of jobs,
1206 as this will greatly reduce the amount of memory used during the build
1207 process. If you have limited memory, you may wish to set this to 1.
1209 * -DLLVM_TARGETS_TO_BUILD
1210 Set this equal to the target you wish to build. You may wish to set this to
1211 X86; however, you will find a full list of targets within the
1212 llvm-project/llvm/lib/Target directory.
1214 * -DLLVM_OPTIMIZED_TABLEGEN
1215 Set this to ON to generate a fully optimized tablegen during your build. This
1216 will significantly improve your build time. This is only useful if you are
1217 using the Debug build type.
1219 * -DLLVM_ENABLE_PROJECTS
1220 Set this equal to the projects you wish to compile (e.g. clang, lld, etc.) If
1221 compiling more than one project, separate the items with a semicolon. Should
1222 you run into issues with the semicolon, try surrounding it with single quotes.
1224 * -DLLVM_ENABLE_RUNTIMES
1225 Set this equal to the runtimes you wish to compile (e.g. libcxx, libcxxabi, etc.)
1226 If compiling more than one runtime, separate the items with a semicolon. Should
1227 you run into issues with the semicolon, try surrounding it with single quotes.
1229 * -DCLANG_ENABLE_STATIC_ANALYZER
1230 Set this option to OFF if you do not require the clang static analyzer. This
1231 should improve your build time slightly.
1233 * -DLLVM_USE_SPLIT_DWARF
1234 Consider setting this to ON if you require a debug build, as this will ease
1235 memory pressure on the linker. This will make linking much faster, as the
1236 binaries will not contain any of the debug information; however, this will
1237 generate the debug information in the form of a DWARF object file (with the
1238 extension .dwo). This only applies to host platforms using ELF, such as Linux.
1245 This document is just an **introduction** on how to use LLVM to do some simple
1246 things... there are many more interesting and complicated things that you can do
1247 that aren't documented here (but we'll gladly accept a patch if you want to
1248 write something up!). For more information about LLVM, check out:
1250 * `LLVM Homepage <https://llvm.org/>`_
1251 * `LLVM Doxygen Tree <https://llvm.org/doxygen/>`_
1252 * `Starting a Project that Uses LLVM <https://llvm.org/docs/Projects.html>`_