blob: b7df928d27a87ec521c0cdb8aa23ab40d49c423f [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
Chris Lattnerd2c1c602009-11-09 03:18:18 +000021involved</a> with the Clang community. If you run into problems, please file
Daniel Dunbarce9f4962009-05-24 01:40:10 +000022bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a> or bring up the issue
Daniel Dunbar05fc1c22009-05-24 01:00:12 +000023on the
Chris Lattnerd8258832009-02-25 05:35:47 +000024<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">Clang development
25mailing list</a>.</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000026
Ted Kremenek9b16da32009-03-27 16:32:57 +000027<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher7dc0e572008-02-08 06:45:49 +000028
Eli Friedmand1e1ef32009-08-03 19:42:28 +000029<h3 id="buildNix">On Unix-like Systems</h3>
30
Ted Kremenek9b16da32009-03-27 16:32:57 +000031<p>If you would like to check out and build Clang, the current procedure is as
32follows:</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000033
34<ol>
Anton Korobeynikov1816e482009-08-06 13:00:08 +000035 <li>Get the required tools.
36 <ul>
37 <li>See
38 <a href="http://llvm.org/docs/GettingStarted.html#requirements">
39 Getting Started with the LLVM System - Requirements</a>.</li>
40 <li>Note also that Python is needed for running the test suite.
41 Get it at: <a href="http://www.python.org/download">
42 http://www.python.org/download</a></li>
43 </ul>
44
Chris Lattner357f7ce2009-08-20 06:17:11 +000045 <li>Checkout LLVM:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000046 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000047 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000048 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000049 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000050 <li>Checkout Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000051 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000052 <li><tt>cd llvm/tools</tt>
53 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
54 </ul>
55 <li>Build LLVM and Clang:</li>
56 <ul>
57 <li><tt>cd ..</tt> (back to llvm)</li>
58 <li><tt>./configure</tt></li>
59 <li><tt>make</tt></li>
60 <li>This builds both LLVM and Clang for debug mode.</li>
61 <li>Note: For subsequent Clang development, you can just do make at the
62 clang directory level.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000063 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000064 <li>If you intend to work on Clang C++ support, you may need to tell it how
65 to find your C++ standard library headers. If Clang cannot find your
Chris Lattnerca8c49f2009-03-10 16:01:44 +000066 system libstdc++ headers, please follow these instructions:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000067 <ul>
Chris Lattnerca8c49f2009-03-10 16:01:44 +000068 <li>'<tt>touch empty.cpp; gcc -v empty.cpp -fsyntax-only</tt>' to get the
Eric Christopher7dc0e572008-02-08 06:45:49 +000069 path.</li>
70 <li>Look for the comment "FIXME: temporary hack:
Chris Lattnerca8c49f2009-03-10 16:01:44 +000071 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher7dc0e572008-02-08 06:45:49 +000072 change the lines below to include that path.</li>
73 </ul>
Eric Christopher7dc0e572008-02-08 06:45:49 +000074 <li>Try it out (assuming you add llvm/Debug/bin to your path):</li>
75 <ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000076 <li><tt>clang-cc --help</tt></li>
77 <li><tt>clang-cc file.c -fsyntax-only</tt> (check for correctness)</li>
78 <li><tt>clang-cc file.c -ast-dump</tt> (internal debug dump of ast)</li>
79 <li><tt>clang-cc file.c -ast-view</tt> (<a
Eric Christopher7dc0e572008-02-08 06:45:49 +000080 href="http://llvm.org/docs/ProgrammersManual.html#ViewGraph">set up graphviz
81 and rebuild llvm first</a>)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +000082 <li><tt>clang-cc file.c -emit-llvm</tt> (print out unoptimized llvm code)</li>
83 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts |
Eric Christopher7dc0e572008-02-08 06:45:49 +000084 llvm-dis</tt> (print out optimized llvm code)</li>
Ted Kremenek9b16da32009-03-27 16:32:57 +000085 <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc
Eric Christopher7dc0e572008-02-08 06:45:49 +000086 &gt; file.s</tt> (output native machine code)</li>
87 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000088 <p><em>Note</em>: Here <tt>clang-cc</tt> is the "low-level" frontend
89 executable that is similar in purpose to <tt>cc1</tt>. Clang also has a <a
90 href="#driver">high-level compiler driver</a> that acts as a drop-in
91 replacement for <tt>gcc</tt>.
Eric Christopher7dc0e572008-02-08 06:45:49 +000092</ol>
93
Ted Kremenek9b16da32009-03-27 16:32:57 +000094<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
95encounter problems with building Clang, make sure you have the latest SVN
96version of LLVM. LLVM contains support libraries for Clang that will be updated
97as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +000098
Ted Kremenek9b16da32009-03-27 16:32:57 +000099<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +0000100
Ted Kremenek9b16da32009-03-27 16:32:57 +0000101<p>Once you have checked out Clang into the llvm source tree it will build along
102with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
103once simply run <tt>make</tt> from the root LLVM directory.</p>
104
105<p><em>Note:</em> Observe that Clang is technically part of a separate
106Subversion repository. As mentioned above, the latest Clang sources are tied to
107the latest sources in the LLVM tree. You can update your toplevel LLVM project
108and all (possibly unrelated) projects inside it with <tt><b>make
109update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
110to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000111
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000112<h3 id="buildWindows">Using Visual Studio</h3>
113
114<p>The following details setting up for and building Clang on Windows using
115Visual Studio:</p>
116
117<ol>
118 <li>Get the required tools:</li>
119 <ul>
120 <li><b>Subversion</b>. Source code control program. Get it from:
121 <a href="http://subversion.tigris.org/getting.html">
122 http://subversion.tigris.org/getting.html</a></li>
123 <li><b>cmake</b>. This is used for generating Visual Studio solution and
124 project files. Get it from:
125 <a href="http://www.cmake.org/cmake/resources/software.html">
126 http://www.cmake.org/cmake/resources/software.html</a></li>
Cedric Venet6bfc8b62009-09-27 10:34:36 +0000127 <li><b>Visual Studio 2005 or 2008</b></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000128 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000129 (which is essential, if you will be developing for clang).
130 Get it from:
131 <a href="http://www.python.org/download">
132 http://www.python.org/download</a></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000133 <li><b>GnuWin32 tools</b>
134 These are also necessary for running the tests.
135 (Note that the grep from MSYS or Cygwin doesn't work with the tests
136 because of embedded double-quotes in the search strings. The GNU
137 grep does work in this case.)
138 Get them from <a href="http://getgnuwin32.sourceforge.net">
139 http://getgnuwin32.sourceforge.net</a>.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000140 </ul>
141
142 <li>Checkout LLVM:</li>
143 <ul>
144 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
145 </ul>
146 <li>Checkout Clang:</li>
147 <ul>
148 <li><tt>cd llvm\tools</tt>
149 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
150 </ul>
151 <li>Run cmake to generate the Visual Studio solution and project files:</li>
152 <ul>
153 <li><tt>cd ..</tt> (Change directory back to the llvm top.)</li>
John Thompson99ff8da2009-11-06 00:06:29 +0000154 <li>If you are using Visual Studio 2005: <tt>cmake .</tt></li>
155 <li>Or if you are using Visual Studio 2008: <tt>cmake -G "Visual Studio 9 2008" .</tt></li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000156 <li>The above, if successful, will have created an LLVM.sln file in the
157 llvm directory.
158 </ul>
159 <li>Build Clang:</li>
160 <ul>
161 <li>Open LLVM.sln in Visual Studio.</li>
162 <li>Build the "clang-cc" project for just the compiler front end.
163 Alternatively, build the "clang" project for the compiler driver
164 (note that the driver is currently broken on Windows),
165 or the "ALL_BUILD" project to build everything, including tools.</li>
166 </ul>
167 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
168 running examples from above.)</li>
169 <li>See <a href="hacking.html#testingWindows">
170 Hacking on clang - Testing using Visual Studio on Windows</a> for information
171 on running regression tests on Windows.</li>
172</ol>
173
174<p>Note that once you have checked out both llvm and clang, to synchronize
175to the latest code base, use the <tt>svn update</tt> command in both the
176llvm and llvm\tools\clang directories, as they are separate repositories.</p>
177
Ted Kremenek9b16da32009-03-27 16:32:57 +0000178<a name="driver"><h2>High-Level Compiler Driver (Drop-in Substitute for GCC)</h2></a>
179
180<p>While the <tt>clang-cc</tt> executable is a low-level frontend executable
181that can perform code generation, program analysis, and other actions, it is not
182designed to be a drop-in replacement for GCC's <tt>cc</tt>. For this purpose,
183use the high-level driver, aptly named <tt>clang</tt>. Here are some
184examples of how to use the high-level driver:
185</p>
186
187<pre class="code">
188$ <b>cat t.c</b>
189#include &lt;stdio.h&gt;
190int main(int argc, char **argv) { printf("hello world\n"); }
191$ <b>clang t.c</b>
192$ <b>./a.out</b>
193hello world
194</pre>
195
Chris Lattner63d423d2009-11-09 03:21:02 +0000196<p>The 'clang' driver is designed to work as closely to GCC as possible to
197 maximize portability. The only major difference between the two is that
198 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnerd73fef62009-11-09 04:04:07 +0000199 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattner63d423d2009-11-09 03:21:02 +0000200 to clang.</p>
201
Ted Kremenek9b16da32009-03-27 16:32:57 +0000202<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>