1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>Creating an LLVM Project</title>
9 <center><h1>Creating an LLVM Project<br></h1></center>
11 <!--===============================================================-->
12 <h2><a name="a">Overview</a><hr></h2>
13 <!--===============================================================-->
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:
20 <li>Set environment variables.
22 There are several environment variables that a Makefile needs to set to
23 use the LLVM build system:
27 The root of the LLVM source tree.
32 The root of the LLVM object tree.
37 The root of the project's source tree.
42 The root of the project's object tree.
47 The directory containing the current source to be compiled.
52 The directory where the current source will place the new object
53 files. This should always be the current directory.
58 The relative path from the current directory to the root of the
63 <li>Include the LLVM Makefile.config from $(LLVM_OBJ_ROOT).
66 <li>Include the LLVM Makefile.rules from $(LLVM_SRC_ROOT).
69 There are two ways that you can set all of these variables:
72 You can write your own Makefiles which hard-code these values.
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.
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
88 <!--===============================================================-->
89 <h2><a name="a">Create a Project from the Sample Project</a><hr></h2>
90 <!--===============================================================-->
92 Follow these simple steps to start your project:
96 Copy the <tt>llvm/projects/sample</tt> directory to any place
97 of your choosing. You can place it anywhere you like. Rename the
98 directory to match the name of your project.
102 Add your source code and Makefiles to your source tree.
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.
114 After updating <tt>autoconf/configure.ac</tt>, regenerate the
115 configure script with these commands:
119 autoconf -o ../configure
124 You must be using Autoconf version 2.57 or higher.
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
133 <dt><tt>--with-llvmsrc=<directory></tt>
135 Tell your project where the LLVM source tree is located.
137 <dt><tt>--with-llvmobj=<directory></tt>
139 Tell your project where the LLVM object tree is located.
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.
146 <!--===============================================================-->
147 <h2><a name="Source Tree Layout">Source Tree Layout</a><hr></h2>
148 <!--===============================================================-->
150 In order to use the LLVM build system, you will want to organize your
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.
157 Underneath your top level directory, you should have the following
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
169 Libraries can be object files, archives, or dynamic libraries.
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
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.
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>.
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.
193 Typically, you will want to build your <b>lib</b> directory first
194 followed by your <b>tools</b> directory.
196 <!--===============================================================-->
197 <h2><a name="Makefile Variables">Writing LLVM Style Makefiles</a><hr></h2>
198 <!--===============================================================-->
199 The LLVM build system provides a convenient way to build libraries and
200 executables. Most of your project Makefiles will only need to define a few
201 variables. Below is a list of the variables one can set and what they can
204 <h3> Required Variables </h3>
208 This variable is the relative path from this Makefile to the
209 top directory of your project's source code. For example, if
210 your source code is in /tmp/src, then the Makefile in
211 /tmp/src/jump/high would set LEVEL to "../..".
214 <h3> Variables for Building Subdirectories</h3>
218 This is a space separated list of subdirectories that should be
219 built. They will be built, one at a time, in the order
225 This is a list of directories that can be built in parallel.
226 These will be built after the directories in DIRS have been
232 This is a list of directories that can be built if they exist,
233 but will not cause an error if they do not exist. They are
234 built serially in the order in which they are listed.
237 <h3> Variables for Building Libraries</h3>
241 This variable contains the base name of the library that will
242 be built. For example, to build a library named
243 <tt>libsample.a</tt>, LIBRARYNAME should be set to
249 By default, a library is a <tt>.o</tt> file that is linked
250 directly into a program. To build an archive (also known as
251 a static library), set the BUILD_ARCHIVE variable.
256 If SHARED_LIBRARY is defined in your Makefile, a shared
257 (or dynamic) library will be built.
260 <h3> Variables for Building Programs</h3>
264 This variable contains the name of the program that will
265 be built. For example, to build an executable named
266 <tt>sample</tt>, TOOLNAME should be set to <tt>sample</tt>.
271 This variable holds a space separated list of libraries that
272 should be linked into the program. These libraries must either
273 be LLVM libraries or libraries that come from your <b>lib</b>
274 directory. The libraries must be specified by their base name.
275 For example, to link libsample.a, you would set USEDLIBS to
278 Note that this works only for statically linked libraries.
283 To link dynamic libraries, add <tt>-l<library base name></tt> to
284 the LIBS variable. The LLVM build system will look in the same places
285 for dynamic libraries as it does for static libraries.
287 For example, to link <tt>libsample.so</tt>, you would have the
288 following line in your <tt>Makefile</tt>:
295 <h3> Miscellaneous Variables</h3>
299 This variable contains a space separated list of extra source
300 files that need to be built. It is useful for including the
301 output of Lex and Yacc programs.
307 This variable can be used to add options to the C and C++
308 compiler, respectively. It is typically used to add options
309 that tell the compiler the location of additional directories
310 to search for header files.
312 It is highly suggested that you append to CFLAGS and CPPFLAGS as
313 opposed to overwriting them. The master Makefiles may already
314 have useful options in them that you may not want to overwrite.
318 <!--===============================================================-->
319 <h2><a name="objcode">Placement of Object Code</a><hr></h2>
320 <!--===============================================================-->
322 The final location of built libraries and executables will depend upon
323 whether you do a Debug, Release, or Profile build.
328 All libraries (static and dynamic) will be stored in
329 BUILD_OBJ_ROOT/lib/<type>, where type is <tt>Debug</tt>,
330 <tt>Release</tt>, or <tt>Profile</tt> for a debug, optimized, or
331 profiled build, respectively.
336 All executables will be stored in BUILD_OBJ_ROOT/lib/<type>,
337 where type is <tt>Debug</tt>, <tt>Release</tt>, or <tt>Profile</tt> for
338 a debug, optimized, or profiled build, respectively.
341 <!--===============================================================-->
342 <h2><a name="help">Further Help</a><hr></h2>
343 <!--===============================================================-->
345 If you have any questions or need any help creating an LLVM project,
346 the LLVM team would be more than happy to help. You can always post your
347 questions to the LLVM Developers Mailing List (<a
348 href="mailto:llvmdev.cs.uiuc.edu">llvmdev@cs.uiuc.edu</a>).
351 Written by <a href="mailto:criswell@uiuc.edu">John Criswell</a>.