blob: f504f8c9ac74e682b88307b0683c823ce86ad1e7 [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
196<h2>Examples of using Clang</h2>
197
198<p>The high-level driver <tt>clang</tt> is designed to understand most of GCC's
199options, and the lower-level <tt>clang-cc</tt> executable also directly takes
200many of GCC's options. You can see which options <tt>clang-cc</tt> accepts with
201'<tt>clang-cc --help</tt>'. Here are a few examples of using <tt>clang</tt> and
202<tt>clang-cc</tt>:</p>
203
Eric Christopher7dc0e572008-02-08 06:45:49 +0000204<!-- Thanks to
205 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
206Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
207tag. -->
208
209<pre class="code">
210$ <b>cat ~/t.c</b>
211typedef float V __attribute__((vector_size(16)));
212V foo(V a, V b) { return a+b*a; }
213</pre>
214
215
Ted Kremenek35f29c52008-06-17 13:48:36 +0000216<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000217
218<pre class="code">
219$ <b>clang ~/t.c -E</b>
220# 1 "/Users/sabre/t.c" 1
221
222typedef float V __attribute__((vector_size(16)));
223
224V foo(V a, V b) { return a+b*a; }
225</pre>
226
227
Ted Kremenek35f29c52008-06-17 13:48:36 +0000228<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000229
230<pre class="code">
231$ <b>clang -fsyntax-only ~/t.c</b>
232</pre>
233
234
Ted Kremenek35f29c52008-06-17 13:48:36 +0000235<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000236
237<pre class="code">
238$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
239/Users/sabre/t.c:2:17: warning: extension used
240typedef float V __attribute__((vector_size(16)));
241 ^
2421 diagnostic generated.
243</pre>
244
245
Ted Kremenek35f29c52008-06-17 13:48:36 +0000246<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000247
248<pre class="code">
Ted Kremenek9b16da32009-03-27 16:32:57 +0000249$ <b>clang-cc ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000250typedef float V __attribute__(( vector_size(16) ));
251V foo(V a, V b) {
252 return a + b * a;
253}
254</pre>
255
256
Ted Kremenek35f29c52008-06-17 13:48:36 +0000257<h3>Code generation with LLVM:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000258
259<pre class="code">
Ted Kremenek9b16da32009-03-27 16:32:57 +0000260$ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llvm-dis</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000261define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
262entry:
263 %mul = mul &lt;4 x float&gt; %b, %a
264 %add = add &lt;4 x float&gt; %mul, %a
265 ret &lt;4 x float&gt; %add
266}
Ted Kremenek9b16da32009-03-27 16:32:57 +0000267$ <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 +0000268..
269_foo:
270 vmaddfp v2, v3, v2, v2
271 blr
Ted Kremenek9b16da32009-03-27 16:32:57 +0000272$ <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 +0000273..
274_foo:
275 mulps %xmm0, %xmm1
276 addps %xmm0, %xmm1
277 movaps %xmm1, %xmm0
278 ret
279</pre>
280
Eric Christopher7dc0e572008-02-08 06:45:49 +0000281</div>
282</body>
283</html>