blob: e2dac2e314d32d696e7595dbf1c56cf688e30795 [file] [log] [blame]
Oscar Fuentes25f1d032008-11-20 22:05:48 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5 <title>Building LLVM with CMake</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8
9<div class="doc_title">
10 Building LLVM with CMake
11</div>
12
13<ul>
14 <li><a href="#intro">Introduction</a></li>
15 <li><a href="#quickstart">Quick start</a></li>
16 <li><a href="#usage">Basic CMake usage</a>
17 <li><a href="#options">Options and variables</a>
Oscar Fuentes0c913732008-12-31 14:36:41 +000018 <ul>
Oscar Fuentes25f1d032008-11-20 22:05:48 +000019 <li><a href="#freccmake">Frequently-used CMake variables</a></li>
20 <li><a href="#llvmvars">LLVM-specific variables</a></li>
21 </ul></li>
22 <li><a href="#testing">Executing the test suite</a>
23 <li><a href="#cross">Cross compiling</a>
24 <li><a href="#embedding">Embedding LLVM in your project</a>
Oscar Fuentes0c913732008-12-31 14:36:41 +000025 <li><a href="#specifics">Compiler/Platform specific topics</a>
26 <ul>
27 <li><a href="#msvc">Microsoft Visual C++</a></li>
28 </ul></li>
Oscar Fuentes25f1d032008-11-20 22:05:48 +000029</ul>
30
31<div class="doc_author">
Oscar Fuentes370387c2008-11-20 23:35:09 +000032<p>Written by <a href="mailto:ofv@wanadoo.es">Oscar Fuentes</a></p>
Oscar Fuentes25f1d032008-11-20 22:05:48 +000033</div>
34
35<!-- *********************************************************************** -->
36<div class="doc_section">
37<a name="intro">Introduction</a>
38</div>
39<!-- *********************************************************************** -->
40
41<div class="doc_text">
42
43 <p><a href="http://www.cmake.org/">CMake</a> is a cross-platform
44 build-generator tool. CMake does not build the project, it generates
45 the files needed by your build tool (GNU make, Visual Studio, etc) for
46 building LLVM.</p>
47
Oscar Fuentes370387c2008-11-20 23:35:09 +000048 <p>If you are really anxious about getting a functional LLVM build,
49 go to the <a href="#quickstart">Quick start</a> section. If you
50 are a CMake novice, start on <a href="#usage">Basic CMake
51 usage</a> and then go back to the <a href="#quickstart">Quick
52 start</a> once you know what you are
53 doing. The <a href="#options">Options and variables</a> section
54 is a reference for customizing your build. If you already have
55 experience with CMake, this is the recommended starting point.
Oscar Fuentes25f1d032008-11-20 22:05:48 +000056</div>
57
58<!-- *********************************************************************** -->
59<div class="doc_section">
60<a name="quickstart">Quick start</a>
61</div>
62<!-- *********************************************************************** -->
63
64<div class="doc_text">
65
66<p> We use here the command-line, non-interactive CMake interface </p>
67
68<ol>
69
Benjamin Kramere15192b2009-08-05 15:42:44 +000070 <li><p><a href="http://www.cmake.org/cmake/resources/software.html">Download</a>
Oscar Fuentes25f1d032008-11-20 22:05:48 +000071 and install CMake. Version 2.6.2 is the minimum required.</p>
72
73 <li><p>Open a shell. Your development tools must be reachable from this
74 shell through the PATH environment variable.</p>
75
76 <li><p>Create a directory for containing the build. It is not
77 supported to build LLVM on the source directory. cd to this
78 directory:</p>
79 <div class="doc_code">
80 <p><tt>mkdir mybuilddir</tt></p>
81 <p><tt>cd mybuilddir</tt></p>
82 </div>
83
84 <li><p>Execute this command on the shell
85 replacing <i>path/to/llvm/source/root</i> with the path to the
86 root of your LLVM source tree:</p>
87 <div class="doc_code">
88 <p><tt>cmake path/to/llvm/source/root</tt></p>
89 </div>
90
91 <p>CMake will detect your development environment, perform a
92 series of test and generate the files required for building
93 LLVM. CMake will use default values for all build
94 parameters. See the <a href="#options">Options and variables</a>
95 section for fine-tuning your build</p>
96
97 <p>This can fail if CMake can't detect your toolset, or if it
98 thinks that the environment is not sane enough. On this case
99 make sure that the toolset that you intend to use is the only
100 one reachable from the shell and that the shell itself is the
101 correct one for you development environment. CMake will refuse
102 to build MinGW makefiles if you have a POSIX shell reachable
103 through the PATH environment variable, for instance. You can
104 force CMake to use a given build tool, see
105 the <a href="#usage">Usage</a> section.</p>
106
107</ol>
108
109</div>
110
111<!-- *********************************************************************** -->
112<div class="doc_section">
113 <a name="usage">Basic CMake usage</a>
114</div>
115<!-- *********************************************************************** -->
116
117<div class="doc_text">
118
Oscar Fuentes370387c2008-11-20 23:35:09 +0000119 <p>This section explains basic aspects of CMake, mostly for
120 explaining those options which you may need on your day-to-day
121 usage.</p>
122
123 <p>CMake comes with extensive documentation in the form of html
124 files and on the cmake executable itself. Execute <i>cmake
125 --help</i> for further help options.</p>
126
127 <p>CMake requires to know for which build tool it shall generate
128 files (GNU make, Visual Studio, Xcode, etc). If not specified on
129 the command line, it tries to guess it based on you
130 environment. Once identified the build tool, CMake uses the
131 corresponding <i>Generator</i> for creating files for your build
132 tool. You can explicitly specify the generator with the command
133 line option <i>-G "Name of the generator"</i>. For knowing the
134 available generators on your platform, execute</p>
135
136 <div class="doc_code">
137 <p><tt>cmake --help</tt></p>
138 </div>
139
140 <p>This will list the generator's names at the end of the help
141 text. Generator's names are case-sensitive. Example:</p>
142
143 <div class="doc_code">
144 <p><tt>cmake -G "Visual Studio 8 2005" path/to/llvm/source/root</tt></p>
145 </div>
146
147 <p>For a given development platform there can be more than one
148 adequate generator. If you use Visual Studio "NMake Makefiles"
149 is a generator you can use for building with NMake. By default,
150 CMake chooses the more specific generator supported by your
151 development environment. If you want an alternative generator,
152 you must tell this to CMake with the <i>-G</i> option.</p>
153
154 <p>TODO: explain variables and cache. Move explanation here from
155 #options section.</p>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000156
157</div>
158
159<!-- *********************************************************************** -->
160<div class="doc_section">
161 <a name="options">Options and variables</a>
162</div>
163<!-- *********************************************************************** -->
164
165<div class="doc_text">
166
167 <p>Variables customize how the build will be generated. Options are
168 boolean variables, with possible values ON/OFF. Options and
169 variables are defined on the CMake command line like this:</p>
170
171 <div class="doc_code">
172 <p><tt>cmake -DVARIABLE=value path/to/llvm/source</tt></p>
173 </div>
174
175 <p>You can set a variable after the initial CMake invocation for
176 changing its value. You can also undefine a variable:</p>
177
178 <div class="doc_code">
179 <p><tt>cmake -UVARIABLE path/to/llvm/source</tt></p>
180 </div>
181
182 <p>Variables are stored on the CMake cache. This is a file
Benjamin Kramere15192b2009-08-05 15:42:44 +0000183 named <tt>CMakeCache.txt</tt> on the root of the build
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000184 directory. Do not hand-edit it.</p>
185
186 <p>Variables are listed here appending its type after a colon. It is
187 correct to write the variable and the type on the CMake command
188 line:</p>
189
190 <div class="doc_code">
191 <p><tt>cmake -DVARIABLE:TYPE=value path/to/llvm/source</tt></p>
192 </div>
193
194</div>
195
196<!-- ======================================================================= -->
197<div class="doc_subsection">
198 <a name="freccmake">Frequently-used CMake variables</a>
199</div>
200
201<div class="doc_text">
202
203<p>Here are listed some of the CMake variables that are used often,
204 along with a brief explanation and LLVM-specific notes. For full
205 documentation, check the CMake docs or execute <i>cmake
Oscar Fuentes0c913732008-12-31 14:36:41 +0000206 --help-variable VARIABLE_NAME</i>.</p>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000207
208<dl>
209 <dt><b>CMAKE_BUILD_TYPE</b>:STRING</dt>
210
211 <dd>Sets the build type for <i>make</i> based generators. Possible
Oscar Fuentes2ba7a902009-10-21 13:22:20 +0000212 values are Release, Debug, RelWithDebInfo and MinSizeRel. On
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000213 systems like Visual Studio the user sets the build type with the IDE
214 settings.</dd>
215
216 <dt><b>CMAKE_INSTALL_PREFIX</b>:PATH</dt>
217 <dd>Path where LLVM will be installed if "make install" is invoked
218 or the "INSTALL" target is built.</dd>
219
Oscar Fuentesaf0f65f2009-06-12 02:49:53 +0000220 <dt><b>LLVM_LIBDIR_SUFFIX</b>:STRING</dt>
221 <dd>Extra suffix to append to the directory where libraries are to
222 be installed. On a 64-bit architecture, one could use
223 -DLLVM_LIBDIR_SUFFIX=64 to install libraries to /usr/lib64.</dd>
224
Oscar Fuentesb6c22022008-11-25 04:23:36 +0000225 <dt><b>CMAKE_C_FLAGS</b>:STRING</dt>
226 <dd>Extra flags to use when compiling C source files.</dd>
227
228 <dt><b>CMAKE_CXX_FLAGS</b>:STRING</dt>
229 <dd>Extra flags to use when compiling C++ source files.</dd>
230
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000231 <dt><b>BUILD_SHARED_LIBS</b>:BOOL</dt>
232 <dd>Flag indicating is shared libraries will be built. Its default
233 value is OFF. Shared libraries are not supported on Windows and
234 not recommended in the other OSes.</dd>
235</dl>
236
237</div>
238
239<!-- ======================================================================= -->
240<div class="doc_subsection">
241 <a name="llvmvars">LLVM-specific variables</a>
242</div>
243
244<div class="doc_text">
245
246<dl>
247 <dt><b>LLVM_TARGETS_TO_BUILD</b>:STRING</dt>
248 <dd>Semicolon-separated list of targets to build, or <i>all</i> for
249 building all targets. Case-sensitive. For Visual C++ defaults
250 to <i>X86</i>. On the other cases defaults to <i>all</i>. Example:
251 <i>-DLLVM_TARGETS_TO_BUILD="X86;PowerPC;Alpha"</i>.</dd>
252
Oscar Fuentesc81f56d2009-08-16 20:56:30 +0000253 <dt><b>LLVM_BUILD_TOOLS</b>:BOOL</dt>
Oscar Fuentesb8352de2009-11-23 00:21:43 +0000254 <dd>Build LLVM tools. Defaults to ON. Targets for building each tool
255 are generated in any case. You can build an tool separately by
256 invoking its target. For example, you can build <i>llvm-as</i>
257 with a makefile-based system executing <i>make llvm-as</i> on the
258 root of your build directory.</dd>
Oscar Fuentesc81f56d2009-08-16 20:56:30 +0000259
Oscar Fuentesa6c1dbd2010-09-25 20:43:06 +0000260 <dt><b>LLVM_INCLUDE_TOOLS</b>:BOOL</dt>
261 <dd>Generate build targets for the LLVM tools. Defaults to
262 ON. You can use that option for disabling the generation of build
263 targets for the LLVM tools.</dd>
264
Oscar Fuentesc81f56d2009-08-16 20:56:30 +0000265 <dt><b>LLVM_BUILD_EXAMPLES</b>:BOOL</dt>
Oscar Fuentesb8352de2009-11-23 00:21:43 +0000266 <dd>Build LLVM examples. Defaults to OFF. Targets for building each
267 example are generated in any case. See documentation
268 for <i>LLVM_BUILD_TOOLS</i> above for more details.</dd>
Oscar Fuentesc81f56d2009-08-16 20:56:30 +0000269
Oscar Fuentesa6c1dbd2010-09-25 20:43:06 +0000270 <dt><b>LLVM_INCLUDE_EXAMPLES</b>:BOOL</dt>
271 <dd>Generate build targets for the LLVM examples. Defaults to
272 ON. You can use that option for disabling the generation of build
273 targets for the LLVM examples.</dd>
274
Oscar Fuentesda975632010-10-28 14:38:35 +0000275 <dt><b>LLVM_BUILD_TESTS</b>:BOOL</dt>
276 <dd>Build LLVM unit tests. Defaults to OFF. Targets for building
277 each unit test are generated in any case. You can build a specific
278 unit test with the target <i>UnitTestNameTests</i> (where at this
279 time <i>UnitTestName</i> can be ADT, Analysis, ExecutionEngine,
280 JIT, Support, Transform, VMCore; see the subdirectories
281 of <i>unittests</i> for an updated list.) It is possible to build
282 all unit tests with the target <i>UnitTests</i>.</dd>
283
284 <dt><b>LLVM_INCLUDE_TESTS</b>:BOOL</dt>
285 <dd>Generate build targets for the LLVM unit tests. Defaults to
286 ON. You can use that option for disabling the generation of build
287 targets for the LLVM unit tests.</dd>
288
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000289 <dt><b>LLVM_ENABLE_THREADS</b>:BOOL</dt>
290 <dd>Build with threads support, if available. Defaults to ON.</dd>
291
Oscar Fuentes76941b22009-06-04 09:26:16 +0000292 <dt><b>LLVM_ENABLE_ASSERTIONS</b>:BOOL</dt>
Oscar Fuentes2ba7a902009-10-21 13:22:20 +0000293 <dd>Enables code assertions. Defaults to OFF if and only if
Oscar Fuentes81a87552009-06-03 15:11:25 +0000294 CMAKE_BUILD_TYPE is <i>Release</i>.</dd>
295
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000296 <dt><b>LLVM_ENABLE_PIC</b>:BOOL</dt>
Oscar Fuentes1e02bf02009-08-18 15:29:35 +0000297 <dd>Add the <i>-fPIC</i> flag for the compiler command-line, if the
298 compiler supports this flag. Some systems, like Windows, do not
299 need this flag. Defaults to ON.</dd>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000300
Oscar Fuentesf12a9002009-12-01 02:21:51 +0000301 <dt><b>LLVM_ENABLE_WARNINGS</b>:BOOL</dt>
302 <dd>Enable all compiler warnings. Defaults to ON.</dd>
303
304 <dt><b>LLVM_ENABLE_PEDANTIC</b>:BOOL</dt>
305 <dd>Enable pedantic mode. This disable compiler specific extensions, is
306 possible. Defaults to ON.</dd>
307
308 <dt><b>LLVM_ENABLE_WERROR</b>:BOOL</dt>
309 <dd>Stop and fail build, if a compiler warning is
310 triggered. Defaults to OFF.</dd>
311
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000312 <dt><b>LLVM_BUILD_32_BITS</b>:BOOL</dt>
313 <dd>Build 32-bits executables and libraries on 64-bits systems. This
Oscar Fuentesf12a9002009-12-01 02:21:51 +0000314 option is available only on some 64-bits unix systems. Defaults to
315 OFF.</dd>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000316
Oscar Fuentesa9ff1392009-09-13 22:18:38 +0000317 <dt><b>LLVM_TARGET_ARCH</b>:STRING</dt>
318 <dd>LLVM target to use for native code generation. This is required
319 for JIT generation. It defaults to "host", meaning that it shall
320 pick the architecture of the machine where LLVM is being built. If
321 you are cross-compiling, set it to the target architecture
322 name.</dd>
323
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000324 <dt><b>LLVM_TABLEGEN</b>:STRING</dt>
325 <dd>Full path to a native TableGen executable (usually
326 named <i>tblgen</i>). This is intented for cross-compiling: if the
327 user sets this variable, no native TableGen will be created.</dd>
NAKAMURA Takumie7ae70b2010-11-11 04:09:35 +0000328
329 <dt><b>LLVM_LIT_ARGS</b>:STRING</dt>
330 <dd>Arguments given to lit.
331 <tt>make check</tt> and <tt>make clang-test</tt> are affected.
332 By default, <tt>&quot;-sv --no-progress-bar&quot;</tt>
333 on Visual C++ and Xcode,
334 <tt>&quot;-sv&quot;</tt> on others.</dd>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000335</dl>
336
337</div>
338
339<!-- *********************************************************************** -->
340<div class="doc_section">
341 <a name="testing">Executing the test suite</a>
342</div>
343<!-- *********************************************************************** -->
344
345<div class="doc_text">
346
Oscar Fuentesfd2f3e62010-08-09 02:49:39 +0000347<p>Testing is performed when the <i>check</i> target is built. For
348 instance, if you are using makefiles, execute this command while on
349 the top level of your build directory:</p>
Oscar Fuentes370387c2008-11-20 23:35:09 +0000350
Oscar Fuentesfd2f3e62010-08-09 02:49:39 +0000351<div class="doc_code">
352 <p><tt>make check</tt></p>
353</div>
354
355<p>Testing is not supported on Visual Studio.</p>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000356
357</div>
358
359<!-- *********************************************************************** -->
360<div class="doc_section">
361 <a name="cross">Cross compiling</a>
362</div>
363<!-- *********************************************************************** -->
364
365<div class="doc_text">
366
Oscar Fuentescbdbcb12009-06-11 04:45:08 +0000367<p>See <a href="http://www.vtk.org/Wiki/CMake_Cross_Compiling">this
368 wiki page</a> for generic instructions on how to cross-compile
369 with CMake. It goes into detailed explanations and may seem
370 daunting, but it is not. On the wiki page there are several
371 examples including toolchain files. Go directly to
372 <a href="http://www.vtk.org/Wiki/CMake_Cross_Compiling#Information_how_to_set_up_various_cross_compiling_toolchains">this
373 section</a> for a quick solution.</p>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000374
Oscar Fuentesa9ff1392009-09-13 22:18:38 +0000375<p>Also see the <a href="#llvmvars">LLVM-specific variables</a>
376 section for variables used when cross-compiling.</p>
377
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000378</div>
379
380<!-- *********************************************************************** -->
381<div class="doc_section">
382 <a name="embedding">Embedding LLVM in your project</a>
383</div>
384<!-- *********************************************************************** -->
385
386<div class="doc_text">
387
Oscar Fuentesc2d86882010-08-09 03:26:48 +0000388 <p>The most difficult part of adding LLVM to the build of a project
389 is to determine the set of LLVM libraries corresponding to the set
390 of required LLVM features. What follows is an example of how to
391 obtain this information:</p>
392
393 <div class="doc_code">
394 <pre>
395 <b># A convenience variable:</b>
396 set(LLVM_ROOT "" CACHE PATH "Root of LLVM install.")
397 <b># A bit of a sanity check:</b>
398 if( NOT EXISTS ${LLVM_ROOT}/include/llvm )
399 message(FATAL_ERROR "LLVM_ROOT (${LLVM_ROOT}) is not a valid LLVM install")
400 endif()
401 <b># We incorporate the CMake features provided by LLVM:</b>
402 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${LLVM_ROOT}/share/llvm/cmake")
403 include(LLVM)
404 <b># Now set the header and library paths:</b>
405 include_directories( ${LLVM_ROOT}/include )
406 link_directories( ${LLVM_ROOT}/lib )
407 <b># Let's suppose we want to build a JIT compiler with support for
408 # binary code (no interpreter):</b>
409 llvm_map_components_to_libraries(REQ_LLVM_LIBRARIES jit native)
410 <b># Finally, we link the LLVM libraries to our executable:</b>
411 target_link_libraries(mycompiler ${REQ_LLVM_LIBRARIES})
412 </pre>
413 </div>
414
415 <p>This assumes that LLVM_ROOT points to an install of LLVM. The
416 procedure works too for uninstalled builds although we need to take
417 care to add an <i>include_directories</i> for the location of the
418 headers on the LLVM source directory (if we are building
419 out-of-source.)</p>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000420
421</div>
422
423<!-- *********************************************************************** -->
424
Oscar Fuentes0c913732008-12-31 14:36:41 +0000425<!-- *********************************************************************** -->
426<div class="doc_section">
427 <a name="specifics">Compiler/Platform specific topics</a>
428</div>
429<!-- *********************************************************************** -->
430
431<div class="doc_text">
432
433<p>Notes for specific compilers and/or platforms.</p>
434
435</div>
436
Oscar Fuentes0c913732008-12-31 14:36:41 +0000437<!-- *********************************************************************** -->
438
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000439<hr>
440<address>
441 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000442 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000443 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000444 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000445
Oscar Fuentes370387c2008-11-20 23:35:09 +0000446 <a href="mailto:ofv@wanadoo.es">Oscar Fuentes</a><br>
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000447 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
Oscar Fuentesc2d86882010-08-09 03:26:48 +0000448 Last modified: $Date: 2010-08-09 03:59:36 +0100 (Mon, 9 Aug 2010) $
Oscar Fuentes25f1d032008-11-20 22:05:48 +0000449</address>
450
451</body>
452</html>