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