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