Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 1 | <!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 Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 18 | <p>This page gives you the shortest path to checking out Clang and demos a few |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 19 | options. This should get you up and running with the minimum of muss and fuss. |
| 20 | If you like what you see, please consider <a href="get_involved.html">getting |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 21 | involved</a> with the Clang community.</p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 22 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 23 | <h2>A Word of Warning</h2> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 24 | |
| 25 | <p>While this work aims to provide a fully functional C/C++/ObjC front-end, it |
Gabor Greif | 9c63ed5 | 2008-02-28 14:59:26 +0000 | [diff] [blame] | 26 | is <em>still early work</em> and is under heavy development. In particular, |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 27 | there is no real C++ support yet (this is obviously a big project), and C/ObjC |
Chris Lattner | d825883 | 2009-02-25 05:35:47 +0000 | [diff] [blame] | 28 | support is still missing some minor features. Some of the more notable missing |
| 29 | pieces of C support are:</p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 30 | |
| 31 | <ol> |
Chris Lattner | d825883 | 2009-02-25 05:35:47 +0000 | [diff] [blame] | 32 | <li>The semantic analyzer does not produce all of the warnings it should.</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 33 | <li>We don't consider the API to be stable yet, and reserve the right to |
| 34 | change fundamental things.</li> |
Chris Lattner | d825883 | 2009-02-25 05:35:47 +0000 | [diff] [blame] | 35 | <li>The driver is currently implemented in python and is "really slow".</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 36 | </ol> |
| 37 | |
Chris Lattner | d825883 | 2009-02-25 05:35:47 +0000 | [diff] [blame] | 38 | <p>At this point, C and Objective-C are generally usable for X86-32 and X86-64 |
| 39 | targets. If you run into problems, please file bugs in <a |
| 40 | href="http://llvm.org/bugs/">LLVM Bugzilla</a> or bring up the issue on the |
| 41 | <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">Clang development |
| 42 | mailing list</a>.</p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 44 | <h2 id="build">Building Clang and Working with the Code</h2> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 46 | <p>If you would like to check out and build Clang, the current procedure is as |
| 47 | follows:</p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 48 | |
| 49 | <ol> |
| 50 | <li><a href="http://www.llvm.org/docs/GettingStarted.html#checkout">Checkout |
| 51 | and build LLVM</a> from SVN head:</li> |
| 52 | |
| 53 | <ul> |
| 54 | <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li> |
| 55 | <li><tt>cd llvm</tt></li> |
| 56 | <li><tt>./configure; make</tt></li> |
| 57 | </ul> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 58 | <li>Checkout Clang:</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 59 | <ul> |
| 60 | <li>From within the <tt>llvm</tt> directory (where you |
| 61 | built llvm):</li> |
Ted Kremenek | 8c22bc3 | 2008-07-14 14:40:22 +0000 | [diff] [blame] | 62 | <li><tt>cd tools</tt> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 63 | <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li> |
| 64 | |
| 65 | </ul> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 66 | <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 Lattner | ca8c49f | 2009-03-10 16:01:44 +0000 | [diff] [blame] | 68 | system libstdc++ headers, please follow these instructions:</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 69 | |
| 70 | <ul> |
Chris Lattner | ca8c49f | 2009-03-10 16:01:44 +0000 | [diff] [blame] | 71 | <li>'<tt>touch empty.cpp; gcc -v empty.cpp -fsyntax-only</tt>' to get the |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 72 | path.</li> |
| 73 | <li>Look for the comment "FIXME: temporary hack: |
Chris Lattner | ca8c49f | 2009-03-10 16:01:44 +0000 | [diff] [blame] | 74 | hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 75 | change the lines below to include that path.</li> |
| 76 | </ul> |
| 77 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 78 | <li>Build Clang:</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 79 | <ul> |
| 80 | <li><tt>cd clang</tt> (assuming that you are in <tt>llvm/tools</tt>)</li> |
| 81 | <li><tt>make</tt> (this will give you a debug build)</li> |
| 82 | </ul> |
| 83 | |
| 84 | <li>Try it out (assuming you add llvm/Debug/bin to your path):</li> |
| 85 | <ul> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 86 | <li><tt>clang-cc --help</tt></li> |
| 87 | <li><tt>clang-cc file.c -fsyntax-only</tt> (check for correctness)</li> |
| 88 | <li><tt>clang-cc file.c -ast-dump</tt> (internal debug dump of ast)</li> |
| 89 | <li><tt>clang-cc file.c -ast-view</tt> (<a |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 90 | href="http://llvm.org/docs/ProgrammersManual.html#ViewGraph">set up graphviz |
| 91 | and rebuild llvm first</a>)</li> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 92 | <li><tt>clang-cc file.c -emit-llvm</tt> (print out unoptimized llvm code)</li> |
| 93 | <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 94 | llvm-dis</tt> (print out optimized llvm code)</li> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 95 | <li><tt>clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 96 | > file.s</tt> (output native machine code)</li> |
| 97 | </ul> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 98 | |
| 99 | <p><em>Note</em>: Here <tt>clang-cc</tt> is the "low-level" frontend |
| 100 | executable that is similar in purpose to <tt>cc1</tt>. Clang also has a <a |
| 101 | href="#driver">high-level compiler driver</a> that acts as a drop-in |
| 102 | replacement for <tt>gcc</tt>. |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 103 | </ol> |
| 104 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 105 | <p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you |
| 106 | encounter problems with building Clang, make sure you have the latest SVN |
| 107 | version of LLVM. LLVM contains support libraries for Clang that will be updated |
| 108 | as well as development on Clang progresses.</p> |
Ted Kremenek | 675a9ba | 2008-02-08 07:32:26 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 110 | <h3>Simultaneously Building Clang and LLVM:</h3> |
Eric Christopher | 562b4f3 | 2008-02-08 07:10:48 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 112 | <p>Once you have checked out Clang into the llvm source tree it will build along |
| 113 | with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at |
| 114 | once simply run <tt>make</tt> from the root LLVM directory.</p> |
| 115 | |
| 116 | <p><em>Note:</em> Observe that Clang is technically part of a separate |
| 117 | Subversion repository. As mentioned above, the latest Clang sources are tied to |
| 118 | the latest sources in the LLVM tree. You can update your toplevel LLVM project |
| 119 | and all (possibly unrelated) projects inside it with <tt><b>make |
| 120 | update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related |
| 121 | to subversion. </p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 123 | <a name="driver"><h2>High-Level Compiler Driver (Drop-in Substitute for GCC)</h2></a> |
| 124 | |
| 125 | <p>While the <tt>clang-cc</tt> executable is a low-level frontend executable |
| 126 | that can perform code generation, program analysis, and other actions, it is not |
| 127 | designed to be a drop-in replacement for GCC's <tt>cc</tt>. For this purpose, |
| 128 | use the high-level driver, aptly named <tt>clang</tt>. Here are some |
| 129 | examples of how to use the high-level driver: |
| 130 | </p> |
| 131 | |
| 132 | <pre class="code"> |
| 133 | $ <b>cat t.c</b> |
| 134 | #include <stdio.h> |
| 135 | int main(int argc, char **argv) { printf("hello world\n"); } |
| 136 | $ <b>clang t.c</b> |
| 137 | $ <b>./a.out</b> |
| 138 | hello world |
| 139 | </pre> |
| 140 | |
| 141 | <h2>Examples of using Clang</h2> |
| 142 | |
| 143 | <p>The high-level driver <tt>clang</tt> is designed to understand most of GCC's |
| 144 | options, and the lower-level <tt>clang-cc</tt> executable also directly takes |
| 145 | many of GCC's options. You can see which options <tt>clang-cc</tt> accepts with |
| 146 | '<tt>clang-cc --help</tt>'. Here are a few examples of using <tt>clang</tt> and |
| 147 | <tt>clang-cc</tt>:</p> |
| 148 | |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 149 | <!-- Thanks to |
| 150 | http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings |
| 151 | Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre> |
| 152 | tag. --> |
| 153 | |
| 154 | <pre class="code"> |
| 155 | $ <b>cat ~/t.c</b> |
| 156 | typedef float V __attribute__((vector_size(16))); |
| 157 | V foo(V a, V b) { return a+b*a; } |
| 158 | </pre> |
| 159 | |
| 160 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 161 | <h3>Preprocessing:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 162 | |
| 163 | <pre class="code"> |
| 164 | $ <b>clang ~/t.c -E</b> |
| 165 | # 1 "/Users/sabre/t.c" 1 |
| 166 | |
| 167 | typedef float V __attribute__((vector_size(16))); |
| 168 | |
| 169 | V foo(V a, V b) { return a+b*a; } |
| 170 | </pre> |
| 171 | |
| 172 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 173 | <h3>Type checking:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 174 | |
| 175 | <pre class="code"> |
| 176 | $ <b>clang -fsyntax-only ~/t.c</b> |
| 177 | </pre> |
| 178 | |
| 179 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 180 | <h3>GCC options:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 181 | |
| 182 | <pre class="code"> |
| 183 | $ <b>clang -fsyntax-only ~/t.c -pedantic</b> |
| 184 | /Users/sabre/t.c:2:17: warning: extension used |
| 185 | typedef float V __attribute__((vector_size(16))); |
| 186 | ^ |
| 187 | 1 diagnostic generated. |
| 188 | </pre> |
| 189 | |
| 190 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 191 | <h3>Pretty printing from the AST:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 192 | |
| 193 | <pre class="code"> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 194 | $ <b>clang-cc ~/t.c -ast-print</b> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 195 | typedef float V __attribute__(( vector_size(16) )); |
| 196 | V foo(V a, V b) { |
| 197 | return a + b * a; |
| 198 | } |
| 199 | </pre> |
| 200 | |
| 201 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 202 | <h3>Code generation with LLVM:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 203 | |
| 204 | <pre class="code"> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 205 | $ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llvm-dis</b> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 206 | define <4 x float> @foo(<4 x float> %a, <4 x float> %b) { |
| 207 | entry: |
| 208 | %mul = mul <4 x float> %b, %a |
| 209 | %add = add <4 x float> %mul, %a |
| 210 | ret <4 x float> %add |
| 211 | } |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 212 | $ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc -march=ppc32 -mcpu=g5</b> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 213 | .. |
| 214 | _foo: |
| 215 | vmaddfp v2, v3, v2, v2 |
| 216 | blr |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 217 | $ <b>clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc -march=x86 -mcpu=yonah</b> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 218 | .. |
| 219 | _foo: |
| 220 | mulps %xmm0, %xmm1 |
| 221 | addps %xmm0, %xmm1 |
| 222 | movaps %xmm1, %xmm0 |
| 223 | ret |
| 224 | </pre> |
| 225 | |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 226 | </div> |
| 227 | </body> |
| 228 | </html> |