blob: ca9911297164b80c2a4e4fb90f8c450508a1735e [file] [log] [blame]
Eric Christopher7dc0e572008-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 Kramer665a8dc2012-01-15 15:26:07 +00005 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Eric Christopher7dc0e572008-02-08 06:45:49 +00006 <title>Clang - Getting Started</title>
Benjamin Kramer665a8dc2012-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 Christopher7dc0e572008-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 Kremenek9b16da32009-03-27 16:32:57 +000018<p>This page gives you the shortest path to checking out Clang and demos a few
Eric Christopher7dc0e572008-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 Lattnerd2c1c602009-11-09 03:18:18 +000021involved</a> with the Clang community. If you run into problems, please file
Douglas Gregor9ff3e662010-10-07 20:20:57 +000022bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000023
Daniel Dunbarfa8e11b2010-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 Kremenek9b16da32009-03-27 16:32:57 +000030<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher7dc0e572008-02-08 06:45:49 +000031
Eli Friedmand1e1ef32009-08-03 19:42:28 +000032<h3 id="buildNix">On Unix-like Systems</h3>
33
Ted Kremenek9b16da32009-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 Christopher7dc0e572008-02-08 06:45:49 +000036
37<ol>
Anton Korobeynikov1816e482009-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 Kramer665a8dc2012-01-15 15:26:07 +000048 <li>Checkout LLVM:
Eric Christopher7dc0e572008-02-08 06:45:49 +000049 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000050 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000051 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000052 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +000053 </li>
54 <li>Checkout Clang:
Eric Christopher7dc0e572008-02-08 06:45:49 +000055 <ul>
Chris Lattner357f7ce2009-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 Kramer665a8dc2012-01-15 15:26:07 +000059 </li>
60 <li>Checkout Compiler-RT:
Daniel Dunbar078f8592011-12-07 03:04:42 +000061 <ul>
62 <li><tt>cd llvm/projects</tt>
63 <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
64 compiler-rt</tt></li>
65 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +000066 </li>
67 <li>Build LLVM and Clang:
Chris Lattner357f7ce2009-08-20 06:17:11 +000068 <ul>
Rafael Espindolafa5fa972010-10-29 22:05:17 +000069 <li><tt>cd ../..</tt> (back to where you started)</li>
70 <li><tt>mkdir build</tt> (for building without polluting the source dir)
71 </li>
72 <li><tt>cd build</tt></li>
73 <li><tt>../llvm/configure</tt></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +000074 <li><tt>make</tt></li>
75 <li>This builds both LLVM and Clang for debug mode.</li>
76 <li>Note: For subsequent Clang development, you can just do make at the
77 clang directory level.</li>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +000078 <li>It is also possible to use CMake instead of the makefiles. With CMake
79 it is also possible to generate project files for several IDEs: Eclipse
80 CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000081 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +000082 </li>
Chris Lattner78781782010-05-06 21:57:57 +000083
Ted Kremenek9b16da32009-03-27 16:32:57 +000084 <li>If you intend to work on Clang C++ support, you may need to tell it how
85 to find your C++ standard library headers. If Clang cannot find your
Benjamin Kramer665a8dc2012-01-15 15:26:07 +000086 system libstdc++ headers, please follow these instructions:
Eric Christopher7dc0e572008-02-08 06:45:49 +000087 <ul>
Gabor Greif74350822010-03-29 21:36:06 +000088 <li>'<tt>gcc -v -x c++ /dev/null -fsyntax-only</tt>' to get the
Eric Christopher7dc0e572008-02-08 06:45:49 +000089 path.</li>
90 <li>Look for the comment "FIXME: temporary hack:
Chris Lattnerca8c49f2009-03-10 16:01:44 +000091 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher7dc0e572008-02-08 06:45:49 +000092 change the lines below to include that path.</li>
93 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +000094 </li>
95 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):
Eric Christopher7dc0e572008-02-08 06:45:49 +000096 <ul>
Daniel Dunbardd63b282009-12-11 23:04:35 +000097 <li><tt>clang --help</tt></li>
98 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
99 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
100 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
101 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000102 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000103 </li>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000104</ol>
105
Ted Kremenek9b16da32009-03-27 16:32:57 +0000106<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
107encounter problems with building Clang, make sure you have the latest SVN
108version of LLVM. LLVM contains support libraries for Clang that will be updated
109as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +0000110
Ted Kremenek9b16da32009-03-27 16:32:57 +0000111<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +0000112
Ted Kremenek9b16da32009-03-27 16:32:57 +0000113<p>Once you have checked out Clang into the llvm source tree it will build along
114with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
115once simply run <tt>make</tt> from the root LLVM directory.</p>
116
117<p><em>Note:</em> Observe that Clang is technically part of a separate
118Subversion repository. As mentioned above, the latest Clang sources are tied to
119the latest sources in the LLVM tree. You can update your toplevel LLVM project
120and all (possibly unrelated) projects inside it with <tt><b>make
121update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
122to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000123
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000124<h3 id="buildWindows">Using Visual Studio</h3>
125
126<p>The following details setting up for and building Clang on Windows using
127Visual Studio:</p>
128
129<ol>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000130 <li>Get the required tools:
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000131 <ul>
132 <li><b>Subversion</b>. Source code control program. Get it from:
133 <a href="http://subversion.tigris.org/getting.html">
134 http://subversion.tigris.org/getting.html</a></li>
135 <li><b>cmake</b>. This is used for generating Visual Studio solution and
136 project files. Get it from:
137 <a href="http://www.cmake.org/cmake/resources/software.html">
138 http://www.cmake.org/cmake/resources/software.html</a></li>
Chandler Carruth245a5382011-11-16 19:29:07 +0000139 <li><b>Visual Studio 2008 or 2010</b></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000140 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000141 (which is essential, if you will be developing for clang).
142 Get it from:
NAKAMURA Takumicc8770a2011-03-27 02:04:21 +0000143 <a href="http://www.python.org/download/">
144 http://www.python.org/download/</a></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000145 <li><b>GnuWin32 tools</b>
146 These are also necessary for running the tests.
147 (Note that the grep from MSYS or Cygwin doesn't work with the tests
148 because of embedded double-quotes in the search strings. The GNU
149 grep does work in this case.)
NAKAMURA Takumicc8770a2011-03-27 02:04:21 +0000150 Get them from <a href="http://getgnuwin32.sourceforge.net/">
151 http://getgnuwin32.sourceforge.net/</a>.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000152 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000153 </li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000154
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000155 <li>Checkout LLVM:
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000156 <ul>
157 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
158 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000159 </li>
160 <li>Checkout Clang:
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000161 <ul>
162 <li><tt>cd llvm\tools</tt>
163 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
164 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000165 </li>
166 <li>Run cmake to generate the Visual Studio solution and project files:
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000167 <ul>
Michael J. Spencer0de57862010-12-16 22:01:14 +0000168 <li><tt>cd ..\..</tt> (back to where you started)</li>
169 <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
170 <li><tt>cd build</tt></li>
Chandler Carruth245a5382011-11-16 19:29:07 +0000171 <li>If you are using Visual Studio 2008: <tt>cmake -G "Visual Studio 9 2008" ..\llvm</tt></li>
Michael J. Spencer0de57862010-12-16 22:01:14 +0000172 <li>Or if you are using Visual Studio 2010: <tt>cmake -G "Visual Studio 10" ..\llvm</tt></li>
John Thompson13eb5652011-04-06 18:22:12 +0000173 <li>By default, cmake will target LLVM to X86. If you want all targets
174 (needed if you want to run the LLVM tests), add the <tt>-DLLVM_TARGETS_TO_BUILD=all</tt> option to the
175 cmake command line. Or specify a target from the LLVM_TARGETS_TO_BUILD
176 definition in CMakeLists.txt.</li>
177 <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
178 more information on other configuration options for cmake.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000179 <li>The above, if successful, will have created an LLVM.sln file in the
NAKAMURA Takumi542c7622011-07-25 12:40:26 +0000180 <tt>build</tt> directory.
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000181 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000182 </li>
183 <li>Build Clang:
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000184 <ul>
185 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbardd63b282009-12-11 23:04:35 +0000186 <li>Build the "clang" project for just the compiler driver and front end, or
187 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000188 </ul>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000189 </li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000190 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
191 running examples from above.)</li>
192 <li>See <a href="hacking.html#testingWindows">
193 Hacking on clang - Testing using Visual Studio on Windows</a> for information
194 on running regression tests on Windows.</li>
195</ol>
196
197<p>Note that once you have checked out both llvm and clang, to synchronize
198to the latest code base, use the <tt>svn update</tt> command in both the
199llvm and llvm\tools\clang directories, as they are separate repositories.</p>
200
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000201<h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000202
Daniel Dunbardd63b282009-12-11 23:04:35 +0000203<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
204designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
205some examples of how to use the high-level driver:
Ted Kremenek9b16da32009-03-27 16:32:57 +0000206</p>
207
208<pre class="code">
209$ <b>cat t.c</b>
210#include &lt;stdio.h&gt;
211int main(int argc, char **argv) { printf("hello world\n"); }
212$ <b>clang t.c</b>
213$ <b>./a.out</b>
214hello world
215</pre>
216
Chris Lattner63d423d2009-11-09 03:21:02 +0000217<p>The 'clang' driver is designed to work as closely to GCC as possible to
218 maximize portability. The only major difference between the two is that
219 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnerd73fef62009-11-09 04:04:07 +0000220 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattner63d423d2009-11-09 03:21:02 +0000221 to clang.</p>
222
Ted Kremenek9b16da32009-03-27 16:32:57 +0000223<h2>Examples of using Clang</h2>
224
Eric Christopher7dc0e572008-02-08 06:45:49 +0000225<!-- Thanks to
226 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
227Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
228tag. -->
229
230<pre class="code">
231$ <b>cat ~/t.c</b>
232typedef float V __attribute__((vector_size(16)));
233V foo(V a, V b) { return a+b*a; }
234</pre>
235
236
Ted Kremenek35f29c52008-06-17 13:48:36 +0000237<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000238
239<pre class="code">
240$ <b>clang ~/t.c -E</b>
241# 1 "/Users/sabre/t.c" 1
242
243typedef float V __attribute__((vector_size(16)));
244
245V foo(V a, V b) { return a+b*a; }
246</pre>
247
248
Ted Kremenek35f29c52008-06-17 13:48:36 +0000249<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000250
251<pre class="code">
252$ <b>clang -fsyntax-only ~/t.c</b>
253</pre>
254
255
Ted Kremenek35f29c52008-06-17 13:48:36 +0000256<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000257
258<pre class="code">
259$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000260/Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
261<span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
262<span style="color:blue"> ^</span>
Eric Christopher7dc0e572008-02-08 06:45:49 +00002631 diagnostic generated.
264</pre>
265
266
Ted Kremenek35f29c52008-06-17 13:48:36 +0000267<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000268
Daniel Dunbardd63b282009-12-11 23:04:35 +0000269<p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and
270not the driver, should be run. The compiler front-end has several additional
271Clang specific features which are not exposed through the GCC compatible driver
272interface.</p>
273
Eric Christopher7dc0e572008-02-08 06:45:49 +0000274<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000275$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000276typedef float V __attribute__(( vector_size(16) ));
277V foo(V a, V b) {
278 return a + b * a;
279}
280</pre>
281
282
Ted Kremenek35f29c52008-06-17 13:48:36 +0000283<h3>Code generation with LLVM:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000284
285<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000286$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000287define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
288entry:
289 %mul = mul &lt;4 x float&gt; %b, %a
290 %add = add &lt;4 x float&gt; %mul, %a
291 ret &lt;4 x float&gt; %add
292}
Daniel Dunbardd63b282009-12-11 23:04:35 +0000293$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
294...
Eric Christopher7dc0e572008-02-08 06:45:49 +0000295_foo:
Daniel Dunbardd63b282009-12-11 23:04:35 +0000296Leh_func_begin1:
297 mulps %xmm0, %xmm1
298 addps %xmm1, %xmm0
299 ret
300Leh_func_end1:
Eric Christopher7dc0e572008-02-08 06:45:49 +0000301</pre>
302
Eric Christopher7dc0e572008-02-08 06:45:49 +0000303</div>
304</body>
305</html>