blob: 86f6cef35c050343a4a4da9736f70aa2bddd48d7 [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>
Cedric Venet6bfc8b62009-09-27 10:34:36 +0000144 <li><b>Visual Studio 2005 or 2008</b></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000145 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000146 (which is essential, if you will be developing for clang).
147 Get it from:
148 <a href="http://www.python.org/download">
149 http://www.python.org/download</a></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000150 <li><b>GnuWin32 tools</b>
151 These are also necessary for running the tests.
152 (Note that the grep from MSYS or Cygwin doesn't work with the tests
153 because of embedded double-quotes in the search strings. The GNU
154 grep does work in this case.)
155 Get them from <a href="http://getgnuwin32.sourceforge.net">
156 http://getgnuwin32.sourceforge.net</a>.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000157 </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>