1 ==================================================================
2 Getting Started with the LLVM System using Microsoft Visual Studio
3 ==================================================================
11 Welcome to LLVM on Windows! This document only covers LLVM on Windows using
12 Visual Studio, not mingw or cygwin. In order to get started, you first need to
13 know some basic information.
15 There are many different projects that compose LLVM. The first piece is the
16 LLVM suite. This contains all of the tools, libraries, and header files needed
17 to use LLVM. It contains an assembler, disassembler, bitcode analyzer and
18 bitcode optimizer. It also contains basic regression tests that can be used to
19 test the LLVM tools and the Clang front end.
21 The second piece is the `Clang <http://clang.llvm.org/>`_ front end. This
22 component compiles C, C++, Objective C, and Objective C++ code into LLVM
23 bitcode. Clang typically uses LLVM libraries to optimize the bitcode and emit
24 machine code. LLVM fully supports the COFF object file format, which is
25 compatible with all other existing Windows toolchains.
27 The last major part of LLVM, the execution Test Suite, does not run on Windows,
28 and this document does not discuss it.
30 Additional information about the LLVM directory structure and tool chain
31 can be found on the main :doc:`GettingStarted` page.
36 Before you begin to use the LLVM system, review the requirements given
37 below. This may save you some trouble by knowing ahead of time what hardware
38 and software you will need.
42 Any system that can adequately run Visual Studio 2013 is fine. The LLVM
43 source tree and object files, libraries and executables will consume
48 You will need Visual Studio 2013 or higher.
50 You will also need the `CMake <http://www.cmake.org/>`_ build system since it
51 generates the project files you will use to build with.
53 If you would like to run the LLVM tests you will need `Python
54 <http://www.python.org/>`_. Version 2.7 and newer are known to work. You will
55 need `GnuWin32 <http://gnuwin32.sourceforge.net/>`_ tools, too.
57 Do not install the LLVM directory tree into a path containing spaces (e.g.
58 ``C:\Documents and Settings\...``) as the configure step will fail.
63 Here's the short story for getting up and running quickly with LLVM:
65 1. Read the documentation.
66 2. Seriously, read the documentation.
67 3. Remember that you were warned twice about reading the documentation.
68 4. Get the Source Code
70 * With the distributed files:
72 1. ``cd <where-you-want-llvm-to-live>``
73 2. ``gunzip --stdout llvm-VERSION.tar.gz | tar -xvf -``
77 * With anonymous Subversion access:
79 1. ``cd <where-you-want-llvm-to-live>``
80 2. ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
83 5. Use `CMake <http://www.cmake.org/>`_ to generate up-to-date project files:
85 * Once CMake is installed then the simplest way is to just start the
86 CMake GUI, select the directory where you have LLVM extracted to, and
87 the default options should all be fine. One option you may really
88 want to change, regardless of anything else, might be the
89 ``CMAKE_INSTALL_PREFIX`` setting to select a directory to INSTALL to
90 once compiling is complete, although installation is not mandatory for
91 using LLVM. Another important option is ``LLVM_TARGETS_TO_BUILD``,
92 which controls the LLVM target architectures that are included on the
94 * See the :doc:`LLVM CMake guide <CMake>` for detailed information about
95 how to configure the LLVM build.
96 * CMake generates project files for all build types. To select a specific
97 build type, use the Configuration manager from the VS IDE or the
98 ``/property:Configuration`` command line option when using MSBuild.
100 6. Start Visual Studio
102 * In the directory you created the project files will have an ``llvm.sln``
103 file, just double-click on that to open Visual Studio.
105 7. Build the LLVM Suite:
107 * The projects may still be built individually, but to build them all do
108 not just select all of them in batch build (as some are meant as
109 configuration projects), but rather select and build just the
110 ``ALL_BUILD`` project to build everything, or the ``INSTALL`` project,
111 which first builds the ``ALL_BUILD`` project, then installs the LLVM
112 headers, libs, and other useful things to the directory set by the
113 ``CMAKE_INSTALL_PREFIX`` setting when you first configured CMake.
114 * The Fibonacci project is a sample program that uses the JIT. Modify the
115 project's debugging properties to provide a numeric command line argument
116 or run it from the command line. The program will print the
117 corresponding fibonacci value.
119 8. Test LLVM in Visual Studio:
121 * If ``%PATH%`` does not contain GnuWin32, you may specify
122 ``LLVM_LIT_TOOLS_DIR`` on CMake for the path to GnuWin32.
123 * You can run LLVM tests by merely building the project "check". The test
124 results will be shown in the VS output window.
126 9. Test LLVM on the command line:
128 * The LLVM tests can be run by changing directory to the llvm source
129 directory and running:
133 C:\..\llvm> python ..\build\bin\llvm-lit --param build_config=Win32 --param build_mode=Debug --param llvm_site_config=../build/test/lit.site.cfg test
135 This example assumes that Python is in your PATH variable, you
136 have built a Win32 Debug version of llvm with a standard out of
137 line build. You should not see any unexpected failures, but will
138 see many unsupported tests and expected failures.
140 A specific test or test directory can be run with:
144 C:\..\llvm> python ..\build\bin\llvm-lit --param build_config=Win32 --param build_mode=Debug --param llvm_site_config=../build/test/lit.site.cfg test/path/to/test
147 An Example Using the LLVM Tool Chain
148 ====================================
150 1. First, create a simple C file, name it '``hello.c``':
156 printf("hello world\n");
160 2. Next, compile the C file into an LLVM bitcode file:
164 C:\..> clang -c hello.c -emit-llvm -o hello.bc
166 This will create the result file ``hello.bc`` which is the LLVM bitcode
167 that corresponds the compiled program and the library facilities that
168 it required. You can execute this file directly using ``lli`` tool,
169 compile it to native assembly with the ``llc``, optimize or analyze it
170 further with the ``opt`` tool, etc.
172 Alternatively you can directly output an executable with clang with:
176 C:\..> clang hello.c -o hello.exe
178 The ``-o hello.exe`` is required because clang currently outputs ``a.out``
179 when neither ``-o`` nor ``-c`` are given.
181 3. Run the program using the just-in-time compiler:
187 4. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code:
191 C:\..> llvm-dis < hello.bc | more
193 5. Compile the program to object code using the LLC code generator:
197 C:\..> llc -filetype=obj hello.bc
199 6. Link to binary using Microsoft link:
203 C:\..> link hello.obj -defaultlib:libcmt
205 7. Execute the native code program:
214 If you are having problems building or using LLVM, or if you have any other
215 general questions about LLVM, please consult the :doc:`Frequently Asked Questions
221 This document is just an **introduction** to how to use LLVM to do some simple
222 things... there are many more interesting and complicated things that you can
223 do that aren't documented here (but we'll gladly accept a patch if you want to
224 write something up!). For more information about LLVM, check out:
226 * `LLVM homepage <http://llvm.org/>`_
227 * `LLVM doxygen tree <http://llvm.org/doxygen/>`_