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 The LLVM Getting Started documentation may be out of date. The `Clang
31 Getting Started <https://clang.llvm.org/get_started.html>`_ page might have more
34 This is an example workflow and configuration to get and build the LLVM source:
36 #. Checkout LLVM (including related subprojects like Clang):
38 * ``git clone https://github.com/llvm/llvm-project.git``
39 * Or, on windows, ``git clone --config core.autocrlf=false
40 https://github.com/llvm/llvm-project.git``
41 * To save storage and speed-up the checkout time, you may want to do a
42 `shallow clone <https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---depthltdepthgt>`_.
43 For example, to get the latest revision of the LLVM project, use
44 ``git clone --depth 1 https://github.com/llvm/llvm-project.git``
46 #. Configure and build LLVM and Clang:
51 * ``cmake -G <generator> [options] ../llvm``
53 Some common build system generators are:
55 * ``Ninja`` --- for generating `Ninja <https://ninja-build.org>`_
56 build files. Most llvm developers use Ninja.
57 * ``Unix Makefiles`` --- for generating make-compatible parallel makefiles.
58 * ``Visual Studio`` --- for generating Visual Studio projects and
60 * ``Xcode`` --- for generating Xcode projects.
64 * ``-DLLVM_ENABLE_PROJECTS='...'`` --- semicolon-separated list of the LLVM
65 subprojects you'd like to additionally build. Can include any of: clang,
66 clang-tools-extra, lldb, compiler-rt, lld, polly, or cross-project-tests.
68 For example, to build LLVM, Clang, libcxx, and libcxxabi, use
69 ``-DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi"``.
71 * ``-DCMAKE_INSTALL_PREFIX=directory`` --- Specify for *directory* the full
72 pathname of where you want the LLVM tools and libraries to be installed
73 (default ``/usr/local``).
75 * ``-DCMAKE_BUILD_TYPE=type`` --- Valid options for *type* are Debug,
76 Release, RelWithDebInfo, and MinSizeRel. Default is Debug.
78 * ``-DLLVM_ENABLE_ASSERTIONS=On`` --- Compile with assertion checks enabled
79 (default is Yes for Debug builds, No for all other build types).
81 * ``cmake --build . [--target <target>]`` or the build system specified
84 * The default target (i.e. ``cmake --build .`` or ``make``) will build all of
87 * The ``check-all`` target (i.e. ``ninja check-all``) will run the
88 regression tests to ensure everything is in working order.
90 * CMake will generate build targets for each tool and library, and most
91 LLVM sub-projects generate their own ``check-<project>`` target.
93 * Running a serial build will be **slow**. To improve speed, try running a
94 parallel build. That's done by default in Ninja; for ``make``, use the
95 option ``-j NN``, where ``NN`` is the number of parallel jobs, e.g. the
96 number of available CPUs.
98 * For more information see `CMake <CMake.html>`__
100 * If you get an "internal compiler error (ICE)" or test failures, see
103 Consult the `Getting Started with LLVM`_ section for detailed information on
104 configuring and compiling LLVM. Go to `Directory Layout`_ to learn about the
105 layout of the source code tree.
110 Before you begin to use the LLVM system, review the requirements given below.
111 This may save you some trouble by knowing ahead of time what hardware and
112 software you will need.
117 LLVM is known to work on the following host platforms:
119 ================== ===================== =============
121 ================== ===================== =============
122 Linux x86\ :sup:`1` GCC, Clang
123 Linux amd64 GCC, Clang
125 Linux Mips GCC, Clang
126 Linux PowerPC GCC, Clang
127 Linux SystemZ GCC, Clang
128 Solaris V9 (Ultrasparc) GCC
129 DragonFlyBSD amd64 GCC, Clang
130 FreeBSD x86\ :sup:`1` GCC, Clang
131 FreeBSD amd64 GCC, Clang
132 NetBSD x86\ :sup:`1` GCC, Clang
133 NetBSD amd64 GCC, Clang
134 OpenBSD x86\ :sup:`1` GCC, Clang
135 OpenBSD amd64 GCC, Clang
136 macOS\ :sup:`2` PowerPC GCC
138 Cygwin/Win32 x86\ :sup:`1, 3` GCC
139 Windows x86\ :sup:`1` Visual Studio
140 Windows x64 x86-64 Visual Studio
141 ================== ===================== =============
145 #. Code generation supported for Pentium processors and up
146 #. Code generation supported for 32-bit ABI only
147 #. To use LLVM modules on Win32-based system, you may configure LLVM
148 with ``-DBUILD_SHARED_LIBS=On``.
150 Note that Debug builds require a lot of time and disk space. An LLVM-only build
151 will need about 1-3 GB of space. A full build of LLVM and Clang will need around
152 15-20 GB of disk space. The exact space requirements will vary by system. (It
153 is so large because of all the debugging information and the fact that the
154 libraries are statically linked into multiple tools).
156 If you are space-constrained, you can build only selected tools or only
157 selected targets. The Release build requires considerably less space.
159 The LLVM suite *may* compile on other platforms, but it is not guaranteed to do
160 so. If compilation is successful, the LLVM utilities should be able to
161 assemble, disassemble, analyze, and optimize LLVM bitcode. Code generation
162 should work as well, although the generated native code may not work on your
168 Compiling LLVM requires that you have several software packages installed. The
169 table below lists those required packages. The Package column is the usual name
170 for the software package that LLVM depends on. The Version column provides
171 "known to work" versions of the package. The Notes column describes how LLVM
172 uses the package and provides other details.
174 =========================================================== ============ ==========================================
175 Package Version Notes
176 =========================================================== ============ ==========================================
177 `CMake <http://cmake.org/>`__ >=3.13.4 Makefile/workspace generator
178 `GCC <http://gcc.gnu.org/>`_ >=7.1.0 C/C++ compiler\ :sup:`1`
179 `python <http://www.python.org/>`_ >=3.6 Automated test suite\ :sup:`2`
180 `zlib <http://zlib.net>`_ >=1.2.3.4 Compression library\ :sup:`3`
181 `GNU Make <http://savannah.gnu.org/projects/make>`_ 3.79, 3.79.1 Makefile/build processor\ :sup:`4`
182 =========================================================== ============ ==========================================
186 #. Only the C and C++ languages are needed so there's no need to build the
187 other languages for LLVM's purposes. See `below` for specific version
189 #. Only needed if you want to run the automated test suite in the
190 ``llvm/test`` directory.
191 #. Optional, adds compression / uncompression capabilities to selected LLVM
193 #. Optional, you can use any other build tool supported by CMake.
195 Additionally, your compilation host is expected to have the usual plethora of
196 Unix utilities. Specifically:
198 * **ar** --- archive library builder
199 * **bzip2** --- bzip2 command for distribution generation
200 * **bunzip2** --- bunzip2 command for distribution checking
201 * **chmod** --- change permissions on a file
202 * **cat** --- output concatenation utility
203 * **cp** --- copy files
204 * **date** --- print the current date/time
205 * **echo** --- print to standard output
206 * **egrep** --- extended regular expression search utility
207 * **find** --- find files/dirs in a file system
208 * **grep** --- regular expression search utility
209 * **gzip** --- gzip command for distribution generation
210 * **gunzip** --- gunzip command for distribution checking
211 * **install** --- install directories/files
212 * **mkdir** --- create a directory
213 * **mv** --- move (rename) files
214 * **ranlib** --- symbol table builder for archive libraries
215 * **rm** --- remove (delete) files and directories
216 * **sed** --- stream editor for transforming output
217 * **sh** --- Bourne shell for make build scripts
218 * **tar** --- tape archive for distribution generation
219 * **test** --- test things in file system
220 * **unzip** --- unzip command for distribution checking
221 * **zip** --- zip command for distribution generation
226 Host C++ Toolchain, both Compiler and Standard Library
227 ------------------------------------------------------
229 LLVM is very demanding of the host C++ compiler, and as such tends to expose
230 bugs in the compiler. We also attempt to follow improvements and developments in
231 the C++ language and library reasonably closely. As such, we require a modern
232 host C++ toolchain, both compiler and standard library, in order to build LLVM.
234 LLVM is written using the subset of C++ documented in :doc:`coding
235 standards<CodingStandards>`. To enforce this language version, we check the most
236 popular host toolchains for specific minimum versions in our build systems:
241 * Visual Studio 2019 16.7
243 Anything older than these toolchains *may* work, but will require forcing the
244 build system with a special option and is not really a supported host platform.
245 Also note that older versions of these compilers have often crashed or
248 For less widely used host toolchains such as ICC or xlC, be aware that a very
249 recent version may be required to support all of the C++ features used in LLVM.
251 We track certain versions of software that are *known* to fail when used as
252 part of the host toolchain. These even include linkers at times.
254 **GNU ld 2.16.X**. Some 2.16.X versions of the ld linker will produce very long
255 warning messages complaining that some "``.gnu.linkonce.t.*``" symbol was
256 defined in a discarded section. You can safely ignore these messages as they are
257 erroneous and the linkage is correct. These messages disappear using ld 2.17.
259 **GNU binutils 2.17**: Binutils 2.17 contains `a bug
260 <http://sourceware.org/bugzilla/show_bug.cgi?id=3111>`__ which causes huge link
261 times (minutes instead of seconds) when building LLVM. We recommend upgrading
262 to a newer version (2.17.50.0.4 or later).
264 **GNU Binutils 2.19.1 Gold**: This version of Gold contained `a bug
265 <http://sourceware.org/bugzilla/show_bug.cgi?id=9836>`__ which causes
266 intermittent failures when building LLVM with position independent code. The
267 symptom is an error about cyclic dependencies. We recommend upgrading to a
268 newer version of Gold.
270 Getting a Modern Host C++ Toolchain
271 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
273 This section mostly applies to Linux and older BSDs. On macOS, you should
274 have a sufficiently modern Xcode, or you will likely need to upgrade until you
275 do. Windows does not have a "system compiler", so you must install either Visual
276 Studio 2019 (or later), or a recent version of mingw64. FreeBSD 10.0 and newer
277 have a modern Clang as the system compiler.
279 However, some Linux distributions and some other or older BSDs sometimes have
280 extremely old versions of GCC. These steps attempt to help you upgrade you
281 compiler even on such a system. However, if at all possible, we encourage you
282 to use a recent version of a distribution with a modern system compiler that
283 meets these requirements. Note that it is tempting to install a prior
284 version of Clang and libc++ to be the host compiler, however libc++ was not
285 well tested or set up to build on Linux until relatively recently. As
286 a consequence, this guide suggests just using libstdc++ and a modern GCC as the
287 initial host in a bootstrap, and then using Clang (and potentially libc++).
289 The first step is to get a recent GCC toolchain installed. The most common
290 distribution on which users have struggled with the version requirements is
291 Ubuntu Precise, 12.04 LTS. For this distribution, one easy option is to install
292 the `toolchain testing PPA`_ and use it to install a modern GCC. There is
293 a really nice discussions of this on the `ask ubuntu stack exchange`_ and a
294 `github gist`_ with updated commands. However, not all users can use PPAs and
295 there are many other distributions, so it may be necessary (or just useful, if
296 you're here you *are* doing compiler development after all) to build and install
297 GCC from source. It is also quite easy to do these days.
299 .. _toolchain testing PPA:
300 https://launchpad.net/~ubuntu-toolchain-r/+archive/test
301 .. _ask ubuntu stack exchange:
302 https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu/581497#58149
304 https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91
306 Easy steps for installing GCC 7.1.0:
308 .. code-block:: console
311 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2
312 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2.sig
313 % wget https://ftp.gnu.org/gnu/gnu-keyring.gpg
314 % signature_invalid=`gpg --verify --no-default-keyring --keyring ./gnu-keyring.gpg gcc-${gcc_version}.tar.bz2.sig`
315 % if [ $signature_invalid ]; then echo "Invalid signature" ; exit 1 ; fi
316 % tar -xvjf gcc-${gcc_version}.tar.bz2
317 % cd gcc-${gcc_version}
318 % ./contrib/download_prerequisites
320 % mkdir gcc-${gcc_version}-build
321 % cd gcc-${gcc_version}-build
322 % $PWD/../gcc-${gcc_version}/configure --prefix=$HOME/toolchains --enable-languages=c,c++
326 For more details, check out the excellent `GCC wiki entry`_, where I got most
327 of this information from.
330 https://gcc.gnu.org/wiki/InstallingGCC
332 Once you have a GCC toolchain, configure your build of LLVM to use the new
333 toolchain for your host compiler and C++ standard library. Because the new
334 version of libstdc++ is not on the system library search path, you need to pass
335 extra linker flags so that it can be found at link time (``-L``) and at runtime
336 (``-rpath``). If you are using CMake, this invocation should produce working
339 .. code-block:: console
343 % CC=$HOME/toolchains/bin/gcc CXX=$HOME/toolchains/bin/g++ \
344 cmake .. -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$HOME/toolchains/lib64 -L$HOME/toolchains/lib64"
346 If you fail to set rpath, most LLVM binaries will fail on startup with a message
347 from the loader similar to ``libstdc++.so.6: version `GLIBCXX_3.4.20' not
348 found``. This means you need to tweak the -rpath linker flag.
350 This method will add an absolute path to the rpath of all executables. That's
351 fine for local development. If you want to distribute the binaries you build
352 so that they can run on older systems, copy ``libstdc++.so.6`` into the
353 ``lib/`` directory. All of LLVM's shipping binaries have an rpath pointing at
354 ``$ORIGIN/../lib``, so they will find ``libstdc++.so.6`` there. Non-distributed
355 binaries don't have an rpath set and won't find ``libstdc++.so.6``. Pass
356 ``-DLLVM_LOCAL_RPATH="$HOME/toolchains/lib64"`` to cmake to add an absolute
357 path to ``libstdc++.so.6`` as above. Since these binaries are not distributed,
358 having an absolute local path is fine for them.
360 When you build Clang, you will need to give *it* access to modern C++
361 standard library in order to use it as your new host in part of a bootstrap.
362 There are two easy ways to do this, either build (and install) libc++ along
363 with Clang and then use it with the ``-stdlib=libc++`` compile and link flag,
364 or install Clang into the same prefix (``$HOME/toolchains`` above) as GCC.
365 Clang will look within its own prefix for libstdc++ and use it if found. You
366 can also add an explicit prefix for Clang to look in for a GCC toolchain with
367 the ``--gcc-toolchain=/opt/my/gcc/prefix`` flag, passing it to both compile and
368 link commands when using your just-built-Clang to bootstrap.
370 .. _Getting Started with LLVM:
372 Getting Started with LLVM
373 =========================
375 The remainder of this guide is meant to get you up and running with LLVM and to
376 give you some basic information about the LLVM environment.
378 The later sections of this guide describe the `general layout`_ of the LLVM
379 source tree, a `simple example`_ using the LLVM tool chain, and `links`_ to find
380 more information about LLVM or to get help via e-mail.
382 Terminology and Notation
383 ------------------------
385 Throughout this manual, the following names are used to denote paths specific to
386 the local system and working environment. *These are not environment variables
387 you need to set but just strings used in the rest of this document below*. In
388 any of the examples below, simply replace each of these names with the
389 appropriate pathname on your local system. All these paths are absolute:
393 This is the top level directory of the LLVM source tree.
397 This is the top level directory of the LLVM object tree (i.e. the tree where
398 object files and compiled programs will be placed. It can be the same as
401 Unpacking the LLVM Archives
402 ---------------------------
404 If you have the LLVM distribution, you will need to unpack it before you can
405 begin to compile it. LLVM is distributed as a number of different
406 subprojects. Each one has its own download which is a TAR archive that is
407 compressed with the gzip program.
409 The files are as follows, with *x.y* marking the version number:
413 Source release for the LLVM libraries and tools.
417 Source release for the Clang frontend.
421 Checkout LLVM from Git
422 ----------------------
424 You can also checkout the source code for LLVM from Git.
428 Passing ``--config core.autocrlf=false`` should not be required in
429 the future after we adjust the .gitattribute settings correctly, but
430 is required for Windows users at the time of this writing.
434 .. code-block:: console
436 % git clone https://github.com/llvm/llvm-project.git
440 .. code-block:: console
442 % git clone --config core.autocrlf=false https://github.com/llvm/llvm-project.git
444 This will create an '``llvm-project``' directory in the current directory and
445 fully populate it with all of the source code, test directories, and local
446 copies of documentation files for LLVM and all the related subprojects. Note
447 that unlike the tarballs, which contain each subproject in a separate file, the
448 git repository contains all of the projects together.
450 If you want to get a specific release (as opposed to the most recent revision),
451 you can check out a tag after cloning the repository. E.g., `git checkout
452 llvmorg-6.0.1` inside the ``llvm-project`` directory created by the above
453 command. Use `git tag -l` to list all of them.
458 Please read `Developer Policy <DeveloperPolicy.html#one-off-patches>`_, too.
460 We don't currently accept github pull requests, so you'll need to send patches
461 either via emailing to llvm-commits, or, preferably, via :ref:`Phabricator
462 <phabricator-reviews>`.
464 You'll generally want to make sure your branch has a single commit,
465 corresponding to the review you wish to send, up-to-date with the upstream
466 ``origin/main`` branch, and doesn't contain merges. Once you have that, you
467 can start `a Phabricator review <Phabricator.html>`_ (or use ``git show`` or
468 ``git format-patch`` to output the diff, and attach it to an email message).
470 However, using the "Arcanist" tool is often easier. After `installing arcanist`_, you
471 will also need to apply a fix to your arcanist repo in order to submit a patch:
473 .. code-block:: console
476 % git fetch https://github.com/rashkov/arcanist update_cacerts
477 % git cherry-pick e3659d43d8911e91739f3b0c5935598bceb859aa
479 Once this is all done, you can upload the latest commit using:
481 .. code-block:: console
485 Additionally, before sending a patch for review, please also try to ensure it's
486 formatted properly. We use ``clang-format`` for this, which has git integration
487 through the ``git-clang-format`` script. On some systems, it may already be
488 installed (or be installable via your package manager). If so, you can simply
489 run it -- the following command will format only the code changed in the most
492 .. code-block:: console
494 % git clang-format HEAD~1
496 Note that this modifies the files, but doesn't commit them -- you'll likely want
499 .. code-block:: console
501 % git commit --amend -a
503 in order to update the last commit with all pending changes.
506 If you don't already have ``clang-format`` or ``git clang-format`` installed
507 on your system, the ``clang-format`` binary will be built alongside clang, and
508 the git integration can be run from
509 ``clang/tools/clang-format/git-clang-format``.
514 For developers to commit changes from Git
515 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
517 Once a patch is reviewed, you should rebase it, re-test locally, and commit the
518 changes to LLVM's main branch. This is done using `git push` if you have the
519 required access rights. See `committing a change
520 <Phabricator.html#committing-a-change>`_ for Phabricator based commits or
521 `obtaining commit access <DeveloperPolicy.html#obtaining-commit-access>`_
524 Here is an example workflow using git. This workflow assumes you have an
525 accepted commit on the branch named `branch-with-change`.
527 .. code-block:: console
529 # Go to the branch with your accepted commit.
530 % git checkout branch-with-change
531 # Rebase your change onto the latest commits on Github.
532 % git pull --rebase origin main
533 # Rerun the appropriate tests if needed.
534 % ninja check-$whatever
535 # Check that the list of commits about to be pushed is correct.
536 % git log origin/main...HEAD --oneline
538 % git push origin HEAD:main
540 LLVM currently has a linear-history policy, which means that merge commits are
541 not allowed. The `llvm-project` repo on github is configured to reject pushes
542 that include merges, so the `git rebase` step above is required.
544 Please ask for help if you're having trouble with your particular git workflow.
547 .. _git_pre_push_hook:
552 We include an optional pre-push hook that run some sanity checks on the revisions
553 you are about to push and ask confirmation if you push multiple commits at once.
554 You can set it up (on Unix systems) by running from the repository root:
556 .. code-block:: console
558 % ln -sf ../../llvm/utils/git/pre-push.py .git/hooks/pre-push
563 See `Bisecting LLVM code <GitBisecting.html>`_ for how to use ``git bisect``
569 When reverting changes using git, the default message will say "This reverts
570 commit XYZ". Leave this at the end of the commit message, but add some details
571 before it as to why the commit is being reverted. A brief explanation and/or
572 links to bots that demonstrate the problem are sufficient.
574 Local LLVM Configuration
575 ------------------------
577 Once checked out repository, the LLVM suite source code must be configured
578 before being built. This process uses CMake. Unlinke the normal ``configure``
579 script, CMake generates the build files in whatever format you request as well
580 as various ``*.inc`` files, and ``llvm/include/Config/config.h``.
582 Variables are passed to ``cmake`` on the command line using the format
583 ``-D<variable name>=<value>``. The following variables are some common options
584 used by people developing LLVM.
586 +-------------------------+----------------------------------------------------+
587 | Variable | Purpose |
588 +=========================+====================================================+
589 | CMAKE_C_COMPILER | Tells ``cmake`` which C compiler to use. By |
590 | | default, this will be /usr/bin/cc. |
591 +-------------------------+----------------------------------------------------+
592 | CMAKE_CXX_COMPILER | Tells ``cmake`` which C++ compiler to use. By |
593 | | default, this will be /usr/bin/c++. |
594 +-------------------------+----------------------------------------------------+
595 | CMAKE_BUILD_TYPE | Tells ``cmake`` what type of build you are trying |
596 | | to generate files for. Valid options are Debug, |
597 | | Release, RelWithDebInfo, and MinSizeRel. Default |
599 +-------------------------+----------------------------------------------------+
600 | CMAKE_INSTALL_PREFIX | Specifies the install directory to target when |
601 | | running the install action of the build files. |
602 +-------------------------+----------------------------------------------------+
603 | PYTHON_EXECUTABLE | Forces CMake to use a specific Python version by |
604 | | passing a path to a Python interpreter. By default |
605 | | the Python version of the interpreter in your PATH |
607 +-------------------------+----------------------------------------------------+
608 | LLVM_TARGETS_TO_BUILD | A semicolon delimited list controlling which |
609 | | targets will be built and linked into llvm. |
610 | | The default list is defined as |
611 | | ``LLVM_ALL_TARGETS``, and can be set to include |
612 | | out-of-tree targets. The default value includes: |
613 | | ``AArch64, AMDGPU, ARM, AVR, BPF, Hexagon, Lanai, |
614 | | Mips, MSP430, NVPTX, PowerPC, RISCV, Sparc, |
615 | | SystemZ, WebAssembly, X86, XCore``. |
617 +-------------------------+----------------------------------------------------+
618 | LLVM_ENABLE_DOXYGEN | Build doxygen-based documentation from the source |
619 | | code This is disabled by default because it is |
620 | | slow and generates a lot of output. |
621 +-------------------------+----------------------------------------------------+
622 | LLVM_ENABLE_PROJECTS | A semicolon-delimited list selecting which of the |
623 | | other LLVM subprojects to additionally build. (Only|
624 | | effective when using a side-by-side project layout |
625 | | e.g. via git). The default list is empty. Can |
626 | | include: clang, clang-tools-extra, |
627 | | cross-project-tests, flang, libc, libclc, lld, |
628 | | lldb, mlir, openmp, polly, or pstl. |
629 +-------------------------+----------------------------------------------------+
630 | LLVM_ENABLE_RUNTIMES | A semicolon-delimited list selecting which of the |
631 | | runtimes to build. (Only effective when using the |
632 | | full monorepo layout). The default list is empty. |
633 | | Can include: compiler-rt, libc, libcxx, libcxxabi, |
634 | | libunwind, or openmp. |
635 +-------------------------+----------------------------------------------------+
636 | LLVM_ENABLE_SPHINX | Build sphinx-based documentation from the source |
637 | | code. This is disabled by default because it is |
638 | | slow and generates a lot of output. Sphinx version |
639 | | 1.5 or later recommended. |
640 +-------------------------+----------------------------------------------------+
641 | LLVM_BUILD_LLVM_DYLIB | Generate libLLVM.so. This library contains a |
642 | | default set of LLVM components that can be |
643 | | overridden with ``LLVM_DYLIB_COMPONENTS``. The |
644 | | default contains most of LLVM and is defined in |
645 | | ``tools/llvm-shlib/CMakelists.txt``. This option is|
646 | | not available on Windows. |
647 +-------------------------+----------------------------------------------------+
648 | LLVM_OPTIMIZED_TABLEGEN | Builds a release tablegen that gets used during |
649 | | the LLVM build. This can dramatically speed up |
651 +-------------------------+----------------------------------------------------+
653 To configure LLVM, follow these steps:
655 #. Change directory into the object root directory:
657 .. code-block:: console
661 #. Run the ``cmake``:
663 .. code-block:: console
665 % cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/install/path
666 [other options] SRC_ROOT
668 Compiling the LLVM Suite Source Code
669 ------------------------------------
671 Unlike with autotools, with CMake your build type is defined at configuration.
672 If you want to change your build type, you can re-run cmake with the following
675 .. code-block:: console
677 % cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=type SRC_ROOT
679 Between runs, CMake preserves the values set for all options. CMake has the
680 following build types defined:
684 These builds are the default. The build system will compile the tools and
685 libraries unoptimized, with debugging information, and asserts enabled.
689 For these builds, the build system will compile the tools and libraries
690 with optimizations enabled and not generate debug info. CMakes default
691 optimization level is -O3. This can be configured by setting the
692 ``CMAKE_CXX_FLAGS_RELEASE`` variable on the CMake command line.
696 These builds are useful when debugging. They generate optimized binaries with
697 debug information. CMakes default optimization level is -O2. This can be
698 configured by setting the ``CMAKE_CXX_FLAGS_RELWITHDEBINFO`` variable on the
701 Once you have LLVM configured, you can build it by entering the *OBJ_ROOT*
702 directory and issuing the following command:
704 .. code-block:: console
708 If the build fails, please `check here`_ to see if you are using a version of
709 GCC that is known not to compile LLVM.
711 If you have multiple processors in your machine, you may wish to use some of the
712 parallel build options provided by GNU Make. For example, you could use the
715 .. code-block:: console
719 There are several special targets which are useful when working with the LLVM
724 Removes all files generated by the build. This includes object files,
725 generated C/C++ files, libraries, and executables.
729 Installs LLVM header files, libraries, tools, and documentation in a hierarchy
730 under ``$PREFIX``, specified with ``CMAKE_INSTALL_PREFIX``, which
731 defaults to ``/usr/local``.
733 ``make docs-llvm-html``
735 If configured with ``-DLLVM_ENABLE_SPHINX=On``, this will generate a directory
736 at ``OBJ_ROOT/docs/html`` which contains the HTML formatted documentation.
741 It is possible to cross-compile LLVM itself. That is, you can create LLVM
742 executables and libraries to be hosted on a platform different from the platform
743 where they are built (a Canadian Cross build). To generate build files for
744 cross-compiling CMake provides a variable ``CMAKE_TOOLCHAIN_FILE`` which can
745 define compiler flags and variables used during the CMake test operations.
747 The result of such a build is executables that are not runnable on the build
748 host but can be executed on the target. As an example the following CMake
749 invocation can generate build files targeting iOS. This will work on macOS
750 with the latest Xcode:
752 .. code-block:: console
754 % cmake -G "Ninja" -DCMAKE_OSX_ARCHITECTURES="armv7;armv7s;arm64"
755 -DCMAKE_TOOLCHAIN_FILE=<PATH_TO_LLVM>/cmake/platforms/iOS.cmake
756 -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_RUNTIME=Off -DLLVM_INCLUDE_TESTS=Off
757 -DLLVM_INCLUDE_EXAMPLES=Off -DLLVM_ENABLE_BACKTRACES=Off [options]
760 Note: There are some additional flags that need to be passed when building for
761 iOS due to limitations in the iOS SDK.
763 Check :doc:`HowToCrossCompileLLVM` and `Clang docs on how to cross-compile in general
764 <https://clang.llvm.org/docs/CrossCompilation.html>`_ for more information
765 about cross-compiling.
767 The Location of LLVM Object Files
768 ---------------------------------
770 The LLVM build system is capable of sharing a single LLVM source tree among
771 several LLVM builds. Hence, it is possible to build LLVM for several different
772 platforms or configurations using the same source tree.
774 * Change directory to where the LLVM object files should live:
776 .. code-block:: console
782 .. code-block:: console
784 % cmake -G "Unix Makefiles" SRC_ROOT
786 The LLVM build will create a structure underneath *OBJ_ROOT* that matches the
787 LLVM source tree. At each level where source files are present in the source
788 tree there will be a corresponding ``CMakeFiles`` directory in the *OBJ_ROOT*.
789 Underneath that directory there is another directory with a name ending in
790 ``.dir`` under which you'll find object files for each source.
794 .. code-block:: console
797 % find lib/Support/ -name APFloat*
798 lib/Support/CMakeFiles/LLVMSupport.dir/APFloat.cpp.o
800 Optional Configuration Items
801 ----------------------------
803 If you're running on a Linux system that supports the `binfmt_misc
804 <http://en.wikipedia.org/wiki/binfmt_misc>`_
805 module, and you have root access on the system, you can set your system up to
806 execute LLVM bitcode files directly. To do this, use commands like this (the
807 first command may not be required if you are already using the module):
809 .. code-block:: console
811 % mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
812 % echo ':llvm:M::BC::/path/to/lli:' > /proc/sys/fs/binfmt_misc/register
813 % chmod u+x hello.bc (if needed)
816 This allows you to execute LLVM bitcode files directly. On Debian, you can also
817 use this command instead of the 'echo' command above:
819 .. code-block:: console
821 % sudo update-binfmts --install llvm /path/to/lli --magic 'BC'
829 One useful source of information about the LLVM source base is the LLVM `doxygen
830 <http://www.doxygen.org/>`_ documentation available at
831 `<https://llvm.org/doxygen/>`_. The following is a brief introduction to code
836 Generates system build files.
838 ``llvm/cmake/modules``
839 Build configuration for llvm user defined options. Checks compiler version and
842 ``llvm/cmake/platforms``
843 Toolchain configuration for Android NDK, iOS systems and non-Windows hosts to
849 - Some simple examples showing how to use LLVM as a compiler for a custom
850 language - including lowering, optimization, and code generation.
852 - Kaleidoscope Tutorial: Kaleidoscope language tutorial run through the
853 implementation of a nice little compiler for a non-trivial language
854 including a hand-written lexer, parser, AST, as well as code generation
855 support using LLVM- both static (ahead of time) and various approaches to
856 Just In Time (JIT) compilation.
857 `Kaleidoscope Tutorial for complete beginner
858 <https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html>`_.
860 - BuildingAJIT: Examples of the `BuildingAJIT tutorial
861 <https://llvm.org/docs/tutorial/BuildingAJIT1.html>`_ that shows how LLVM’s
862 ORC JIT APIs interact with other parts of LLVM. It also, teaches how to
863 recombine them to build a custom JIT that is suited to your use-case.
868 Public header files exported from the LLVM library. The three main subdirectories:
870 ``llvm/include/llvm``
872 All LLVM-specific header files, and subdirectories for different portions of
873 LLVM: ``Analysis``, ``CodeGen``, ``Target``, ``Transforms``, etc...
875 ``llvm/include/llvm/Support``
877 Generic support libraries provided with LLVM but not necessarily specific to
878 LLVM. For example, some C++ STL utilities and a Command Line option processing
879 library store header files here.
881 ``llvm/include/llvm/Config``
883 Header files configured by ``cmake``. They wrap "standard" UNIX and
884 C header files. Source code can include these header files which
885 automatically take care of the conditional #includes that ``cmake``
891 Most source files are here. By putting code in libraries, LLVM makes it easy to
892 share code among the `tools`_.
896 Core LLVM source files that implement core classes like Instruction and
899 ``llvm/lib/AsmParser/``
901 Source code for the LLVM assembly language parser library.
903 ``llvm/lib/Bitcode/``
905 Code for reading and writing bitcode.
907 ``llvm/lib/Analysis/``
909 A variety of program analyses, such as Call Graphs, Induction Variables,
910 Natural Loop Identification, etc.
912 ``llvm/lib/Transforms/``
914 IR-to-IR program transformations, such as Aggressive Dead Code Elimination,
915 Sparse Conditional Constant Propagation, Inlining, Loop Invariant Code Motion,
916 Dead Global Elimination, and many others.
920 Files describing target architectures for code generation. For example,
921 ``llvm/lib/Target/X86`` holds the X86 machine description.
923 ``llvm/lib/CodeGen/``
925 The major parts of the code generator: Instruction Selector, Instruction
926 Scheduling, and Register Allocation.
930 The libraries represent and process code at machine code level. Handles
931 assembly and object-file emission.
933 ``llvm/lib/ExecutionEngine/``
935 Libraries for directly executing bitcode at runtime in interpreted and
936 JIT-compiled scenarios.
938 ``llvm/lib/Support/``
940 Source code that corresponding to the header files in ``llvm/include/ADT/``
941 and ``llvm/include/Support/``.
944 ----------------------
946 Contains bindings for the LLVM compiler infrastructure to allow
947 programs written in languages other than C or C++ to take advantage of the LLVM
949 LLVM project provides language bindings for Go, OCaml and Python.
954 Projects not strictly part of LLVM but shipped with LLVM. This is also the
955 directory for creating your own LLVM-based projects which leverage the LLVM
961 Feature and regression tests and other sanity checks on LLVM infrastructure. These
962 are intended to run quickly and cover a lot of territory without being exhaustive.
967 A comprehensive correctness, performance, and benchmarking test suite
968 for LLVM. This comes in a ``separate git repository
969 <https://github.com/llvm/llvm-test-suite>``, because it contains a
970 large amount of third-party code under a variety of licenses. For
971 details see the :doc:`Testing Guide <TestingGuide>` document.
978 Executables built out of the libraries
979 above, which form the main part of the user interface. You can always get help
980 for a tool by typing ``tool_name -help``. The following is a brief introduction
981 to the most important tools. More detailed information is in
982 the `Command Guide <CommandGuide/index.html>`_.
986 ``bugpoint`` is used to debug optimization passes or code generation backends
987 by narrowing down the given test case to the minimum number of passes and/or
988 instructions that still cause a problem, whether it is a crash or
989 miscompilation. See `<HowToSubmitABug.html>`_ for more information on using
994 The archiver produces an archive containing the given LLVM bitcode files,
995 optionally with an index for faster lookup.
999 The assembler transforms the human readable LLVM assembly to LLVM bitcode.
1003 The disassembler transforms the LLVM bitcode to human readable LLVM assembly.
1007 ``llvm-link``, not surprisingly, links multiple LLVM modules into a single
1012 ``lli`` is the LLVM interpreter, which can directly execute LLVM bitcode
1013 (although very slowly...). For architectures that support it (currently x86,
1014 Sparc, and PowerPC), by default, ``lli`` will function as a Just-In-Time
1015 compiler (if the functionality was compiled in), and will execute the code
1016 *much* faster than the interpreter.
1020 ``llc`` is the LLVM backend compiler, which translates LLVM bitcode to a
1021 native code assembly file.
1025 ``opt`` reads LLVM bitcode, applies a series of LLVM to LLVM transformations
1026 (which are specified on the command line), and outputs the resultant
1027 bitcode. '``opt -help``' is a good way to get a list of the
1028 program transformations available in LLVM.
1030 ``opt`` can also run a specific analysis on an input LLVM bitcode
1031 file and print the results. Primarily useful for debugging
1032 analyses, or familiarizing yourself with what an analysis does.
1037 Utilities for working with LLVM source code; some are part of the build process
1038 because they are code generators for parts of the infrastructure.
1043 ``codegen-diff`` finds differences between code that LLC
1044 generates and code that LLI generates. This is useful if you are
1045 debugging one of them, assuming that the other generates correct output. For
1046 the full user manual, run ```perldoc codegen-diff'``.
1050 Emacs and XEmacs syntax highlighting for LLVM assembly files and TableGen
1051 description files. See the ``README`` for information on using them.
1055 Finds and outputs all non-generated source files,
1056 useful if one wishes to do a lot of development across directories
1057 and does not want to find each file. One way to use it is to run,
1058 for example: ``xemacs `utils/getsources.sh``` from the top of the LLVM source
1063 Performs an ``egrep -H -n`` on each source file in LLVM and
1064 passes to it a regular expression provided on ``llvmgrep``'s command
1065 line. This is an efficient way of searching the source base for a
1066 particular regular expression.
1070 Contains the tool used to generate register
1071 descriptions, instruction set descriptions, and even assemblers from common
1072 TableGen description files.
1076 vim syntax-highlighting for LLVM assembly files
1077 and TableGen description files. See the ``README`` for how to use them.
1081 An Example Using the LLVM Tool Chain
1082 ====================================
1084 This section gives an example of using LLVM with the Clang front end.
1089 #. First, create a simple C file, name it 'hello.c':
1096 printf("hello world\n");
1100 #. Next, compile the C file into a native executable:
1102 .. code-block:: console
1104 % clang hello.c -o hello
1108 Clang works just like GCC by default. The standard -S and -c arguments
1109 work as usual (producing a native .s or .o file, respectively).
1111 #. Next, compile the C file into an LLVM bitcode file:
1113 .. code-block:: console
1115 % clang -O3 -emit-llvm hello.c -c -o hello.bc
1117 The -emit-llvm option can be used with the -S or -c options to emit an LLVM
1118 ``.ll`` or ``.bc`` file (respectively) for the code. This allows you to use
1119 the `standard LLVM tools <CommandGuide/index.html>`_ on the bitcode file.
1121 #. Run the program in both forms. To run the program, use:
1123 .. code-block:: console
1129 .. code-block:: console
1133 The second examples shows how to invoke the LLVM JIT, :doc:`lli
1134 <CommandGuide/lli>`.
1136 #. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code:
1138 .. code-block:: console
1140 % llvm-dis < hello.bc | less
1142 #. Compile the program to native assembly using the LLC code generator:
1144 .. code-block:: console
1146 % llc hello.bc -o hello.s
1148 #. Assemble the native assembly language file into a program:
1150 .. code-block:: console
1152 % /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.native # On Solaris
1154 % gcc hello.s -o hello.native # On others
1156 #. Execute the native code program:
1158 .. code-block:: console
1162 Note that using clang to compile directly to native code (i.e. when the
1163 ``-emit-llvm`` option is not present) does steps 6/7/8 for you.
1168 If you are having problems building or using LLVM, or if you have any other
1169 general questions about LLVM, please consult the `Frequently Asked
1170 Questions <FAQ.html>`_ page.
1172 If you are having problems with limited memory and build time, please try
1173 building with ninja instead of make. Please consider configuring the
1174 following options with cmake:
1177 Setting this option will allow you to build with ninja instead of make.
1178 Building with ninja significantly improves your build time, especially with
1179 incremental builds, and improves your memory usage.
1182 Setting this option to lld will significantly reduce linking time for LLVM
1183 executables on ELF-based platforms, such as Linux. If you are building LLVM
1184 for the first time and lld is not available to you as a binary package, then
1185 you may want to use the gold linker as a faster alternative to GNU ld.
1187 * -DCMAKE_BUILD_TYPE
1189 - Debug --- This is the default build type. This disables optimizations while
1190 compiling LLVM and enables debug info. On ELF-based platforms (e.g. Linux)
1191 linking with debug info may consume a large amount of memory.
1193 - Release --- Turns on optimizations and disables debug info. Combining the
1194 Release build type with -DLLVM_ENABLE_ASSERTIONS=ON may be a good trade-off
1195 between speed and debugability during development, particularly for running
1198 * -DLLVM_ENABLE_ASSERTIONS
1199 This option defaults to ON for Debug builds and defaults to OFF for Release
1200 builds. As mentioned in the previous option, using the Release build type and
1201 enabling assertions may be a good alternative to using the Debug build type.
1203 * -DLLVM_PARALLEL_LINK_JOBS
1204 Set this equal to number of jobs you wish to run simultaneously. This is
1205 similar to the -j option used with make, but only for link jobs. This option
1206 can only be used with ninja. You may wish to use a very low number of jobs,
1207 as this will greatly reduce the amount of memory used during the build
1208 process. If you have limited memory, you may wish to set this to 1.
1210 * -DLLVM_TARGETS_TO_BUILD
1211 Set this equal to the target you wish to build. You may wish to set this to
1212 X86; however, you will find a full list of targets within the
1213 llvm-project/llvm/lib/Target directory.
1215 * -DLLVM_OPTIMIZED_TABLEGEN
1216 Set this to ON to generate a fully optimized tablegen during your build. This
1217 will significantly improve your build time. This is only useful if you are
1218 using the Debug build type.
1220 * -DLLVM_ENABLE_PROJECTS
1221 Set this equal to the projects you wish to compile (e.g. clang, lld, etc.) If
1222 compiling more than one project, separate the items with a semicolon. Should
1223 you run into issues with the semicolon, try surrounding it with single quotes.
1225 * -DLLVM_ENABLE_RUNTIMES
1226 Set this equal to the runtimes you wish to compile (e.g. libcxx, libcxxabi, etc.)
1227 If compiling more than one runtime, separate the items with a semicolon. Should
1228 you run into issues with the semicolon, try surrounding it with single quotes.
1230 * -DCLANG_ENABLE_STATIC_ANALYZER
1231 Set this option to OFF if you do not require the clang static analyzer. This
1232 should improve your build time slightly.
1234 * -DLLVM_USE_SPLIT_DWARF
1235 Consider setting this to ON if you require a debug build, as this will ease
1236 memory pressure on the linker. This will make linking much faster, as the
1237 binaries will not contain any of the debug information; however, this will
1238 generate the debug information in the form of a DWARF object file (with the
1239 extension .dwo). This only applies to host platforms using ELF, such as Linux.
1246 This document is just an **introduction** on how to use LLVM to do some simple
1247 things... there are many more interesting and complicated things that you can do
1248 that aren't documented here (but we'll gladly accept a patch if you want to
1249 write something up!). For more information about LLVM, check out:
1251 * `LLVM Homepage <https://llvm.org/>`_
1252 * `LLVM Doxygen Tree <https://llvm.org/doxygen/>`_
1253 * `Starting a Project that Uses LLVM <https://llvm.org/docs/Projects.html>`_
1255 .. _installing arcanist: https://secure.phabricator.com/book/phabricator/article/arcanist_quick_start/