2008-02-08 09:45:49 +03:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
|
|
|
"http://www.w3.org/TR/html4/strict.dtd">
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
|
|
|
<title>Clang - Getting Started</title>
|
|
|
|
<link type="text/css" rel="stylesheet" href="menu.css" />
|
|
|
|
<link type="text/css" rel="stylesheet" href="content.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<!--#include virtual="menu.html.incl"-->
|
|
|
|
|
|
|
|
<div id="content">
|
|
|
|
|
|
|
|
<h1>Getting Started: Building and Running Clang</h1>
|
|
|
|
|
2009-03-27 19:32:57 +03:00
|
|
|
<p>This page gives you the shortest path to checking out Clang and demos a few
|
2008-02-08 09:45:49 +03:00
|
|
|
options. This should get you up and running with the minimum of muss and fuss.
|
|
|
|
If you like what you see, please consider <a href="get_involved.html">getting
|
2009-11-09 06:18:18 +03:00
|
|
|
involved</a> with the Clang community. If you run into problems, please file
|
2010-10-08 00:20:57 +04:00
|
|
|
bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
2010-11-03 01:34:04 +03:00
|
|
|
<h2 id="download">Release Clang Versions</h2>
|
|
|
|
|
|
|
|
<p>Clang has been released as part of regular LLVM releases since LLVM 2.6. You
|
|
|
|
can download the release versions
|
|
|
|
from <a href="http://llvm.org/releases/">http://llvm.org/releases/</a>.</p>
|
|
|
|
|
2009-03-27 19:32:57 +03:00
|
|
|
<h2 id="build">Building Clang and Working with the Code</h2>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
2009-08-03 23:42:28 +04:00
|
|
|
<h3 id="buildNix">On Unix-like Systems</h3>
|
|
|
|
|
2009-03-27 19:32:57 +03:00
|
|
|
<p>If you would like to check out and build Clang, the current procedure is as
|
|
|
|
follows:</p>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
|
|
|
<ol>
|
2009-08-06 17:00:08 +04:00
|
|
|
<li>Get the required tools.
|
|
|
|
<ul>
|
|
|
|
<li>See
|
|
|
|
<a href="http://llvm.org/docs/GettingStarted.html#requirements">
|
|
|
|
Getting Started with the LLVM System - Requirements</a>.</li>
|
|
|
|
<li>Note also that Python is needed for running the test suite.
|
|
|
|
Get it at: <a href="http://www.python.org/download">
|
|
|
|
http://www.python.org/download</a></li>
|
|
|
|
</ul>
|
|
|
|
|
2009-08-20 10:17:11 +04:00
|
|
|
<li>Checkout LLVM:</li>
|
2008-02-08 09:45:49 +03:00
|
|
|
<ul>
|
2009-08-20 10:17:11 +04:00
|
|
|
<li>Change directory to where you want the llvm directory placed.</li>
|
2008-02-08 09:45:49 +03:00
|
|
|
<li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
|
|
|
|
</ul>
|
2009-03-27 19:32:57 +03:00
|
|
|
<li>Checkout Clang:</li>
|
2008-02-08 09:45:49 +03:00
|
|
|
<ul>
|
2009-08-20 10:17:11 +04:00
|
|
|
<li><tt>cd llvm/tools</tt>
|
|
|
|
<li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
|
|
|
|
</ul>
|
|
|
|
<li>Build LLVM and Clang:</li>
|
|
|
|
<ul>
|
2010-10-30 02:05:17 +04:00
|
|
|
<li><tt>cd ../..</tt> (back to where you started)</li>
|
|
|
|
<li><tt>mkdir build</tt> (for building without polluting the source dir)
|
|
|
|
</li>
|
|
|
|
<li><tt>cd build</tt></li>
|
|
|
|
<li><tt>../llvm/configure</tt></li>
|
2009-08-20 10:17:11 +04:00
|
|
|
<li><tt>make</tt></li>
|
|
|
|
<li>This builds both LLVM and Clang for debug mode.</li>
|
|
|
|
<li>Note: For subsequent Clang development, you can just do make at the
|
|
|
|
clang directory level.</li>
|
2008-02-08 09:45:49 +03:00
|
|
|
</ul>
|
2010-05-07 01:57:57 +04:00
|
|
|
|
|
|
|
<p>It is also possible to use CMake instead of the makefiles. With CMake it
|
|
|
|
is also possible to generate project files for several IDEs: Eclipse CDT4,
|
|
|
|
CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</p>
|
|
|
|
|
2009-03-27 19:32:57 +03:00
|
|
|
<li>If you intend to work on Clang C++ support, you may need to tell it how
|
|
|
|
to find your C++ standard library headers. If Clang cannot find your
|
2009-03-10 19:01:44 +03:00
|
|
|
system libstdc++ headers, please follow these instructions:</li>
|
2008-02-08 09:45:49 +03:00
|
|
|
<ul>
|
2010-03-30 01:36:06 +04:00
|
|
|
<li>'<tt>gcc -v -x c++ /dev/null -fsyntax-only</tt>' to get the
|
2008-02-08 09:45:49 +03:00
|
|
|
path.</li>
|
|
|
|
<li>Look for the comment "FIXME: temporary hack:
|
2009-03-10 19:01:44 +03:00
|
|
|
hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
|
2008-02-08 09:45:49 +03:00
|
|
|
change the lines below to include that path.</li>
|
|
|
|
</ul>
|
2010-07-27 02:51:00 +04:00
|
|
|
<li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):</li>
|
2008-02-08 09:45:49 +03:00
|
|
|
<ul>
|
2009-12-12 02:04:35 +03:00
|
|
|
<li><tt>clang --help</tt></li>
|
|
|
|
<li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
|
|
|
|
<li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
|
|
|
|
<li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
|
|
|
|
<li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
|
2008-02-08 09:45:49 +03:00
|
|
|
</ul>
|
|
|
|
</ol>
|
|
|
|
|
2009-03-27 19:32:57 +03:00
|
|
|
<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
|
|
|
|
encounter problems with building Clang, make sure you have the latest SVN
|
|
|
|
version of LLVM. LLVM contains support libraries for Clang that will be updated
|
|
|
|
as well as development on Clang progresses.</p>
|
2008-02-08 10:32:26 +03:00
|
|
|
|
2009-03-27 19:32:57 +03:00
|
|
|
<h3>Simultaneously Building Clang and LLVM:</h3>
|
|
|
|
|
|
|
|
<p>Once you have checked out Clang into the llvm source tree it will build along
|
|
|
|
with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
|
|
|
|
once simply run <tt>make</tt> from the root LLVM directory.</p>
|
|
|
|
|
|
|
|
<p><em>Note:</em> Observe that Clang is technically part of a separate
|
|
|
|
Subversion repository. As mentioned above, the latest Clang sources are tied to
|
|
|
|
the latest sources in the LLVM tree. You can update your toplevel LLVM project
|
|
|
|
and all (possibly unrelated) projects inside it with <tt><b>make
|
|
|
|
update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
|
|
|
|
to subversion. </p>
|
|
|
|
|
2009-08-03 23:42:28 +04:00
|
|
|
<h3 id="buildWindows">Using Visual Studio</h3>
|
|
|
|
|
|
|
|
<p>The following details setting up for and building Clang on Windows using
|
|
|
|
Visual Studio:</p>
|
|
|
|
|
|
|
|
<ol>
|
|
|
|
<li>Get the required tools:</li>
|
|
|
|
<ul>
|
|
|
|
<li><b>Subversion</b>. Source code control program. Get it from:
|
|
|
|
<a href="http://subversion.tigris.org/getting.html">
|
|
|
|
http://subversion.tigris.org/getting.html</a></li>
|
|
|
|
<li><b>cmake</b>. This is used for generating Visual Studio solution and
|
|
|
|
project files. Get it from:
|
|
|
|
<a href="http://www.cmake.org/cmake/resources/software.html">
|
|
|
|
http://www.cmake.org/cmake/resources/software.html</a></li>
|
2010-12-17 01:01:14 +03:00
|
|
|
<li><b>Visual Studio 2005, 2008, or 2010</b></li>
|
2009-08-20 10:17:11 +04:00
|
|
|
<li><b>Python</b>. This is needed only if you will be running the tests
|
2009-08-03 23:42:28 +04:00
|
|
|
(which is essential, if you will be developing for clang).
|
|
|
|
Get it from:
|
2011-03-27 06:04:21 +04:00
|
|
|
<a href="http://www.python.org/download/">
|
|
|
|
http://www.python.org/download/</a></li>
|
2009-08-20 10:17:11 +04:00
|
|
|
<li><b>GnuWin32 tools</b>
|
|
|
|
These are also necessary for running the tests.
|
|
|
|
(Note that the grep from MSYS or Cygwin doesn't work with the tests
|
|
|
|
because of embedded double-quotes in the search strings. The GNU
|
|
|
|
grep does work in this case.)
|
2011-03-27 06:04:21 +04:00
|
|
|
Get them from <a href="http://getgnuwin32.sourceforge.net/">
|
|
|
|
http://getgnuwin32.sourceforge.net/</a>.</li>
|
2009-08-03 23:42:28 +04:00
|
|
|
</ul>
|
|
|
|
|
|
|
|
<li>Checkout LLVM:</li>
|
|
|
|
<ul>
|
|
|
|
<li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
|
|
|
|
</ul>
|
|
|
|
<li>Checkout Clang:</li>
|
|
|
|
<ul>
|
|
|
|
<li><tt>cd llvm\tools</tt>
|
|
|
|
<li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
|
|
|
|
</ul>
|
|
|
|
<li>Run cmake to generate the Visual Studio solution and project files:</li>
|
|
|
|
<ul>
|
2010-12-17 01:01:14 +03:00
|
|
|
<li><tt>cd ..\..</tt> (back to where you started)</li>
|
|
|
|
<li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
|
|
|
|
<li><tt>cd build</tt></li>
|
|
|
|
<li>If you are using Visual Studio 2005: <tt>cmake -G "Visual Studio 8 2005" ..\llvm</tt></li>
|
|
|
|
<li>Or if you are using Visual Studio 2008: <tt>cmake -G "Visual Studio 9 2008" ..\llvm</tt></li>
|
|
|
|
<li>Or if you are using Visual Studio 2010: <tt>cmake -G "Visual Studio 10" ..\llvm</tt></li>
|
2009-08-03 23:42:28 +04:00
|
|
|
<li>The above, if successful, will have created an LLVM.sln file in the
|
|
|
|
llvm directory.
|
|
|
|
</ul>
|
|
|
|
<li>Build Clang:</li>
|
|
|
|
<ul>
|
|
|
|
<li>Open LLVM.sln in Visual Studio.</li>
|
2009-12-12 02:04:35 +03:00
|
|
|
<li>Build the "clang" project for just the compiler driver and front end, or
|
|
|
|
the "ALL_BUILD" project to build everything, including tools.</li>
|
2009-08-03 23:42:28 +04:00
|
|
|
</ul>
|
|
|
|
<li>Try it out (assuming you added llvm/debug/bin to your path). (See the
|
|
|
|
running examples from above.)</li>
|
|
|
|
<li>See <a href="hacking.html#testingWindows">
|
|
|
|
Hacking on clang - Testing using Visual Studio on Windows</a> for information
|
|
|
|
on running regression tests on Windows.</li>
|
|
|
|
</ol>
|
|
|
|
|
|
|
|
<p>Note that once you have checked out both llvm and clang, to synchronize
|
|
|
|
to the latest code base, use the <tt>svn update</tt> command in both the
|
|
|
|
llvm and llvm\tools\clang directories, as they are separate repositories.</p>
|
|
|
|
|
2009-12-12 02:04:35 +03:00
|
|
|
<a name="driver"><h2>Clang Compiler Driver (Drop-in Substitute for GCC)</h2></a>
|
2009-03-27 19:32:57 +03:00
|
|
|
|
2009-12-12 02:04:35 +03:00
|
|
|
<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
|
|
|
|
designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
|
|
|
|
some examples of how to use the high-level driver:
|
2009-03-27 19:32:57 +03:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<pre class="code">
|
|
|
|
$ <b>cat t.c</b>
|
|
|
|
#include <stdio.h>
|
|
|
|
int main(int argc, char **argv) { printf("hello world\n"); }
|
|
|
|
$ <b>clang t.c</b>
|
|
|
|
$ <b>./a.out</b>
|
|
|
|
hello world
|
|
|
|
</pre>
|
|
|
|
|
2009-11-09 06:21:02 +03:00
|
|
|
<p>The 'clang' driver is designed to work as closely to GCC as possible to
|
|
|
|
maximize portability. The only major difference between the two is that
|
|
|
|
Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
|
2009-11-09 07:04:07 +03:00
|
|
|
weird link-time errors relating to inline functions, try passing -std=gnu89
|
2009-11-09 06:21:02 +03:00
|
|
|
to clang.</p>
|
|
|
|
|
2009-03-27 19:32:57 +03:00
|
|
|
<h2>Examples of using Clang</h2>
|
2008-02-08 10:10:48 +03:00
|
|
|
|
2008-02-08 09:45:49 +03:00
|
|
|
<!-- Thanks to
|
|
|
|
http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
|
|
|
|
Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
|
|
|
|
tag. -->
|
|
|
|
|
|
|
|
<pre class="code">
|
|
|
|
$ <b>cat ~/t.c</b>
|
|
|
|
typedef float V __attribute__((vector_size(16)));
|
|
|
|
V foo(V a, V b) { return a+b*a; }
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
2008-06-17 17:48:36 +04:00
|
|
|
<h3>Preprocessing:</h3>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
|
|
|
<pre class="code">
|
|
|
|
$ <b>clang ~/t.c -E</b>
|
|
|
|
# 1 "/Users/sabre/t.c" 1
|
|
|
|
|
|
|
|
typedef float V __attribute__((vector_size(16)));
|
|
|
|
|
|
|
|
V foo(V a, V b) { return a+b*a; }
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
2008-06-17 17:48:36 +04:00
|
|
|
<h3>Type checking:</h3>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
|
|
|
<pre class="code">
|
|
|
|
$ <b>clang -fsyntax-only ~/t.c</b>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
2008-06-17 17:48:36 +04:00
|
|
|
<h3>GCC options:</h3>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
|
|
|
<pre class="code">
|
|
|
|
$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
|
|
|
|
/Users/sabre/t.c:2:17: warning: extension used
|
|
|
|
typedef float V __attribute__((vector_size(16)));
|
|
|
|
^
|
|
|
|
1 diagnostic generated.
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
2008-06-17 17:48:36 +04:00
|
|
|
<h3>Pretty printing from the AST:</h3>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
2009-12-12 02:04:35 +03:00
|
|
|
<p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and
|
|
|
|
not the driver, should be run. The compiler front-end has several additional
|
|
|
|
Clang specific features which are not exposed through the GCC compatible driver
|
|
|
|
interface.</p>
|
|
|
|
|
2008-02-08 09:45:49 +03:00
|
|
|
<pre class="code">
|
2009-12-12 02:04:35 +03:00
|
|
|
$ <b>clang -cc1 ~/t.c -ast-print</b>
|
2008-02-08 09:45:49 +03:00
|
|
|
typedef float V __attribute__(( vector_size(16) ));
|
|
|
|
V foo(V a, V b) {
|
|
|
|
return a + b * a;
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
2008-06-17 17:48:36 +04:00
|
|
|
<h3>Code generation with LLVM:</h3>
|
2008-02-08 09:45:49 +03:00
|
|
|
|
|
|
|
<pre class="code">
|
2009-12-12 02:04:35 +03:00
|
|
|
$ <b>clang ~/t.c -S -emit-llvm -o -</b>
|
2008-02-08 09:45:49 +03:00
|
|
|
define <4 x float> @foo(<4 x float> %a, <4 x float> %b) {
|
|
|
|
entry:
|
|
|
|
%mul = mul <4 x float> %b, %a
|
|
|
|
%add = add <4 x float> %mul, %a
|
|
|
|
ret <4 x float> %add
|
|
|
|
}
|
2009-12-12 02:04:35 +03:00
|
|
|
$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
|
|
|
|
...
|
2008-02-08 09:45:49 +03:00
|
|
|
_foo:
|
2009-12-12 02:04:35 +03:00
|
|
|
Leh_func_begin1:
|
|
|
|
mulps %xmm0, %xmm1
|
|
|
|
addps %xmm1, %xmm0
|
|
|
|
ret
|
|
|
|
Leh_func_end1:
|
2008-02-08 09:45:49 +03:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|