blob: 34fed955879d88920c342e39984e5ba7e9f99b25 [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
Douglas Gregor9ff3e662010-10-07 20:20:57 +000022bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000023
Ted Kremenek9b16da32009-03-27 16:32:57 +000024<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher7dc0e572008-02-08 06:45:49 +000025
Eli Friedmand1e1ef32009-08-03 19:42:28 +000026<h3 id="buildNix">On Unix-like Systems</h3>
27
Ted Kremenek9b16da32009-03-27 16:32:57 +000028<p>If you would like to check out and build Clang, the current procedure is as
29follows:</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000030
31<ol>
Anton Korobeynikov1816e482009-08-06 13:00:08 +000032 <li>Get the required tools.
33 <ul>
34 <li>See
35 <a href="http://llvm.org/docs/GettingStarted.html#requirements">
36 Getting Started with the LLVM System - Requirements</a>.</li>
37 <li>Note also that Python is needed for running the test suite.
38 Get it at: <a href="http://www.python.org/download">
39 http://www.python.org/download</a></li>
40 </ul>
41
Chris Lattner357f7ce2009-08-20 06:17:11 +000042 <li>Checkout LLVM:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000043 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000044 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000045 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000046 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000047 <li>Checkout Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000048 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000049 <li><tt>cd llvm/tools</tt>
50 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
51 </ul>
52 <li>Build LLVM and Clang:</li>
53 <ul>
54 <li><tt>cd ..</tt> (back to llvm)</li>
55 <li><tt>./configure</tt></li>
56 <li><tt>make</tt></li>
57 <li>This builds both LLVM and Clang for debug mode.</li>
58 <li>Note: For subsequent Clang development, you can just do make at the
59 clang directory level.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000060 </ul>
Chris Lattner78781782010-05-06 21:57:57 +000061
62 <p>It is also possible to use CMake instead of the makefiles. With CMake it
63 is also possible to generate project files for several IDEs: Eclipse CDT4,
64 CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</p>
65
Ted Kremenek9b16da32009-03-27 16:32:57 +000066 <li>If you intend to work on Clang C++ support, you may need to tell it how
67 to find your C++ standard library headers. If Clang cannot find your
Chris Lattnerca8c49f2009-03-10 16:01:44 +000068 system libstdc++ headers, please follow these instructions:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000069 <ul>
Gabor Greif74350822010-03-29 21:36:06 +000070 <li>'<tt>gcc -v -x c++ /dev/null -fsyntax-only</tt>' to get the
Eric Christopher7dc0e572008-02-08 06:45:49 +000071 path.</li>
72 <li>Look for the comment "FIXME: temporary hack:
Chris Lattnerca8c49f2009-03-10 16:01:44 +000073 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher7dc0e572008-02-08 06:45:49 +000074 change the lines below to include that path.</li>
75 </ul>
Chris Lattnerbe9a5ca2010-07-26 22:51:00 +000076 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000077 <ul>
Daniel Dunbardd63b282009-12-11 23:04:35 +000078 <li><tt>clang --help</tt></li>
79 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
80 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
81 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
82 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000083 </ul>
84</ol>
85
Ted Kremenek9b16da32009-03-27 16:32:57 +000086<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
87encounter problems with building Clang, make sure you have the latest SVN
88version of LLVM. LLVM contains support libraries for Clang that will be updated
89as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +000090
Ted Kremenek9b16da32009-03-27 16:32:57 +000091<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +000092
Ted Kremenek9b16da32009-03-27 16:32:57 +000093<p>Once you have checked out Clang into the llvm source tree it will build along
94with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
95once simply run <tt>make</tt> from the root LLVM directory.</p>
96
97<p><em>Note:</em> Observe that Clang is technically part of a separate
98Subversion repository. As mentioned above, the latest Clang sources are tied to
99the latest sources in the LLVM tree. You can update your toplevel LLVM project
100and all (possibly unrelated) projects inside it with <tt><b>make
101update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
102to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000103
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000104<h3 id="buildWindows">Using Visual Studio</h3>
105
106<p>The following details setting up for and building Clang on Windows using
107Visual Studio:</p>
108
109<ol>
110 <li>Get the required tools:</li>
111 <ul>
112 <li><b>Subversion</b>. Source code control program. Get it from:
113 <a href="http://subversion.tigris.org/getting.html">
114 http://subversion.tigris.org/getting.html</a></li>
115 <li><b>cmake</b>. This is used for generating Visual Studio solution and
116 project files. Get it from:
117 <a href="http://www.cmake.org/cmake/resources/software.html">
118 http://www.cmake.org/cmake/resources/software.html</a></li>
Cedric Venet6bfc8b62009-09-27 10:34:36 +0000119 <li><b>Visual Studio 2005 or 2008</b></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000120 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000121 (which is essential, if you will be developing for clang).
122 Get it from:
123 <a href="http://www.python.org/download">
124 http://www.python.org/download</a></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000125 <li><b>GnuWin32 tools</b>
126 These are also necessary for running the tests.
127 (Note that the grep from MSYS or Cygwin doesn't work with the tests
128 because of embedded double-quotes in the search strings. The GNU
129 grep does work in this case.)
130 Get them from <a href="http://getgnuwin32.sourceforge.net">
131 http://getgnuwin32.sourceforge.net</a>.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000132 </ul>
133
134 <li>Checkout LLVM:</li>
135 <ul>
136 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
137 </ul>
138 <li>Checkout Clang:</li>
139 <ul>
140 <li><tt>cd llvm\tools</tt>
141 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
142 </ul>
143 <li>Run cmake to generate the Visual Studio solution and project files:</li>
144 <ul>
145 <li><tt>cd ..</tt> (Change directory back to the llvm top.)</li>
John Thompson99ff8da2009-11-06 00:06:29 +0000146 <li>If you are using Visual Studio 2005: <tt>cmake .</tt></li>
147 <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 +0000148 <li>The above, if successful, will have created an LLVM.sln file in the
149 llvm directory.
150 </ul>
151 <li>Build Clang:</li>
152 <ul>
153 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbardd63b282009-12-11 23:04:35 +0000154 <li>Build the "clang" project for just the compiler driver and front end, or
155 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000156 </ul>
157 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
158 running examples from above.)</li>
159 <li>See <a href="hacking.html#testingWindows">
160 Hacking on clang - Testing using Visual Studio on Windows</a> for information
161 on running regression tests on Windows.</li>
162</ol>
163
164<p>Note that once you have checked out both llvm and clang, to synchronize
165to the latest code base, use the <tt>svn update</tt> command in both the
166llvm and llvm\tools\clang directories, as they are separate repositories.</p>
167
Daniel Dunbardd63b282009-12-11 23:04:35 +0000168<a name="driver"><h2>Clang Compiler Driver (Drop-in Substitute for GCC)</h2></a>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000169
Daniel Dunbardd63b282009-12-11 23:04:35 +0000170<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
171designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
172some examples of how to use the high-level driver:
Ted Kremenek9b16da32009-03-27 16:32:57 +0000173</p>
174
175<pre class="code">
176$ <b>cat t.c</b>
177#include &lt;stdio.h&gt;
178int main(int argc, char **argv) { printf("hello world\n"); }
179$ <b>clang t.c</b>
180$ <b>./a.out</b>
181hello world
182</pre>
183
Chris Lattner63d423d2009-11-09 03:21:02 +0000184<p>The 'clang' driver is designed to work as closely to GCC as possible to
185 maximize portability. The only major difference between the two is that
186 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnerd73fef62009-11-09 04:04:07 +0000187 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattner63d423d2009-11-09 03:21:02 +0000188 to clang.</p>
189
Ted Kremenek9b16da32009-03-27 16:32:57 +0000190<h2>Examples of using Clang</h2>
191
Eric Christopher7dc0e572008-02-08 06:45:49 +0000192<!-- Thanks to
193 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
194Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
195tag. -->
196
197<pre class="code">
198$ <b>cat ~/t.c</b>
199typedef float V __attribute__((vector_size(16)));
200V foo(V a, V b) { return a+b*a; }
201</pre>
202
203
Ted Kremenek35f29c52008-06-17 13:48:36 +0000204<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000205
206<pre class="code">
207$ <b>clang ~/t.c -E</b>
208# 1 "/Users/sabre/t.c" 1
209
210typedef float V __attribute__((vector_size(16)));
211
212V foo(V a, V b) { return a+b*a; }
213</pre>
214
215
Ted Kremenek35f29c52008-06-17 13:48:36 +0000216<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000217
218<pre class="code">
219$ <b>clang -fsyntax-only ~/t.c</b>
220</pre>
221
222
Ted Kremenek35f29c52008-06-17 13:48:36 +0000223<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000224
225<pre class="code">
226$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
227/Users/sabre/t.c:2:17: warning: extension used
228typedef float V __attribute__((vector_size(16)));
229 ^
2301 diagnostic generated.
231</pre>
232
233
Ted Kremenek35f29c52008-06-17 13:48:36 +0000234<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000235
Daniel Dunbardd63b282009-12-11 23:04:35 +0000236<p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and
237not the driver, should be run. The compiler front-end has several additional
238Clang specific features which are not exposed through the GCC compatible driver
239interface.</p>
240
Eric Christopher7dc0e572008-02-08 06:45:49 +0000241<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000242$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000243typedef float V __attribute__(( vector_size(16) ));
244V foo(V a, V b) {
245 return a + b * a;
246}
247</pre>
248
249
Ted Kremenek35f29c52008-06-17 13:48:36 +0000250<h3>Code generation with LLVM:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000251
252<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000253$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000254define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
255entry:
256 %mul = mul &lt;4 x float&gt; %b, %a
257 %add = add &lt;4 x float&gt; %mul, %a
258 ret &lt;4 x float&gt; %add
259}
Daniel Dunbardd63b282009-12-11 23:04:35 +0000260$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
261...
Eric Christopher7dc0e572008-02-08 06:45:49 +0000262_foo:
Daniel Dunbardd63b282009-12-11 23:04:35 +0000263Leh_func_begin1:
264 mulps %xmm0, %xmm1
265 addps %xmm1, %xmm0
266 ret
267Leh_func_end1:
Eric Christopher7dc0e572008-02-08 06:45:49 +0000268</pre>
269
Eric Christopher7dc0e572008-02-08 06:45:49 +0000270</div>
271</body>
272</html>