blob: 742c7b96bc891d00289f12ea6eb2f4d28e8a4c43 [file] [log] [blame]
Bill Wendling954e4302012-10-07 07:10:13 +00001====================================
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +00002Getting Started with the LLVM System
Bill Wendling954e4302012-10-07 07:10:13 +00003====================================
4
Sean Silvafe156162012-12-20 03:32:39 +00005.. contents::
6 :local:
7
Bill Wendling954e4302012-10-07 07:10:13 +00008Overview
9========
10
James Y Knight78655882019-01-14 22:27:32 +000011Welcome to the LLVM project! In order to get started, you first need to know
12some basic information.
Bill Wendling954e4302012-10-07 07:10:13 +000013
James Y Knight78655882019-01-14 22:27:32 +000014First, 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.
Bill Wendling954e4302012-10-07 07:10:13 +000019
James Y Knight78655882019-01-14 22:27:32 +000020Another 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.
Bill Wendling954e4302012-10-07 07:10:13 +000023
James Y Knight78655882019-01-14 22:27:32 +000024There are other components as well:
25the `libc++ C++ standard library <https://libcxx.llvm.org>`_,
26the `LLD linker <https://lld.llvm.org>`_, and more.
Bill Wendling954e4302012-10-07 07:10:13 +000027
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.
Philip Reames98402412016-02-06 19:20:26 +000040
James Y Knight78655882019-01-14 22:27:32 +000041#. Checkout LLVM (including related subprojects like Clang):
Philip Reames98402412016-02-06 19:20:26 +000042
James Y Knight78655882019-01-14 22:27:32 +000043 * ``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``
Bill Wendling954e4302012-10-07 07:10:13 +000046
James Y Knight78655882019-01-14 22:27:32 +000047#. Configure and build LLVM and Clang:.
Bill Wendling954e4302012-10-07 07:10:13 +000048
James Y Knight78655882019-01-14 22:27:32 +000049 * ``cd llvm-project``
Chris Bieneman9f611e32015-03-13 01:58:14 +000050 * ``mkdir build``
51 * ``cd build``
James Y Knight78655882019-01-14 22:27:32 +000052 * ``cmake -G <generator> [options] ../llvm``
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +000053
Chris Bieneman9f611e32015-03-13 01:58:14 +000054 Some common generators are:
55
Nico Webercc8ee8e2016-03-28 20:32:38 +000056 * ``Ninja`` --- for generating `Ninja <https://ninja-build.org>`_
Nico Weber83c3bd32016-03-28 21:00:21 +000057 build files. Most llvm developers use Ninja.
James Y Knight78655882019-01-14 22:27:32 +000058 * ``Unix Makefiles`` --- for generating make-compatible parallel makefiles.
Chris Bieneman9f611e32015-03-13 01:58:14 +000059 * ``Visual Studio`` --- for generating Visual Studio projects and
Nico Weber83c3bd32016-03-28 21:00:21 +000060 solutions.
Chris Bieneman9f611e32015-03-13 01:58:14 +000061 * ``Xcode`` --- for generating Xcode projects.
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +000062
Chris Bieneman9f611e32015-03-13 01:58:14 +000063 Some Common options:
64
James Y Knight78655882019-01-14 22:27:32 +000065 * ``-DLLVM_ENABLE_PROJECTS='...'`` --- semicolon-separated list of the LLVM
66 subprojects you'd like to additionally build. Can include any of: clang,
Nico Weber04a1ee42019-02-14 20:26:35 +000067 clang-tools-extra, libcxx, libcxxabi, libunwind, lldb, compiler-rt, lld,
68 polly, or debuginfo-tests.
James Y Knight78655882019-01-14 22:27:32 +000069
70 For example, to build LLVM, Clang, libcxx, and libcxxabi, use
71 ``-DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi"``.
72
Chris Bieneman9f611e32015-03-13 01:58:14 +000073 * ``-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
Chris Bieneman3bec07a2015-03-14 21:20:32 +000083 * Run your build tool of choice!
84
James Y Knight78655882019-01-14 22:27:32 +000085 * The default target (i.e. ``ninja`` or ``make``) will build all of LLVM.
Chris Bieneman3bec07a2015-03-14 21:20:32 +000086
James Y Knight78655882019-01-14 22:27:32 +000087 * The ``check-all`` target (i.e. ``ninja check-all``) will run the
Chris Bieneman3bec07a2015-03-14 21:20:32 +000088 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
James Y Knight78655882019-01-14 22:27:32 +000093 * 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.)
Philip Reamesc4139662016-02-06 19:43:40 +000097
Chris Bieneman9f611e32015-03-13 01:58:14 +000098 * For more information see `CMake <CMake.html>`_
99
Bill Wendling954e4302012-10-07 07:10:13 +0000100 * If you get an "internal compiler error (ICE)" or test failures, see
Chris Bieneman3bec07a2015-03-14 21:20:32 +0000101 `below`_.
Bill Wendling954e4302012-10-07 07:10:13 +0000102
103Consult the `Getting Started with LLVM`_ section for detailed information on
Mitch Phillipsf65169842017-10-23 17:37:41 +0000104configuring and compiling LLVM. Go to `Directory Layout`_ to learn about the
Philip Reamesd2445022016-02-07 15:49:57 +0000105layout of the source code tree.
Bill Wendling954e4302012-10-07 07:10:13 +0000106
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
Chandler Carruth41e1b912014-02-27 09:33:55 +0000117LLVM is known to work on the following host platforms:
Bill Wendling954e4302012-10-07 07:10:13 +0000118
Chandler Carruth4ef7efe2014-02-27 09:41:13 +0000119================== ===================== =============
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000120OS Arch Compilers
Chandler Carruth4ef7efe2014-02-27 09:41:13 +0000121================== ===================== =============
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000122Linux x86\ :sup:`1` GCC, Clang
123Linux amd64 GCC, Clang
James Y Knight78655882019-01-14 22:27:32 +0000124Linux ARM GCC, Clang
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000125Linux PowerPC GCC, Clang
126Solaris V9 (Ultrasparc) GCC
127FreeBSD x86\ :sup:`1` GCC, Clang
128FreeBSD amd64 GCC, Clang
Kamil Rytarowskifc32c3a2017-04-22 16:11:23 +0000129NetBSD x86\ :sup:`1` GCC, Clang
130NetBSD amd64 GCC, Clang
J. Ryan Stinnettd45eaf92019-05-30 16:46:22 +0000131macOS\ :sup:`2` PowerPC GCC
132macOS x86 GCC, Clang
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000133Cygwin/Win32 x86\ :sup:`1, 3` GCC
134Windows x86\ :sup:`1` Visual Studio
135Windows x64 x86-64 Visual Studio
Chandler Carruth4ef7efe2014-02-27 09:41:13 +0000136================== ===================== =============
Bill Wendling954e4302012-10-07 07:10:13 +0000137
138.. note::
139
Bill Wendling954e4302012-10-07 07:10:13 +0000140 #. Code generation supported for Pentium processors and up
141 #. Code generation supported for 32-bit ABI only
Bill Wendling954e4302012-10-07 07:10:13 +0000142 #. To use LLVM modules on Win32-based system, you may configure LLVM
Philip Reamesa8feaf62016-02-07 15:58:35 +0000143 with ``-DBUILD_SHARED_LIBS=On``.
Bill Wendling954e4302012-10-07 07:10:13 +0000144
Philip Reamesa8feaf62016-02-07 15:58:35 +0000145Note 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
Mitch Phillipsf65169842017-10-23 17:37:41 +0000148is so large because of all the debugging information and the fact that the
149libraries are statically linked into multiple tools).
Philip Reamesa8feaf62016-02-07 15:58:35 +0000150
Hans Wennborg005dd842018-06-13 09:11:10 +0000151If you are space-constrained, you can build only selected tools or only
Philip Reamesa8feaf62016-02-07 15:58:35 +0000152selected targets. The Release build requires considerably less space.
Bill Wendling954e4302012-10-07 07:10:13 +0000153
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
Chandler Carruthe04872d2014-02-27 09:46:09 +0000169=========================================================== ============ ==========================================
170Package Version Notes
171=========================================================== ============ ==========================================
172`GNU Make <http://savannah.gnu.org/projects/make>`_ 3.79, 3.79.1 Makefile/build processor
JF Bastien388cefa2019-02-07 05:20:00 +0000173`GCC <http://gcc.gnu.org/>`_ >=5.1.0 C/C++ compiler\ :sup:`1`
Rafael Espindola21a400852014-12-12 15:29:31 +0000174`python <http://www.python.org/>`_ >=2.7 Automated test suite\ :sup:`2`
Nico Weber70b89592016-03-28 21:24:46 +0000175`zlib <http://zlib.net>`_ >=1.2.3.4 Compression library\ :sup:`3`
Chandler Carruthe04872d2014-02-27 09:46:09 +0000176=========================================================== ============ ==========================================
Bill Wendling954e4302012-10-07 07:10:13 +0000177
178.. note::
179
180 #. Only the C and C++ languages are needed so there's no need to build the
181 other languages for LLVM's purposes. See `below` for specific version
182 info.
Bill Wendling954e4302012-10-07 07:10:13 +0000183 #. Only needed if you want to run the automated test suite in the
184 ``llvm/test`` directory.
Sean Silva216f1ee2014-03-02 00:21:42 +0000185 #. Optional, adds compression / uncompression capabilities to selected LLVM
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000186 tools.
Bill Wendling954e4302012-10-07 07:10:13 +0000187
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
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000197* **date** --- print the current date/time
Bill Wendling954e4302012-10-07 07:10:13 +0000198* **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
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000204* **install** --- install directories/files
Bill Wendling954e4302012-10-07 07:10:13 +0000205* **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
Chandler Carruth55cc48f2014-02-27 10:35:57 +0000219Host C++ Toolchain, both Compiler and Standard Library
220------------------------------------------------------
Bill Wendling954e4302012-10-07 07:10:13 +0000221
222LLVM is very demanding of the host C++ compiler, and as such tends to expose
JF Bastien6e69db52019-01-21 23:53:52 +0000223bugs 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.
Bill Wendling954e4302012-10-07 07:10:13 +0000226
JF Bastien6e69db52019-01-21 23:53:52 +0000227LLVM 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:
Bill Wendling954e4302012-10-07 07:10:13 +0000230
JF Bastien388cefa2019-02-07 05:20:00 +0000231* Clang 3.5
232* Apple Clang 6.0
233* GCC 5.1
234* Visual Studio 2017
235
236The below versions currently soft-error as we transition to the new compiler
237versions listed above. The LLVM codebase is currently known to compile correctly
238with the following compilers, though this will change in the near future:
239
Chandler Carruth55cc48f2014-02-27 10:35:57 +0000240* Clang 3.1
JF Bastien6e69db52019-01-21 23:53:52 +0000241* Apple Clang 3.1
Teresa Johnsonf7f02fa2016-10-18 17:17:37 +0000242* GCC 4.8
Simon Pilgrim456fc4f2019-07-09 10:10:48 +0000243* Visual Studio 2017
Bill Wendling954e4302012-10-07 07:10:13 +0000244
Chandler Carruth55cc48f2014-02-27 10:35:57 +0000245Anything older than these toolchains *may* work, but will require forcing the
246build system with a special option and is not really a supported host platform.
247Also note that older versions of these compilers have often crashed or
248miscompiled LLVM.
Bill Wendling954e4302012-10-07 07:10:13 +0000249
Chandler Carruth55cc48f2014-02-27 10:35:57 +0000250For less widely used host toolchains such as ICC or xlC, be aware that a very
251recent version may be required to support all of the C++ features used in LLVM.
Bill Wendling954e4302012-10-07 07:10:13 +0000252
Chandler Carruth55cc48f2014-02-27 10:35:57 +0000253We track certain versions of software that are *known* to fail when used as
254part of the host toolchain. These even include linkers at times.
Bill Wendling954e4302012-10-07 07:10:13 +0000255
256**GNU ld 2.16.X**. Some 2.16.X versions of the ld linker will produce very long
257warning messages complaining that some "``.gnu.linkonce.t.*``" symbol was
258defined in a discarded section. You can safely ignore these messages as they are
259erroneous and the linkage is correct. These messages disappear using ld 2.17.
260
261**GNU binutils 2.17**: Binutils 2.17 contains `a bug
Sean Silvaa4fe9922012-10-07 18:49:28 +0000262<http://sourceware.org/bugzilla/show_bug.cgi?id=3111>`__ which causes huge link
Bill Wendling954e4302012-10-07 07:10:13 +0000263times (minutes instead of seconds) when building LLVM. We recommend upgrading
264to a newer version (2.17.50.0.4 or later).
265
266**GNU Binutils 2.19.1 Gold**: This version of Gold contained `a bug
Sean Silvaa4fe9922012-10-07 18:49:28 +0000267<http://sourceware.org/bugzilla/show_bug.cgi?id=9836>`__ which causes
Bill Wendling954e4302012-10-07 07:10:13 +0000268intermittent failures when building LLVM with position independent code. The
269symptom is an error about cyclic dependencies. We recommend upgrading to a
270newer version of Gold.
271
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000272Getting a Modern Host C++ Toolchain
273^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
274
J. Ryan Stinnettd45eaf92019-05-30 16:46:22 +0000275This section mostly applies to Linux and older BSDs. On macOS, you should
Chandler Carruthe7001692014-02-28 11:11:41 +0000276have a sufficiently modern Xcode, or you will likely need to upgrade until you
Reid Kleckner6130a2d2016-10-19 23:04:41 +0000277do. Windows does not have a "system compiler", so you must install either Visual
Simon Pilgrim456fc4f2019-07-09 10:10:48 +0000278Studio 2017 or a recent version of mingw64. FreeBSD 10.0 and newer have a modern
Chandler Carruthe7001692014-02-28 11:11:41 +0000279Clang as the system compiler.
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000280
Chandler Carruthb2719d42014-02-28 11:09:33 +0000281However, some Linux distributions and some other or older BSDs sometimes have
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000282extremely old versions of GCC. These steps attempt to help you upgrade you
283compiler even on such a system. However, if at all possible, we encourage you
284to use a recent version of a distribution with a modern system compiler that
Hiroshi Inouec8e92452018-01-29 05:17:03 +0000285meets these requirements. Note that it is tempting to install a prior
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000286version of Clang and libc++ to be the host compiler, however libc++ was not
287well tested or set up to build on Linux until relatively recently. As
288a consequence, this guide suggests just using libstdc++ and a modern GCC as the
289initial host in a bootstrap, and then using Clang (and potentially libc++).
290
291The first step is to get a recent GCC toolchain installed. The most common
292distribution on which users have struggled with the version requirements is
293Ubuntu Precise, 12.04 LTS. For this distribution, one easy option is to install
Chandler Carruth2423e012014-02-28 12:09:14 +0000294the `toolchain testing PPA`_ and use it to install a modern GCC. There is
JF Bastien388cefa2019-02-07 05:20:00 +0000295a really nice discussions of this on the `ask ubuntu stack exchange`_ and a
296`github gist`_ with updated commands. However, not all users can use PPAs and
297there are many other distributions, so it may be necessary (or just useful, if
298you're here you *are* doing compiler development after all) to build and install
299GCC from source. It is also quite easy to do these days.
Chandler Carruth2423e012014-02-28 12:09:14 +0000300
301.. _toolchain testing PPA:
302 https://launchpad.net/~ubuntu-toolchain-r/+archive/test
303.. _ask ubuntu stack exchange:
JF Bastien388cefa2019-02-07 05:20:00 +0000304 https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu/581497#58149
305.. _github gist:
306 https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000307
JF Bastien388cefa2019-02-07 05:20:00 +0000308Easy steps for installing GCC 5.1.0:
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000309
310.. code-block:: console
311
JF Bastien388cefa2019-02-07 05:20:00 +0000312 % gcc_version=5.1.0
313 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2
314 % wget https://ftp.gnu.org/gnu/gcc/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2.sig
JF Bastienfa31f9f2015-07-01 03:32:08 +0000315 % wget https://ftp.gnu.org/gnu/gnu-keyring.gpg
JF Bastien388cefa2019-02-07 05:20:00 +0000316 % signature_invalid=`gpg --verify --no-default-keyring --keyring ./gnu-keyring.gpg gcc-${gcc_version}.tar.bz2.sig`
JF Bastienfa31f9f2015-07-01 03:32:08 +0000317 % if [ $signature_invalid ]; then echo "Invalid signature" ; exit 1 ; fi
JF Bastien388cefa2019-02-07 05:20:00 +0000318 % tar -xvjf gcc-${gcc_version}.tar.bz2
319 % cd gcc-${gcc_version}
Chandler Carruth518e5822014-02-28 12:14:56 +0000320 % ./contrib/download_prerequisites
321 % cd ..
JF Bastien388cefa2019-02-07 05:20:00 +0000322 % mkdir gcc-${gcc_version}-build
323 % cd gcc-${gcc_version}-build
324 % $PWD/../gcc-${gcc_version}/configure --prefix=$HOME/toolchains --enable-languages=c,c++
Chandler Carruth518e5822014-02-28 12:14:56 +0000325 % make -j$(nproc)
326 % make install
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000327
Chandler Carruth2423e012014-02-28 12:09:14 +0000328For more details, check out the excellent `GCC wiki entry`_, where I got most
329of this information from.
330
331.. _GCC wiki entry:
JF Bastien388cefa2019-02-07 05:20:00 +0000332 https://gcc.gnu.org/wiki/InstallingGCC
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000333
Reid Kleckner2a069a82014-08-01 21:40:53 +0000334Once you have a GCC toolchain, configure your build of LLVM to use the new
335toolchain for your host compiler and C++ standard library. Because the new
336version of libstdc++ is not on the system library search path, you need to pass
337extra linker flags so that it can be found at link time (``-L``) and at runtime
338(``-rpath``). If you are using CMake, this invocation should produce working
339binaries:
340
341.. code-block:: console
342
343 % mkdir build
344 % cd build
345 % CC=$HOME/toolchains/bin/gcc CXX=$HOME/toolchains/bin/g++ \
346 cmake .. -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$HOME/toolchains/lib64 -L$HOME/toolchains/lib64"
347
348If you fail to set rpath, most LLVM binaries will fail on startup with a message
349from the loader similar to ``libstdc++.so.6: version `GLIBCXX_3.4.20' not
350found``. This means you need to tweak the -rpath linker flag.
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000351
JF Bastien6e69db52019-01-21 23:53:52 +0000352When you build Clang, you will need to give *it* access to modern C++
Chandler Carruth0ec4f902014-02-28 10:56:57 +0000353standard library in order to use it as your new host in part of a bootstrap.
354There are two easy ways to do this, either build (and install) libc++ along
355with Clang and then use it with the ``-stdlib=libc++`` compile and link flag,
356or install Clang into the same prefix (``$HOME/toolchains`` above) as GCC.
357Clang will look within its own prefix for libstdc++ and use it if found. You
358can also add an explicit prefix for Clang to look in for a GCC toolchain with
359the ``--gcc-toolchain=/opt/my/gcc/prefix`` flag, passing it to both compile and
360link commands when using your just-built-Clang to bootstrap.
361
Bill Wendling954e4302012-10-07 07:10:13 +0000362.. _Getting Started with LLVM:
363
364Getting Started with LLVM
365=========================
366
367The remainder of this guide is meant to get you up and running with LLVM and to
368give you some basic information about the LLVM environment.
369
370The later sections of this guide describe the `general layout`_ of the LLVM
371source tree, a `simple example`_ using the LLVM tool chain, and `links`_ to find
372more information about LLVM or to get help via e-mail.
373
374Terminology and Notation
375------------------------
376
377Throughout this manual, the following names are used to denote paths specific to
378the local system and working environment. *These are not environment variables
379you need to set but just strings used in the rest of this document below*. In
380any of the examples below, simply replace each of these names with the
381appropriate pathname on your local system. All these paths are absolute:
382
383``SRC_ROOT``
384
385 This is the top level directory of the LLVM source tree.
386
387``OBJ_ROOT``
388
389 This is the top level directory of the LLVM object tree (i.e. the tree where
390 object files and compiled programs will be placed. It can be the same as
391 SRC_ROOT).
392
Bill Wendling954e4302012-10-07 07:10:13 +0000393Unpacking the LLVM Archives
394---------------------------
395
396If you have the LLVM distribution, you will need to unpack it before you can
James Y Knight78655882019-01-14 22:27:32 +0000397begin to compile it. LLVM is distributed as a number of different
398subprojects. Each one has its own download which is a TAR archive that is
399compressed with the gzip program.
Bill Wendling954e4302012-10-07 07:10:13 +0000400
401The files are as follows, with *x.y* marking the version number:
402
403``llvm-x.y.tar.gz``
404
405 Source release for the LLVM libraries and tools.
406
James Y Knight78655882019-01-14 22:27:32 +0000407``cfe-x.y.tar.gz``
Bill Wendling954e4302012-10-07 07:10:13 +0000408
James Y Knight78655882019-01-14 22:27:32 +0000409 Source release for the Clang frontend.
Bill Wendling954e4302012-10-07 07:10:13 +0000410
Sean Silva8e68b792013-01-10 06:39:37 +0000411.. _checkout:
412
James Y Knight78655882019-01-14 22:27:32 +0000413Checkout LLVM from Git
414----------------------
Bill Wendling954e4302012-10-07 07:10:13 +0000415
James Y Knight78655882019-01-14 22:27:32 +0000416You can also checkout the source code for LLVM from Git. While the LLVM
417project's official source-code repository is Subversion, we are in the process
418of migrating to git. We currently recommend that all developers use Git for
419day-to-day development.
420
421.. note::
422
423 Passing ``--config core.autocrlf=false`` should not be required in
424 the future after we adjust the .gitattribute settings correctly, but
425 is required for Windows users at the time of this writing.
426
427Simply run:
428
429.. code-block:: console
430
Douglas Yungabfa98c2019-01-24 01:22:32 +0000431 % git clone https://github.com/llvm/llvm-project.git
James Y Knight78655882019-01-14 22:27:32 +0000432
433or on Windows,
434
435.. code-block:: console
436
437 % git clone --config core.autocrlf=false https://github.com/llvm/llvm-project.git
438
439This will create an '``llvm-project``' directory in the current directory and
440fully populate it with all of the source code, test directories, and local
441copies of documentation files for LLVM and all the related subprojects. Note
442that unlike the tarballs, which contain each subproject in a separate file, the
443git repository contains all of the projects together.
444
445If you want to get a specific release (as opposed to the most recent revision),
446you can check out a tag after cloning the repository. E.g., `git checkout
447llvmorg-6.0.1` inside the ``llvm-project`` directory created by the above
448command. Use `git tag -l` to list all of them.
449
450Sending patches
451^^^^^^^^^^^^^^^
452
453Please read `Developer Policy <DeveloperPolicy.html#one-off-patches>`_, too.
454
455We don't currently accept github pull requests, so you'll need to send patches
456either via emailing to llvm-commits, or, preferably, via :ref:`Phabricator
457<phabricator-reviews>`.
458
459You'll generally want to make sure your branch has a single commit,
460corresponding to the review you wish to send, up-to-date with the upstream
461``origin/master`` branch, and doesn't contain merges. Once you have that, you
462can use ``git show`` or ``git format-patch`` to output the diff, and attach it
463to a Phabricator review (or to an email message).
464
465However, using the "Arcanist" tool is often easier. After `installing
466arcanist`_, you can upload the latest commit using:
467
468.. code-block:: console
469
470 % arc diff HEAD~1
471
472Additionally, before sending a patch for review, please also try to ensure it's
473formatted properly. We use ``clang-format`` for this, which has git integration
474through the ``git-clang-format`` script. On some systems, it may already be
475installed (or be installable via your package manager). If so, you can simply
476run it -- the following command will format only the code changed in the most
477recent commit:
478
479.. code-block:: console
480
481 % git clang-format HEAD~1
482
483Note that this modifies the files, but doesn't commit them -- you'll likely want
484to run
485
486.. code-block:: console
487
488 % git commit --amend -a
489
490in order to update the last commit with all pending changes.
491
492.. note::
493 If you don't already have ``clang-format`` or ``git clang-format`` installed
494 on your system, the ``clang-format`` binary will be built alongside clang, and
495 the git integration can be run from
496 ``clang/tools/clang-format/git-clang-format``.
497
498
499.. _commit_from_git:
500
501For developers to commit changes from Git
502^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
503
504A helper script is provided in ``llvm/utils/git-svn/git-llvm``. After you add it
505to your path, you can push committed changes upstream with ``git llvm
506push``. While this creates a Subversion checkout and patches it under the hood,
507it does not require you to have interaction with it.
508
509.. code-block:: console
510
511 % export PATH=$PATH:$TOP_LEVEL_DIR/llvm-project/llvm/utils/git-svn/
512 % git llvm push
513
514Within a couple minutes after pushing to subversion, the svn commit will have
515been converted back to a Git commit, and made its way into the official Git
516repository. At that point, ``git pull`` should get back the changes as they were
517committed.
518
519You'll likely want to ``git pull --rebase`` to get the official git commit
520downloaded back to your repository. The SVN revision numbers of each commit can
521be found at the end of the commit message, e.g. ``llvm-svn: 350914``.
522
523You may also find the ``-n`` flag useful, like ``git llvm push -n``. This runs
524through all the steps of committing _without_ actually doing the commit, and
525tell you what it would have done. That can be useful if you're unsure whether
526the right thing will happen.
527
Jordan Rupprecht13d16b92019-06-10 17:00:49 +0000528Reverting a change when using Git
529^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
530
531If you're using Git and need to revert a patch, Git needs to be supplied a
532commit hash, not an svn revision. To make things easier, you can use
533``git llvm revert`` to revert with either an SVN revision or a Git hash instead.
534
535Additionally, you can first run with ``git llvm revert -n`` to print which Git
536commands will run, without doing anything.
537
538Running ``git llvm revert`` will only revert things in your local repository. To
539push the revert upstream, you still need to run ``git llvm push`` as described
540earlier.
541
542.. code-block:: console
543
544 % git llvm revert rNNNNNN # Revert by SVN id
545 % git llvm revert abcdef123456 # Revert by Git commit hash
546 % git llvm revert -n rNNNNNN # Print the commands without doing anything
547
James Y Knight78655882019-01-14 22:27:32 +0000548Checkout via SVN (deprecated)
549^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
550
551Until we have fully migrated to Git, you may also get a fresh copy of
552the code from the official Subversion repository.
Bill Wendling954e4302012-10-07 07:10:13 +0000553
554* ``cd where-you-want-llvm-to-live``
555* Read-Only: ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
Chris Bieneman9f611e32015-03-13 01:58:14 +0000556* Read-Write: ``svn co https://user@llvm.org/svn/llvm-project/llvm/trunk llvm``
Bill Wendling954e4302012-10-07 07:10:13 +0000557
558This will create an '``llvm``' directory in the current directory and fully
559populate it with the LLVM source code, Makefiles, test directories, and local
560copies of documentation files.
561
562If you want to get a specific release (as opposed to the most recent revision),
Hans Wennborg2c1e6822017-11-30 23:47:30 +0000563you can check it out from the '``tags``' directory (instead of '``trunk``'). The
Bill Wendling954e4302012-10-07 07:10:13 +0000564following releases are located in the following subdirectories of the '``tags``'
565directory:
566
Hans Wennborg2c1e6822017-11-30 23:47:30 +0000567* Release 3.5.0 and later: **RELEASE_350/final** and so on
568* Release 2.9 through 3.4: **RELEASE_29/final** and so on
569* Release 1.1 through 2.8: **RELEASE_11** and so on
Bill Wendling954e4302012-10-07 07:10:13 +0000570* Release 1.0: **RELEASE_1**
571
Bill Wendling954e4302012-10-07 07:10:13 +0000572Local LLVM Configuration
573------------------------
574
James Y Knight78655882019-01-14 22:27:32 +0000575Once checked out repository, the LLVM suite source code must be configured
576before being built. This process uses CMake. Unlinke the normal ``configure``
577script, CMake generates the build files in whatever format you request as well
578as various ``*.inc`` files, and ``llvm/include/Config/config.h``.
Bill Wendling954e4302012-10-07 07:10:13 +0000579
Chris Bieneman9f611e32015-03-13 01:58:14 +0000580Variables are passed to ``cmake`` on the command line using the format
581``-D<variable name>=<value>``. The following variables are some common options
582used by people developing LLVM.
Bill Wendling954e4302012-10-07 07:10:13 +0000583
Chris Bieneman9f611e32015-03-13 01:58:14 +0000584+-------------------------+----------------------------------------------------+
585| Variable | Purpose |
586+=========================+====================================================+
587| CMAKE_C_COMPILER | Tells ``cmake`` which C compiler to use. By |
588| | default, this will be /usr/bin/cc. |
589+-------------------------+----------------------------------------------------+
590| CMAKE_CXX_COMPILER | Tells ``cmake`` which C++ compiler to use. By |
591| | default, this will be /usr/bin/c++. |
592+-------------------------+----------------------------------------------------+
593| CMAKE_BUILD_TYPE | Tells ``cmake`` what type of build you are trying |
594| | to generate files for. Valid options are Debug, |
595| | Release, RelWithDebInfo, and MinSizeRel. Default |
596| | is Debug. |
597+-------------------------+----------------------------------------------------+
598| CMAKE_INSTALL_PREFIX | Specifies the install directory to target when |
599| | running the install action of the build files. |
600+-------------------------+----------------------------------------------------+
601| LLVM_TARGETS_TO_BUILD | A semicolon delimited list controlling which |
James Y Knight78655882019-01-14 22:27:32 +0000602| | targets will be built and linked into llvm. |
603| | The default list is defined as |
604| | ``LLVM_ALL_TARGETS``, and can be set to include |
Chris Bieneman9f611e32015-03-13 01:58:14 +0000605| | out-of-tree targets. The default value includes: |
James Y Knight0c145c02016-05-05 14:35:40 +0000606| | ``AArch64, AMDGPU, ARM, BPF, Hexagon, Mips, |
607| | MSP430, NVPTX, PowerPC, Sparc, SystemZ, X86, |
608| | XCore``. |
James Y Knight78655882019-01-14 22:27:32 +0000609| | |
Chris Bieneman9f611e32015-03-13 01:58:14 +0000610+-------------------------+----------------------------------------------------+
611| LLVM_ENABLE_DOXYGEN | Build doxygen-based documentation from the source |
612| | code This is disabled by default because it is |
613| | slow and generates a lot of output. |
614+-------------------------+----------------------------------------------------+
James Y Knight78655882019-01-14 22:27:32 +0000615| LLVM_ENABLE_PROJECTS | A semicolon-delimited list selecting which of the |
616| | other LLVM subprojects to additionally build. (Only|
617| | effective when using a side-by-side project layout |
618| | e.g. via git). The default list is empty. Can |
619| | include: clang, libcxx, libcxxabi, libunwind, lldb,|
620| | compiler-rt, lld, polly, or debuginfo-tests. |
621+-------------------------+----------------------------------------------------+
Chris Bieneman9f611e32015-03-13 01:58:14 +0000622| LLVM_ENABLE_SPHINX | Build sphinx-based documentation from the source |
623| | code. This is disabled by default because it is |
Jonathan Roelofs8d1ba892017-03-20 17:49:36 +0000624| | slow and generates a lot of output. Sphinx version |
625| | 1.5 or later recommended. |
Chris Bieneman9f611e32015-03-13 01:58:14 +0000626+-------------------------+----------------------------------------------------+
627| LLVM_BUILD_LLVM_DYLIB | Generate libLLVM.so. This library contains a |
628| | default set of LLVM components that can be |
629| | overridden with ``LLVM_DYLIB_COMPONENTS``. The |
630| | default contains most of LLVM and is defined in |
631| | ``tools/llvm-shlib/CMakelists.txt``. |
632+-------------------------+----------------------------------------------------+
633| LLVM_OPTIMIZED_TABLEGEN | Builds a release tablegen that gets used during |
634| | the LLVM build. This can dramatically speed up |
635| | debug builds. |
636+-------------------------+----------------------------------------------------+
Bill Wendling954e4302012-10-07 07:10:13 +0000637
Bill Wendling954e4302012-10-07 07:10:13 +0000638To configure LLVM, follow these steps:
639
640#. Change directory into the object root directory:
641
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000642 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000643
644 % cd OBJ_ROOT
645
Chris Bieneman9f611e32015-03-13 01:58:14 +0000646#. Run the ``cmake``:
Bill Wendling954e4302012-10-07 07:10:13 +0000647
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000648 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000649
Joel Galenson764b3d82018-07-30 15:14:24 +0000650 % cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/install/path
Chris Bieneman9f611e32015-03-13 01:58:14 +0000651 [other options] SRC_ROOT
Bill Wendling954e4302012-10-07 07:10:13 +0000652
653Compiling the LLVM Suite Source Code
654------------------------------------
655
Chris Bieneman9f611e32015-03-13 01:58:14 +0000656Unlike with autotools, with CMake your build type is defined at configuration.
657If you want to change your build type, you can re-run cmake with the following
658invocation:
Bill Wendling954e4302012-10-07 07:10:13 +0000659
Chris Bieneman9f611e32015-03-13 01:58:14 +0000660 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000661
Chris Bieneman9f611e32015-03-13 01:58:14 +0000662 % cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=type SRC_ROOT
Bill Wendling954e4302012-10-07 07:10:13 +0000663
Chris Bieneman9f611e32015-03-13 01:58:14 +0000664Between runs, CMake preserves the values set for all options. CMake has the
665following build types defined:
Bill Wendling954e4302012-10-07 07:10:13 +0000666
Chris Bieneman9f611e32015-03-13 01:58:14 +0000667Debug
Bill Wendling954e4302012-10-07 07:10:13 +0000668
Chris Bieneman9f611e32015-03-13 01:58:14 +0000669 These builds are the default. The build system will compile the tools and
670 libraries unoptimized, with debugging information, and asserts enabled.
Bill Wendling954e4302012-10-07 07:10:13 +0000671
Chris Bieneman9f611e32015-03-13 01:58:14 +0000672Release
673
674 For these builds, the build system will compile the tools and libraries
675 with optimizations enabled and not generate debug info. CMakes default
676 optimization level is -O3. This can be configured by setting the
677 ``CMAKE_CXX_FLAGS_RELEASE`` variable on the CMake command line.
678
679RelWithDebInfo
680
681 These builds are useful when debugging. They generate optimized binaries with
682 debug information. CMakes default optimization level is -O2. This can be
683 configured by setting the ``CMAKE_CXX_FLAGS_RELWITHDEBINFO`` variable on the
684 CMake command line.
Bill Wendling954e4302012-10-07 07:10:13 +0000685
686Once you have LLVM configured, you can build it by entering the *OBJ_ROOT*
687directory and issuing the following command:
688
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000689.. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000690
Chris Bieneman9f611e32015-03-13 01:58:14 +0000691 % make
Bill Wendling954e4302012-10-07 07:10:13 +0000692
693If the build fails, please `check here`_ to see if you are using a version of
694GCC that is known not to compile LLVM.
695
696If you have multiple processors in your machine, you may wish to use some of the
697parallel build options provided by GNU Make. For example, you could use the
698command:
699
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000700.. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000701
Chris Bieneman9f611e32015-03-13 01:58:14 +0000702 % make -j2
Bill Wendling954e4302012-10-07 07:10:13 +0000703
704There are several special targets which are useful when working with the LLVM
705source code:
706
Chris Bieneman9f611e32015-03-13 01:58:14 +0000707``make clean``
Bill Wendling954e4302012-10-07 07:10:13 +0000708
709 Removes all files generated by the build. This includes object files,
710 generated C/C++ files, libraries, and executables.
711
Chris Bieneman9f611e32015-03-13 01:58:14 +0000712``make install``
Bill Wendling954e4302012-10-07 07:10:13 +0000713
714 Installs LLVM header files, libraries, tools, and documentation in a hierarchy
Chris Bieneman9f611e32015-03-13 01:58:14 +0000715 under ``$PREFIX``, specified with ``CMAKE_INSTALL_PREFIX``, which
Bill Wendling954e4302012-10-07 07:10:13 +0000716 defaults to ``/usr/local``.
717
Chris Bieneman9f611e32015-03-13 01:58:14 +0000718``make docs-llvm-html``
Bill Wendling954e4302012-10-07 07:10:13 +0000719
Chris Bieneman9f611e32015-03-13 01:58:14 +0000720 If configured with ``-DLLVM_ENABLE_SPHINX=On``, this will generate a directory
721 at ``OBJ_ROOT/docs/html`` which contains the HTML formatted documentation.
Joel Jones8fe56e02012-11-15 21:15:08 +0000722
Bill Wendling954e4302012-10-07 07:10:13 +0000723Cross-Compiling LLVM
724--------------------
725
726It is possible to cross-compile LLVM itself. That is, you can create LLVM
727executables and libraries to be hosted on a platform different from the platform
Chris Bieneman9f611e32015-03-13 01:58:14 +0000728where they are built (a Canadian Cross build). To generate build files for
729cross-compiling CMake provides a variable ``CMAKE_TOOLCHAIN_FILE`` which can
730define compiler flags and variables used during the CMake test operations.
Bill Wendling954e4302012-10-07 07:10:13 +0000731
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000732The result of such a build is executables that are not runnable on the build
Chris Bieneman9f611e32015-03-13 01:58:14 +0000733host but can be executed on the target. As an example the following CMake
J. Ryan Stinnettd45eaf92019-05-30 16:46:22 +0000734invocation can generate build files targeting iOS. This will work on macOS
Chris Bieneman9f611e32015-03-13 01:58:14 +0000735with the latest Xcode:
736
737.. code-block:: console
738
Pete Cooperb51aafd2015-12-04 21:59:04 +0000739 % cmake -G "Ninja" -DCMAKE_OSX_ARCHITECTURES="armv7;armv7s;arm64"
Chris Bieneman9f611e32015-03-13 01:58:14 +0000740 -DCMAKE_TOOLCHAIN_FILE=<PATH_TO_LLVM>/cmake/platforms/iOS.cmake
741 -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_RUNTIME=Off -DLLVM_INCLUDE_TESTS=Off
742 -DLLVM_INCLUDE_EXAMPLES=Off -DLLVM_ENABLE_BACKTRACES=Off [options]
743 <PATH_TO_LLVM>
744
745Note: There are some additional flags that need to be passed when building for
746iOS due to limitations in the iOS SDK.
Bill Wendling954e4302012-10-07 07:10:13 +0000747
Renato Golin22a2d962013-09-26 08:57:07 +0000748Check :doc:`HowToCrossCompileLLVM` and `Clang docs on how to cross-compile in general
749<http://clang.llvm.org/docs/CrossCompilation.html>`_ for more information
750about cross-compiling.
751
Bill Wendling954e4302012-10-07 07:10:13 +0000752The Location of LLVM Object Files
753---------------------------------
754
755The LLVM build system is capable of sharing a single LLVM source tree among
756several LLVM builds. Hence, it is possible to build LLVM for several different
757platforms or configurations using the same source tree.
758
Bill Wendling954e4302012-10-07 07:10:13 +0000759* Change directory to where the LLVM object files should live:
760
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000761 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000762
763 % cd OBJ_ROOT
764
Chris Bieneman9f611e32015-03-13 01:58:14 +0000765* Run ``cmake``:
Bill Wendling954e4302012-10-07 07:10:13 +0000766
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000767 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000768
Chris Bieneman9f611e32015-03-13 01:58:14 +0000769 % cmake -G "Unix Makefiles" SRC_ROOT
Bill Wendling954e4302012-10-07 07:10:13 +0000770
Chris Bieneman9f611e32015-03-13 01:58:14 +0000771The LLVM build will create a structure underneath *OBJ_ROOT* that matches the
772LLVM source tree. At each level where source files are present in the source
773tree there will be a corresponding ``CMakeFiles`` directory in the *OBJ_ROOT*.
774Underneath that directory there is another directory with a name ending in
775``.dir`` under which you'll find object files for each source.
Bill Wendling954e4302012-10-07 07:10:13 +0000776
Chris Bieneman9f611e32015-03-13 01:58:14 +0000777For example:
Bill Wendling954e4302012-10-07 07:10:13 +0000778
Chris Bieneman9f611e32015-03-13 01:58:14 +0000779 .. code-block:: console
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000780
Chris Bieneman9f611e32015-03-13 01:58:14 +0000781 % cd llvm_build_dir
782 % find lib/Support/ -name APFloat*
783 lib/Support/CMakeFiles/LLVMSupport.dir/APFloat.cpp.o
Bill Wendling954e4302012-10-07 07:10:13 +0000784
785Optional Configuration Items
786----------------------------
787
788If you're running on a Linux system that supports the `binfmt_misc
Dmitri Gribenko51cb2fa2013-03-25 17:08:25 +0000789<http://en.wikipedia.org/wiki/binfmt_misc>`_
Bill Wendling954e4302012-10-07 07:10:13 +0000790module, and you have root access on the system, you can set your system up to
791execute LLVM bitcode files directly. To do this, use commands like this (the
792first command may not be required if you are already using the module):
793
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000794.. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000795
796 % mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
797 % echo ':llvm:M::BC::/path/to/lli:' > /proc/sys/fs/binfmt_misc/register
798 % chmod u+x hello.bc (if needed)
799 % ./hello.bc
800
801This allows you to execute LLVM bitcode files directly. On Debian, you can also
802use this command instead of the 'echo' command above:
803
Dmitri Gribenko125939cb2012-12-12 13:56:37 +0000804.. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +0000805
806 % sudo update-binfmts --install llvm /path/to/lli --magic 'BC'
807
808.. _Program Layout:
809.. _general layout:
810
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000811Directory Layout
812================
Bill Wendling954e4302012-10-07 07:10:13 +0000813
814One useful source of information about the LLVM source base is the LLVM `doxygen
Mitch Phillipsf65169842017-10-23 17:37:41 +0000815<http://www.doxygen.org/>`_ documentation available at
Bill Wendling954e4302012-10-07 07:10:13 +0000816`<http://llvm.org/doxygen/>`_. The following is a brief introduction to code
817layout:
818
819``llvm/examples``
820-----------------
821
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000822Simple examples using the LLVM IR and JIT.
Bill Wendling954e4302012-10-07 07:10:13 +0000823
824``llvm/include``
825----------------
826
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000827Public header files exported from the LLVM library. The three main subdirectories:
Bill Wendling954e4302012-10-07 07:10:13 +0000828
829``llvm/include/llvm``
830
Mitch Phillipsf65169842017-10-23 17:37:41 +0000831 All LLVM-specific header files, and subdirectories for different portions of
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000832 LLVM: ``Analysis``, ``CodeGen``, ``Target``, ``Transforms``, etc...
Bill Wendling954e4302012-10-07 07:10:13 +0000833
834``llvm/include/llvm/Support``
835
Mitch Phillipsf65169842017-10-23 17:37:41 +0000836 Generic support libraries provided with LLVM but not necessarily specific to
837 LLVM. For example, some C++ STL utilities and a Command Line option processing
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000838 library store header files here.
Bill Wendling954e4302012-10-07 07:10:13 +0000839
840``llvm/include/llvm/Config``
841
James Y Knight78655882019-01-14 22:27:32 +0000842 Header files configured by ``cmake``. They wrap "standard" UNIX and
843 C header files. Source code can include these header files which
844 automatically take care of the conditional #includes that ``cmake``
845 generates.
Bill Wendling954e4302012-10-07 07:10:13 +0000846
847``llvm/lib``
848------------
849
Mitch Phillipsf65169842017-10-23 17:37:41 +0000850Most source files are here. By putting code in libraries, LLVM makes it easy to
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000851share code among the `tools`_.
Bill Wendling954e4302012-10-07 07:10:13 +0000852
Seo Sanghyeona4fcf712014-10-29 05:20:39 +0000853``llvm/lib/IR/``
Bill Wendling954e4302012-10-07 07:10:13 +0000854
Mitch Phillipsf65169842017-10-23 17:37:41 +0000855 Core LLVM source files that implement core classes like Instruction and
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000856 BasicBlock.
Bill Wendling954e4302012-10-07 07:10:13 +0000857
858``llvm/lib/AsmParser/``
859
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000860 Source code for the LLVM assembly language parser library.
Bill Wendling954e4302012-10-07 07:10:13 +0000861
Michael Liaoe7fdb132013-06-11 18:09:21 +0000862``llvm/lib/Bitcode/``
Bill Wendling954e4302012-10-07 07:10:13 +0000863
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000864 Code for reading and writing bitcode.
Bill Wendling954e4302012-10-07 07:10:13 +0000865
866``llvm/lib/Analysis/``
867
Mitch Phillipsf65169842017-10-23 17:37:41 +0000868 A variety of program analyses, such as Call Graphs, Induction Variables,
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000869 Natural Loop Identification, etc.
Bill Wendling954e4302012-10-07 07:10:13 +0000870
871``llvm/lib/Transforms/``
872
Mitch Phillipsf65169842017-10-23 17:37:41 +0000873 IR-to-IR program transformations, such as Aggressive Dead Code Elimination,
874 Sparse Conditional Constant Propagation, Inlining, Loop Invariant Code Motion,
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000875 Dead Global Elimination, and many others.
Bill Wendling954e4302012-10-07 07:10:13 +0000876
877``llvm/lib/Target/``
878
Mitch Phillipsf65169842017-10-23 17:37:41 +0000879 Files describing target architectures for code generation. For example,
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000880 ``llvm/lib/Target/X86`` holds the X86 machine description.
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000881
Bill Wendling954e4302012-10-07 07:10:13 +0000882``llvm/lib/CodeGen/``
883
Mitch Phillipsf65169842017-10-23 17:37:41 +0000884 The major parts of the code generator: Instruction Selector, Instruction
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000885 Scheduling, and Register Allocation.
Bill Wendling954e4302012-10-07 07:10:13 +0000886
887``llvm/lib/MC/``
888
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000889 (FIXME: T.B.D.) ....?
Bill Wendling954e4302012-10-07 07:10:13 +0000890
891``llvm/lib/ExecutionEngine/``
892
Mitch Phillipsf65169842017-10-23 17:37:41 +0000893 Libraries for directly executing bitcode at runtime in interpreted and
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000894 JIT-compiled scenarios.
Bill Wendling954e4302012-10-07 07:10:13 +0000895
896``llvm/lib/Support/``
897
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000898 Source code that corresponding to the header files in ``llvm/include/ADT/``
899 and ``llvm/include/Support/``.
Bill Wendling954e4302012-10-07 07:10:13 +0000900
901``llvm/projects``
902-----------------
903
Mitch Phillipsf65169842017-10-23 17:37:41 +0000904Projects not strictly part of LLVM but shipped with LLVM. This is also the
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000905directory for creating your own LLVM-based projects which leverage the LLVM
906build system.
Bill Wendling954e4302012-10-07 07:10:13 +0000907
908``llvm/test``
909-------------
910
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000911Feature and regression tests and other sanity checks on LLVM infrastructure. These
912are intended to run quickly and cover a lot of territory without being exhaustive.
Bill Wendling954e4302012-10-07 07:10:13 +0000913
914``test-suite``
915--------------
916
James Y Knight78655882019-01-14 22:27:32 +0000917A comprehensive correctness, performance, and benchmarking test suite
918for LLVM. This comes in a ``separate git repository
919<https://github.com/llvm/llvm-test-suite>``, because it contains a
920large amount of third-party code under a variety of licenses. For
921details see the :doc:`Testing Guide <TestingGuide>` document.
Bill Wendling954e4302012-10-07 07:10:13 +0000922
923.. _tools:
924
925``llvm/tools``
926--------------
927
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000928Executables built out of the libraries
Bill Wendling954e4302012-10-07 07:10:13 +0000929above, which form the main part of the user interface. You can always get help
930for a tool by typing ``tool_name -help``. The following is a brief introduction
931to the most important tools. More detailed information is in
932the `Command Guide <CommandGuide/index.html>`_.
933
934``bugpoint``
935
936 ``bugpoint`` is used to debug optimization passes or code generation backends
937 by narrowing down the given test case to the minimum number of passes and/or
938 instructions that still cause a problem, whether it is a crash or
939 miscompilation. See `<HowToSubmitABug.html>`_ for more information on using
940 ``bugpoint``.
941
942``llvm-ar``
943
944 The archiver produces an archive containing the given LLVM bitcode files,
945 optionally with an index for faster lookup.
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000946
Bill Wendling954e4302012-10-07 07:10:13 +0000947``llvm-as``
948
949 The assembler transforms the human readable LLVM assembly to LLVM bitcode.
950
951``llvm-dis``
952
953 The disassembler transforms the LLVM bitcode to human readable LLVM assembly.
954
955``llvm-link``
956
957 ``llvm-link``, not surprisingly, links multiple LLVM modules into a single
958 program.
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +0000959
Bill Wendling954e4302012-10-07 07:10:13 +0000960``lli``
961
962 ``lli`` is the LLVM interpreter, which can directly execute LLVM bitcode
963 (although very slowly...). For architectures that support it (currently x86,
964 Sparc, and PowerPC), by default, ``lli`` will function as a Just-In-Time
965 compiler (if the functionality was compiled in), and will execute the code
966 *much* faster than the interpreter.
967
968``llc``
969
970 ``llc`` is the LLVM backend compiler, which translates LLVM bitcode to a
Rafael Espindolabed91eb2017-03-29 14:27:38 +0000971 native code assembly file.
Bill Wendling954e4302012-10-07 07:10:13 +0000972
973``opt``
974
975 ``opt`` reads LLVM bitcode, applies a series of LLVM to LLVM transformations
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000976 (which are specified on the command line), and outputs the resultant
977 bitcode. '``opt -help``' is a good way to get a list of the
Bill Wendling954e4302012-10-07 07:10:13 +0000978 program transformations available in LLVM.
979
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000980 ``opt`` can also run a specific analysis on an input LLVM bitcode
981 file and print the results. Primarily useful for debugging
Bill Wendling954e4302012-10-07 07:10:13 +0000982 analyses, or familiarizing yourself with what an analysis does.
983
984``llvm/utils``
985--------------
986
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000987Utilities for working with LLVM source code; some are part of the build process
988because they are code generators for parts of the infrastructure.
Bill Wendling954e4302012-10-07 07:10:13 +0000989
990
991``codegen-diff``
992
Philip Reames38a8a5fc2016-02-07 16:23:32 +0000993 ``codegen-diff`` finds differences between code that LLC
994 generates and code that LLI generates. This is useful if you are
Bill Wendling954e4302012-10-07 07:10:13 +0000995 debugging one of them, assuming that the other generates correct output. For
996 the full user manual, run ```perldoc codegen-diff'``.
997
998``emacs/``
999
Mitch Phillipsf65169842017-10-23 17:37:41 +00001000 Emacs and XEmacs syntax highlighting for LLVM assembly files and TableGen
Philip Reames38a8a5fc2016-02-07 16:23:32 +00001001 description files. See the ``README`` for information on using them.
Bill Wendling954e4302012-10-07 07:10:13 +00001002
1003``getsrcs.sh``
1004
Philip Reames38a8a5fc2016-02-07 16:23:32 +00001005 Finds and outputs all non-generated source files,
1006 useful if one wishes to do a lot of development across directories
1007 and does not want to find each file. One way to use it is to run,
1008 for example: ``xemacs `utils/getsources.sh``` from the top of the LLVM source
Bill Wendling954e4302012-10-07 07:10:13 +00001009 tree.
1010
1011``llvmgrep``
1012
Philip Reames38a8a5fc2016-02-07 16:23:32 +00001013 Performs an ``egrep -H -n`` on each source file in LLVM and
Bill Wendling954e4302012-10-07 07:10:13 +00001014 passes to it a regular expression provided on ``llvmgrep``'s command
Philip Reames38a8a5fc2016-02-07 16:23:32 +00001015 line. This is an efficient way of searching the source base for a
Bill Wendling954e4302012-10-07 07:10:13 +00001016 particular regular expression.
1017
Bill Wendling954e4302012-10-07 07:10:13 +00001018``TableGen/``
1019
Philip Reames38a8a5fc2016-02-07 16:23:32 +00001020 Contains the tool used to generate register
Bill Wendling954e4302012-10-07 07:10:13 +00001021 descriptions, instruction set descriptions, and even assemblers from common
1022 TableGen description files.
1023
1024``vim/``
1025
Philip Reames38a8a5fc2016-02-07 16:23:32 +00001026 vim syntax-highlighting for LLVM assembly files
1027 and TableGen description files. See the ``README`` for how to use them.
Bill Wendling954e4302012-10-07 07:10:13 +00001028
1029.. _simple example:
1030
1031An Example Using the LLVM Tool Chain
1032====================================
1033
1034This section gives an example of using LLVM with the Clang front end.
1035
1036Example with clang
1037------------------
1038
1039#. First, create a simple C file, name it 'hello.c':
1040
Sean Silva9ce5c062012-10-10 17:07:23 +00001041 .. code-block:: c
Bill Wendling954e4302012-10-07 07:10:13 +00001042
1043 #include <stdio.h>
1044
1045 int main() {
1046 printf("hello world\n");
1047 return 0;
1048 }
1049
1050#. Next, compile the C file into a native executable:
1051
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001052 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001053
1054 % clang hello.c -o hello
1055
1056 .. note::
1057
1058 Clang works just like GCC by default. The standard -S and -c arguments
1059 work as usual (producing a native .s or .o file, respectively).
1060
Daniel Dunbar06b9f9e2013-08-16 23:30:19 +00001061#. Next, compile the C file into an LLVM bitcode file:
Bill Wendling954e4302012-10-07 07:10:13 +00001062
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001063 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001064
1065 % clang -O3 -emit-llvm hello.c -c -o hello.bc
1066
1067 The -emit-llvm option can be used with the -S or -c options to emit an LLVM
1068 ``.ll`` or ``.bc`` file (respectively) for the code. This allows you to use
1069 the `standard LLVM tools <CommandGuide/index.html>`_ on the bitcode file.
1070
1071#. Run the program in both forms. To run the program, use:
1072
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001073 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001074
1075 % ./hello
Mehdi Aminifb6fd5c2015-08-03 23:17:44 +00001076
Bill Wendling954e4302012-10-07 07:10:13 +00001077 and
1078
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001079 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001080
1081 % lli hello.bc
1082
Dmitri Gribenko162c5242012-11-18 10:32:14 +00001083 The second examples shows how to invoke the LLVM JIT, :doc:`lli
1084 <CommandGuide/lli>`.
Bill Wendling954e4302012-10-07 07:10:13 +00001085
1086#. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code:
1087
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001088 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001089
1090 % llvm-dis < hello.bc | less
1091
1092#. Compile the program to native assembly using the LLC code generator:
1093
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001094 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001095
1096 % llc hello.bc -o hello.s
1097
1098#. Assemble the native assembly language file into a program:
1099
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001100 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001101
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001102 % /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.native # On Solaris
Bill Wendling954e4302012-10-07 07:10:13 +00001103
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001104 % gcc hello.s -o hello.native # On others
Bill Wendling954e4302012-10-07 07:10:13 +00001105
1106#. Execute the native code program:
1107
Dmitri Gribenko125939cb2012-12-12 13:56:37 +00001108 .. code-block:: console
Bill Wendling954e4302012-10-07 07:10:13 +00001109
1110 % ./hello.native
1111
1112 Note that using clang to compile directly to native code (i.e. when the
1113 ``-emit-llvm`` option is not present) does steps 6/7/8 for you.
1114
1115Common Problems
1116===============
1117
1118If you are having problems building or using LLVM, or if you have any other
1119general questions about LLVM, please consult the `Frequently Asked
1120Questions <FAQ.html>`_ page.
1121
1122.. _links:
1123
1124Links
1125=====
1126
1127This document is just an **introduction** on how to use LLVM to do some simple
1128things... there are many more interesting and complicated things that you can do
1129that aren't documented here (but we'll gladly accept a patch if you want to
1130write something up!). For more information about LLVM, check out:
1131
1132* `LLVM Homepage <http://llvm.org/>`_
1133* `LLVM Doxygen Tree <http://llvm.org/doxygen/>`_
1134* `Starting a Project that Uses LLVM <http://llvm.org/docs/Projects.html>`_
James Y Knight78655882019-01-14 22:27:32 +00001135
1136.. _installing arcanist: https://secure.phabricator.com/book/phabricator/article/arcanist_quick_start/