blob: a2706231b6d82a428829168bdc1465efbf5b098c [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
Chris Lattner357f7ce2009-08-20 06:17:11 +000062 <li>Checkout LLVM:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000063 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000064 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000065 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000066 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000067 <li>Checkout Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000068 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000069 <li><tt>cd llvm/tools</tt>
70 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
71 </ul>
72 <li>Build LLVM and Clang:</li>
73 <ul>
74 <li><tt>cd ..</tt> (back to llvm)</li>
75 <li><tt>./configure</tt></li>
76 <li><tt>make</tt></li>
77 <li>This builds both LLVM and Clang for debug mode.</li>
78 <li>Note: For subsequent Clang development, you can just do make at the
79 clang directory level.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000080 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000081 <li>If you intend to work on Clang C++ support, you may need to tell it how
82 to find your C++ standard library headers. If Clang cannot find your
Chris Lattnerca8c49f2009-03-10 16:01:44 +000083 system libstdc++ headers, please follow these instructions:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000084 <ul>
Chris Lattnerca8c49f2009-03-10 16:01:44 +000085 <li>'<tt>touch empty.cpp; gcc -v empty.cpp -fsyntax-only</tt>' to get the
Eric Christopher7dc0e572008-02-08 06:45:49 +000086 path.</li>
87 <li>Look for the comment "FIXME: temporary hack:
Chris Lattnerca8c49f2009-03-10 16:01:44 +000088 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher7dc0e572008-02-08 06:45:49 +000089 change the lines below to include that path.</li>
90 </ul>
Eric Christopher7dc0e572008-02-08 06:45:49 +000091 <li>Try it out (assuming you add llvm/Debug/bin to your path):</li>
92 <ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000093 <li><tt>clang-cc --help</tt></li>
94 <li><tt>clang-cc file.c -fsyntax-only</tt> (check for correctness)</li>
95 <li><tt>clang-cc file.c -ast-dump</tt> (internal debug dump of ast)</li>
96 <li><tt>clang-cc file.c -ast-view</tt> (<a
Eric Christopher7dc0e572008-02-08 06:45:49 +000097 href="http://llvm.org/docs/ProgrammersManual.html#ViewGraph">set up graphviz
98 and rebuild llvm first</a>)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +000099 <li><tt>clang-cc file.c -emit-llvm</tt> (print out unoptimized llvm code)</li>
100 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts |
Eric Christopher7dc0e572008-02-08 06:45:49 +0000101 llvm-dis</tt> (print out optimized llvm code)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000102 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc
Eric Christopher7dc0e572008-02-08 06:45:49 +0000103 &gt; file.s</tt> (output native machine code)</li>
104 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000105 <p><em>Note</em>: Here <tt>clang-cc</tt> is the "low-level" frontend
106 executable that is similar in purpose to <tt>cc1</tt>. Clang also has a <a
107 href="#driver">high-level compiler driver</a> that acts as a drop-in
108 replacement for <tt>gcc</tt>.
Eric Christopher7dc0e572008-02-08 06:45:49 +0000109</ol>
110
Ted Kremenek9b16da32009-03-27 16:32:57 +0000111<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
112encounter problems with building Clang, make sure you have the latest SVN
113version of LLVM. LLVM contains support libraries for Clang that will be updated
114as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +0000115
Ted Kremenek9b16da32009-03-27 16:32:57 +0000116<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +0000117
Ted Kremenek9b16da32009-03-27 16:32:57 +0000118<p>Once you have checked out Clang into the llvm source tree it will build along
119with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
120once simply run <tt>make</tt> from the root LLVM directory.</p>
121
122<p><em>Note:</em> Observe that Clang is technically part of a separate
123Subversion repository. As mentioned above, the latest Clang sources are tied to
124the latest sources in the LLVM tree. You can update your toplevel LLVM project
125and all (possibly unrelated) projects inside it with <tt><b>make
126update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
127to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000128
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000129<h3 id="buildWindows">Using Visual Studio</h3>
130
131<p>The following details setting up for and building Clang on Windows using
132Visual Studio:</p>
133
134<ol>
135 <li>Get the required tools:</li>
136 <ul>
137 <li><b>Subversion</b>. Source code control program. Get it from:
138 <a href="http://subversion.tigris.org/getting.html">
139 http://subversion.tigris.org/getting.html</a></li>
140 <li><b>cmake</b>. This is used for generating Visual Studio solution and
141 project files. Get it from:
142 <a href="http://www.cmake.org/cmake/resources/software.html">
143 http://www.cmake.org/cmake/resources/software.html</a></li>
144 <li><b>Visual Studio 2005</b>
145 (VS 2008 may work also - cmake outputs VS2005 project files)</li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000146 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000147 (which is essential, if you will be developing for clang).
148 Get it from:
149 <a href="http://www.python.org/download">
150 http://www.python.org/download</a></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000151 <li><b>GnuWin32 tools</b>
152 These are also necessary for running the tests.
153 (Note that the grep from MSYS or Cygwin doesn't work with the tests
154 because of embedded double-quotes in the search strings. The GNU
155 grep does work in this case.)
156 Get them from <a href="http://getgnuwin32.sourceforge.net">
157 http://getgnuwin32.sourceforge.net</a>.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000158 </ul>
159
160 <li>Checkout LLVM:</li>
161 <ul>
162 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
163 </ul>
164 <li>Checkout Clang:</li>
165 <ul>
166 <li><tt>cd llvm\tools</tt>
167 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
168 </ul>
169 <li>Run cmake to generate the Visual Studio solution and project files:</li>
170 <ul>
171 <li><tt>cd ..</tt> (Change directory back to the llvm top.)</li>
172 <li><tt>cmake .</tt></li>
173 <li>The above, if successful, will have created an LLVM.sln file in the
174 llvm directory.
175 </ul>
176 <li>Build Clang:</li>
177 <ul>
178 <li>Open LLVM.sln in Visual Studio.</li>
179 <li>Build the "clang-cc" project for just the compiler front end.
180 Alternatively, build the "clang" project for the compiler driver
181 (note that the driver is currently broken on Windows),
182 or the "ALL_BUILD" project to build everything, including tools.</li>
183 </ul>
184 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
185 running examples from above.)</li>
186 <li>See <a href="hacking.html#testingWindows">
187 Hacking on clang - Testing using Visual Studio on Windows</a> for information
188 on running regression tests on Windows.</li>
189</ol>
190
191<p>Note that once you have checked out both llvm and clang, to synchronize
192to the latest code base, use the <tt>svn update</tt> command in both the
193llvm and llvm\tools\clang directories, as they are separate repositories.</p>
194
Ted Kremenek9b16da32009-03-27 16:32:57 +0000195<a name="driver"><h2>High-Level Compiler Driver (Drop-in Substitute for GCC)</h2></a>
196
197<p>While the <tt>clang-cc</tt> executable is a low-level frontend executable
198that can perform code generation, program analysis, and other actions, it is not
199designed to be a drop-in replacement for GCC's <tt>cc</tt>. For this purpose,
200use the high-level driver, aptly named <tt>clang</tt>. Here are some
201examples of how to use the high-level driver:
202</p>
203
204<pre class="code">
205$ <b>cat t.c</b>
206#include &lt;stdio.h&gt;
207int main(int argc, char **argv) { printf("hello world\n"); }
208$ <b>clang t.c</b>
209$ <b>./a.out</b>
210hello world
211</pre>
212
213<h2>Examples of using Clang</h2>
214
215<p>The high-level driver <tt>clang</tt> is designed to understand most of GCC's
216options, and the lower-level <tt>clang-cc</tt> executable also directly takes
217many of GCC's options. You can see which options <tt>clang-cc</tt> accepts with
218'<tt>clang-cc --help</tt>'. Here are a few examples of using <tt>clang</tt> and
219<tt>clang-cc</tt>:</p>
220
Eric Christopher7dc0e572008-02-08 06:45:49 +0000221<!-- Thanks to
222 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
223Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
224tag. -->
225
226<pre class="code">
227$ <b>cat ~/t.c</b>
228typedef float V __attribute__((vector_size(16)));
229V foo(V a, V b) { return a+b*a; }
230</pre>
231
232
Ted Kremenek35f29c52008-06-17 13:48:36 +0000233<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000234
235<pre class="code">
236$ <b>clang ~/t.c -E</b>
237# 1 "/Users/sabre/t.c" 1
238
239typedef float V __attribute__((vector_size(16)));
240
241V foo(V a, V b) { return a+b*a; }
242</pre>
243
244
Ted Kremenek35f29c52008-06-17 13:48:36 +0000245<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000246
247<pre class="code">
248$ <b>clang -fsyntax-only ~/t.c</b>
249</pre>
250
251
Ted Kremenek35f29c52008-06-17 13:48:36 +0000252<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000253
254<pre class="code">
255$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
256/Users/sabre/t.c:2:17: warning: extension used
257typedef float V __attribute__((vector_size(16)));
258 ^
2591 diagnostic generated.
260</pre>
261
262
Ted Kremenek35f29c52008-06-17 13:48:36 +0000263<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000264
265<pre class="code">
Ted Kremenek9b16da32009-03-27 16:32:57 +0000266$ <b>clang-cc ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000267typedef float V __attribute__(( vector_size(16) ));
268V foo(V a, V b) {
269 return a + b * a;
270}
271</pre>
272
273
Ted Kremenek35f29c52008-06-17 13:48:36 +0000274<h3>Code generation with LLVM:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000275
276<pre class="code">
Ted Kremenek9b16da32009-03-27 16:32:57 +0000277$ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llvm-dis</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000278define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
279entry:
280 %mul = mul &lt;4 x float&gt; %b, %a
281 %add = add &lt;4 x float&gt; %mul, %a
282 ret &lt;4 x float&gt; %add
283}
Ted Kremenek9b16da32009-03-27 16:32:57 +0000284$ <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 +0000285..
286_foo:
287 vmaddfp v2, v3, v2, v2
288 blr
Ted Kremenek9b16da32009-03-27 16:32:57 +0000289$ <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 +0000290..
291_foo:
292 mulps %xmm0, %xmm1
293 addps %xmm0, %xmm1
294 movaps %xmm1, %xmm0
295 ret
296</pre>
297
Eric Christopher7dc0e572008-02-08 06:45:49 +0000298</div>
299</body>
300</html>