blob: f750fa09216cef519a7d7deba357769db4cd8e8e [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>
Daniel Dunbardd63b282009-12-11 23:04:35 +000076 <li><tt>clang --help</tt></li>
77 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
78 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
79 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
80 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000081 </ul>
82</ol>
83
Ted Kremenek9b16da32009-03-27 16:32:57 +000084<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
85encounter problems with building Clang, make sure you have the latest SVN
86version of LLVM. LLVM contains support libraries for Clang that will be updated
87as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +000088
Ted Kremenek9b16da32009-03-27 16:32:57 +000089<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +000090
Ted Kremenek9b16da32009-03-27 16:32:57 +000091<p>Once you have checked out Clang into the llvm source tree it will build along
92with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
93once simply run <tt>make</tt> from the root LLVM directory.</p>
94
95<p><em>Note:</em> Observe that Clang is technically part of a separate
96Subversion repository. As mentioned above, the latest Clang sources are tied to
97the latest sources in the LLVM tree. You can update your toplevel LLVM project
98and all (possibly unrelated) projects inside it with <tt><b>make
99update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
100to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000101
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000102<h3 id="buildWindows">Using Visual Studio</h3>
103
104<p>The following details setting up for and building Clang on Windows using
105Visual Studio:</p>
106
107<ol>
108 <li>Get the required tools:</li>
109 <ul>
110 <li><b>Subversion</b>. Source code control program. Get it from:
111 <a href="http://subversion.tigris.org/getting.html">
112 http://subversion.tigris.org/getting.html</a></li>
113 <li><b>cmake</b>. This is used for generating Visual Studio solution and
114 project files. Get it from:
115 <a href="http://www.cmake.org/cmake/resources/software.html">
116 http://www.cmake.org/cmake/resources/software.html</a></li>
Cedric Venet6bfc8b62009-09-27 10:34:36 +0000117 <li><b>Visual Studio 2005 or 2008</b></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000118 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000119 (which is essential, if you will be developing for clang).
120 Get it from:
121 <a href="http://www.python.org/download">
122 http://www.python.org/download</a></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000123 <li><b>GnuWin32 tools</b>
124 These are also necessary for running the tests.
125 (Note that the grep from MSYS or Cygwin doesn't work with the tests
126 because of embedded double-quotes in the search strings. The GNU
127 grep does work in this case.)
128 Get them from <a href="http://getgnuwin32.sourceforge.net">
129 http://getgnuwin32.sourceforge.net</a>.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000130 </ul>
131
132 <li>Checkout LLVM:</li>
133 <ul>
134 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
135 </ul>
136 <li>Checkout Clang:</li>
137 <ul>
138 <li><tt>cd llvm\tools</tt>
139 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
140 </ul>
141 <li>Run cmake to generate the Visual Studio solution and project files:</li>
142 <ul>
143 <li><tt>cd ..</tt> (Change directory back to the llvm top.)</li>
John Thompson99ff8da2009-11-06 00:06:29 +0000144 <li>If you are using Visual Studio 2005: <tt>cmake .</tt></li>
145 <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 +0000146 <li>The above, if successful, will have created an LLVM.sln file in the
147 llvm directory.
148 </ul>
149 <li>Build Clang:</li>
150 <ul>
151 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbardd63b282009-12-11 23:04:35 +0000152 <li>Build the "clang" project for just the compiler driver and front end, or
153 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000154 </ul>
155 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
156 running examples from above.)</li>
157 <li>See <a href="hacking.html#testingWindows">
158 Hacking on clang - Testing using Visual Studio on Windows</a> for information
159 on running regression tests on Windows.</li>
160</ol>
161
162<p>Note that once you have checked out both llvm and clang, to synchronize
163to the latest code base, use the <tt>svn update</tt> command in both the
164llvm and llvm\tools\clang directories, as they are separate repositories.</p>
165
Daniel Dunbardd63b282009-12-11 23:04:35 +0000166<a name="driver"><h2>Clang Compiler Driver (Drop-in Substitute for GCC)</h2></a>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000167
Daniel Dunbardd63b282009-12-11 23:04:35 +0000168<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
169designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
170some examples of how to use the high-level driver:
Ted Kremenek9b16da32009-03-27 16:32:57 +0000171</p>
172
173<pre class="code">
174$ <b>cat t.c</b>
175#include &lt;stdio.h&gt;
176int main(int argc, char **argv) { printf("hello world\n"); }
177$ <b>clang t.c</b>
178$ <b>./a.out</b>
179hello world
180</pre>
181
Chris Lattner63d423d2009-11-09 03:21:02 +0000182<p>The 'clang' driver is designed to work as closely to GCC as possible to
183 maximize portability. The only major difference between the two is that
184 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnerd73fef62009-11-09 04:04:07 +0000185 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattner63d423d2009-11-09 03:21:02 +0000186 to clang.</p>
187
Ted Kremenek9b16da32009-03-27 16:32:57 +0000188<h2>Examples of using Clang</h2>
189
Eric Christopher7dc0e572008-02-08 06:45:49 +0000190<!-- Thanks to
191 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
192Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
193tag. -->
194
195<pre class="code">
196$ <b>cat ~/t.c</b>
197typedef float V __attribute__((vector_size(16)));
198V foo(V a, V b) { return a+b*a; }
199</pre>
200
201
Ted Kremenek35f29c52008-06-17 13:48:36 +0000202<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000203
204<pre class="code">
205$ <b>clang ~/t.c -E</b>
206# 1 "/Users/sabre/t.c" 1
207
208typedef float V __attribute__((vector_size(16)));
209
210V foo(V a, V b) { return a+b*a; }
211</pre>
212
213
Ted Kremenek35f29c52008-06-17 13:48:36 +0000214<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000215
216<pre class="code">
217$ <b>clang -fsyntax-only ~/t.c</b>
218</pre>
219
220
Ted Kremenek35f29c52008-06-17 13:48:36 +0000221<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000222
223<pre class="code">
224$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
225/Users/sabre/t.c:2:17: warning: extension used
226typedef float V __attribute__((vector_size(16)));
227 ^
2281 diagnostic generated.
229</pre>
230
231
Ted Kremenek35f29c52008-06-17 13:48:36 +0000232<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000233
Daniel Dunbardd63b282009-12-11 23:04:35 +0000234<p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and
235not the driver, should be run. The compiler front-end has several additional
236Clang specific features which are not exposed through the GCC compatible driver
237interface.</p>
238
Eric Christopher7dc0e572008-02-08 06:45:49 +0000239<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000240$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000241typedef float V __attribute__(( vector_size(16) ));
242V foo(V a, V b) {
243 return a + b * a;
244}
245</pre>
246
247
Ted Kremenek35f29c52008-06-17 13:48:36 +0000248<h3>Code generation with LLVM:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000249
250<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000251$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000252define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
253entry:
254 %mul = mul &lt;4 x float&gt; %b, %a
255 %add = add &lt;4 x float&gt; %mul, %a
256 ret &lt;4 x float&gt; %add
257}
Daniel Dunbardd63b282009-12-11 23:04:35 +0000258$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
259...
Eric Christopher7dc0e572008-02-08 06:45:49 +0000260_foo:
Daniel Dunbardd63b282009-12-11 23:04:35 +0000261Leh_func_begin1:
262 mulps %xmm0, %xmm1
263 addps %xmm1, %xmm0
264 ret
265Leh_func_end1:
Eric Christopher7dc0e572008-02-08 06:45:49 +0000266</pre>
267
Eric Christopher7dc0e572008-02-08 06:45:49 +0000268</div>
269</body>
270</html>