blob: 566a0f88c9c5791315347000cd8e45c2a8bc303b [file] [log] [blame]
Michael J. Spencerd93399a2012-06-21 23:27:09 +00001==================================================================
2Getting Started with the LLVM System using Microsoft Visual Studio
3==================================================================
4
5.. contents::
6 :local:
7
8
9Overview
10========
11Welcome to LLVM on Windows! This document only covers LLVM on Windows using
12Visual Studio, not mingw or cygwin. In order to get started, you first need to
13know some basic information.
14
Reid Klecknerba19d182016-01-11 20:51:57 +000015There are many different projects that compose LLVM. The first piece is the
16LLVM suite. This contains all of the tools, libraries, and header files needed
17to use LLVM. It contains an assembler, disassembler, bitcode analyzer and
18bitcode optimizer. It also contains basic regression tests that can be used to
19test the LLVM tools and the Clang front end.
Michael J. Spencerd93399a2012-06-21 23:27:09 +000020
Reid Klecknerba19d182016-01-11 20:51:57 +000021The second piece is the `Clang <http://clang.llvm.org/>`_ front end. This
22component compiles C, C++, Objective C, and Objective C++ code into LLVM
23bitcode. Clang typically uses LLVM libraries to optimize the bitcode and emit
24machine code. LLVM fully supports the COFF object file format, which is
25compatible with all other existing Windows toolchains.
Michael J. Spencerd93399a2012-06-21 23:27:09 +000026
Reid Klecknerba19d182016-01-11 20:51:57 +000027The last major part of LLVM, the execution Test Suite, does not run on Windows,
28and this document does not discuss it.
Michael J. Spencerd93399a2012-06-21 23:27:09 +000029
30Additional information about the LLVM directory structure and tool chain
Sean Silva1703e702014-04-08 21:06:22 +000031can be found on the main :doc:`GettingStarted` page.
Michael J. Spencerd93399a2012-06-21 23:27:09 +000032
33
34Requirements
35============
36Before you begin to use the LLVM system, review the requirements given
37below. This may save you some trouble by knowing ahead of time what hardware
38and software you will need.
39
40Hardware
41--------
Benjamin Kramerde1a1932015-02-15 19:34:17 +000042Any system that can adequately run Visual Studio 2013 is fine. The LLVM
Michael J. Spencerd93399a2012-06-21 23:27:09 +000043source tree and object files, libraries and executables will consume
44approximately 3GB.
45
46Software
47--------
Benjamin Kramerde1a1932015-02-15 19:34:17 +000048You will need Visual Studio 2013 or higher.
Michael J. Spencerd93399a2012-06-21 23:27:09 +000049
50You will also need the `CMake <http://www.cmake.org/>`_ build system since it
51generates the project files you will use to build with.
52
53If you would like to run the LLVM tests you will need `Python
Rafael Espindola21a400852014-12-12 15:29:31 +000054<http://www.python.org/>`_. Version 2.7 and newer are known to work. You will
55need `GnuWin32 <http://gnuwin32.sourceforge.net/>`_ tools, too.
Michael J. Spencerd93399a2012-06-21 23:27:09 +000056
57Do not install the LLVM directory tree into a path containing spaces (e.g.
58``C:\Documents and Settings\...``) as the configure step will fail.
59
60
61Getting Started
62===============
63Here's the short story for getting up and running quickly with LLVM:
64
651. Read the documentation.
662. Seriously, read the documentation.
673. Remember that you were warned twice about reading the documentation.
684. Get the Source Code
69
70 * With the distributed files:
71
72 1. ``cd <where-you-want-llvm-to-live>``
73 2. ``gunzip --stdout llvm-VERSION.tar.gz | tar -xvf -``
74 (*or use WinZip*)
75 3. ``cd llvm``
76
77 * With anonymous Subversion access:
78
79 1. ``cd <where-you-want-llvm-to-live>``
80 2. ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
81 3. ``cd llvm``
82
835. Use `CMake <http://www.cmake.org/>`_ to generate up-to-date project files:
84
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
93 build.
Keno Fischer3c305442016-02-07 19:36:54 +000094 * If CMake complains that it cannot find the compiler, make sure that
95 you have the Visual Studio C++ Tools installed, not just Visual Studio
96 itself (trying to create a C++ project in Visual Studio will generally
97 download the C++ tools if they haven't already been).
Sean Silva1703e702014-04-08 21:06:22 +000098 * See the :doc:`LLVM CMake guide <CMake>` for detailed information about
Michael J. Spencerd93399a2012-06-21 23:27:09 +000099 how to configure the LLVM build.
Yaron Kereneadc9b32014-06-05 16:42:26 +0000100 * CMake generates project files for all build types. To select a specific
101 build type, use the Configuration manager from the VS IDE or the
102 ``/property:Configuration`` command line option when using MSBuild.
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000103
1046. Start Visual Studio
105
106 * In the directory you created the project files will have an ``llvm.sln``
107 file, just double-click on that to open Visual Studio.
108
1097. Build the LLVM Suite:
110
111 * The projects may still be built individually, but to build them all do
112 not just select all of them in batch build (as some are meant as
113 configuration projects), but rather select and build just the
114 ``ALL_BUILD`` project to build everything, or the ``INSTALL`` project,
115 which first builds the ``ALL_BUILD`` project, then installs the LLVM
116 headers, libs, and other useful things to the directory set by the
117 ``CMAKE_INSTALL_PREFIX`` setting when you first configured CMake.
118 * The Fibonacci project is a sample program that uses the JIT. Modify the
119 project's debugging properties to provide a numeric command line argument
120 or run it from the command line. The program will print the
121 corresponding fibonacci value.
122
Aaron Ballman1e9d9b22014-01-23 20:46:44 +00001238. Test LLVM in Visual Studio:
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000124
125 * If ``%PATH%`` does not contain GnuWin32, you may specify
126 ``LLVM_LIT_TOOLS_DIR`` on CMake for the path to GnuWin32.
127 * You can run LLVM tests by merely building the project "check". The test
128 results will be shown in the VS output window.
129
Aaron Ballman1e9d9b22014-01-23 20:46:44 +00001309. Test LLVM on the command line:
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000131
132 * The LLVM tests can be run by changing directory to the llvm source
133 directory and running:
134
135 .. code-block:: bat
136
Aaron Ballmanc8aefe42013-05-01 19:13:50 +0000137 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
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000138
Aaron Ballmanc8aefe42013-05-01 19:13:50 +0000139 This example assumes that Python is in your PATH variable, you
140 have built a Win32 Debug version of llvm with a standard out of
141 line build. You should not see any unexpected failures, but will
142 see many unsupported tests and expected failures.
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000143
144 A specific test or test directory can be run with:
145
146 .. code-block:: bat
147
Aaron Ballmanc8aefe42013-05-01 19:13:50 +0000148 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
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000149
150
151An Example Using the LLVM Tool Chain
152====================================
153
1541. First, create a simple C file, name it '``hello.c``':
155
156 .. code-block:: c
157
158 #include <stdio.h>
159 int main() {
160 printf("hello world\n");
161 return 0;
162 }
163
Daniel Dunbar06b9f9e2013-08-16 23:30:19 +00001642. Next, compile the C file into an LLVM bitcode file:
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000165
166 .. code-block:: bat
167
168 C:\..> clang -c hello.c -emit-llvm -o hello.bc
169
170 This will create the result file ``hello.bc`` which is the LLVM bitcode
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000171 that corresponds the compiled program and the library facilities that
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000172 it required. You can execute this file directly using ``lli`` tool,
173 compile it to native assembly with the ``llc``, optimize or analyze it
174 further with the ``opt`` tool, etc.
175
176 Alternatively you can directly output an executable with clang with:
177
178 .. code-block:: bat
179
180 C:\..> clang hello.c -o hello.exe
181
182 The ``-o hello.exe`` is required because clang currently outputs ``a.out``
183 when neither ``-o`` nor ``-c`` are given.
184
1853. Run the program using the just-in-time compiler:
186
187 .. code-block:: bat
188
189 C:\..> lli hello.bc
190
1914. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code:
192
193 .. code-block:: bat
194
195 C:\..> llvm-dis < hello.bc | more
196
1975. Compile the program to object code using the LLC code generator:
198
199 .. code-block:: bat
200
201 C:\..> llc -filetype=obj hello.bc
202
2036. Link to binary using Microsoft link:
204
205 .. code-block:: bat
206
207 C:\..> link hello.obj -defaultlib:libcmt
208
2097. Execute the native code program:
210
211 .. code-block:: bat
212
213 C:\..> hello.exe
214
215
216Common Problems
217===============
218If you are having problems building or using LLVM, or if you have any other
Sean Silva1703e702014-04-08 21:06:22 +0000219general questions about LLVM, please consult the :doc:`Frequently Asked Questions
220<FAQ>` page.
Michael J. Spencerd93399a2012-06-21 23:27:09 +0000221
222
223Links
224=====
225This document is just an **introduction** to how to use LLVM to do some simple
226things... there are many more interesting and complicated things that you can
227do that aren't documented here (but we'll gladly accept a patch if you want to
228write something up!). For more information about LLVM, check out:
229
230* `LLVM homepage <http://llvm.org/>`_
231* `LLVM doxygen tree <http://llvm.org/doxygen/>`_
232