blob: d8b279134e1c0d03ea71a43615a08a52a6c8f153 [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>
52 <li><a href="http://www.llvm.org/docs/GettingStarted.html#checkout">Checkout
53 and build LLVM</a> from SVN head:</li>
54
55 <ul>
56 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
57 <li><tt>cd llvm</tt></li>
58 <li><tt>./configure; make</tt></li>
59 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000060 <li>Checkout Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000061 <ul>
62 <li>From within the <tt>llvm</tt> directory (where you
63 built llvm):</li>
Ted Kremenek8c22bc32008-07-14 14:40:22 +000064 <li><tt>cd tools</tt>
Eric Christopher7dc0e572008-02-08 06:45:49 +000065 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
66
67 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000068 <li>If you intend to work on Clang C++ support, you may need to tell it how
69 to find your C++ standard library headers. If Clang cannot find your
Chris Lattnerca8c49f2009-03-10 16:01:44 +000070 system libstdc++ headers, please follow these instructions:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000071
72 <ul>
Chris Lattnerca8c49f2009-03-10 16:01:44 +000073 <li>'<tt>touch empty.cpp; gcc -v empty.cpp -fsyntax-only</tt>' to get the
Eric Christopher7dc0e572008-02-08 06:45:49 +000074 path.</li>
75 <li>Look for the comment "FIXME: temporary hack:
Chris Lattnerca8c49f2009-03-10 16:01:44 +000076 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher7dc0e572008-02-08 06:45:49 +000077 change the lines below to include that path.</li>
78 </ul>
79
Ted Kremenek9b16da32009-03-27 16:32:57 +000080 <li>Build Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000081 <ul>
82 <li><tt>cd clang</tt> (assuming that you are in <tt>llvm/tools</tt>)</li>
83 <li><tt>make</tt> (this will give you a debug build)</li>
84 </ul>
85
86 <li>Try it out (assuming you add llvm/Debug/bin to your path):</li>
87 <ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000088 <li><tt>clang-cc --help</tt></li>
89 <li><tt>clang-cc file.c -fsyntax-only</tt> (check for correctness)</li>
90 <li><tt>clang-cc file.c -ast-dump</tt> (internal debug dump of ast)</li>
91 <li><tt>clang-cc file.c -ast-view</tt> (<a
Eric Christopher7dc0e572008-02-08 06:45:49 +000092 href="http://llvm.org/docs/ProgrammersManual.html#ViewGraph">set up graphviz
93 and rebuild llvm first</a>)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +000094 <li><tt>clang-cc file.c -emit-llvm</tt> (print out unoptimized llvm code)</li>
95 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts |
Eric Christopher7dc0e572008-02-08 06:45:49 +000096 llvm-dis</tt> (print out optimized llvm code)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +000097 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc
Eric Christopher7dc0e572008-02-08 06:45:49 +000098 &gt; file.s</tt> (output native machine code)</li>
99 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000100
101 <p><em>Note</em>: Here <tt>clang-cc</tt> is the "low-level" frontend
102 executable that is similar in purpose to <tt>cc1</tt>. Clang also has a <a
103 href="#driver">high-level compiler driver</a> that acts as a drop-in
104 replacement for <tt>gcc</tt>.
Eric Christopher7dc0e572008-02-08 06:45:49 +0000105</ol>
106
Ted Kremenek9b16da32009-03-27 16:32:57 +0000107<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
108encounter problems with building Clang, make sure you have the latest SVN
109version of LLVM. LLVM contains support libraries for Clang that will be updated
110as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +0000111
Ted Kremenek9b16da32009-03-27 16:32:57 +0000112<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +0000113
Ted Kremenek9b16da32009-03-27 16:32:57 +0000114<p>Once you have checked out Clang into the llvm source tree it will build along
115with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
116once simply run <tt>make</tt> from the root LLVM directory.</p>
117
118<p><em>Note:</em> Observe that Clang is technically part of a separate
119Subversion repository. As mentioned above, the latest Clang sources are tied to
120the latest sources in the LLVM tree. You can update your toplevel LLVM project
121and all (possibly unrelated) projects inside it with <tt><b>make
122update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
123to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000124
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000125<h3 id="buildWindows">Using Visual Studio</h3>
126
127<p>The following details setting up for and building Clang on Windows using
128Visual Studio:</p>
129
130<ol>
131 <li>Get the required tools:</li>
132 <ul>
133 <li><b>Subversion</b>. Source code control program. Get it from:
134 <a href="http://subversion.tigris.org/getting.html">
135 http://subversion.tigris.org/getting.html</a></li>
136 <li><b>cmake</b>. This is used for generating Visual Studio solution and
137 project files. Get it from:
138 <a href="http://www.cmake.org/cmake/resources/software.html">
139 http://www.cmake.org/cmake/resources/software.html</a></li>
140 <li><b>Visual Studio 2005</b>
141 (VS 2008 may work also - cmake outputs VS2005 project files)</li>
142 <li><b>Python</b>. This is need only if you will be running the tests
143 (which is essential, if you will be developing for clang).
144 Get it from:
145 <a href="http://www.python.org/download">
146 http://www.python.org/download</a></li>
147 </ul>
148
149 <li>Checkout LLVM:</li>
150 <ul>
151 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
152 </ul>
153 <li>Checkout Clang:</li>
154 <ul>
155 <li><tt>cd llvm\tools</tt>
156 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
157 </ul>
158 <li>Run cmake to generate the Visual Studio solution and project files:</li>
159 <ul>
160 <li><tt>cd ..</tt> (Change directory back to the llvm top.)</li>
161 <li><tt>cmake .</tt></li>
162 <li>The above, if successful, will have created an LLVM.sln file in the
163 llvm directory.
164 </ul>
165 <li>Build Clang:</li>
166 <ul>
167 <li>Open LLVM.sln in Visual Studio.</li>
168 <li>Build the "clang-cc" project for just the compiler front end.
169 Alternatively, build the "clang" project for the compiler driver
170 (note that the driver is currently broken on Windows),
171 or the "ALL_BUILD" project to build everything, including tools.</li>
172 </ul>
173 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
174 running examples from above.)</li>
175 <li>See <a href="hacking.html#testingWindows">
176 Hacking on clang - Testing using Visual Studio on Windows</a> for information
177 on running regression tests on Windows.</li>
178</ol>
179
180<p>Note that once you have checked out both llvm and clang, to synchronize
181to the latest code base, use the <tt>svn update</tt> command in both the
182llvm and llvm\tools\clang directories, as they are separate repositories.</p>
183
Ted Kremenek9b16da32009-03-27 16:32:57 +0000184<a name="driver"><h2>High-Level Compiler Driver (Drop-in Substitute for GCC)</h2></a>
185
186<p>While the <tt>clang-cc</tt> executable is a low-level frontend executable
187that can perform code generation, program analysis, and other actions, it is not
188designed to be a drop-in replacement for GCC's <tt>cc</tt>. For this purpose,
189use the high-level driver, aptly named <tt>clang</tt>. Here are some
190examples of how to use the high-level driver:
191</p>
192
193<pre class="code">
194$ <b>cat t.c</b>
195#include &lt;stdio.h&gt;
196int main(int argc, char **argv) { printf("hello world\n"); }
197$ <b>clang t.c</b>
198$ <b>./a.out</b>
199hello world
200</pre>
201
202<h2>Examples of using Clang</h2>
203
204<p>The high-level driver <tt>clang</tt> is designed to understand most of GCC's
205options, and the lower-level <tt>clang-cc</tt> executable also directly takes
206many of GCC's options. You can see which options <tt>clang-cc</tt> accepts with
207'<tt>clang-cc --help</tt>'. Here are a few examples of using <tt>clang</tt> and
208<tt>clang-cc</tt>:</p>
209
Eric Christopher7dc0e572008-02-08 06:45:49 +0000210<!-- Thanks to
211 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
212Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
213tag. -->
214
215<pre class="code">
216$ <b>cat ~/t.c</b>
217typedef float V __attribute__((vector_size(16)));
218V foo(V a, V b) { return a+b*a; }
219</pre>
220
221
Ted Kremenek35f29c52008-06-17 13:48:36 +0000222<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000223
224<pre class="code">
225$ <b>clang ~/t.c -E</b>
226# 1 "/Users/sabre/t.c" 1
227
228typedef float V __attribute__((vector_size(16)));
229
230V foo(V a, V b) { return a+b*a; }
231</pre>
232
233
Ted Kremenek35f29c52008-06-17 13:48:36 +0000234<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000235
236<pre class="code">
237$ <b>clang -fsyntax-only ~/t.c</b>
238</pre>
239
240
Ted Kremenek35f29c52008-06-17 13:48:36 +0000241<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000242
243<pre class="code">
244$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
245/Users/sabre/t.c:2:17: warning: extension used
246typedef float V __attribute__((vector_size(16)));
247 ^
2481 diagnostic generated.
249</pre>
250
251
Ted Kremenek35f29c52008-06-17 13:48:36 +0000252<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000253
254<pre class="code">
Ted Kremenek9b16da32009-03-27 16:32:57 +0000255$ <b>clang-cc ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000256typedef float V __attribute__(( vector_size(16) ));
257V foo(V a, V b) {
258 return a + b * a;
259}
260</pre>
261
262
Ted Kremenek35f29c52008-06-17 13:48:36 +0000263<h3>Code generation with LLVM:</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 -emit-llvm -o - | llvm-as | opt -std-compile-opts | llvm-dis</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000267define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
268entry:
269 %mul = mul &lt;4 x float&gt; %b, %a
270 %add = add &lt;4 x float&gt; %mul, %a
271 ret &lt;4 x float&gt; %add
272}
Ted Kremenek9b16da32009-03-27 16:32:57 +0000273$ <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 +0000274..
275_foo:
276 vmaddfp v2, v3, v2, v2
277 blr
Ted Kremenek9b16da32009-03-27 16:32:57 +0000278$ <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 +0000279..
280_foo:
281 mulps %xmm0, %xmm1
282 addps %xmm0, %xmm1
283 movaps %xmm1, %xmm0
284 ret
285</pre>
286
Eric Christopher7dc0e572008-02-08 06:45:49 +0000287</div>
288</body>
289</html>