Home | History | Annotate | Download | only in www
      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 
     18 <p>This page gives you the shortest path to checking out Clang and demos a few
     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
     21 involved</a> with the Clang community.  If you run into problems, please file
     22 bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
     23 
     24 <h2 id="download">Release Clang Versions</h2>
     25 
     26 <p>Clang has been released as part of regular LLVM releases since LLVM 2.6. You
     27 can download the release versions
     28 from <a href="http://llvm.org/releases/">http://llvm.org/releases/</a>.</p>
     29 
     30 <h2 id="build">Building Clang and Working with the Code</h2>
     31 
     32 <h3 id="buildNix">On Unix-like Systems</h3>
     33 
     34 <p>If you would like to check out and build Clang, the current procedure is as
     35 follows:</p>
     36 
     37 <ol>
     38   <li>Get the required tools.
     39   <ul>
     40     <li>See
     41       <a href="http://llvm.org/docs/GettingStarted.html#requirements">
     42       Getting Started with the LLVM System - Requirements</a>.</li>
     43     <li>Note also that Python is needed for running the test suite.
     44       Get it at: <a href="http://www.python.org/download">
     45       http://www.python.org/download</a></li>
     46   </ul>
     47 
     48   <li>Checkout LLVM:
     49   <ul>
     50     <li>Change directory to where you want the llvm directory placed.</li>
     51     <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
     52   </ul>
     53   </li>
     54   <li>Checkout Clang:
     55   <ul>
     56     <li><tt>cd llvm/tools</tt></li>
     57     <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
     58     <li><tt>cd ../..</tt></li>
     59   </ul>
     60   </li>
     61   <li>Checkout extra Clang Tools: (optional)
     62   <ul>
     63     <li><tt>cd llvm/tools/clang/tools</tt></li>
     64     <li><tt>svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk
     65         extra</tt></li>
     66     <li><tt>cd ../../../..</tt></li>
     67   </ul>
     68   </li>
     69   <li>Checkout Compiler-RT:
     70   <ul>
     71     <li><tt>cd llvm/projects</tt></li>
     72     <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
     73         compiler-rt</tt></li>
     74     <li><tt>cd ../..</tt></li>
     75   </ul>
     76   </li>
     77   <li>Build LLVM and Clang:
     78   <ul>
     79     <li><tt>mkdir build</tt> (for building without polluting the source dir)
     80     </li>
     81     <li><tt>cd build</tt></li>
     82     <li><tt>../llvm/configure</tt></li>
     83     <li><tt>make</tt></li>
     84     <li>This builds both LLVM and Clang for debug mode.</li>
     85     <li>Note: For subsequent Clang development, you can just do make at the
     86     clang directory level.</li>
     87     <li>It is also possible to use CMake instead of the makefiles. With CMake
     88     it is also possible to generate project files for several IDEs: Eclipse
     89     CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</li>
     90   </ul>
     91   </li>
     92 
     93   <li>If you intend to work on Clang C++ support, you may need to tell it how
     94       to find your C++ standard library headers. In general, Clang will detect
     95       the best version of libstdc++ headers available and use them - it will
     96       look both for system installations of libstdc++ as well as installations
     97       adjacent to Clang itself. If your configuration fits neither of these
     98       scenarios, you can use the <tt>--with-gcc-toolchain</tt> configure option
     99       to tell Clang where the gcc containing the desired libstdc++ is installed.
    100   </li>
    101   <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):
    102   <ul>
    103     <li><tt>clang --help</tt></li>
    104     <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
    105     <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
    106     <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
    107     <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
    108   </ul>
    109   </li>
    110 </ol>
    111 
    112 <p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
    113 encounter problems with building Clang, make sure you have the latest SVN
    114 version of LLVM. LLVM contains support libraries for Clang that will be updated
    115 as well as development on Clang progresses.</p>
    116   
    117 <h3>Simultaneously Building Clang and LLVM:</h3>
    118 
    119 <p>Once you have checked out Clang into the llvm source tree it will build along
    120 with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
    121 once simply run <tt>make</tt> from the root LLVM directory.</p>
    122     
    123 <p><em>Note:</em> Observe that Clang is technically part of a separate
    124 Subversion repository. As mentioned above, the latest Clang sources are tied to
    125 the latest sources in the LLVM tree. You can update your toplevel LLVM project
    126 and all (possibly unrelated) projects inside it with <tt><b>make
    127 update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
    128 to subversion. </p>
    129 
    130 <h3 id="buildWindows">Using Visual Studio</h3>
    131 
    132 <p>The following details setting up for and building Clang on Windows using
    133 Visual Studio:</p>
    134 
    135 <ol>
    136   <li>Get the required tools:
    137   <ul>
    138     <li><b>Subversion</b>.  Source code control program.  Get it from:
    139         <a href="http://subversion.tigris.org/getting.html">
    140         http://subversion.tigris.org/getting.html</a></li>
    141     <li><b>cmake</b>.  This is used for generating Visual Studio solution and
    142         project files.  Get it from:
    143         <a href="http://www.cmake.org/cmake/resources/software.html">
    144         http://www.cmake.org/cmake/resources/software.html</a></li>
    145     <li><b>Visual Studio 2008 or 2010</b></li>
    146     <li><b>Python</b>.  This is needed only if you will be running the tests
    147         (which is essential, if you will be developing for clang).
    148         Get it from:
    149         <a href="http://www.python.org/download/">
    150         http://www.python.org/download/</a></li>
    151     <li><b>GnuWin32 tools</b>
    152         These are also necessary for running the tests.
    153         (Note that the grep from MSYS or Cygwin doesn't work with the tests
    154         because of embedded double-quotes in the search strings.  The GNU
    155         grep does work in this case.)
    156         Get them from <a href="http://getgnuwin32.sourceforge.net/">
    157         http://getgnuwin32.sourceforge.net/</a>.</li>
    158   </ul>
    159   </li>
    160 
    161   <li>Checkout LLVM:
    162   <ul>
    163     <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
    164   </ul>
    165   </li>
    166   <li>Checkout Clang:
    167   <ul>
    168      <li><tt>cd llvm\tools</tt>
    169      <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
    170   </ul>
    171   </li>
    172   <li>Run cmake to generate the Visual Studio solution and project files:
    173   <ul>
    174     <li><tt>cd ..\..</tt>  (back to where you started)</li>
    175     <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
    176     <li><tt>cd build</tt></li>
    177     <li>If you are using Visual Studio 2008:  <tt>cmake -G "Visual Studio 9 2008" ..\llvm</tt></li>
    178     <li>Or if you are using Visual Studio 2010:  <tt>cmake -G "Visual Studio 10" ..\llvm</tt></li>
    179     <li>By default, cmake will target LLVM to X86.  If you want all targets
    180         (needed if you want to run the LLVM tests), add the <tt>-DLLVM_TARGETS_TO_BUILD=all</tt> option to the
    181         cmake command line.  Or specify a target from the LLVM_TARGETS_TO_BUILD
    182         definition in CMakeLists.txt.</li>
    183     <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
    184         more information on other configuration options for cmake.</li>
    185     <li>The above, if successful, will have created an LLVM.sln file in the
    186        <tt>build</tt> directory.
    187   </ul>
    188   </li>
    189   <li>Build Clang:
    190   <ul>
    191     <li>Open LLVM.sln in Visual Studio.</li>
    192     <li>Build the "clang" project for just the compiler driver and front end, or
    193       the "ALL_BUILD" project to build everything, including tools.</li>
    194   </ul>
    195   </li>
    196   <li>Try it out (assuming you added llvm/debug/bin to your path).  (See the
    197     running examples from above.)</li>
    198   <li>See <a href="hacking.html#testingWindows">
    199      Hacking on clang - Testing using Visual Studio on Windows</a> for information
    200      on running regression tests on Windows.</li>
    201 </ol>
    202 
    203 <p>Note that once you have checked out both llvm and clang, to synchronize
    204 to the latest code base, use the <tt>svn update</tt> command in both the
    205 llvm and llvm\tools\clang directories, as they are separate repositories.</p>
    206 
    207 <h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
    208 
    209 <p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
    210 designed to be a drop-in replacement for the <tt>gcc</tt> command.  Here are
    211 some examples of how to use the high-level driver:
    212 </p>
    213 
    214 <pre class="code">
    215 $ <b>cat t.c</b>
    216 #include &lt;stdio.h&gt;
    217 int main(int argc, char **argv) { printf("hello world\n"); }
    218 $ <b>clang t.c</b>
    219 $ <b>./a.out</b>
    220 hello world
    221 </pre>
    222 
    223 <p>The 'clang' driver is designed to work as closely to GCC as possible to
    224   maximize portability.  The only major difference between the two is that
    225   Clang defaults to gnu99 mode while GCC defaults to gnu89 mode.  If you see
    226   weird link-time errors relating to inline functions, try passing -std=gnu89
    227   to clang.</p>
    228 
    229 <h2>Examples of using Clang</h2>
    230 
    231 <!-- Thanks to
    232  http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
    233 Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
    234 tag. -->
    235 
    236 <pre class="code">
    237 $ <b>cat ~/t.c</b>
    238 typedef float V __attribute__((vector_size(16)));
    239 V foo(V a, V b) { return a+b*a; }
    240 </pre>
    241 
    242 
    243 <h3>Preprocessing:</h3>
    244 
    245 <pre class="code">
    246 $ <b>clang ~/t.c -E</b>
    247 # 1 "/Users/sabre/t.c" 1
    248 
    249 typedef float V __attribute__((vector_size(16)));
    250 
    251 V foo(V a, V b) { return a+b*a; }
    252 </pre>
    253 
    254 
    255 <h3>Type checking:</h3>
    256 
    257 <pre class="code">
    258 $ <b>clang -fsyntax-only ~/t.c</b>
    259 </pre>
    260 
    261 
    262 <h3>GCC options:</h3>
    263 
    264 <pre class="code">
    265 $ <b>clang -fsyntax-only ~/t.c -pedantic</b>
    266 /Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
    267 <span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
    268 <span style="color:blue">                ^</span>
    269 1 diagnostic generated.
    270 </pre>
    271 
    272 
    273 <h3>Pretty printing from the AST:</h3>
    274 
    275 <p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
    276 not the driver, should be run. The compiler front-end has several additional
    277 Clang specific features which are not exposed through the GCC compatible driver
    278 interface.</p>
    279 
    280 <pre class="code">
    281 $ <b>clang -cc1 ~/t.c -ast-print</b>
    282 typedef float V __attribute__(( vector_size(16) ));
    283 V foo(V a, V b) {
    284    return a + b * a;
    285 }
    286 </pre>
    287 
    288 
    289 <h3>Code generation with LLVM:</h3>
    290 
    291 <pre class="code">
    292 $ <b>clang ~/t.c -S -emit-llvm -o -</b>
    293 define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
    294 entry:
    295          %mul = mul &lt;4 x float&gt; %b, %a
    296          %add = add &lt;4 x float&gt; %mul, %a
    297          ret &lt;4 x float&gt; %add
    298 }
    299 $ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
    300 ...
    301 _foo:
    302 Leh_func_begin1:
    303 	mulps	%xmm0, %xmm1
    304 	addps	%xmm1, %xmm0
    305 	ret
    306 Leh_func_end1:
    307 </pre>
    308 
    309 </div>
    310 </body>
    311 </html>
    312