258 lines · plain
1========================2Creating an LLVM Project3========================4 5.. contents::6 :local:7 8Overview9========10 11The LLVM build system is designed to facilitate the building of third party12projects that use LLVM header files, libraries, and tools. In order to use13these facilities, a ``Makefile`` from a project must do the following things:14 15* Set ``make`` variables. There are several variables that a ``Makefile`` needs16 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 the25 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 includes36 ``Makefiles``, a configure script that can be used to configure the location37 of LLVM, and the ability to support multiple object directories from a single38 source directory.39 40If you want to devise your own build system, studying other projects and LLVM41``Makefiles`` will probably provide enough information on how to write your own42``Makefiles``.43 44Source Tree Layout45==================46 47In order to use the LLVM build system, you will want to organize your source48code so that it can benefit from the build system's features. Mainly, you want49your 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 each56 library that you build, you will have one directory in **lib** that will57 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 in61 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 your66 project. By global, we mean that they are used by more than one library or67 executable of your project.68 69 By placing your header files in **include**, they will be found70 automatically by the LLVM build system. For example, if you have a file71 **include/jazz/note.h**, then your source files can include it simply with72 **#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 works83 correctly. Automated tests are especially useful.84 85 Currently, the LLVM build system provides basic support for tests. The LLVM86 system provides the following:87 88* LLVM contains regression tests in ``llvm/test``. These tests are run by the89 :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 the91 :doc:`TestingGuide` for more details.92 93* LLVM contains an optional package called ``llvm-test``, which provides94 benchmarks and programs that are known to compile with the Clang front95 end. You can use these programs to test your code, gather statistical96 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 source100 provided within that directory on your own.101 102Typically, you will want to build your **lib** directory first followed by your103**tools** directory.104 105Writing LLVM Style Makefiles106============================107 108The LLVM build system provides a convenient way to build libraries and109executables. Most of your project Makefiles will only need to define a few110variables. Below is a list of the variables one can set and what they can111do:112 113Required Variables114------------------115 116``LEVEL``117 118 This variable is the relative path from this ``Makefile`` to the top119 directory of your project's source code. For example, if your source code120 is in ``/tmp/src``, then the ``Makefile`` in ``/tmp/src/jump/high``121 would set ``LEVEL`` to ``"../.."``.122 123Variables for Building Subdirectories124-------------------------------------125 126``DIRS``127 128 This is a space separated list of subdirectories that should be built. They129 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 be134 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 not139 cause an error if they do not exist. They are built serially in the order140 in which they are listed.141 142Variables for Building Libraries143--------------------------------144 145``LIBRARYNAME``146 147 This variable contains the base name of the library that will be built. For148 example, to build a library named ``libsample.a``, ``LIBRARYNAME`` should149 be set to ``sample``.150 151``BUILD_ARCHIVE``152 153 By default, a library is a ``.o`` file that is linked directly into a154 program. To build an archive (also known as a static library), set the155 ``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 Programs163-------------------------------164 165``TOOLNAME``166 167 This variable contains the name of the program that will be built. For168 example, to build an executable named ``sample``, ``TOOLNAME`` should be set169 to ``sample``.170 171``USEDLIBS``172 173 This variable holds a space separated list of libraries that should be174 linked into the program. These libraries must be libraries that come from175 your **lib** directory. The libraries must be specified without their176 ``lib`` prefix. For example, to link ``libsample.a``, you would set177 ``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 be184 linked into the program. These libraries must be LLVM libraries. The185 libraries must be specified without their ``lib`` prefix. For example, to186 link with a driver that performs an IR transformation you might set187 ``LLVMLIBS`` to this minimal set of libraries ``LLVMSupport.a LLVMCore.a188 LLVMBitReader.a LLVMAsmParser.a LLVMAnalysis.a LLVMTransformUtils.a189 LLVMScalarOpts.a LLVMTarget.a``.190 191 Note that this works only for statically linked libraries. LLVM is split192 into a large number of static libraries, and the list of libraries you193 require may be much longer than the list above. To see a full list of194 libraries use: ``llvm-config --libs all``. Using ``LINK_COMPONENTS`` as195 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 LLVM200 ``Makefiles`` pass to the ``llvm-config`` tool to generate a link line for201 the program. For example, to link with all LLVM libraries use202 ``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 dynamic208 libraries as it does for static libraries.209 210 For example, to link ``libsample.so``, you would have the following line in211 your ``Makefile``:212 213 .. code-block:: makefile214 215 LIBS += -lsample216 217Note that ``LIBS`` must occur in the Makefile after the inclusion of218``Makefile.common``.219 220Miscellaneous Variables221-----------------------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 compiler227 the location of additional directories to search for header files.228 229 It is highly suggested that you append to ``CFLAGS`` and ``CPPFLAGS`` as230 opposed to overwriting them. The LLVM ``Makefiles`` may already have231 useful options in them that you may not want to overwrite.232 233Placement of Object Code234========================235 236The final location of built libraries and executables will depend upon whether237you do a ``Debug``, ``Release``, or ``Profile`` build.238 239Libraries240 241 All libraries (static and dynamic) will be stored in242 ``PROJ_OBJ_ROOT/<type>/lib``, where *type* is ``Debug``, ``Release``, or243 ``Profile`` for a debug, optimized, or profiled build, respectively.244 245Executables246 247 All executables will be stored in ``PROJ_OBJ_ROOT/<type>/bin``, where *type*248 is ``Debug``, ``Release``, or ``Profile`` for a debug, optimized, or249 profiled build, respectively.250 251Further Help252============253 254If you have any questions or need any help creating an LLVM project, the LLVM255team would be more than happy to help. You can always post your questions to256the `Discourse forums257<https://discourse.llvm.org>`_.258