GettingStarted.rst 44 KB

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