blob: 8e339b20146ae4443ee36f705d36f679b780f86a [file] [log] [blame]
Eric Christopher2dc30a52008-02-08 06:45:49 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Benjamin Kramereaa262b2012-01-15 15:26:07 +00005 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Eric Christopher2dc30a52008-02-08 06:45:49 +00006 <title>Clang - Getting Started</title>
Benjamin Kramereaa262b2012-01-15 15:26:07 +00007 <link type="text/css" rel="stylesheet" href="menu.css">
8 <link type="text/css" rel="stylesheet" href="content.css">
Eric Christopher2dc30a52008-02-08 06:45:49 +00009</head>
10<body>
11
12<!--#include virtual="menu.html.incl"-->
13
14<div id="content">
15
16<h1>Getting Started: Building and Running Clang</h1>
17
Ted Kremenekb3e8f652009-03-27 16:32:57 +000018<p>This page gives you the shortest path to checking out Clang and demos a few
Eric Christopher2dc30a52008-02-08 06:45:49 +000019options. This should get you up and running with the minimum of muss and fuss.
20If you like what you see, please consider <a href="get_involved.html">getting
Chris Lattnerb8ca9542009-11-09 03:18:18 +000021involved</a> with the Clang community. If you run into problems, please file
Douglas Gregora8810102010-10-07 20:20:57 +000022bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
Eric Christopher2dc30a52008-02-08 06:45:49 +000023
Daniel Dunbar6b835382010-11-02 22:34:04 +000024<h2 id="download">Release Clang Versions</h2>
25
26<p>Clang has been released as part of regular LLVM releases since LLVM 2.6. You
27can download the release versions
28from <a href="http://llvm.org/releases/">http://llvm.org/releases/</a>.</p>
29
Ted Kremenekb3e8f652009-03-27 16:32:57 +000030<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher2dc30a52008-02-08 06:45:49 +000031
Eli Friedman05194922009-08-03 19:42:28 +000032<h3 id="buildNix">On Unix-like Systems</h3>
33
Ted Kremenekb3e8f652009-03-27 16:32:57 +000034<p>If you would like to check out and build Clang, the current procedure is as
35follows:</p>
Eric Christopher2dc30a52008-02-08 06:45:49 +000036
37<ol>
Anton Korobeynikov3ebc08b2009-08-06 13:00:08 +000038 <li>Get the required tools.
39 <ul>
40 <li>See
41 <a href="http://llvm.org/docs/GettingStarted.html#requirements">
42 Getting Started with the LLVM System - Requirements</a>.</li>
43 <li>Note also that Python is needed for running the test suite.
44 Get it at: <a href="http://www.python.org/download">
45 http://www.python.org/download</a></li>
46 </ul>
47
Benjamin Kramereaa262b2012-01-15 15:26:07 +000048 <li>Checkout LLVM:
Eric Christopher2dc30a52008-02-08 06:45:49 +000049 <ul>
Chris Lattnerf6bad862009-08-20 06:17:11 +000050 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000051 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000052 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000053 </li>
54 <li>Checkout Clang:
Eric Christopher2dc30a52008-02-08 06:45:49 +000055 <ul>
Chris Lattnerf6bad862009-08-20 06:17:11 +000056 <li><tt>cd llvm/tools</tt>
57 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
58 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000059 </li>
60 <li>Checkout Compiler-RT:
Daniel Dunbar2931cd92011-12-07 03:04:42 +000061 <ul>
Benjamin Kramer5998c032012-02-09 16:04:50 +000062 <li><tt>cd ../..</tt> (back to where you started)</li>
Daniel Dunbar2931cd92011-12-07 03:04:42 +000063 <li><tt>cd llvm/projects</tt>
64 <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
65 compiler-rt</tt></li>
66 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000067 </li>
68 <li>Build LLVM and Clang:
Chris Lattnerf6bad862009-08-20 06:17:11 +000069 <ul>
Rafael Espindola4f8857e2010-10-29 22:05:17 +000070 <li><tt>cd ../..</tt> (back to where you started)</li>
71 <li><tt>mkdir build</tt> (for building without polluting the source dir)
72 </li>
73 <li><tt>cd build</tt></li>
74 <li><tt>../llvm/configure</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +000075 <li><tt>make</tt></li>
76 <li>This builds both LLVM and Clang for debug mode.</li>
77 <li>Note: For subsequent Clang development, you can just do make at the
78 clang directory level.</li>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000079 <li>It is also possible to use CMake instead of the makefiles. With CMake
80 it is also possible to generate project files for several IDEs: Eclipse
81 CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000082 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000083 </li>
Chris Lattner6eeb2212010-05-06 21:57:57 +000084
Ted Kremenekb3e8f652009-03-27 16:32:57 +000085 <li>If you intend to work on Clang C++ support, you may need to tell it how
Chandler Carruthcc312312012-05-16 08:18:58 +000086 to find your C++ standard library headers. In general, Clang will detect
87 the best version of libstdc++ headers available and use them - it will
88 look both for system installations of libstdc++ as well as installations
89 adjacent to Clang itself. If your configuration fits neither of these
90 scenarios, you can use the <tt>--with-gcc-toolchain</tt> configure option
91 to tell Clang where the gcc containing the desired libstdc++ is installed.
Benjamin Kramereaa262b2012-01-15 15:26:07 +000092 </li>
93 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):
Eric Christopher2dc30a52008-02-08 06:45:49 +000094 <ul>
Daniel Dunbar520d1e62009-12-11 23:04:35 +000095 <li><tt>clang --help</tt></li>
96 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
97 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
98 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
99 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000100 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000101 </li>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000102</ol>
103
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000104<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
105encounter problems with building Clang, make sure you have the latest SVN
106version of LLVM. LLVM contains support libraries for Clang that will be updated
107as well as development on Clang progresses.</p>
Ted Kremenek09d6ba12008-02-08 07:32:26 +0000108
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000109<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher6aa87cb2008-02-08 07:10:48 +0000110
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000111<p>Once you have checked out Clang into the llvm source tree it will build along
112with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
113once simply run <tt>make</tt> from the root LLVM directory.</p>
114
115<p><em>Note:</em> Observe that Clang is technically part of a separate
116Subversion repository. As mentioned above, the latest Clang sources are tied to
117the latest sources in the LLVM tree. You can update your toplevel LLVM project
118and all (possibly unrelated) projects inside it with <tt><b>make
119update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
120to subversion. </p>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000121
Eli Friedman05194922009-08-03 19:42:28 +0000122<h3 id="buildWindows">Using Visual Studio</h3>
123
124<p>The following details setting up for and building Clang on Windows using
125Visual Studio:</p>
126
127<ol>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000128 <li>Get the required tools:
Eli Friedman05194922009-08-03 19:42:28 +0000129 <ul>
130 <li><b>Subversion</b>. Source code control program. Get it from:
131 <a href="http://subversion.tigris.org/getting.html">
132 http://subversion.tigris.org/getting.html</a></li>
133 <li><b>cmake</b>. This is used for generating Visual Studio solution and
134 project files. Get it from:
135 <a href="http://www.cmake.org/cmake/resources/software.html">
136 http://www.cmake.org/cmake/resources/software.html</a></li>
Chandler Carruth74f0df42011-11-16 19:29:07 +0000137 <li><b>Visual Studio 2008 or 2010</b></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +0000138 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedman05194922009-08-03 19:42:28 +0000139 (which is essential, if you will be developing for clang).
140 Get it from:
NAKAMURA Takumi823f3eb2011-03-27 02:04:21 +0000141 <a href="http://www.python.org/download/">
142 http://www.python.org/download/</a></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +0000143 <li><b>GnuWin32 tools</b>
144 These are also necessary for running the tests.
145 (Note that the grep from MSYS or Cygwin doesn't work with the tests
146 because of embedded double-quotes in the search strings. The GNU
147 grep does work in this case.)
NAKAMURA Takumi823f3eb2011-03-27 02:04:21 +0000148 Get them from <a href="http://getgnuwin32.sourceforge.net/">
149 http://getgnuwin32.sourceforge.net/</a>.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000150 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000151 </li>
Eli Friedman05194922009-08-03 19:42:28 +0000152
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000153 <li>Checkout LLVM:
Eli Friedman05194922009-08-03 19:42:28 +0000154 <ul>
155 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
156 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000157 </li>
158 <li>Checkout Clang:
Eli Friedman05194922009-08-03 19:42:28 +0000159 <ul>
160 <li><tt>cd llvm\tools</tt>
161 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
162 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000163 </li>
164 <li>Run cmake to generate the Visual Studio solution and project files:
Eli Friedman05194922009-08-03 19:42:28 +0000165 <ul>
Michael J. Spencer7ed7afc2010-12-16 22:01:14 +0000166 <li><tt>cd ..\..</tt> (back to where you started)</li>
167 <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
168 <li><tt>cd build</tt></li>
Chandler Carruth74f0df42011-11-16 19:29:07 +0000169 <li>If you are using Visual Studio 2008: <tt>cmake -G "Visual Studio 9 2008" ..\llvm</tt></li>
Michael J. Spencer7ed7afc2010-12-16 22:01:14 +0000170 <li>Or if you are using Visual Studio 2010: <tt>cmake -G "Visual Studio 10" ..\llvm</tt></li>
John Thompson9bfba162011-04-06 18:22:12 +0000171 <li>By default, cmake will target LLVM to X86. If you want all targets
172 (needed if you want to run the LLVM tests), add the <tt>-DLLVM_TARGETS_TO_BUILD=all</tt> option to the
173 cmake command line. Or specify a target from the LLVM_TARGETS_TO_BUILD
174 definition in CMakeLists.txt.</li>
175 <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
176 more information on other configuration options for cmake.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000177 <li>The above, if successful, will have created an LLVM.sln file in the
NAKAMURA Takumib44cd822011-07-25 12:40:26 +0000178 <tt>build</tt> directory.
Eli Friedman05194922009-08-03 19:42:28 +0000179 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000180 </li>
181 <li>Build Clang:
Eli Friedman05194922009-08-03 19:42:28 +0000182 <ul>
183 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000184 <li>Build the "clang" project for just the compiler driver and front end, or
185 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000186 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000187 </li>
Eli Friedman05194922009-08-03 19:42:28 +0000188 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
189 running examples from above.)</li>
190 <li>See <a href="hacking.html#testingWindows">
191 Hacking on clang - Testing using Visual Studio on Windows</a> for information
192 on running regression tests on Windows.</li>
193</ol>
194
195<p>Note that once you have checked out both llvm and clang, to synchronize
196to the latest code base, use the <tt>svn update</tt> command in both the
197llvm and llvm\tools\clang directories, as they are separate repositories.</p>
198
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000199<h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000200
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000201<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
202designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
203some examples of how to use the high-level driver:
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000204</p>
205
206<pre class="code">
207$ <b>cat t.c</b>
208#include &lt;stdio.h&gt;
209int main(int argc, char **argv) { printf("hello world\n"); }
210$ <b>clang t.c</b>
211$ <b>./a.out</b>
212hello world
213</pre>
214
Chris Lattnere5eb7262009-11-09 03:21:02 +0000215<p>The 'clang' driver is designed to work as closely to GCC as possible to
216 maximize portability. The only major difference between the two is that
217 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnere6e64be2009-11-09 04:04:07 +0000218 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattnere5eb7262009-11-09 03:21:02 +0000219 to clang.</p>
220
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000221<h2>Examples of using Clang</h2>
222
Eric Christopher2dc30a52008-02-08 06:45:49 +0000223<!-- Thanks to
224 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
225Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
226tag. -->
227
228<pre class="code">
229$ <b>cat ~/t.c</b>
230typedef float V __attribute__((vector_size(16)));
231V foo(V a, V b) { return a+b*a; }
232</pre>
233
234
Ted Kremenek77251e92008-06-17 13:48:36 +0000235<h3>Preprocessing:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000236
237<pre class="code">
238$ <b>clang ~/t.c -E</b>
239# 1 "/Users/sabre/t.c" 1
240
241typedef float V __attribute__((vector_size(16)));
242
243V foo(V a, V b) { return a+b*a; }
244</pre>
245
246
Ted Kremenek77251e92008-06-17 13:48:36 +0000247<h3>Type checking:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000248
249<pre class="code">
250$ <b>clang -fsyntax-only ~/t.c</b>
251</pre>
252
253
Ted Kremenek77251e92008-06-17 13:48:36 +0000254<h3>GCC options:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000255
256<pre class="code">
257$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000258/Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
259<span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
260<span style="color:blue"> ^</span>
Eric Christopher2dc30a52008-02-08 06:45:49 +00002611 diagnostic generated.
262</pre>
263
264
Ted Kremenek77251e92008-06-17 13:48:36 +0000265<h3>Pretty printing from the AST:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000266
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000267<p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000268not the driver, should be run. The compiler front-end has several additional
269Clang specific features which are not exposed through the GCC compatible driver
270interface.</p>
271
Eric Christopher2dc30a52008-02-08 06:45:49 +0000272<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000273$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000274typedef float V __attribute__(( vector_size(16) ));
275V foo(V a, V b) {
276 return a + b * a;
277}
278</pre>
279
280
Ted Kremenek77251e92008-06-17 13:48:36 +0000281<h3>Code generation with LLVM:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000282
283<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000284$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000285define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
286entry:
287 %mul = mul &lt;4 x float&gt; %b, %a
288 %add = add &lt;4 x float&gt; %mul, %a
289 ret &lt;4 x float&gt; %add
290}
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000291$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
292...
Eric Christopher2dc30a52008-02-08 06:45:49 +0000293_foo:
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000294Leh_func_begin1:
295 mulps %xmm0, %xmm1
296 addps %xmm1, %xmm0
297 ret
298Leh_func_end1:
Eric Christopher2dc30a52008-02-08 06:45:49 +0000299</pre>
300
Eric Christopher2dc30a52008-02-08 06:45:49 +0000301</div>
302</body>
303</html>