blob: 690a91120d16a209ed7124b8a1d8864468067506 [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>
5 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
6 <title>Clang - Getting Started</title>
7 <link type="text/css" rel="stylesheet" href="menu.css" />
8 <link type="text/css" rel="stylesheet" href="content.css" />
9</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
Ted Kremenek9b16da32009-03-27 16:32:57 +000021involved</a> with the Clang community.</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000022
Ted Kremenek9b16da32009-03-27 16:32:57 +000023<h2>A Word of Warning</h2>
Eric Christopher7dc0e572008-02-08 06:45:49 +000024
25<p>While this work aims to provide a fully functional C/C++/ObjC front-end, it
Daniel Dunbar05fc1c22009-05-24 01:00:12 +000026is still relatively new and under heavy development. Currently we believe clang
27to be very usable as a C and Objective-C compiler, however there is no real C++
28support yet (this is obviously a big project). Additionally, for C and
29Objective-C:</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000030
31<ol>
Chris Lattnerd8258832009-02-25 05:35:47 +000032 <li>The semantic analyzer does not produce all of the warnings it should.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000033 <li>We don't consider the API to be stable yet, and reserve the right to
34 change fundamental things.</li>
Daniel Dunbar05fc1c22009-05-24 01:00:12 +000035 <li>Only the X86-32 and X86-64 targets have been well tested.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000036</ol>
37
Daniel Dunbar05fc1c22009-05-24 01:00:12 +000038<p>If you run into problems, please file
Daniel Dunbarce9f4962009-05-24 01:40:10 +000039bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a> or bring up the issue
Daniel Dunbar05fc1c22009-05-24 01:00:12 +000040on the
Chris Lattnerd8258832009-02-25 05:35:47 +000041<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">Clang development
42mailing list</a>.</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000043
Ted Kremenek9b16da32009-03-27 16:32:57 +000044<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher7dc0e572008-02-08 06:45:49 +000045
Eli Friedmand1e1ef32009-08-03 19:42:28 +000046<h3 id="buildNix">On Unix-like Systems</h3>
47
Ted Kremenek9b16da32009-03-27 16:32:57 +000048<p>If you would like to check out and build Clang, the current procedure is as
49follows:</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000050
51<ol>
Anton Korobeynikov1816e482009-08-06 13:00:08 +000052 <li>Get the required tools.
53 <ul>
54 <li>See
55 <a href="http://llvm.org/docs/GettingStarted.html#requirements">
56 Getting Started with the LLVM System - Requirements</a>.</li>
57 <li>Note also that Python is needed for running the test suite.
58 Get it at: <a href="http://www.python.org/download">
59 http://www.python.org/download</a></li>
60 </ul>
61
Eric Christopher7dc0e572008-02-08 06:45:49 +000062 <li><a href="http://www.llvm.org/docs/GettingStarted.html#checkout">Checkout
63 and build LLVM</a> from SVN head:</li>
64
65 <ul>
66 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
67 <li><tt>cd llvm</tt></li>
68 <li><tt>./configure; make</tt></li>
69 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000070 <li>Checkout Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000071 <ul>
72 <li>From within the <tt>llvm</tt> directory (where you
73 built llvm):</li>
Ted Kremenek8c22bc32008-07-14 14:40:22 +000074 <li><tt>cd tools</tt>
Eric Christopher7dc0e572008-02-08 06:45:49 +000075 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
76
77 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000078 <li>If you intend to work on Clang C++ support, you may need to tell it how
79 to find your C++ standard library headers. If Clang cannot find your
Chris Lattnerca8c49f2009-03-10 16:01:44 +000080 system libstdc++ headers, please follow these instructions:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000081
82 <ul>
Chris Lattnerca8c49f2009-03-10 16:01:44 +000083 <li>'<tt>touch empty.cpp; gcc -v empty.cpp -fsyntax-only</tt>' to get the
Eric Christopher7dc0e572008-02-08 06:45:49 +000084 path.</li>
85 <li>Look for the comment "FIXME: temporary hack:
Chris Lattnerca8c49f2009-03-10 16:01:44 +000086 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher7dc0e572008-02-08 06:45:49 +000087 change the lines below to include that path.</li>
88 </ul>
89
Ted Kremenek9b16da32009-03-27 16:32:57 +000090 <li>Build Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000091 <ul>
92 <li><tt>cd clang</tt> (assuming that you are in <tt>llvm/tools</tt>)</li>
93 <li><tt>make</tt> (this will give you a debug build)</li>
94 </ul>
95
96 <li>Try it out (assuming you add llvm/Debug/bin to your path):</li>
97 <ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000098 <li><tt>clang-cc --help</tt></li>
99 <li><tt>clang-cc file.c -fsyntax-only</tt> (check for correctness)</li>
100 <li><tt>clang-cc file.c -ast-dump</tt> (internal debug dump of ast)</li>
101 <li><tt>clang-cc file.c -ast-view</tt> (<a
Eric Christopher7dc0e572008-02-08 06:45:49 +0000102 href="http://llvm.org/docs/ProgrammersManual.html#ViewGraph">set up graphviz
103 and rebuild llvm first</a>)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000104 <li><tt>clang-cc file.c -emit-llvm</tt> (print out unoptimized llvm code)</li>
105 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts |
Eric Christopher7dc0e572008-02-08 06:45:49 +0000106 llvm-dis</tt> (print out optimized llvm code)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000107 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc
Eric Christopher7dc0e572008-02-08 06:45:49 +0000108 &gt; file.s</tt> (output native machine code)</li>
109 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000110
111 <p><em>Note</em>: Here <tt>clang-cc</tt> is the "low-level" frontend
112 executable that is similar in purpose to <tt>cc1</tt>. Clang also has a <a
113 href="#driver">high-level compiler driver</a> that acts as a drop-in
114 replacement for <tt>gcc</tt>.
Eric Christopher7dc0e572008-02-08 06:45:49 +0000115</ol>
116
Ted Kremenek9b16da32009-03-27 16:32:57 +0000117<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
118encounter problems with building Clang, make sure you have the latest SVN
119version of LLVM. LLVM contains support libraries for Clang that will be updated
120as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +0000121
Ted Kremenek9b16da32009-03-27 16:32:57 +0000122<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +0000123
Ted Kremenek9b16da32009-03-27 16:32:57 +0000124<p>Once you have checked out Clang into the llvm source tree it will build along
125with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
126once simply run <tt>make</tt> from the root LLVM directory.</p>
127
128<p><em>Note:</em> Observe that Clang is technically part of a separate
129Subversion repository. As mentioned above, the latest Clang sources are tied to
130the latest sources in the LLVM tree. You can update your toplevel LLVM project
131and all (possibly unrelated) projects inside it with <tt><b>make
132update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
133to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000134
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000135<h3 id="buildWindows">Using Visual Studio</h3>
136
137<p>The following details setting up for and building Clang on Windows using
138Visual Studio:</p>
139
140<ol>
141 <li>Get the required tools:</li>
142 <ul>
143 <li><b>Subversion</b>. Source code control program. Get it from:
144 <a href="http://subversion.tigris.org/getting.html">
145 http://subversion.tigris.org/getting.html</a></li>
146 <li><b>cmake</b>. This is used for generating Visual Studio solution and
147 project files. Get it from:
148 <a href="http://www.cmake.org/cmake/resources/software.html">
149 http://www.cmake.org/cmake/resources/software.html</a></li>
150 <li><b>Visual Studio 2005</b>
151 (VS 2008 may work also - cmake outputs VS2005 project files)</li>
152 <li><b>Python</b>. This is need only if you will be running the tests
153 (which is essential, if you will be developing for clang).
154 Get it from:
155 <a href="http://www.python.org/download">
156 http://www.python.org/download</a></li>
157 </ul>
158
159 <li>Checkout LLVM:</li>
160 <ul>
161 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
162 </ul>
163 <li>Checkout Clang:</li>
164 <ul>
165 <li><tt>cd llvm\tools</tt>
166 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
167 </ul>
168 <li>Run cmake to generate the Visual Studio solution and project files:</li>
169 <ul>
170 <li><tt>cd ..</tt> (Change directory back to the llvm top.)</li>
171 <li><tt>cmake .</tt></li>
172 <li>The above, if successful, will have created an LLVM.sln file in the
173 llvm directory.
174 </ul>
175 <li>Build Clang:</li>
176 <ul>
177 <li>Open LLVM.sln in Visual Studio.</li>
178 <li>Build the "clang-cc" project for just the compiler front end.
179 Alternatively, build the "clang" project for the compiler driver
180 (note that the driver is currently broken on Windows),
181 or the "ALL_BUILD" project to build everything, including tools.</li>
182 </ul>
183 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
184 running examples from above.)</li>
185 <li>See <a href="hacking.html#testingWindows">
186 Hacking on clang - Testing using Visual Studio on Windows</a> for information
187 on running regression tests on Windows.</li>
188</ol>
189
190<p>Note that once you have checked out both llvm and clang, to synchronize
191to the latest code base, use the <tt>svn update</tt> command in both the
192llvm and llvm\tools\clang directories, as they are separate repositories.</p>
193
Ted Kremenek9b16da32009-03-27 16:32:57 +0000194<a name="driver"><h2>High-Level Compiler Driver (Drop-in Substitute for GCC)</h2></a>
195
196<p>While the <tt>clang-cc</tt> executable is a low-level frontend executable
197that can perform code generation, program analysis, and other actions, it is not
198designed to be a drop-in replacement for GCC's <tt>cc</tt>. For this purpose,
199use the high-level driver, aptly named <tt>clang</tt>. Here are some
200examples of how to use the high-level driver:
201</p>
202
203<pre class="code">
204$ <b>cat t.c</b>
205#include &lt;stdio.h&gt;
206int main(int argc, char **argv) { printf("hello world\n"); }
207$ <b>clang t.c</b>
208$ <b>./a.out</b>
209hello world
210</pre>
211
212<h2>Examples of using Clang</h2>
213
214<p>The high-level driver <tt>clang</tt> is designed to understand most of GCC's
215options, and the lower-level <tt>clang-cc</tt> executable also directly takes
216many of GCC's options. You can see which options <tt>clang-cc</tt> accepts with
217'<tt>clang-cc --help</tt>'. Here are a few examples of using <tt>clang</tt> and
218<tt>clang-cc</tt>:</p>
219
Eric Christopher7dc0e572008-02-08 06:45:49 +0000220<!-- Thanks to
221 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
222Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
223tag. -->
224
225<pre class="code">
226$ <b>cat ~/t.c</b>
227typedef float V __attribute__((vector_size(16)));
228V foo(V a, V b) { return a+b*a; }
229</pre>
230
231
Ted Kremenek35f29c52008-06-17 13:48:36 +0000232<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000233
234<pre class="code">
235$ <b>clang ~/t.c -E</b>
236# 1 "/Users/sabre/t.c" 1
237
238typedef float V __attribute__((vector_size(16)));
239
240V foo(V a, V b) { return a+b*a; }
241</pre>
242
243
Ted Kremenek35f29c52008-06-17 13:48:36 +0000244<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000245
246<pre class="code">
247$ <b>clang -fsyntax-only ~/t.c</b>
248</pre>
249
250
Ted Kremenek35f29c52008-06-17 13:48:36 +0000251<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000252
253<pre class="code">
254$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
255/Users/sabre/t.c:2:17: warning: extension used
256typedef float V __attribute__((vector_size(16)));
257 ^
2581 diagnostic generated.
259</pre>
260
261
Ted Kremenek35f29c52008-06-17 13:48:36 +0000262<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000263
264<pre class="code">
Ted Kremenek9b16da32009-03-27 16:32:57 +0000265$ <b>clang-cc ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000266typedef float V __attribute__(( vector_size(16) ));
267V foo(V a, V b) {
268 return a + b * a;
269}
270</pre>
271
272
Ted Kremenek35f29c52008-06-17 13:48:36 +0000273<h3>Code generation with LLVM:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000274
275<pre class="code">
Ted Kremenek9b16da32009-03-27 16:32:57 +0000276$ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llvm-dis</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000277define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
278entry:
279 %mul = mul &lt;4 x float&gt; %b, %a
280 %add = add &lt;4 x float&gt; %mul, %a
281 ret &lt;4 x float&gt; %add
282}
Ted Kremenek9b16da32009-03-27 16:32:57 +0000283$ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc -march=ppc32 -mcpu=g5</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000284..
285_foo:
286 vmaddfp v2, v3, v2, v2
287 blr
Ted Kremenek9b16da32009-03-27 16:32:57 +0000288$ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc -march=x86 -mcpu=yonah</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000289..
290_foo:
291 mulps %xmm0, %xmm1
292 addps %xmm0, %xmm1
293 movaps %xmm1, %xmm0
294 ret
295</pre>
296
Eric Christopher7dc0e572008-02-08 06:45:49 +0000297</div>
298</body>
299</html>