1==================================== 2Getting Started with the LLVM System 3==================================== 4 5.. contents:: 6 :local: 7 8Overview 9======== 10 11Welcome to the LLVM project! 12 13The LLVM project has multiple components. The core of the project is 14itself called "LLVM". This contains all of the tools, libraries, and header 15files needed to process intermediate representations and converts it into 16object files. Tools include an assembler, disassembler, bitcode analyzer, and 17bitcode optimizer. It also contains basic regression tests. 18 19C-like languages use the `Clang <https://clang.llvm.org/>`_ front end. This 20component compiles C, C++, Objective C, and Objective C++ code into LLVM bitcode 21-- and from there into object files, using LLVM. 22 23Other components include: 24the `libc++ C++ standard library <https://libcxx.llvm.org>`_, 25the `LLD linker <https://lld.llvm.org>`_, and more. 26 27Getting the Source Code and Building LLVM 28========================================= 29 30The LLVM Getting Started documentation may be out of date. The `Clang 31Getting Started <https://clang.llvm.org/get_started.html>`_ page might have more 32accurate information. 33 34This is an example workflow and configuration to get and build the LLVM source: 35 36#. Checkout LLVM (including related subprojects like Clang): 37 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 42#. Configure and build LLVM and Clang: 43 44 * ``cd llvm-project`` 45 * ``mkdir build`` 46 * ``cd build`` 47 * ``cmake -G <generator> [options] ../llvm`` 48 49 Some common build system generators are: 50 51 * ``Ninja`` --- for generating `Ninja <https://ninja-build.org>`_ 52 build files. Most llvm developers use Ninja. 53 * ``Unix Makefiles`` --- for generating make-compatible parallel makefiles. 54 * ``Visual Studio`` --- for generating Visual Studio projects and 55 solutions. 56 * ``Xcode`` --- for generating Xcode projects. 57 58 Some Common options: 59 60 * ``-DLLVM_ENABLE_PROJECTS='...'`` --- semicolon-separated list of the LLVM 61 subprojects you'd like to additionally build. Can include any of: clang, 62 clang-tools-extra, libcxx, libcxxabi, libunwind, lldb, compiler-rt, lld, 63 polly, or debuginfo-tests. 64 65 For example, to build LLVM, Clang, libcxx, and libcxxabi, use 66 ``-DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi"``. 67 68 * ``-DCMAKE_INSTALL_PREFIX=directory`` --- Specify for *directory* the full 69 pathname of where you want the LLVM tools and libraries to be installed 70 (default ``/usr/local``). 71 72 * ``-DCMAKE_BUILD_TYPE=type`` --- Valid options for *type* are Debug, 73 Release, RelWithDebInfo, and MinSizeRel. Default is Debug. 74 75 * ``-DLLVM_ENABLE_ASSERTIONS=On`` --- Compile with assertion checks enabled 76 (default is Yes for Debug builds, No for all other build types). 77 78 * ``cmake --build . [--target <target>]`` or the build system specified 79 above directly. 80 81 * The default target (i.e. ``cmake --build .`` or ``make``) will build all of 82 LLVM. 83 84 * The ``check-all`` target (i.e. ``ninja check-all``) will run the 85 regression tests to ensure everything is in working order. 86 87 * CMake will generate build targets for each tool and library, and most 88 LLVM sub-projects generate their own ``check-<project>`` target. 89 90 * Running a serial build will be **slow**. To improve speed, try running a 91 parallel build. That's done by default in Ninja; for ``make``, use the 92 option ``-j NN``, where ``NN`` is the number of parallel jobs, e.g. the 93 number of available CPUs. 94 95 * For more information see `CMake <CMake.html>`__ 96 97 * If you get an "internal compiler error (ICE)" or test failures, see 98 `below`_. 99 100Consult the `Getting Started with LLVM`_ section for detailed information on 101configuring and compiling LLVM. Go to `Directory Layout`_ to learn about the 102layout of the source code tree. 103 104Requirements 105============ 106 107Before you begin to use the LLVM system, review the requirements given below. 108This may save you some trouble by knowing ahead of time what hardware and 109software you will need. 110 111Hardware 112-------- 113 114LLVM is known to work on the following host platforms: 115 116================== ===================== ============= 117OS Arch Compilers 118================== ===================== ============= 119Linux x86\ :sup:`1` GCC, Clang 120Linux amd64 GCC, Clang 121Linux ARM GCC, Clang 122Linux Mips GCC, Clang 123Linux PowerPC GCC, Clang 124Solaris V9 (Ultrasparc) GCC 125FreeBSD x86\ :sup:`1` GCC, Clang 126FreeBSD amd64 GCC, Clang 127NetBSD x86\ :sup:`1` GCC, Clang 128NetBSD amd64 GCC, Clang 129macOS\ :sup:`2` PowerPC GCC 130macOS x86 GCC, Clang 131Cygwin/Win32 x86\ :sup:`1, 3` GCC 132Windows x86\ :sup:`1` Visual Studio 133Windows x64 x86-64 Visual Studio 134================== ===================== ============= 135 136.. note:: 137 138 #. Code generation supported for Pentium processors and up 139 #. Code generation supported for 32-bit ABI only 140 #. To use LLVM modules on Win32-based system, you may configure LLVM 141 with ``-DBUILD_SHARED_LIBS=On``. 142 143Note that Debug builds require a lot of time and disk space. An LLVM-only build 144will need about 1-3 GB of space. A full build of LLVM and Clang will need around 14515-20 GB of disk space. The exact space requirements will vary by system. (It 146is so large because of all the debugging information and the fact that the 147libraries are statically linked into multiple tools). 148 149If you are space-constrained, you can build only selected tools or only 150selected targets. The Release build requires considerably less space. 151 152The LLVM suite *may* compile on other platforms, but it is not guaranteed to do 153so. If compilation is successful, the LLVM utilities should be able to 154assemble, disassemble, analyze, and optimize LLVM bitcode. Code generation 155should work as well, although the generated native code may not work on your 156platform. 157 158Software 159-------- 160 161Compiling LLVM requires that you have several software packages installed. The 162table below lists those required packages. The Package column is the usual name 163for the software package that LLVM depends on. The Version column provides 164"known to work" versions of the package. The Notes column describes how LLVM 165uses the package and provides other details. 166 167=========================================================== ============ ========================================== 168Package Version Notes 169=========================================================== ============ ========================================== 170`CMake <http://cmake.org/>`__ >=3.4.3 Makefile/workspace generator 171`GCC <http://gcc.gnu.org/>`_ >=5.1.0 C/C++ compiler\ :sup:`1` 172`python <http://www.python.org/>`_ >=2.7 Automated test suite\ :sup:`2` 173`zlib <http://zlib.net>`_ >=1.2.3.4 Compression library\ :sup:`3` 174`GNU Make <http://savannah.gnu.org/projects/make>`_ 3.79, 3.79.1 Makefile/build processor\ :sup:`4` 175=========================================================== ============ ========================================== 176 177.. note:: 178 179 #. Only the C and C++ languages are needed so there's no need to build the 180 other languages for LLVM's purposes. See `below` for specific version 181 info. 182 #. Only needed if you want to run the automated test suite in the 183 ``llvm/test`` directory. 184 #. Optional, adds compression / uncompression capabilities to selected LLVM 185 tools. 186 #. Optional, you can use any other build tool supported by CMake. 187 188Additionally, your compilation host is expected to have the usual plethora of 189Unix utilities. Specifically: 190 191* **ar** --- archive library builder 192* **bzip2** --- bzip2 command for distribution generation 193* **bunzip2** --- bunzip2 command for distribution checking 194* **chmod** --- change permissions on a file 195* **cat** --- output concatenation utility 196* **cp** --- copy files 197* **date** --- print the current date/time 198* **echo** --- print to standard output 199* **egrep** --- extended regular expression search utility 200* **find** --- find files/dirs in a file system 201* **grep** --- regular expression search utility 202* **gzip** --- gzip command for distribution generation 203* **gunzip** --- gunzip command for distribution checking 204* **install** --- install directories/files 205* **mkdir** --- create a directory 206* **mv** --- move (rename) files 207* **ranlib** --- symbol table builder for archive libraries 208* **rm** --- remove (delete) files and directories 209* **sed** --- stream editor for transforming output 210* **sh** --- Bourne shell for make build scripts 211* **tar** --- tape archive for distribution generation 212* **test** --- test things in file system 213* **unzip** --- unzip command for distribution checking 214* **zip** --- zip command for distribution generation 215 216.. _below: 217.. _check here: 218 219Host C++ Toolchain, both Compiler and Standard Library 220------------------------------------------------------ 221 222LLVM is very demanding of the host C++ compiler, and as such tends to expose 223bugs in the compiler. We also attempt to follow improvements and developments in 224the C++ language and library reasonably closely. As such, we require a modern 225host C++ toolchain, both compiler and standard library, in order to build LLVM. 226 227LLVM is written using the subset of C++ documented in :doc:`coding 228standards<CodingStandards>`. To enforce this language version, we check the most 229popular host toolchains for specific minimum versions in our build systems: 230 231* Clang 3.5 232* Apple Clang 6.0 233* GCC 5.1 234* Visual Studio 2017 235 236Anything older than these toolchains *may* work, but will require forcing the 237build system with a special option and is not really a supported host platform. 238Also note that older versions of these compilers have often crashed or 239miscompiled LLVM. 240 241For less widely used host toolchains such as ICC or xlC, be aware that a very 242recent version may be required to support all of the C++ features used in LLVM. 243 244We track certain versions of software that are *known* to fail when used as 245part of the host toolchain. These even include linkers at times. 246 247**GNU ld 2.16.X**. Some 2.16.X versions of the ld linker will produce very long 248warning messages complaining that some "``.gnu.linkonce.t.*``" symbol was 249defined in a discarded section. You can safely ignore these messages as they are 250erroneous and the linkage is correct. These messages disappear using ld 2.17. 251 252**GNU binutils 2.17**: Binutils 2.17 contains `a bug 253<http://sourceware.org/bugzilla/show_bug.cgi?id=3111>`__ which causes huge link 254times (minutes instead of seconds) when building LLVM. We recommend upgrading 255to a newer version (2.17.50.0.4 or later). 256 257**GNU Binutils 2.19.1 Gold**: This version of Gold contained `a bug 258<http://sourceware.org/bugzilla/show_bug.cgi?id=9836>`__ which causes 259intermittent failures when building LLVM with position independent code. The 260symptom is an error about cyclic dependencies. We recommend upgrading to a 261newer version of Gold. 262 263Getting a Modern Host C++ Toolchain 264^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 265 266This section mostly applies to Linux and older BSDs. On macOS, you should 267have a sufficiently modern Xcode, or you will likely need to upgrade until you 268do. Windows does not have a "system compiler", so you must install either Visual 269Studio 2017 or a recent version of mingw64. FreeBSD 10.0 and newer have a modern 270Clang as the system compiler. 271 272However, some Linux distributions and some other or older BSDs sometimes have 273extremely old versions of GCC. These steps attempt to help you upgrade you 274compiler even on such a system. However, if at all possible, we encourage you 275to use a recent version of a distribution with a modern system compiler that 276meets these requirements. Note that it is tempting to install a prior 277version of Clang and libc++ to be the host compiler, however libc++ was not 278well tested or set up to build on Linux until relatively recently. As 279a consequence, this guide suggests just using libstdc++ and a modern GCC as the 280initial host in a bootstrap, and then using Clang (and potentially libc++). 281 282The first step is to get a recent GCC toolchain installed. The most common 283distribution on which users have struggled with the version requirements is 284Ubuntu Precise, 12.04 LTS. For this distribution, one easy option is to install 285the `toolchain testing PPA`_ and use it to install a modern GCC. There is 286a really nice discussions of this on the `ask ubuntu stack exchange`_ and a 287`github gist`_ with updated commands. However, not all users can use PPAs and 288there are many other distributions, so it may be necessary (or just useful, if 289you're here you *are* doing compiler development after all) to build and install 290GCC from source. It is also quite easy to do these days. 291 292.. _toolchain testing PPA: 293 https://launchpad.net/~ubuntu-toolchain-r/+archive/test 294.. _ask ubuntu stack exchange: 295 https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu/581497#58149 296.. _github gist: 297 https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91 298 299Easy steps for installing GCC 5.1.0: 300 301.. code-block:: console 302 303 % gcc_version=5.1.0 304 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2 305 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2.sig 306 % wget https://ftp.gnu.org/gnu/gnu-keyring.gpg 307 % signature_invalid=`gpg --verify --no-default-keyring --keyring ./gnu-keyring.gpg gcc-${gcc_version}.tar.bz2.sig` 308 % if [ $signature_invalid ]; then echo "Invalid signature" ; exit 1 ; fi 309 % tar -xvjf gcc-${gcc_version}.tar.bz2 310 % cd gcc-${gcc_version} 311 % ./contrib/download_prerequisites 312 % cd .. 313 % mkdir gcc-${gcc_version}-build 314 % cd gcc-${gcc_version}-build 315 % $PWD/../gcc-${gcc_version}/configure --prefix=$HOME/toolchains --enable-languages=c,c++ 316 % make -j$(nproc) 317 % make install 318 319For more details, check out the excellent `GCC wiki entry`_, where I got most 320of this information from. 321 322.. _GCC wiki entry: 323 https://gcc.gnu.org/wiki/InstallingGCC 324 325Once you have a GCC toolchain, configure your build of LLVM to use the new 326toolchain for your host compiler and C++ standard library. Because the new 327version of libstdc++ is not on the system library search path, you need to pass 328extra linker flags so that it can be found at link time (``-L``) and at runtime 329(``-rpath``). If you are using CMake, this invocation should produce working 330binaries: 331 332.. code-block:: console 333 334 % mkdir build 335 % cd build 336 % CC=$HOME/toolchains/bin/gcc CXX=$HOME/toolchains/bin/g++ \ 337 cmake .. -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$HOME/toolchains/lib64 -L$HOME/toolchains/lib64" 338 339If you fail to set rpath, most LLVM binaries will fail on startup with a message 340from the loader similar to ``libstdc++.so.6: version `GLIBCXX_3.4.20' not 341found``. This means you need to tweak the -rpath linker flag. 342 343When you build Clang, you will need to give *it* access to modern C++ 344standard library in order to use it as your new host in part of a bootstrap. 345There are two easy ways to do this, either build (and install) libc++ along 346with Clang and then use it with the ``-stdlib=libc++`` compile and link flag, 347or install Clang into the same prefix (``$HOME/toolchains`` above) as GCC. 348Clang will look within its own prefix for libstdc++ and use it if found. You 349can also add an explicit prefix for Clang to look in for a GCC toolchain with 350the ``--gcc-toolchain=/opt/my/gcc/prefix`` flag, passing it to both compile and 351link commands when using your just-built-Clang to bootstrap. 352 353.. _Getting Started with LLVM: 354 355Getting Started with LLVM 356========================= 357 358The remainder of this guide is meant to get you up and running with LLVM and to 359give you some basic information about the LLVM environment. 360 361The later sections of this guide describe the `general layout`_ of the LLVM 362source tree, a `simple example`_ using the LLVM tool chain, and `links`_ to find 363more information about LLVM or to get help via e-mail. 364 365Terminology and Notation 366------------------------ 367 368Throughout this manual, the following names are used to denote paths specific to 369the local system and working environment. *These are not environment variables 370you need to set but just strings used in the rest of this document below*. In 371any of the examples below, simply replace each of these names with the 372appropriate pathname on your local system. All these paths are absolute: 373 374``SRC_ROOT`` 375 376 This is the top level directory of the LLVM source tree. 377 378``OBJ_ROOT`` 379 380 This is the top level directory of the LLVM object tree (i.e. the tree where 381 object files and compiled programs will be placed. It can be the same as 382 SRC_ROOT). 383 384Unpacking the LLVM Archives 385--------------------------- 386 387If you have the LLVM distribution, you will need to unpack it before you can 388begin to compile it. LLVM is distributed as a number of different 389subprojects. Each one has its own download which is a TAR archive that is 390compressed with the gzip program. 391 392The files are as follows, with *x.y* marking the version number: 393 394``llvm-x.y.tar.gz`` 395 396 Source release for the LLVM libraries and tools. 397 398``cfe-x.y.tar.gz`` 399 400 Source release for the Clang frontend. 401 402.. _checkout: 403 404Checkout LLVM from Git 405---------------------- 406 407You can also checkout the source code for LLVM from Git. 408 409.. note:: 410 411 Passing ``--config core.autocrlf=false`` should not be required in 412 the future after we adjust the .gitattribute settings correctly, but 413 is required for Windows users at the time of this writing. 414 415Simply run: 416 417.. code-block:: console 418 419 % git clone https://github.com/llvm/llvm-project.git 420 421or on Windows, 422 423.. code-block:: console 424 425 % git clone --config core.autocrlf=false https://github.com/llvm/llvm-project.git 426 427This will create an '``llvm-project``' directory in the current directory and 428fully populate it with all of the source code, test directories, and local 429copies of documentation files for LLVM and all the related subprojects. Note 430that unlike the tarballs, which contain each subproject in a separate file, the 431git repository contains all of the projects together. 432 433If you want to get a specific release (as opposed to the most recent revision), 434you can check out a tag after cloning the repository. E.g., `git checkout 435llvmorg-6.0.1` inside the ``llvm-project`` directory created by the above 436command. Use `git tag -l` to list all of them. 437 438Sending patches 439^^^^^^^^^^^^^^^ 440 441Please read `Developer Policy <DeveloperPolicy.html#one-off-patches>`_, too. 442 443We don't currently accept github pull requests, so you'll need to send patches 444either via emailing to llvm-commits, or, preferably, via :ref:`Phabricator 445<phabricator-reviews>`. 446 447You'll generally want to make sure your branch has a single commit, 448corresponding to the review you wish to send, up-to-date with the upstream 449``origin/master`` branch, and doesn't contain merges. Once you have that, you 450can start `a Phabricator review <Phabricator.html>`_ (or use ``git show`` or 451``git format-patch`` to output the diff, and attach it to an email message). 452 453However, using the "Arcanist" tool is often easier. After `installing 454arcanist`_, you can upload the latest commit using: 455 456.. code-block:: console 457 458 % arc diff HEAD~1 459 460Additionally, before sending a patch for review, please also try to ensure it's 461formatted properly. We use ``clang-format`` for this, which has git integration 462through the ``git-clang-format`` script. On some systems, it may already be 463installed (or be installable via your package manager). If so, you can simply 464run it -- the following command will format only the code changed in the most 465recent commit: 466 467.. code-block:: console 468 469 % git clang-format HEAD~1 470 471Note that this modifies the files, but doesn't commit them -- you'll likely want 472to run 473 474.. code-block:: console 475 476 % git commit --amend -a 477 478in order to update the last commit with all pending changes. 479 480.. note:: 481 If you don't already have ``clang-format`` or ``git clang-format`` installed 482 on your system, the ``clang-format`` binary will be built alongside clang, and 483 the git integration can be run from 484 ``clang/tools/clang-format/git-clang-format``. 485 486 487.. _commit_from_git: 488 489For developers to commit changes from Git 490^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 491 492Once a patch is reviewed, you should rebase it, re-test locally, and commit the 493changes to LLVM's master branch. This is done using `git push` if you have the 494required access rights. See `committing a change 495<Phabricator.html#committing-a-change>`_ for Phabricator based commits or 496`obtaining commit access <DeveloperPolicy.html#obtaining-commit-access>`_ 497for commit access. 498 499LLVM currently has a linear-history policy, which means that merge commits are 500not allowed. The `llvm-project` repo on github is configured to reject pushes 501that include merges, so the `git rebase` step above is required. 502 503Bisecting commits 504^^^^^^^^^^^^^^^^^ 505 506See `Bisecting LLVM code <GitBisecting.html>`_ for how to use ``git bisect`` 507on LLVM. 508 509Reverting a change 510^^^^^^^^^^^^^^^^^^ 511 512When reverting changes using git, the default message will say "This reverts 513commit XYZ". Leave this at the end of the commit message, but add some details 514before it as to why the commit is being reverted. A brief explanation and/or 515links to bots that demonstrate the problem are sufficient. 516 517Checkout via SVN (deprecated) 518^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 519 520The SVN repository is no longer updated, but it is still available for now. If 521you need to check the code out of SVN rather than git for some reason, you can 522do it like so: 523 524* ``cd where-you-want-llvm-to-live`` 525* Read-Only: ``svn co https://llvm.org/svn/llvm-project/llvm/trunk llvm`` 526* Read-Write: ``svn co https://[email protected]/svn/llvm-project/llvm/trunk llvm`` 527 528This will create an '``llvm``' directory in the current directory and fully 529populate it with the LLVM source code, Makefiles, test directories, and local 530copies of documentation files. 531 532If you want to get a specific release (as opposed to the most recent revision), 533you can check it out from the '``tags``' directory (instead of '``trunk``'). The 534following releases are located in the following subdirectories of the '``tags``' 535directory: 536 537* Release 3.5.0 and later: **RELEASE_350/final** and so on 538* Release 2.9 through 3.4: **RELEASE_29/final** and so on 539* Release 1.1 through 2.8: **RELEASE_11** and so on 540* Release 1.0: **RELEASE_1** 541 542Local LLVM Configuration 543------------------------ 544 545Once checked out repository, the LLVM suite source code must be configured 546before being built. This process uses CMake. Unlinke the normal ``configure`` 547script, CMake generates the build files in whatever format you request as well 548as various ``*.inc`` files, and ``llvm/include/Config/config.h``. 549 550Variables are passed to ``cmake`` on the command line using the format 551``-D<variable name>=<value>``. The following variables are some common options 552used by people developing LLVM. 553 554+-------------------------+----------------------------------------------------+ 555| Variable | Purpose | 556+=========================+====================================================+ 557| CMAKE_C_COMPILER | Tells ``cmake`` which C compiler to use. By | 558| | default, this will be /usr/bin/cc. | 559+-------------------------+----------------------------------------------------+ 560| CMAKE_CXX_COMPILER | Tells ``cmake`` which C++ compiler to use. By | 561| | default, this will be /usr/bin/c++. | 562+-------------------------+----------------------------------------------------+ 563| CMAKE_BUILD_TYPE | Tells ``cmake`` what type of build you are trying | 564| | to generate files for. Valid options are Debug, | 565| | Release, RelWithDebInfo, and MinSizeRel. Default | 566| | is Debug. | 567+-------------------------+----------------------------------------------------+ 568| CMAKE_INSTALL_PREFIX | Specifies the install directory to target when | 569| | running the install action of the build files. | 570+-------------------------+----------------------------------------------------+ 571| PYTHON_EXECUTABLE | Forces CMake to use a specific Python version by | 572| | passing a path to a Python interpreter. By default | 573| | the Python version of the interpreter in your PATH | 574| | is used. | 575+-------------------------+----------------------------------------------------+ 576| LLVM_TARGETS_TO_BUILD | A semicolon delimited list controlling which | 577| | targets will be built and linked into llvm. | 578| | The default list is defined as | 579| | ``LLVM_ALL_TARGETS``, and can be set to include | 580| | out-of-tree targets. The default value includes: | 581| | ``AArch64, AMDGPU, ARM, BPF, Hexagon, Mips, | 582| | MSP430, NVPTX, PowerPC, Sparc, SystemZ, X86, | 583| | XCore``. | 584| | | 585+-------------------------+----------------------------------------------------+ 586| LLVM_ENABLE_DOXYGEN | Build doxygen-based documentation from the source | 587| | code This is disabled by default because it is | 588| | slow and generates a lot of output. | 589+-------------------------+----------------------------------------------------+ 590| LLVM_ENABLE_PROJECTS | A semicolon-delimited list selecting which of the | 591| | other LLVM subprojects to additionally build. (Only| 592| | effective when using a side-by-side project layout | 593| | e.g. via git). The default list is empty. Can | 594| | include: clang, libcxx, libcxxabi, libunwind, lldb,| 595| | compiler-rt, lld, polly, or debuginfo-tests. | 596+-------------------------+----------------------------------------------------+ 597| LLVM_ENABLE_SPHINX | Build sphinx-based documentation from the source | 598| | code. This is disabled by default because it is | 599| | slow and generates a lot of output. Sphinx version | 600| | 1.5 or later recommended. | 601+-------------------------+----------------------------------------------------+ 602| LLVM_BUILD_LLVM_DYLIB | Generate libLLVM.so. This library contains a | 603| | default set of LLVM components that can be | 604| | overridden with ``LLVM_DYLIB_COMPONENTS``. The | 605| | default contains most of LLVM and is defined in | 606| | ``tools/llvm-shlib/CMakelists.txt``. This option is| 607| | not available on Windows. | 608+-------------------------+----------------------------------------------------+ 609| LLVM_OPTIMIZED_TABLEGEN | Builds a release tablegen that gets used during | 610| | the LLVM build. This can dramatically speed up | 611| | debug builds. | 612+-------------------------+----------------------------------------------------+ 613 614To configure LLVM, follow these steps: 615 616#. Change directory into the object root directory: 617 618 .. code-block:: console 619 620 % cd OBJ_ROOT 621 622#. Run the ``cmake``: 623 624 .. code-block:: console 625 626 % cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/install/path 627 [other options] SRC_ROOT 628 629Compiling the LLVM Suite Source Code 630------------------------------------ 631 632Unlike with autotools, with CMake your build type is defined at configuration. 633If you want to change your build type, you can re-run cmake with the following 634invocation: 635 636 .. code-block:: console 637 638 % cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=type SRC_ROOT 639 640Between runs, CMake preserves the values set for all options. CMake has the 641following build types defined: 642 643Debug 644 645 These builds are the default. The build system will compile the tools and 646 libraries unoptimized, with debugging information, and asserts enabled. 647 648Release 649 650 For these builds, the build system will compile the tools and libraries 651 with optimizations enabled and not generate debug info. CMakes default 652 optimization level is -O3. This can be configured by setting the 653 ``CMAKE_CXX_FLAGS_RELEASE`` variable on the CMake command line. 654 655RelWithDebInfo 656 657 These builds are useful when debugging. They generate optimized binaries with 658 debug information. CMakes default optimization level is -O2. This can be 659 configured by setting the ``CMAKE_CXX_FLAGS_RELWITHDEBINFO`` variable on the 660 CMake command line. 661 662Once you have LLVM configured, you can build it by entering the *OBJ_ROOT* 663directory and issuing the following command: 664 665.. code-block:: console 666 667 % make 668 669If the build fails, please `check here`_ to see if you are using a version of 670GCC that is known not to compile LLVM. 671 672If you have multiple processors in your machine, you may wish to use some of the 673parallel build options provided by GNU Make. For example, you could use the 674command: 675 676.. code-block:: console 677 678 % make -j2 679 680There are several special targets which are useful when working with the LLVM 681source code: 682 683``make clean`` 684 685 Removes all files generated by the build. This includes object files, 686 generated C/C++ files, libraries, and executables. 687 688``make install`` 689 690 Installs LLVM header files, libraries, tools, and documentation in a hierarchy 691 under ``$PREFIX``, specified with ``CMAKE_INSTALL_PREFIX``, which 692 defaults to ``/usr/local``. 693 694``make docs-llvm-html`` 695 696 If configured with ``-DLLVM_ENABLE_SPHINX=On``, this will generate a directory 697 at ``OBJ_ROOT/docs/html`` which contains the HTML formatted documentation. 698 699Cross-Compiling LLVM 700-------------------- 701 702It is possible to cross-compile LLVM itself. That is, you can create LLVM 703executables and libraries to be hosted on a platform different from the platform 704where they are built (a Canadian Cross build). To generate build files for 705cross-compiling CMake provides a variable ``CMAKE_TOOLCHAIN_FILE`` which can 706define compiler flags and variables used during the CMake test operations. 707 708The result of such a build is executables that are not runnable on the build 709host but can be executed on the target. As an example the following CMake 710invocation can generate build files targeting iOS. This will work on macOS 711with the latest Xcode: 712 713.. code-block:: console 714 715 % cmake -G "Ninja" -DCMAKE_OSX_ARCHITECTURES="armv7;armv7s;arm64" 716 -DCMAKE_TOOLCHAIN_FILE=<PATH_TO_LLVM>/cmake/platforms/iOS.cmake 717 -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_RUNTIME=Off -DLLVM_INCLUDE_TESTS=Off 718 -DLLVM_INCLUDE_EXAMPLES=Off -DLLVM_ENABLE_BACKTRACES=Off [options] 719 <PATH_TO_LLVM> 720 721Note: There are some additional flags that need to be passed when building for 722iOS due to limitations in the iOS SDK. 723 724Check :doc:`HowToCrossCompileLLVM` and `Clang docs on how to cross-compile in general 725<https://clang.llvm.org/docs/CrossCompilation.html>`_ for more information 726about cross-compiling. 727 728The Location of LLVM Object Files 729--------------------------------- 730 731The LLVM build system is capable of sharing a single LLVM source tree among 732several LLVM builds. Hence, it is possible to build LLVM for several different 733platforms or configurations using the same source tree. 734 735* Change directory to where the LLVM object files should live: 736 737 .. code-block:: console 738 739 % cd OBJ_ROOT 740 741* Run ``cmake``: 742 743 .. code-block:: console 744 745 % cmake -G "Unix Makefiles" SRC_ROOT 746 747The LLVM build will create a structure underneath *OBJ_ROOT* that matches the 748LLVM source tree. At each level where source files are present in the source 749tree there will be a corresponding ``CMakeFiles`` directory in the *OBJ_ROOT*. 750Underneath that directory there is another directory with a name ending in 751``.dir`` under which you'll find object files for each source. 752 753For example: 754 755 .. code-block:: console 756 757 % cd llvm_build_dir 758 % find lib/Support/ -name APFloat* 759 lib/Support/CMakeFiles/LLVMSupport.dir/APFloat.cpp.o 760 761Optional Configuration Items 762---------------------------- 763 764If you're running on a Linux system that supports the `binfmt_misc 765<http://en.wikipedia.org/wiki/binfmt_misc>`_ 766module, and you have root access on the system, you can set your system up to 767execute LLVM bitcode files directly. To do this, use commands like this (the 768first command may not be required if you are already using the module): 769 770.. code-block:: console 771 772 % mount -t binfmt_misc none /proc/sys/fs/binfmt_misc 773 % echo ':llvm:M::BC::/path/to/lli:' > /proc/sys/fs/binfmt_misc/register 774 % chmod u+x hello.bc (if needed) 775 % ./hello.bc 776 777This allows you to execute LLVM bitcode files directly. On Debian, you can also 778use this command instead of the 'echo' command above: 779 780.. code-block:: console 781 782 % sudo update-binfmts --install llvm /path/to/lli --magic 'BC' 783 784.. _Program Layout: 785.. _general layout: 786 787Directory Layout 788================ 789 790One useful source of information about the LLVM source base is the LLVM `doxygen 791<http://www.doxygen.org/>`_ documentation available at 792`<https://llvm.org/doxygen/>`_. The following is a brief introduction to code 793layout: 794 795``llvm/examples`` 796----------------- 797 798Simple examples using the LLVM IR and JIT. 799 800``llvm/include`` 801---------------- 802 803Public header files exported from the LLVM library. The three main subdirectories: 804 805``llvm/include/llvm`` 806 807 All LLVM-specific header files, and subdirectories for different portions of 808 LLVM: ``Analysis``, ``CodeGen``, ``Target``, ``Transforms``, etc... 809 810``llvm/include/llvm/Support`` 811 812 Generic support libraries provided with LLVM but not necessarily specific to 813 LLVM. For example, some C++ STL utilities and a Command Line option processing 814 library store header files here. 815 816``llvm/include/llvm/Config`` 817 818 Header files configured by ``cmake``. They wrap "standard" UNIX and 819 C header files. Source code can include these header files which 820 automatically take care of the conditional #includes that ``cmake`` 821 generates. 822 823``llvm/lib`` 824------------ 825 826Most source files are here. By putting code in libraries, LLVM makes it easy to 827share code among the `tools`_. 828 829``llvm/lib/IR/`` 830 831 Core LLVM source files that implement core classes like Instruction and 832 BasicBlock. 833 834``llvm/lib/AsmParser/`` 835 836 Source code for the LLVM assembly language parser library. 837 838``llvm/lib/Bitcode/`` 839 840 Code for reading and writing bitcode. 841 842``llvm/lib/Analysis/`` 843 844 A variety of program analyses, such as Call Graphs, Induction Variables, 845 Natural Loop Identification, etc. 846 847``llvm/lib/Transforms/`` 848 849 IR-to-IR program transformations, such as Aggressive Dead Code Elimination, 850 Sparse Conditional Constant Propagation, Inlining, Loop Invariant Code Motion, 851 Dead Global Elimination, and many others. 852 853``llvm/lib/Target/`` 854 855 Files describing target architectures for code generation. For example, 856 ``llvm/lib/Target/X86`` holds the X86 machine description. 857 858``llvm/lib/CodeGen/`` 859 860 The major parts of the code generator: Instruction Selector, Instruction 861 Scheduling, and Register Allocation. 862 863``llvm/lib/MC/`` 864 865 (FIXME: T.B.D.) ....? 866 867``llvm/lib/ExecutionEngine/`` 868 869 Libraries for directly executing bitcode at runtime in interpreted and 870 JIT-compiled scenarios. 871 872``llvm/lib/Support/`` 873 874 Source code that corresponding to the header files in ``llvm/include/ADT/`` 875 and ``llvm/include/Support/``. 876 877``llvm/projects`` 878----------------- 879 880Projects not strictly part of LLVM but shipped with LLVM. This is also the 881directory for creating your own LLVM-based projects which leverage the LLVM 882build system. 883 884``llvm/test`` 885------------- 886 887Feature and regression tests and other sanity checks on LLVM infrastructure. These 888are intended to run quickly and cover a lot of territory without being exhaustive. 889 890``test-suite`` 891-------------- 892 893A comprehensive correctness, performance, and benchmarking test suite 894for LLVM. This comes in a ``separate git repository 895<https://github.com/llvm/llvm-test-suite>``, because it contains a 896large amount of third-party code under a variety of licenses. For 897details see the :doc:`Testing Guide <TestingGuide>` document. 898 899.. _tools: 900 901``llvm/tools`` 902-------------- 903 904Executables built out of the libraries 905above, which form the main part of the user interface. You can always get help 906for a tool by typing ``tool_name -help``. The following is a brief introduction 907to the most important tools. More detailed information is in 908the `Command Guide <CommandGuide/index.html>`_. 909 910``bugpoint`` 911 912 ``bugpoint`` is used to debug optimization passes or code generation backends 913 by narrowing down the given test case to the minimum number of passes and/or 914 instructions that still cause a problem, whether it is a crash or 915 miscompilation. See `<HowToSubmitABug.html>`_ for more information on using 916 ``bugpoint``. 917 918``llvm-ar`` 919 920 The archiver produces an archive containing the given LLVM bitcode files, 921 optionally with an index for faster lookup. 922 923``llvm-as`` 924 925 The assembler transforms the human readable LLVM assembly to LLVM bitcode. 926 927``llvm-dis`` 928 929 The disassembler transforms the LLVM bitcode to human readable LLVM assembly. 930 931``llvm-link`` 932 933 ``llvm-link``, not surprisingly, links multiple LLVM modules into a single 934 program. 935 936``lli`` 937 938 ``lli`` is the LLVM interpreter, which can directly execute LLVM bitcode 939 (although very slowly...). For architectures that support it (currently x86, 940 Sparc, and PowerPC), by default, ``lli`` will function as a Just-In-Time 941 compiler (if the functionality was compiled in), and will execute the code 942 *much* faster than the interpreter. 943 944``llc`` 945 946 ``llc`` is the LLVM backend compiler, which translates LLVM bitcode to a 947 native code assembly file. 948 949``opt`` 950 951 ``opt`` reads LLVM bitcode, applies a series of LLVM to LLVM transformations 952 (which are specified on the command line), and outputs the resultant 953 bitcode. '``opt -help``' is a good way to get a list of the 954 program transformations available in LLVM. 955 956 ``opt`` can also run a specific analysis on an input LLVM bitcode 957 file and print the results. Primarily useful for debugging 958 analyses, or familiarizing yourself with what an analysis does. 959 960``llvm/utils`` 961-------------- 962 963Utilities for working with LLVM source code; some are part of the build process 964because they are code generators for parts of the infrastructure. 965 966 967``codegen-diff`` 968 969 ``codegen-diff`` finds differences between code that LLC 970 generates and code that LLI generates. This is useful if you are 971 debugging one of them, assuming that the other generates correct output. For 972 the full user manual, run ```perldoc codegen-diff'``. 973 974``emacs/`` 975 976 Emacs and XEmacs syntax highlighting for LLVM assembly files and TableGen 977 description files. See the ``README`` for information on using them. 978 979``getsrcs.sh`` 980 981 Finds and outputs all non-generated source files, 982 useful if one wishes to do a lot of development across directories 983 and does not want to find each file. One way to use it is to run, 984 for example: ``xemacs `utils/getsources.sh``` from the top of the LLVM source 985 tree. 986 987``llvmgrep`` 988 989 Performs an ``egrep -H -n`` on each source file in LLVM and 990 passes to it a regular expression provided on ``llvmgrep``'s command 991 line. This is an efficient way of searching the source base for a 992 particular regular expression. 993 994``TableGen/`` 995 996 Contains the tool used to generate register 997 descriptions, instruction set descriptions, and even assemblers from common 998 TableGen description files. 999 1000``vim/`` 1001 1002 vim syntax-highlighting for LLVM assembly files 1003 and TableGen description files. See the ``README`` for how to use them. 1004 1005.. _simple example: 1006 1007An Example Using the LLVM Tool Chain 1008==================================== 1009 1010This section gives an example of using LLVM with the Clang front end. 1011 1012Example with clang 1013------------------ 1014 1015#. First, create a simple C file, name it 'hello.c': 1016 1017 .. code-block:: c 1018 1019 #include <stdio.h> 1020 1021 int main() { 1022 printf("hello world\n"); 1023 return 0; 1024 } 1025 1026#. Next, compile the C file into a native executable: 1027 1028 .. code-block:: console 1029 1030 % clang hello.c -o hello 1031 1032 .. note:: 1033 1034 Clang works just like GCC by default. The standard -S and -c arguments 1035 work as usual (producing a native .s or .o file, respectively). 1036 1037#. Next, compile the C file into an LLVM bitcode file: 1038 1039 .. code-block:: console 1040 1041 % clang -O3 -emit-llvm hello.c -c -o hello.bc 1042 1043 The -emit-llvm option can be used with the -S or -c options to emit an LLVM 1044 ``.ll`` or ``.bc`` file (respectively) for the code. This allows you to use 1045 the `standard LLVM tools <CommandGuide/index.html>`_ on the bitcode file. 1046 1047#. Run the program in both forms. To run the program, use: 1048 1049 .. code-block:: console 1050 1051 % ./hello 1052 1053 and 1054 1055 .. code-block:: console 1056 1057 % lli hello.bc 1058 1059 The second examples shows how to invoke the LLVM JIT, :doc:`lli 1060 <CommandGuide/lli>`. 1061 1062#. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code: 1063 1064 .. code-block:: console 1065 1066 % llvm-dis < hello.bc | less 1067 1068#. Compile the program to native assembly using the LLC code generator: 1069 1070 .. code-block:: console 1071 1072 % llc hello.bc -o hello.s 1073 1074#. Assemble the native assembly language file into a program: 1075 1076 .. code-block:: console 1077 1078 % /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.native # On Solaris 1079 1080 % gcc hello.s -o hello.native # On others 1081 1082#. Execute the native code program: 1083 1084 .. code-block:: console 1085 1086 % ./hello.native 1087 1088 Note that using clang to compile directly to native code (i.e. when the 1089 ``-emit-llvm`` option is not present) does steps 6/7/8 for you. 1090 1091Common Problems 1092=============== 1093 1094If you are having problems building or using LLVM, or if you have any other 1095general questions about LLVM, please consult the `Frequently Asked 1096Questions <FAQ.html>`_ page. 1097 1098If you are having problems with limited memory and build time, please try 1099building with ninja instead of make. Please consider configuring the 1100following options with cmake: 1101 1102 * -G Ninja 1103 Setting this option will allow you to build with ninja instead of make. 1104 Building with ninja significantly improves your build time, especially with 1105 incremental builds, and improves your memory usage. 1106 1107 * -DLLVM_USE_LINKER 1108 Setting this option to either gold or lld will significantly improve 1109 performance. In the case that you are compiling lld, you may wish to use the 1110 gold linker as a faster alternative. 1111 1112 * -DCMAKE_BUILD_TYPE 1113 This option defaults to Debug; however, this may consume more memory during 1114 the linking phase. So, you may wish to use the build type Release. Another 1115 build type you may wish to consider is release-with-asserts which compiles at 1116 nearly the same rate as the Release build; however, it may not be as easy 1117 to debug. MinSizeRel is another build type you may wish to consider, if you 1118 are still having problems with slow build time. 1119 1120 * -DLLVM_PARALLEL_LINK_JOBS 1121 Set this equal to number of jobs you wish to run simultaneously. This is 1122 similar to the -j option used with make, but only for link jobs. This option 1123 is of course only meaningful if you plan to build with ninja. You may wish to 1124 use a very low number of jobs, as this will greatly reduce the amount memory 1125 used during the build process. If you have limited memory, you may wish to 1126 set this to 1. 1127 1128 * -DLLVM_TARGETS_TO_BUILD 1129 Set this equal to the target you wish to build. You may wish to set this to 1130 X86; however, you will find a full list of targets within the 1131 llvm-project/llvm/lib/Target directory. 1132 1133 * -DLLVM_OPTIMIZED_TABLEGEN 1134 Set this to ON to generate a fully optimized tablegen during build. This will 1135 significantly improve your build time. 1136 1137 * -DLLVM_ENABLE_PROJECTS 1138 Set this equal to the projects you wish to compile (e.g. clang, lld, etc.) If 1139 compiling more than one project, deliniate the list with a semicolon. Should 1140 you run into issues with the semicolon, try surrounding it with single quotes. 1141 1142 * -DCLANG_ENABLE_STATIC_ANALYZER 1143 Set this option to OFF if you do not require the clang static analyzer. This 1144 should improve your build time significantly. 1145 1146 * -DLLVM_USE_SPLIT_DWARF 1147 Consider setting this to ON if you require a debug build, as this will ease 1148 memory pressure on the linker. This will make linking much faster, as the 1149 binaries will not contain any of the debug information; however, this will 1150 generate the debug information in the form of a DWARF object file (with the 1151 extension .dwo). 1152 1153.. _links: 1154 1155Links 1156===== 1157 1158This document is just an **introduction** on how to use LLVM to do some simple 1159things... there are many more interesting and complicated things that you can do 1160that aren't documented here (but we'll gladly accept a patch if you want to 1161write something up!). For more information about LLVM, check out: 1162 1163* `LLVM Homepage <https://llvm.org/>`_ 1164* `LLVM Doxygen Tree <https://llvm.org/doxygen/>`_ 1165* `Starting a Project that Uses LLVM <https://llvm.org/docs/Projects.html>`_ 1166 1167.. _installing arcanist: https://secure.phabricator.com/book/phabricator/article/arcanist_quick_start/ 1168