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