1========================
2Creating an LLVM Project
3========================
4
5.. contents::
6   :local:
7
8Overview
9========
10
11The LLVM build system is designed to facilitate the building of third party
12projects that use LLVM header files, libraries, and tools.  In order to use
13these facilities, a ``Makefile`` from a project must do the following things:
14
15* Set ``make`` variables. There are several variables that a ``Makefile`` needs
16  to set to use the LLVM build system:
17
18  * ``PROJECT_NAME`` - The name by which your project is known.
19  * ``LLVM_SRC_ROOT`` - The root of the LLVM source tree.
20  * ``LLVM_OBJ_ROOT`` - The root of the LLVM object tree.
21  * ``PROJ_SRC_ROOT`` - The root of the project's source tree.
22  * ``PROJ_OBJ_ROOT`` - The root of the project's object tree.
23  * ``PROJ_INSTALL_ROOT`` - The root installation directory.
24  * ``LEVEL`` - The relative path from the current directory to the
25    project's root ``($PROJ_OBJ_ROOT)``.
26
27* Include ``Makefile.config`` from ``$(LLVM_OBJ_ROOT)``.
28
29* Include ``Makefile.rules`` from ``$(LLVM_SRC_ROOT)``.
30
31There are two ways that you can set all of these variables:
32
33* You can write your own ``Makefiles`` which hard-code these values.
34
35* You can use the pre-made LLVM sample project. This sample project includes
36  ``Makefiles``, a configure script that can be used to configure the location
37  of LLVM, and the ability to support multiple object directories from a single
38  source directory.
39
40If you want to devise your own build system, studying other projects and LLVM
41``Makefiles`` will probably provide enough information on how to write your own
42``Makefiles``.
43
44Source Tree Layout
45==================
46
47In order to use the LLVM build system, you will want to organize your source
48code so that it can benefit from the build system's features.  Mainly, you want
49your source tree layout to look similar to the LLVM source tree layout.
50
51Underneath your top level directory, you should have the following directories:
52
53**lib**
54
55    This subdirectory should contain all of your library source code.  For each
56    library that you build, you will have one directory in **lib** that will
57    contain that library's source code.
58
59    Libraries can be object files, archives, or dynamic libraries.  The **lib**
60    directory is just a convenient place for libraries as it places them all in
61    a directory from which they can be linked later.
62
63**include**
64
65    This subdirectory should contain any header files that are global to your
66    project. By global, we mean that they are used by more than one library or
67    executable of your project.
68
69    By placing your header files in **include**, they will be found
70    automatically by the LLVM build system.  For example, if you have a file
71    **include/jazz/note.h**, then your source files can include it simply with
72    **#include "jazz/note.h"**.
73
74**tools**
75
76    This subdirectory should contain all of your source code for executables.
77    For each program that you build, you will have one directory in **tools**
78    that will contain that program's source code.
79
80**test**
81
82    This subdirectory should contain tests that verify that your code works
83    correctly.  Automated tests are especially useful.
84
85    Currently, the LLVM build system provides basic support for tests. The LLVM
86    system provides the following:
87
88* LLVM contains regression tests in ``llvm/test``.  These tests are run by the
89  :doc:`Lit <CommandGuide/lit>` testing tool.  This test procedure uses ``RUN``
90  lines in the actual test case to determine how to run the test.  See the
91  :doc:`TestingGuide` for more details.
92
93* LLVM contains an optional package called ``llvm-test``, which provides
94  benchmarks and programs that are known to compile with the Clang front
95  end. You can use these programs to test your code, gather statistical
96  information, and compare it to the current LLVM performance statistics.
97
98  Currently, there is no way to hook your tests directly into the ``llvm/test``
99  testing harness. You will simply need to find a way to use the source
100  provided within that directory on your own.
101
102Typically, you will want to build your **lib** directory first followed by your
103**tools** directory.
104
105Writing LLVM Style Makefiles
106============================
107
108The LLVM build system provides a convenient way to build libraries and
109executables.  Most of your project Makefiles will only need to define a few
110variables.  Below is a list of the variables one can set and what they can
111do:
112
113Required Variables
114------------------
115
116``LEVEL``
117
118    This variable is the relative path from this ``Makefile`` to the top
119    directory of your project's source code.  For example, if your source code
120    is in ``/tmp/src``, then the ``Makefile`` in ``/tmp/src/jump/high``
121    would set ``LEVEL`` to ``"../.."``.
122
123Variables for Building Subdirectories
124-------------------------------------
125
126``DIRS``
127
128    This is a space separated list of subdirectories that should be built.  They
129    will be built, one at a time, in the order specified.
130
131``PARALLEL_DIRS``
132
133    This is a list of directories that can be built in parallel. These will be
134    built after the directories in DIRS have been built.
135
136``OPTIONAL_DIRS``
137
138    This is a list of directories that can be built if they exist, but will not
139    cause an error if they do not exist.  They are built serially in the order
140    in which they are listed.
141
142Variables for Building Libraries
143--------------------------------
144
145``LIBRARYNAME``
146
147    This variable contains the base name of the library that will be built.  For
148    example, to build a library named ``libsample.a``, ``LIBRARYNAME`` should
149    be set to ``sample``.
150
151``BUILD_ARCHIVE``
152
153    By default, a library is a ``.o`` file that is linked directly into a
154    program.  To build an archive (also known as a static library), set the
155    ``BUILD_ARCHIVE`` variable.
156
157``SHARED_LIBRARY``
158
159    If ``SHARED_LIBRARY`` is defined in your Makefile, a shared (or dynamic)
160    library will be built.
161
162Variables for Building Programs
163-------------------------------
164
165``TOOLNAME``
166
167    This variable contains the name of the program that will be built.  For
168    example, to build an executable named ``sample``, ``TOOLNAME`` should be set
169    to ``sample``.
170
171``USEDLIBS``
172
173    This variable holds a space separated list of libraries that should be
174    linked into the program.  These libraries must be libraries that come from
175    your **lib** directory.  The libraries must be specified without their
176    ``lib`` prefix.  For example, to link ``libsample.a``, you would set
177    ``USEDLIBS`` to ``sample.a``.
178
179    Note that this works only for statically linked libraries.
180
181``LLVMLIBS``
182
183    This variable holds a space separated list of libraries that should be
184    linked into the program.  These libraries must be LLVM libraries.  The
185    libraries must be specified without their ``lib`` prefix.  For example, to
186    link with a driver that performs an IR transformation you might set
187    ``LLVMLIBS`` to this minimal set of libraries ``LLVMSupport.a LLVMCore.a
188    LLVMBitReader.a LLVMAsmParser.a LLVMAnalysis.a LLVMTransformUtils.a
189    LLVMScalarOpts.a LLVMTarget.a``.
190
191    Note that this works only for statically linked libraries. LLVM is split
192    into a large number of static libraries, and the list of libraries you
193    require may be much longer than the list above. To see a full list of
194    libraries use: ``llvm-config --libs all``.  Using ``LINK_COMPONENTS`` as
195    described below, obviates the need to set ``LLVMLIBS``.
196
197``LINK_COMPONENTS``
198
199    This variable holds a space separated list of components that the LLVM
200    ``Makefiles`` pass to the ``llvm-config`` tool to generate a link line for
201    the program. For example, to link with all LLVM libraries use
202    ``LINK_COMPONENTS = all``.
203
204``LIBS``
205
206    To link dynamic libraries, add ``-l<library base name>`` to the ``LIBS``
207    variable.  The LLVM build system will look in the same places for dynamic
208    libraries as it does for static libraries.
209
210    For example, to link ``libsample.so``, you would have the following line in
211    your ``Makefile``:
212
213        .. code-block:: makefile
214
215          LIBS += -lsample
216
217Note that ``LIBS`` must occur in the Makefile after the inclusion of
218``Makefile.common``.
219
220Miscellaneous Variables
221-----------------------
222
223``CFLAGS`` & ``CPPFLAGS``
224
225    This variable can be used to add options to the C and C++ compiler,
226    respectively.  It is typically used to add options that tell the compiler
227    the location of additional directories to search for header files.
228
229    It is highly suggested that you append to ``CFLAGS`` and ``CPPFLAGS`` as
230    opposed to overwriting them.  The master ``Makefiles`` may already have
231    useful options in them that you may not want to overwrite.
232
233Placement of Object Code
234========================
235
236The final location of built libraries and executables will depend upon whether
237you do a ``Debug``, ``Release``, or ``Profile`` build.
238
239Libraries
240
241    All libraries (static and dynamic) will be stored in
242    ``PROJ_OBJ_ROOT/<type>/lib``, where *type* is ``Debug``, ``Release``, or
243    ``Profile`` for a debug, optimized, or profiled build, respectively.
244
245Executables
246
247    All executables will be stored in ``PROJ_OBJ_ROOT/<type>/bin``, where *type*
248    is ``Debug``, ``Release``, or ``Profile`` for a debug, optimized, or
249    profiled build, respectively.
250
251Further Help
252============
253
254If you have any questions or need any help creating an LLVM project, the LLVM
255team would be more than happy to help.  You can always post your questions to
256the `LLVM Developers Mailing List
257<http://lists.llvm.org/pipermail/llvm-dev/>`_.
258