Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 1 | .. _projects: |
| 2 | |
| 3 | ======================== |
| 4 | Creating an LLVM Project |
| 5 | ======================== |
| 6 | |
| 7 | .. contents:: |
| 8 | :local: |
| 9 | |
| 10 | Overview |
| 11 | ======== |
| 12 | |
| 13 | The LLVM build system is designed to facilitate the building of third party |
| 14 | projects that use LLVM header files, libraries, and tools. In order to use |
| 15 | these facilities, a ``Makefile`` from a project must do the following things: |
| 16 | |
| 17 | * Set ``make`` variables. There are several variables that a ``Makefile`` needs |
| 18 | to set to use the LLVM build system: |
| 19 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 20 | * ``PROJECT_NAME`` - The name by which your project is known. |
| 21 | * ``LLVM_SRC_ROOT`` - The root of the LLVM source tree. |
| 22 | * ``LLVM_OBJ_ROOT`` - The root of the LLVM object tree. |
| 23 | * ``PROJ_SRC_ROOT`` - The root of the project's source tree. |
| 24 | * ``PROJ_OBJ_ROOT`` - The root of the project's object tree. |
| 25 | * ``PROJ_INSTALL_ROOT`` - The root installation directory. |
| 26 | * ``LEVEL`` - The relative path from the current directory to the |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 27 | project's root ``($PROJ_OBJ_ROOT)``. |
| 28 | |
| 29 | * Include ``Makefile.config`` from ``$(LLVM_OBJ_ROOT)``. |
| 30 | |
| 31 | * Include ``Makefile.rules`` from ``$(LLVM_SRC_ROOT)``. |
| 32 | |
| 33 | There are two ways that you can set all of these variables: |
| 34 | |
| 35 | * You can write your own ``Makefiles`` which hard-code these values. |
| 36 | |
| 37 | * You can use the pre-made LLVM sample project. This sample project includes |
| 38 | ``Makefiles``, a configure script that can be used to configure the location |
| 39 | of LLVM, and the ability to support multiple object directories from a single |
| 40 | source directory. |
| 41 | |
| 42 | This document assumes that you will base your project on the LLVM sample project |
| 43 | found in ``llvm/projects/sample``. If you want to devise your own build system, |
| 44 | studying the sample project and LLVM ``Makefiles`` will probably provide enough |
| 45 | information on how to write your own ``Makefiles``. |
| 46 | |
| 47 | Create a Project from the Sample Project |
| 48 | ======================================== |
| 49 | |
| 50 | Follow these simple steps to start your project: |
| 51 | |
Bill Wendling | ea2a7d7 | 2012-06-19 09:25:04 +0000 | [diff] [blame] | 52 | 1. Copy the ``llvm/projects/sample`` directory to any place of your choosing. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 53 | You can place it anywhere you like. Rename the directory to match the name |
| 54 | of your project. |
| 55 | |
Bill Wendling | ea2a7d7 | 2012-06-19 09:25:04 +0000 | [diff] [blame] | 56 | 2. If you downloaded LLVM using Subversion, remove all the directories named |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 57 | ``.svn`` (and all the files therein) from your project's new source tree. |
| 58 | This will keep Subversion from thinking that your project is inside |
| 59 | ``llvm/trunk/projects/sample``. |
| 60 | |
Bill Wendling | ea2a7d7 | 2012-06-19 09:25:04 +0000 | [diff] [blame] | 61 | 3. Add your source code and Makefiles to your source tree. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 62 | |
Bill Wendling | ea2a7d7 | 2012-06-19 09:25:04 +0000 | [diff] [blame] | 63 | 4. If you want your project to be configured with the ``configure`` script then |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 64 | you need to edit ``autoconf/configure.ac`` as follows: |
| 65 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 66 | * **AC_INIT** - Place the name of your project, its version number and a |
| 67 | contact email address for your project as the arguments to this macro |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 68 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 69 | * **AC_CONFIG_AUX_DIR** - If your project isn't in the ``llvm/projects`` |
| 70 | directory then you might need to adjust this so that it specifies a |
| 71 | relative path to the ``llvm/autoconf`` directory. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 72 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 73 | * **LLVM_CONFIG_PROJECT** - Just leave this alone. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 74 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 75 | * **AC_CONFIG_SRCDIR** - Specify a path to a file name that identifies your |
| 76 | project; or just leave it at ``Makefile.common.in``. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 77 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 78 | * **AC_CONFIG_FILES** - Do not change. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 79 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 80 | * **AC_CONFIG_MAKEFILE** - Use one of these macros for each Makefile that |
| 81 | your project uses. This macro arranges for your makefiles to be copied from |
| 82 | the source directory, unmodified, to the build directory. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 83 | |
Bill Wendling | ea2a7d7 | 2012-06-19 09:25:04 +0000 | [diff] [blame] | 84 | 5. After updating ``autoconf/configure.ac``, regenerate the configure script |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 85 | with these commands. (You must be using ``Autoconf`` version 2.59 or later |
| 86 | and your ``aclocal`` version should be 1.9 or later.) |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 87 | |
Bill Wendling | 6573c99 | 2012-06-19 17:48:06 +0000 | [diff] [blame] | 88 | .. code-block:: bash |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 89 | |
Bill Wendling | 6573c99 | 2012-06-19 17:48:06 +0000 | [diff] [blame] | 90 | % cd autoconf |
| 91 | % ./AutoRegen.sh |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 92 | |
Bill Wendling | ea2a7d7 | 2012-06-19 09:25:04 +0000 | [diff] [blame] | 93 | 6. Run ``configure`` in the directory in which you want to place object code. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 94 | Use the following options to tell your project where it can find LLVM: |
| 95 | |
| 96 | ``--with-llvmsrc=<directory>`` |
| 97 | Tell your project where the LLVM source tree is located. |
| 98 | |
| 99 | ``--with-llvmobj=<directory>`` |
| 100 | Tell your project where the LLVM object tree is located. |
| 101 | |
| 102 | ``--prefix=<directory>`` |
| 103 | Tell your project where it should get installed. |
| 104 | |
| 105 | That's it! Now all you have to do is type ``gmake`` (or ``make`` if your on a |
| 106 | GNU/Linux system) in the root of your object directory, and your project should |
| 107 | build. |
| 108 | |
| 109 | Source Tree Layout |
| 110 | ================== |
| 111 | |
| 112 | In order to use the LLVM build system, you will want to organize your source |
| 113 | code so that it can benefit from the build system's features. Mainly, you want |
| 114 | your source tree layout to look similar to the LLVM source tree layout. The |
| 115 | best way to do this is to just copy the project tree from |
| 116 | ``llvm/projects/sample`` and modify it to meet your needs, but you can certainly |
| 117 | add to it if you want. |
| 118 | |
| 119 | Underneath your top level directory, you should have the following directories: |
| 120 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 121 | **lib** |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 122 | |
| 123 | This subdirectory should contain all of your library source code. For each |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 124 | library that you build, you will have one directory in **lib** that will |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 125 | contain that library's source code. |
| 126 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 127 | Libraries can be object files, archives, or dynamic libraries. The **lib** |
| 128 | directory is just a convenient place for libraries as it places them all in |
| 129 | a directory from which they can be linked later. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 130 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 131 | **include** |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 132 | |
| 133 | This subdirectory should contain any header files that are global to your |
| 134 | project. By global, we mean that they are used by more than one library or |
| 135 | executable of your project. |
| 136 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 137 | By placing your header files in **include**, they will be found |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 138 | automatically by the LLVM build system. For example, if you have a file |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 139 | **include/jazz/note.h**, then your source files can include it simply with |
| 140 | **#include "jazz/note.h"**. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 141 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 142 | **tools** |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 143 | |
| 144 | This subdirectory should contain all of your source code for executables. |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 145 | For each program that you build, you will have one directory in **tools** |
| 146 | that will contain that program's source code. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 147 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 148 | **test** |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 149 | |
| 150 | This subdirectory should contain tests that verify that your code works |
| 151 | correctly. Automated tests are especially useful. |
| 152 | |
| 153 | Currently, the LLVM build system provides basic support for tests. The LLVM |
| 154 | system provides the following: |
| 155 | |
| 156 | * LLVM provides a ``tcl`` procedure that is used by ``Dejagnu`` to run tests. |
| 157 | It can be found in ``llvm/lib/llvm-dg.exp``. This test procedure uses ``RUN`` |
| 158 | lines in the actual test case to determine how to run the test. See the |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 159 | `TestingGuide <TestingGuide.html>`_ for more details. You can easily write |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 160 | Makefile support similar to the Makefiles in ``llvm/test`` to use ``Dejagnu`` |
| 161 | to run your project's tests. |
| 162 | |
| 163 | * LLVM contains an optional package called ``llvm-test``, which provides |
| 164 | benchmarks and programs that are known to compile with the Clang front |
| 165 | end. You can use these programs to test your code, gather statistical |
| 166 | information, and compare it to the current LLVM performance statistics. |
| 167 | |
| 168 | Currently, there is no way to hook your tests directly into the ``llvm/test`` |
| 169 | testing harness. You will simply need to find a way to use the source |
| 170 | provided within that directory on your own. |
| 171 | |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 172 | Typically, you will want to build your **lib** directory first followed by your |
| 173 | **tools** directory. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 174 | |
| 175 | Writing LLVM Style Makefiles |
| 176 | ============================ |
| 177 | |
| 178 | The LLVM build system provides a convenient way to build libraries and |
| 179 | executables. Most of your project Makefiles will only need to define a few |
| 180 | variables. Below is a list of the variables one can set and what they can |
| 181 | do: |
| 182 | |
| 183 | Required Variables |
| 184 | ------------------ |
| 185 | |
| 186 | ``LEVEL`` |
| 187 | |
| 188 | This variable is the relative path from this ``Makefile`` to the top |
| 189 | directory of your project's source code. For example, if your source code |
| 190 | is in ``/tmp/src``, then the ``Makefile`` in ``/tmp/src/jump/high`` |
| 191 | would set ``LEVEL`` to ``"../.."``. |
| 192 | |
| 193 | Variables for Building Subdirectories |
| 194 | ------------------------------------- |
| 195 | |
| 196 | ``DIRS`` |
| 197 | |
| 198 | This is a space separated list of subdirectories that should be built. They |
| 199 | will be built, one at a time, in the order specified. |
| 200 | |
| 201 | ``PARALLEL_DIRS`` |
| 202 | |
| 203 | This is a list of directories that can be built in parallel. These will be |
| 204 | built after the directories in DIRS have been built. |
| 205 | |
| 206 | ``OPTIONAL_DIRS`` |
| 207 | |
| 208 | This is a list of directories that can be built if they exist, but will not |
| 209 | cause an error if they do not exist. They are built serially in the order |
| 210 | in which they are listed. |
| 211 | |
| 212 | Variables for Building Libraries |
| 213 | -------------------------------- |
| 214 | |
| 215 | ``LIBRARYNAME`` |
| 216 | |
| 217 | This variable contains the base name of the library that will be built. For |
| 218 | example, to build a library named ``libsample.a``, ``LIBRARYNAME`` should |
| 219 | be set to ``sample``. |
| 220 | |
| 221 | ``BUILD_ARCHIVE`` |
| 222 | |
| 223 | By default, a library is a ``.o`` file that is linked directly into a |
| 224 | program. To build an archive (also known as a static library), set the |
| 225 | ``BUILD_ARCHIVE`` variable. |
| 226 | |
| 227 | ``SHARED_LIBRARY`` |
| 228 | |
| 229 | If ``SHARED_LIBRARY`` is defined in your Makefile, a shared (or dynamic) |
| 230 | library will be built. |
| 231 | |
| 232 | Variables for Building Programs |
| 233 | ------------------------------- |
| 234 | |
| 235 | ``TOOLNAME`` |
| 236 | |
| 237 | This variable contains the name of the program that will be built. For |
| 238 | example, to build an executable named ``sample``, ``TOOLNAME`` should be set |
| 239 | to ``sample``. |
| 240 | |
| 241 | ``USEDLIBS`` |
| 242 | |
| 243 | This variable holds a space separated list of libraries that should be |
| 244 | linked into the program. These libraries must be libraries that come from |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 245 | your **lib** directory. The libraries must be specified without their |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 246 | ``lib`` prefix. For example, to link ``libsample.a``, you would set |
| 247 | ``USEDLIBS`` to ``sample.a``. |
| 248 | |
| 249 | Note that this works only for statically linked libraries. |
| 250 | |
| 251 | ``LLVMLIBS`` |
| 252 | |
| 253 | This variable holds a space separated list of libraries that should be |
| 254 | linked into the program. These libraries must be LLVM libraries. The |
| 255 | libraries must be specified without their ``lib`` prefix. For example, to |
| 256 | link with a driver that performs an IR transformation you might set |
| 257 | ``LLVMLIBS`` to this minimal set of libraries ``LLVMSupport.a LLVMCore.a |
| 258 | LLVMBitReader.a LLVMAsmParser.a LLVMAnalysis.a LLVMTransformUtils.a |
| 259 | LLVMScalarOpts.a LLVMTarget.a``. |
| 260 | |
| 261 | Note that this works only for statically linked libraries. LLVM is split |
| 262 | into a large number of static libraries, and the list of libraries you |
| 263 | require may be much longer than the list above. To see a full list of |
| 264 | libraries use: ``llvm-config --libs all``. Using ``LINK_COMPONENTS`` as |
| 265 | described below, obviates the need to set ``LLVMLIBS``. |
| 266 | |
| 267 | ``LINK_COMPONENTS`` |
| 268 | |
| 269 | This variable holds a space separated list of components that the LLVM |
| 270 | ``Makefiles`` pass to the ``llvm-config`` tool to generate a link line for |
| 271 | the program. For example, to link with all LLVM libraries use |
| 272 | ``LINK_COMPONENTS = all``. |
| 273 | |
| 274 | ``LIBS`` |
| 275 | |
Bill Wendling | c110423 | 2012-06-19 17:43:57 +0000 | [diff] [blame] | 276 | To link dynamic libraries, add ``-l<library base name>`` to the ``LIBS`` |
| 277 | variable. The LLVM build system will look in the same places for dynamic |
| 278 | libraries as it does for static libraries. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 279 | |
| 280 | For example, to link ``libsample.so``, you would have the following line in |
| 281 | your ``Makefile``: |
| 282 | |
Bill Wendling | 2e355f6 | 2012-06-19 22:25:17 +0000 | [diff] [blame] | 283 | .. code-block:: makefile |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 284 | |
Bill Wendling | 6573c99 | 2012-06-19 17:48:06 +0000 | [diff] [blame] | 285 | LIBS += -lsample |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 286 | |
| 287 | Note that ``LIBS`` must occur in the Makefile after the inclusion of |
| 288 | ``Makefile.common``. |
| 289 | |
| 290 | Miscellaneous Variables |
| 291 | ----------------------- |
| 292 | |
Bill Wendling | 540fe7d | 2012-06-19 09:29:05 +0000 | [diff] [blame] | 293 | ``CFLAGS`` & ``CPPFLAGS`` |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 294 | |
| 295 | This variable can be used to add options to the C and C++ compiler, |
| 296 | respectively. It is typically used to add options that tell the compiler |
| 297 | the location of additional directories to search for header files. |
| 298 | |
| 299 | It is highly suggested that you append to ``CFLAGS`` and ``CPPFLAGS`` as |
| 300 | opposed to overwriting them. The master ``Makefiles`` may already have |
| 301 | useful options in them that you may not want to overwrite. |
| 302 | |
| 303 | Placement of Object Code |
| 304 | ======================== |
| 305 | |
| 306 | The final location of built libraries and executables will depend upon whether |
| 307 | you do a ``Debug``, ``Release``, or ``Profile`` build. |
| 308 | |
| 309 | Libraries |
| 310 | |
| 311 | All libraries (static and dynamic) will be stored in |
Bill Wendling | ce6e0a1 | 2012-06-19 09:27:54 +0000 | [diff] [blame] | 312 | ``PROJ_OBJ_ROOT/<type>/lib``, where *type* is ``Debug``, ``Release``, or |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 313 | ``Profile`` for a debug, optimized, or profiled build, respectively. |
| 314 | |
| 315 | Executables |
| 316 | |
Bill Wendling | ce6e0a1 | 2012-06-19 09:27:54 +0000 | [diff] [blame] | 317 | All executables will be stored in ``PROJ_OBJ_ROOT/<type>/bin``, where *type* |
| 318 | is ``Debug``, ``Release``, or ``Profile`` for a debug, optimized, or |
| 319 | profiled build, respectively. |
Bill Wendling | 4caaa1a | 2012-06-19 09:18:34 +0000 | [diff] [blame] | 320 | |
| 321 | Further Help |
| 322 | ============ |
| 323 | |
| 324 | If you have any questions or need any help creating an LLVM project, the LLVM |
| 325 | team would be more than happy to help. You can always post your questions to |
Bill Wendling | e2658d6 | 2012-06-19 09:23:23 +0000 | [diff] [blame] | 326 | the `LLVM Developers Mailing List |
Bill Wendling | 67d31c6 | 2012-06-19 09:26:15 +0000 | [diff] [blame] | 327 | <http://lists.cs.uiuc.edu/pipermail/llvmdev/>`_. |