blob: cbf8f5012b908df4d76a87e14d3892576d57d24a [file] [log] [blame]
Bill Wendlinga84e2df2012-06-19 09:18:34 +00001========================
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
Bill Wendlingf06ac972012-06-19 09:23:23 +000018 * ``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
Bill Wendlinga84e2df2012-06-19 09:18:34 +000025 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
Rafael Espindola730df072014-03-12 22:40:22 +000040If 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``.
Bill Wendlinga84e2df2012-06-19 09:18:34 +000043
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
Rafael Espindola730df072014-03-12 22:40:22 +000049your source tree layout to look similar to the LLVM source tree layout.
Bill Wendlinga84e2df2012-06-19 09:18:34 +000050
51Underneath your top level directory, you should have the following directories:
52
Bill Wendlingf06ac972012-06-19 09:23:23 +000053**lib**
Bill Wendlinga84e2df2012-06-19 09:18:34 +000054
55 This subdirectory should contain all of your library source code. For each
Bill Wendlingf06ac972012-06-19 09:23:23 +000056 library that you build, you will have one directory in **lib** that will
Bill Wendlinga84e2df2012-06-19 09:18:34 +000057 contain that library's source code.
58
Bill Wendlingf06ac972012-06-19 09:23:23 +000059 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.
Bill Wendlinga84e2df2012-06-19 09:18:34 +000062
Bill Wendlingf06ac972012-06-19 09:23:23 +000063**include**
Bill Wendlinga84e2df2012-06-19 09:18:34 +000064
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
Bill Wendlingf06ac972012-06-19 09:23:23 +000069 By placing your header files in **include**, they will be found
Bill Wendlinga84e2df2012-06-19 09:18:34 +000070 automatically by the LLVM build system. For example, if you have a file
Bill Wendlingf06ac972012-06-19 09:23:23 +000071 **include/jazz/note.h**, then your source files can include it simply with
72 **#include "jazz/note.h"**.
Bill Wendlinga84e2df2012-06-19 09:18:34 +000073
Bill Wendlingf06ac972012-06-19 09:23:23 +000074**tools**
Bill Wendlinga84e2df2012-06-19 09:18:34 +000075
76 This subdirectory should contain all of your source code for executables.
Bill Wendlingf06ac972012-06-19 09:23:23 +000077 For each program that you build, you will have one directory in **tools**
78 that will contain that program's source code.
Bill Wendlinga84e2df2012-06-19 09:18:34 +000079
Bill Wendlingf06ac972012-06-19 09:23:23 +000080**test**
Bill Wendlinga84e2df2012-06-19 09:18:34 +000081
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
Dmitri Gribenko09a682a2013-01-18 19:27:43 +000088* 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``
Bill Wendlinga84e2df2012-06-19 09:18:34 +000090 lines in the actual test case to determine how to run the test. See the
Dmitri Gribenko09a682a2013-01-18 19:27:43 +000091 :doc:`TestingGuide` for more details.
Bill Wendlinga84e2df2012-06-19 09:18:34 +000092
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.
Shao-Ce SUN0c660252021-11-15 09:17:08 +080097
Bill Wendlinga84e2df2012-06-19 09:18:34 +000098 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
Bill Wendlingf06ac972012-06-19 09:23:23 +0000102Typically, you will want to build your **lib** directory first followed by your
103**tools** directory.
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000104
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
Bill Wendlingf06ac972012-06-19 09:23:23 +0000175 your **lib** directory. The libraries must be specified without their
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000176 ``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
Bill Wendlingfb370512012-06-19 17:43:57 +0000206 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.
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000209
210 For example, to link ``libsample.so``, you would have the following line in
211 your ``Makefile``:
212
Bill Wendling34ffc892012-06-19 22:25:17 +0000213 .. code-block:: makefile
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000214
Bill Wendling7f0d23e2012-06-19 17:48:06 +0000215 LIBS += -lsample
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000216
217Note that ``LIBS`` must occur in the Makefile after the inclusion of
218``Makefile.common``.
219
220Miscellaneous Variables
221-----------------------
222
Bill Wendling6bf44172012-06-19 09:29:05 +0000223``CFLAGS`` & ``CPPFLAGS``
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000224
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
Quinn Phama712b662021-11-18 14:13:03 -0600230 opposed to overwriting them. The LLVM ``Makefiles`` may already have
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000231 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
Bill Wendlingf0d91f32012-06-19 09:27:54 +0000242 ``PROJ_OBJ_ROOT/<type>/lib``, where *type* is ``Debug``, ``Release``, or
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000243 ``Profile`` for a debug, optimized, or profiled build, respectively.
244
245Executables
246
Bill Wendlingf0d91f32012-06-19 09:27:54 +0000247 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.
Bill Wendlinga84e2df2012-06-19 09:18:34 +0000250
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
tlattner520d29f2022-07-28 16:54:38 -0700256the `Discourse forums
257<https://discourse.llvm.org>`_.