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