Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 1 | ======================== |
| 2 | Creating an LLVM Project |
| 3 | ======================== |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | Overview |
| 9 | ======== |
| 10 | |
| 11 | The LLVM build system is designed to facilitate the building of third party |
| 12 | projects that use LLVM header files, libraries, and tools. In order to use |
| 13 | these 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 Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 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 |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 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 | |
| 31 | There 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 Espindola | 730df07 | 2014-03-12 22:40:22 +0000 | [diff] [blame] | 40 | If 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 Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 43 | |
| 44 | Source Tree Layout |
| 45 | ================== |
| 46 | |
| 47 | In order to use the LLVM build system, you will want to organize your source |
| 48 | code so that it can benefit from the build system's features. Mainly, you want |
Rafael Espindola | 730df07 | 2014-03-12 22:40:22 +0000 | [diff] [blame] | 49 | your source tree layout to look similar to the LLVM source tree layout. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 50 | |
| 51 | Underneath your top level directory, you should have the following directories: |
| 52 | |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 53 | **lib** |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 54 | |
| 55 | This subdirectory should contain all of your library source code. For each |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 56 | library that you build, you will have one directory in **lib** that will |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 57 | contain that library's source code. |
| 58 | |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 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. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 62 | |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 63 | **include** |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 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 | |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 69 | By placing your header files in **include**, they will be found |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 70 | automatically by the LLVM build system. For example, if you have a file |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 71 | **include/jazz/note.h**, then your source files can include it simply with |
| 72 | **#include "jazz/note.h"**. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 73 | |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 74 | **tools** |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 75 | |
| 76 | This subdirectory should contain all of your source code for executables. |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 77 | For each program that you build, you will have one directory in **tools** |
| 78 | that will contain that program's source code. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 79 | |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 80 | **test** |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 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 | |
Dmitri Gribenko | 09a682a | 2013-01-18 19:27:43 +0000 | [diff] [blame] | 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`` |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 90 | lines in the actual test case to determine how to run the test. See the |
Dmitri Gribenko | 09a682a | 2013-01-18 19:27:43 +0000 | [diff] [blame] | 91 | :doc:`TestingGuide` for more details. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 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. |
Shao-Ce SUN | 0c66025 | 2021-11-15 09:17:08 +0800 | [diff] [blame] | 97 | |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 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 | |
Bill Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 102 | Typically, you will want to build your **lib** directory first followed by your |
| 103 | **tools** directory. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 104 | |
| 105 | Writing LLVM Style Makefiles |
| 106 | ============================ |
| 107 | |
| 108 | The LLVM build system provides a convenient way to build libraries and |
| 109 | executables. Most of your project Makefiles will only need to define a few |
| 110 | variables. Below is a list of the variables one can set and what they can |
| 111 | do: |
| 112 | |
| 113 | Required 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 | |
| 123 | Variables 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 | |
| 142 | Variables 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 | |
| 162 | Variables 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 Wendling | f06ac97 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 175 | your **lib** directory. The libraries must be specified without their |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 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 | |
Bill Wendling | fb37051 | 2012-06-19 17:43:57 +0000 | [diff] [blame] | 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. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 209 | |
| 210 | For example, to link ``libsample.so``, you would have the following line in |
| 211 | your ``Makefile``: |
| 212 | |
Bill Wendling | 34ffc89 | 2012-06-19 22:25:17 +0000 | [diff] [blame] | 213 | .. code-block:: makefile |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 214 | |
Bill Wendling | 7f0d23e | 2012-06-19 17:48:06 +0000 | [diff] [blame] | 215 | LIBS += -lsample |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 216 | |
| 217 | Note that ``LIBS`` must occur in the Makefile after the inclusion of |
| 218 | ``Makefile.common``. |
| 219 | |
| 220 | Miscellaneous Variables |
| 221 | ----------------------- |
| 222 | |
Bill Wendling | 6bf4417 | 2012-06-19 09:29:05 +0000 | [diff] [blame] | 223 | ``CFLAGS`` & ``CPPFLAGS`` |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 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 |
Quinn Pham | a712b66 | 2021-11-18 14:13:03 -0600 | [diff] [blame] | 230 | opposed to overwriting them. The LLVM ``Makefiles`` may already have |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 231 | useful options in them that you may not want to overwrite. |
| 232 | |
| 233 | Placement of Object Code |
| 234 | ======================== |
| 235 | |
| 236 | The final location of built libraries and executables will depend upon whether |
| 237 | you do a ``Debug``, ``Release``, or ``Profile`` build. |
| 238 | |
| 239 | Libraries |
| 240 | |
| 241 | All libraries (static and dynamic) will be stored in |
Bill Wendling | f0d91f3 | 2012-06-19 09:27:54 +0000 | [diff] [blame] | 242 | ``PROJ_OBJ_ROOT/<type>/lib``, where *type* is ``Debug``, ``Release``, or |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 243 | ``Profile`` for a debug, optimized, or profiled build, respectively. |
| 244 | |
| 245 | Executables |
| 246 | |
Bill Wendling | f0d91f3 | 2012-06-19 09:27:54 +0000 | [diff] [blame] | 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. |
Bill Wendling | a84e2df | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 250 | |
| 251 | Further Help |
| 252 | ============ |
| 253 | |
| 254 | If you have any questions or need any help creating an LLVM project, the LLVM |
| 255 | team would be more than happy to help. You can always post your questions to |
tlattner | 520d29f | 2022-07-28 16:54:38 -0700 | [diff] [blame] | 256 | the `Discourse forums |
| 257 | <https://discourse.llvm.org>`_. |