John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Creating an LLVM Project</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <center><h1>Creating an LLVM Project<br></h1></center> |
| 10 | |
| 11 | <!--===============================================================--> |
| 12 | <h2><a name="a">Overview</a><hr></h2> |
| 13 | <!--===============================================================--> |
| 14 | |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 15 | The LLVM build system is designed to facilitate the building of third party |
| 16 | projects that use LLVM header files, libraries, and tools. In order to use |
| 17 | these facilities, a Makefile from a project must do the following things: |
| 18 | |
| 19 | <ol> |
| 20 | <li>Set environment variables. |
| 21 | <p> |
| 22 | There are several environment variables that a Makefile needs to set to |
| 23 | use the LLVM build system: |
| 24 | <dl compact> |
| 25 | <dt>LLVM_SRC_ROOT |
| 26 | <dd> |
| 27 | The root of the LLVM source tree. |
| 28 | <p> |
| 29 | |
| 30 | <dt>LLVM_OBJ_ROOT |
| 31 | <dd> |
| 32 | The root of the LLVM object tree. |
| 33 | <p> |
| 34 | |
| 35 | <dt>BUILD_SRC_ROOT |
| 36 | <dd> |
| 37 | The root of the project's source tree. |
| 38 | <p> |
| 39 | |
| 40 | <dt>BUILD_OBJ_ROOT |
| 41 | <dd> |
| 42 | The root of the project's object tree. |
| 43 | <p> |
| 44 | |
| 45 | <dt>BUILD_SRC_DIR |
| 46 | <dd> |
| 47 | The directory containing the current source to be compiled. |
| 48 | <p> |
| 49 | |
| 50 | <dt>BUILD_OBJ_DIR |
| 51 | <dd> |
| 52 | The directory where the current source will place the new object |
| 53 | files. This should always be the current directory. |
| 54 | <p> |
| 55 | |
| 56 | <dt>LEVEL |
| 57 | <dd> |
| 58 | The relative path from the current directory to the root of the |
| 59 | object tree. |
| 60 | <p> |
| 61 | </dl> |
| 62 | |
| 63 | <li>Include the LLVM Makefile.config from $(LLVM_OBJ_ROOT). |
| 64 | <p> |
| 65 | |
| 66 | <li>Include the LLVM Makefile.rules from $(LLVM_SRC_ROOT). |
| 67 | </ol> |
| 68 | |
| 69 | There are two ways that you can set all of these variables: |
| 70 | <ol> |
| 71 | <li> |
| 72 | You can write your own Makefiles which hard-code these values. |
| 73 | |
| 74 | <li> |
| 75 | You can use the pre-made LLVM sample project. This sample project |
| 76 | includes Makefiles, a configure script that can be used to configure |
| 77 | the location of LLVM, and the ability to support multiple object |
| 78 | directories from a single source directory. |
| 79 | </ol> |
| 80 | |
| 81 | This document assumes that you will base your project off of the LLVM |
| 82 | sample project found in <tt>llvm/projects/sample</tt>. If you want to |
| 83 | devise your own build system, studying the sample project and LLVM |
| 84 | Makefiles will probably provide enough information on how to write your own |
| 85 | Makefiles. |
| 86 | <p> |
| 87 | |
| 88 | <!--===============================================================--> |
| 89 | <h2><a name="a">Create a Project from the Sample Project</a><hr></h2> |
| 90 | <!--===============================================================--> |
| 91 | |
| 92 | Follow these simple steps to start your project: |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 93 | |
| 94 | <ol> |
| 95 | <li> |
| 96 | Copy the <tt>llvm/projects/sample</tt> directory to any place |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 97 | of your choosing. You can place it anywhere you like. Rename the |
| 98 | directory to match the name of your project. |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 99 | <p> |
| 100 | |
| 101 | <li> |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 102 | Add your source code and Makefiles to your source tree. |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 103 | <p> |
| 104 | |
| 105 | <li> |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 106 | If you want your Makefiles to be configured by the |
| 107 | <tt>configure</tt> script, or if you want to support multiple |
| 108 | object directories, add your Makefiles to the <tt>configure</tt> |
| 109 | script by adding them into the <tt>autoconf/configure.ac</tt> file. |
| 110 | The macro <tt>AC_CONFIG_MAKEFILE</tt> will copy a file, unmodified, |
| 111 | from the source directory to the object directory. |
| 112 | |
| 113 | <p> |
| 114 | After updating <tt>autoconf/configure.ac</tt>, regenerate the |
| 115 | configure script with these commands: |
| 116 | <p> |
| 117 | <tt> |
| 118 | cd autoconf<br> |
| 119 | autoconf -o ../configure |
| 120 | </tt> |
| 121 | |
| 122 | <p> |
| 123 | |
| 124 | You must be using Autoconf version 2.57 or higher. |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 125 | <p> |
| 126 | |
| 127 | <li> |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 128 | Run <tt>configure</tt> in the directory in which you want to place |
| 129 | object code. Use the following options to tell your project where it |
| 130 | can find LLVM: |
| 131 | |
| 132 | <dl compact> |
| 133 | <dt><tt>--with-llvmsrc=<directory></tt> |
| 134 | <dd> |
| 135 | Tell your project where the LLVM source tree is located. |
| 136 | <p> |
| 137 | <dt><tt>--with-llvmobj=<directory></tt> |
| 138 | <dd> |
| 139 | Tell your project where the LLVM object tree is located. |
| 140 | </dl> |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 141 | </ol> |
| 142 | |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 143 | That's it! Now all you have to do is type <tt>gmake</tt> in the root of |
| 144 | your object directory, and your project should build. |
| 145 | |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 146 | <!--===============================================================--> |
| 147 | <h2><a name="Source Tree Layout">Source Tree Layout</a><hr></h2> |
| 148 | <!--===============================================================--> |
| 149 | |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 150 | In order to use the LLVM build system, you will want to organize your |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 151 | source code so that it can benefit from the build system's features. |
| 152 | Mainly, you want your source tree layout to look similar to the LLVM |
| 153 | source tree layout. The best way to do this is to just copy the |
| 154 | project tree from <tt>llvm/projects/sample</tt> and modify it to meet |
| 155 | your needs, but you can certainly add to it if you want. |
| 156 | |
| 157 | Underneath your top level directory, you should have the following |
| 158 | directories: |
| 159 | |
| 160 | <dl compact> |
| 161 | <dt><b>lib</b> |
| 162 | <dd> |
| 163 | This subdirectory should contain all of your library source |
| 164 | code. For each library that you build, you will have one |
| 165 | directory in <b>lib</b> that will contain that library's source |
| 166 | code. |
| 167 | |
| 168 | <p> |
| 169 | Libraries can be object files, archives, or dynamic libraries. |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 170 | The <b>lib</b> directory is just a convenient place for libraries |
| 171 | as it places them all in a directory from which they can be linked |
John Criswell | c7b6ce4 | 2003-10-15 16:52:57 +0000 | [diff] [blame] | 172 | later. |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 173 | |
| 174 | <dt><b>include</b> |
| 175 | <dd> |
| 176 | This subdirectory should contain any header files that are |
| 177 | global to your project. By global, we mean that they are used |
| 178 | by more than one library or executable of your project. |
| 179 | <p> |
| 180 | By placing your header files in <b>include</b>, they will be |
| 181 | found automatically by the LLVM build system. For example, if |
| 182 | you have a file <b>include/jazz/note.h</b>, then your source |
| 183 | files can include it simply with <b>#include "jazz/note.h"</b>. |
| 184 | |
| 185 | <dt><b>tools</b> |
| 186 | <dd> |
| 187 | This subdirectory should contain all of your source |
| 188 | code for executables. For each program that you build, you |
| 189 | will have one directory in <b>tools</b> that will contain that |
| 190 | program's source code. |
John Criswell | 2b89a6a | 2003-10-21 19:35:06 +0000 | [diff] [blame] | 191 | <p> |
| 192 | |
| 193 | <dt><b>test</b> |
| 194 | <dd> |
| 195 | This subdirectory should contain tests that verify that your code |
| 196 | works correctly. Automated tests are especially useful. |
| 197 | <p> |
| 198 | Currently, the LLVM build system provides little support for tests, |
| 199 | although some exists. Expanded support for tests will hopefully |
| 200 | occur in the future. In the meantime, the LLVM system does provide the |
| 201 | following: |
| 202 | <ul> |
| 203 | <li> |
| 204 | LLVM provides several QMTest test classes that can be used to |
| 205 | create tests. They can be found in |
| 206 | <tt>llvm/test/QMTest/llvm.py</tt>. These test classes perform a |
| 207 | variety of functions, including code optimization tests, assembly |
| 208 | tests, and code analysis tests. The Makefile in |
| 209 | <tt>llvm/test</tt> provides the QMTest context needed by LLVM test |
| 210 | classes. |
| 211 | <p> |
| 212 | |
| 213 | <li> |
| 214 | The LLVM source tree provides benchmarks and programs which are |
| 215 | known to compile with the LLVM GCC front ends. You can use these |
| 216 | programs to test your code, gather statistics information, and |
| 217 | compare it to the current LLVM performance statistics. These |
| 218 | programs are found in the <tt>llvm/test/Programs</tt> directory. |
| 219 | <p> |
| 220 | Currently, there is no way to hook your tests directly into the |
| 221 | <tt>llvm/test/Programs</tt> testing harness. You will simply |
| 222 | need to find a way to use the source provided within that directory |
| 223 | on your own. |
| 224 | </ul> |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 225 | </dl> |
| 226 | |
| 227 | Typically, you will want to build your <b>lib</b> directory first |
| 228 | followed by your <b>tools</b> directory. |
| 229 | |
| 230 | <!--===============================================================--> |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 231 | <h2><a name="Makefile Variables">Writing LLVM Style Makefiles</a><hr></h2> |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 232 | <!--===============================================================--> |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 233 | The LLVM build system provides a convenient way to build libraries and |
| 234 | executables. Most of your project Makefiles will only need to define a few |
| 235 | variables. Below is a list of the variables one can set and what they can |
| 236 | do: |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 237 | |
| 238 | <h3> Required Variables </h3> |
| 239 | <dl compact> |
| 240 | <dt>LEVEL |
| 241 | <dd> |
| 242 | This variable is the relative path from this Makefile to the |
| 243 | top directory of your project's source code. For example, if |
| 244 | your source code is in /tmp/src, then the Makefile in |
| 245 | /tmp/src/jump/high would set LEVEL to "../..". |
| 246 | </dl> |
| 247 | |
| 248 | <h3> Variables for Building Subdirectories</h3> |
| 249 | <dl compact> |
| 250 | <dt>DIRS |
| 251 | <dd> |
| 252 | This is a space separated list of subdirectories that should be |
| 253 | built. They will be built, one at a time, in the order |
| 254 | specified. |
| 255 | <p> |
| 256 | |
| 257 | <dt>PARALLEL_DIRS |
| 258 | <dd> |
| 259 | This is a list of directories that can be built in parallel. |
| 260 | These will be built after the directories in DIRS have been |
| 261 | built. |
| 262 | <p> |
| 263 | |
| 264 | <dt>OPTIONAL_DIRS |
| 265 | <dd> |
| 266 | This is a list of directories that can be built if they exist, |
| 267 | but will not cause an error if they do not exist. They are |
| 268 | built serially in the order in which they are listed. |
| 269 | </dl> |
| 270 | |
| 271 | <h3> Variables for Building Libraries</h3> |
| 272 | <dl compact> |
| 273 | <dt>LIBRARYNAME |
| 274 | <dd> |
| 275 | This variable contains the base name of the library that will |
| 276 | be built. For example, to build a library named |
| 277 | <tt>libsample.a</tt>, LIBRARYNAME should be set to |
| 278 | <tt>sample</tt>. |
| 279 | <p> |
| 280 | |
| 281 | <dt>BUILD_ARCHIVE |
| 282 | <dd> |
| 283 | By default, a library is a <tt>.o</tt> file that is linked |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 284 | directly into a program. To build an archive (also known as |
| 285 | a static library), set the BUILD_ARCHIVE variable. |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 286 | <p> |
| 287 | |
| 288 | <dt>SHARED_LIBRARY |
| 289 | <dd> |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 290 | If SHARED_LIBRARY is defined in your Makefile, a shared |
| 291 | (or dynamic) library will be built. |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 292 | </dl> |
| 293 | |
| 294 | <h3> Variables for Building Programs</h3> |
| 295 | <dl compact> |
| 296 | <dt>TOOLNAME |
| 297 | <dd> |
| 298 | This variable contains the name of the program that will |
| 299 | be built. For example, to build an executable named |
| 300 | <tt>sample</tt>, TOOLNAME should be set to <tt>sample</tt>. |
| 301 | <p> |
| 302 | |
| 303 | <dt>USEDLIBS |
| 304 | <dd> |
| 305 | This variable holds a space separated list of libraries that |
| 306 | should be linked into the program. These libraries must either |
| 307 | be LLVM libraries or libraries that come from your <b>lib</b> |
| 308 | directory. The libraries must be specified by their base name. |
| 309 | For example, to link libsample.a, you would set USEDLIBS to |
| 310 | <tt>sample</tt>. |
| 311 | <p> |
John Criswell | 37c154a | 2003-10-17 21:50:38 +0000 | [diff] [blame] | 312 | Note that this works only for statically linked libraries. |
| 313 | <p> |
| 314 | |
| 315 | <dt>LIBS |
| 316 | <dd> |
| 317 | To link dynamic libraries, add <tt>-l<library base name></tt> to |
| 318 | the LIBS variable. The LLVM build system will look in the same places |
| 319 | for dynamic libraries as it does for static libraries. |
| 320 | <p> |
| 321 | For example, to link <tt>libsample.so</tt>, you would have the |
| 322 | following line in your <tt>Makefile</tt>: |
| 323 | <p> |
| 324 | <tt> |
| 325 | LIBS+=-lsample |
| 326 | </tt> |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 327 | </dl> |
| 328 | |
| 329 | <h3> Miscellaneous Variables</h3> |
| 330 | <dl compact> |
| 331 | <dt>ExtraSource |
| 332 | <dd> |
| 333 | This variable contains a space separated list of extra source |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 334 | files that need to be built. It is useful for including the |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 335 | output of Lex and Yacc programs. |
| 336 | <p> |
| 337 | |
| 338 | <dt>CFLAGS |
| 339 | <dt>CPPFLAGS |
| 340 | <dd> |
| 341 | This variable can be used to add options to the C and C++ |
| 342 | compiler, respectively. It is typically used to add options |
| 343 | that tell the compiler the location of additional directories |
| 344 | to search for header files. |
| 345 | <p> |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 346 | It is highly suggested that you append to CFLAGS and CPPFLAGS as |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 347 | opposed to overwriting them. The master Makefiles may already |
| 348 | have useful options in them that you may not want to overwrite. |
| 349 | <p> |
| 350 | </dl> |
| 351 | |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 352 | <!--===============================================================--> |
| 353 | <h2><a name="objcode">Placement of Object Code</a><hr></h2> |
| 354 | <!--===============================================================--> |
| 355 | |
| 356 | The final location of built libraries and executables will depend upon |
| 357 | whether you do a Debug, Release, or Profile build. |
| 358 | |
| 359 | <dl compact> |
| 360 | <dt>Libraries |
| 361 | <dd> |
| 362 | All libraries (static and dynamic) will be stored in |
| 363 | BUILD_OBJ_ROOT/lib/<type>, where type is <tt>Debug</tt>, |
| 364 | <tt>Release</tt>, or <tt>Profile</tt> for a debug, optimized, or |
| 365 | profiled build, respectively. |
| 366 | <p> |
| 367 | |
| 368 | <dt>Executables |
| 369 | <dd> |
| 370 | All executables will be stored in BUILD_OBJ_ROOT/lib/<type>, |
| 371 | where type is <tt>Debug</tt>, <tt>Release</tt>, or <tt>Profile</tt> for |
| 372 | a debug, optimized, or profiled build, respectively. |
| 373 | </dl> |
| 374 | |
| 375 | <!--===============================================================--> |
| 376 | <h2><a name="help">Further Help</a><hr></h2> |
| 377 | <!--===============================================================--> |
| 378 | |
| 379 | If you have any questions or need any help creating an LLVM project, |
| 380 | the LLVM team would be more than happy to help. You can always post your |
Misha Brukman | 733adcb | 2003-10-30 01:23:40 +0000 | [diff] [blame] | 381 | questions to the <a |
| 382 | href="http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev">LLVM Developers |
| 383 | Mailing List</a>. |
John Criswell | 7a4b96d | 2003-10-16 19:53:53 +0000 | [diff] [blame] | 384 | |
| 385 | <hr> |
Misha Brukman | 733adcb | 2003-10-30 01:23:40 +0000 | [diff] [blame] | 386 | <address><a href="mailto:criswell@uiuc.edu">John Criswell</a></address><br> |
| 387 | <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br> |
| 388 | Last modified: $Date$ |
| 389 | |
John Criswell | f2413ae | 2003-07-03 15:37:52 +0000 | [diff] [blame] | 390 | </body> |
| 391 | </html> |