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 |
Chris Lattner | d2c1c60 | 2009-11-09 03:18:18 +0000 | [diff] [blame] | 21 | involved</a> with the Clang community. If you run into problems, please file |
Daniel Dunbar | ce9f496 | 2009-05-24 01:40:10 +0000 | [diff] [blame] | 22 | bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a> or bring up the issue |
Daniel Dunbar | 05fc1c2 | 2009-05-24 01:00:12 +0000 | [diff] [blame] | 23 | on the |
Chris Lattner | d825883 | 2009-02-25 05:35:47 +0000 | [diff] [blame] | 24 | <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">Clang development |
| 25 | mailing list</a>.</p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 27 | <h2 id="build">Building Clang and Working with the Code</h2> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 28 | |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 29 | <h3 id="buildNix">On Unix-like Systems</h3> |
| 30 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 31 | <p>If you would like to check out and build Clang, the current procedure is as |
| 32 | follows:</p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 33 | |
| 34 | <ol> |
Anton Korobeynikov | 1816e48 | 2009-08-06 13:00:08 +0000 | [diff] [blame] | 35 | <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 Lattner | 357f7ce | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 45 | <li>Checkout LLVM:</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 46 | <ul> |
Chris Lattner | 357f7ce | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 47 | <li>Change directory to where you want the llvm directory placed.</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 48 | <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 49 | </ul> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 50 | <li>Checkout Clang:</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 51 | <ul> |
Chris Lattner | 357f7ce | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 52 | <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 Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 63 | </ul> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 64 | <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 Lattner | ca8c49f | 2009-03-10 16:01:44 +0000 | [diff] [blame] | 66 | system libstdc++ headers, please follow these instructions:</li> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 67 | <ul> |
Chris Lattner | ca8c49f | 2009-03-10 16:01:44 +0000 | [diff] [blame] | 68 | <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] | 69 | path.</li> |
| 70 | <li>Look for the comment "FIXME: temporary hack: |
Chris Lattner | ca8c49f | 2009-03-10 16:01:44 +0000 | [diff] [blame] | 71 | hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 72 | change the lines below to include that path.</li> |
| 73 | </ul> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 74 | <li>Try it out (assuming you add llvm/Debug/bin to your path):</li> |
| 75 | <ul> |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 76 | <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 Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 81 | </ul> |
| 82 | </ol> |
| 83 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 84 | <p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you |
| 85 | encounter problems with building Clang, make sure you have the latest SVN |
| 86 | version of LLVM. LLVM contains support libraries for Clang that will be updated |
| 87 | as well as development on Clang progresses.</p> |
Ted Kremenek | 675a9ba | 2008-02-08 07:32:26 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 89 | <h3>Simultaneously Building Clang and LLVM:</h3> |
Eric Christopher | 562b4f3 | 2008-02-08 07:10:48 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 91 | <p>Once you have checked out Clang into the llvm source tree it will build along |
| 92 | with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at |
| 93 | once 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 |
| 96 | Subversion repository. As mentioned above, the latest Clang sources are tied to |
| 97 | the latest sources in the LLVM tree. You can update your toplevel LLVM project |
| 98 | and all (possibly unrelated) projects inside it with <tt><b>make |
| 99 | update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related |
| 100 | to subversion. </p> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 101 | |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 102 | <h3 id="buildWindows">Using Visual Studio</h3> |
| 103 | |
| 104 | <p>The following details setting up for and building Clang on Windows using |
| 105 | Visual 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 Venet | 6bfc8b6 | 2009-09-27 10:34:36 +0000 | [diff] [blame] | 117 | <li><b>Visual Studio 2005 or 2008</b></li> |
Chris Lattner | 357f7ce | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 118 | <li><b>Python</b>. This is needed only if you will be running the tests |
Eli Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 119 | (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 Lattner | 357f7ce | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 123 | <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 Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 130 | </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 Thompson | 99ff8da | 2009-11-06 00:06:29 +0000 | [diff] [blame] | 144 | <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 Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 146 | <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 Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 152 | <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 Friedman | d1e1ef3 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 154 | </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 |
| 163 | to the latest code base, use the <tt>svn update</tt> command in both the |
| 164 | llvm and llvm\tools\clang directories, as they are separate repositories.</p> |
| 165 | |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 166 | <a name="driver"><h2>Clang Compiler Driver (Drop-in Substitute for GCC)</h2></a> |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 168 | <p>The <tt>clang</tt> tool is the compiler driver and front-end, which is |
| 169 | designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are |
| 170 | some examples of how to use the high-level driver: |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 171 | </p> |
| 172 | |
| 173 | <pre class="code"> |
| 174 | $ <b>cat t.c</b> |
| 175 | #include <stdio.h> |
| 176 | int main(int argc, char **argv) { printf("hello world\n"); } |
| 177 | $ <b>clang t.c</b> |
| 178 | $ <b>./a.out</b> |
| 179 | hello world |
| 180 | </pre> |
| 181 | |
Chris Lattner | 63d423d | 2009-11-09 03:21:02 +0000 | [diff] [blame] | 182 | <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 Lattner | d73fef6 | 2009-11-09 04:04:07 +0000 | [diff] [blame] | 185 | weird link-time errors relating to inline functions, try passing -std=gnu89 |
Chris Lattner | 63d423d | 2009-11-09 03:21:02 +0000 | [diff] [blame] | 186 | to clang.</p> |
| 187 | |
Ted Kremenek | 9b16da3 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 188 | <h2>Examples of using Clang</h2> |
| 189 | |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 190 | <!-- Thanks to |
| 191 | http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings |
| 192 | Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre> |
| 193 | tag. --> |
| 194 | |
| 195 | <pre class="code"> |
| 196 | $ <b>cat ~/t.c</b> |
| 197 | typedef float V __attribute__((vector_size(16))); |
| 198 | V foo(V a, V b) { return a+b*a; } |
| 199 | </pre> |
| 200 | |
| 201 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 202 | <h3>Preprocessing:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 203 | |
| 204 | <pre class="code"> |
| 205 | $ <b>clang ~/t.c -E</b> |
| 206 | # 1 "/Users/sabre/t.c" 1 |
| 207 | |
| 208 | typedef float V __attribute__((vector_size(16))); |
| 209 | |
| 210 | V foo(V a, V b) { return a+b*a; } |
| 211 | </pre> |
| 212 | |
| 213 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 214 | <h3>Type checking:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 215 | |
| 216 | <pre class="code"> |
| 217 | $ <b>clang -fsyntax-only ~/t.c</b> |
| 218 | </pre> |
| 219 | |
| 220 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 221 | <h3>GCC options:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 222 | |
| 223 | <pre class="code"> |
| 224 | $ <b>clang -fsyntax-only ~/t.c -pedantic</b> |
| 225 | /Users/sabre/t.c:2:17: warning: extension used |
| 226 | typedef float V __attribute__((vector_size(16))); |
| 227 | ^ |
| 228 | 1 diagnostic generated. |
| 229 | </pre> |
| 230 | |
| 231 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 232 | <h3>Pretty printing from the AST:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 233 | |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 234 | <p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and |
| 235 | not the driver, should be run. The compiler front-end has several additional |
| 236 | Clang specific features which are not exposed through the GCC compatible driver |
| 237 | interface.</p> |
| 238 | |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 239 | <pre class="code"> |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 240 | $ <b>clang -cc1 ~/t.c -ast-print</b> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 241 | typedef float V __attribute__(( vector_size(16) )); |
| 242 | V foo(V a, V b) { |
| 243 | return a + b * a; |
| 244 | } |
| 245 | </pre> |
| 246 | |
| 247 | |
Ted Kremenek | 35f29c5 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 248 | <h3>Code generation with LLVM:</h3> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 249 | |
| 250 | <pre class="code"> |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 251 | $ <b>clang ~/t.c -S -emit-llvm -o -</b> |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 252 | define <4 x float> @foo(<4 x float> %a, <4 x float> %b) { |
| 253 | entry: |
| 254 | %mul = mul <4 x float> %b, %a |
| 255 | %add = add <4 x float> %mul, %a |
| 256 | ret <4 x float> %add |
| 257 | } |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 258 | $ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i> |
| 259 | ... |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 260 | _foo: |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 261 | Leh_func_begin1: |
| 262 | mulps %xmm0, %xmm1 |
| 263 | addps %xmm1, %xmm0 |
| 264 | ret |
| 265 | Leh_func_end1: |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 266 | </pre> |
| 267 | |
Eric Christopher | 7dc0e57 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 268 | </div> |
| 269 | </body> |
| 270 | </html> |