1==================================================================
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
15There 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.
20
21The second piece is the `Clang <https://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.
26
27The last major part of LLVM, the execution Test Suite, does not run on Windows,
28and this document does not discuss it.
29
30Additional information about the LLVM directory structure and tool chain
31can be found on the main :doc:`GettingStarted` page.
32
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--------
42Any system that can adequately run Visual Studio 2017 is fine. The LLVM
43source tree and object files, libraries and executables will consume
44approximately 3GB.
45
46Software
47--------
48You will need Visual Studio 2017 or higher, with the latest Update installed.
49
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
54<http://www.python.org/>`_. Version 3.6 and newer are known to work. You will
55need `GnuWin32 <http://gnuwin32.sourceforge.net/>`_ tools, too.
56
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
60To simplify the installation procedure, you can also use
61`Chocolatey <https://chocolatey.org/>`_ as package manager. After the
62`installation <https://chocolatey.org/install>`_ of Chocolatey, run these
63commands in an admin shell to install the required tools:
64
65.. code-block:: bat
66
67   choco install -y ninja git cmake gnuwin python3
68   pip3 install psutil
69
70There is also a Windows
71`Dockerfile <https://github.com/llvm/llvm-zorg/blob/main/buildbot/google/docker/windows-base-vscode2019/Dockerfile>`_
72with the entire build tool chain. This can be used to test the build with a
73tool chain different from your host installation or to create build servers.
74
75Getting Started
76===============
77Here's the short story for getting up and running quickly with LLVM:
78
791. Read the documentation.
802. Seriously, read the documentation.
813. Remember that you were warned twice about reading the documentation.
824. Get the Source Code
83
84   * With the distributed files:
85
86      1. ``cd <where-you-want-llvm-to-live>``
87      2. ``gunzip --stdout llvm-VERSION.tar.gz | tar -xvf -``
88         (*or use WinZip*)
89      3. ``cd llvm``
90
91   * With git access:
92
93     *Note:* some regression tests require Unix-style line ending (``\n``).
94
95      1. ``cd <where-you-want-llvm-to-live>``
96      2. ``git clone https://github.com/llvm/llvm-project.git llvm``
97      3. ``cd llvm``
98
995. Use `CMake <http://www.cmake.org/>`_ to generate up-to-date project files:
100
101   * Once CMake is installed then the simplest way is to just start the
102     CMake GUI, select the directory where you have LLVM extracted to, and
103     the default options should all be fine.  One option you may really
104     want to change, regardless of anything else, might be the
105     ``CMAKE_INSTALL_PREFIX`` setting to select a directory to INSTALL to
106     once compiling is complete, although installation is not mandatory for
107     using LLVM.  Another important option is ``LLVM_TARGETS_TO_BUILD``,
108     which controls the LLVM target architectures that are included on the
109     build.
110   * If CMake complains that it cannot find the compiler, make sure that
111     you have the Visual Studio C++ Tools installed, not just Visual Studio
112     itself (trying to create a C++ project in Visual Studio will generally
113     download the C++ tools if they haven't already been).
114   * See the :doc:`LLVM CMake guide <CMake>` for detailed information about
115     how to configure the LLVM build.
116   * CMake generates project files for all build types. To select a specific
117     build type, use the Configuration manager from the VS IDE or the
118     ``/property:Configuration`` command line option when using MSBuild.
119   * By default, the Visual Studio project files generated by CMake use the
120     32-bit toolset. If you are developing on a 64-bit version of Windows and
121     want to use the 64-bit toolset, pass the ``-Thost=x64`` flag when
122     generating the Visual Studio solution. This requires CMake 3.8.0 or later.
123
1246. Start Visual Studio
125
126   * In the directory you created the project files will have an ``llvm.sln``
127     file, just double-click on that to open Visual Studio.
128
1297. Build the LLVM Suite:
130
131   * The projects may still be built individually, but to build them all do
132     not just select all of them in batch build (as some are meant as
133     configuration projects), but rather select and build just the
134     ``ALL_BUILD`` project to build everything, or the ``INSTALL`` project,
135     which first builds the ``ALL_BUILD`` project, then installs the LLVM
136     headers, libs, and other useful things to the directory set by the
137     ``CMAKE_INSTALL_PREFIX`` setting when you first configured CMake.
138   * The Fibonacci project is a sample program that uses the JIT. Modify the
139     project's debugging properties to provide a numeric command line argument
140     or run it from the command line.  The program will print the
141     corresponding fibonacci value.
142
1438. Test LLVM in Visual Studio:
144
145   * If ``%PATH%`` does not contain GnuWin32, you may specify
146     ``LLVM_LIT_TOOLS_DIR`` on CMake for the path to GnuWin32.
147   * You can run LLVM tests by merely building the project "check". The test
148     results will be shown in the VS output window.
149
1509. Test LLVM on the command line:
151
152   * The LLVM tests can be run by changing directory to the llvm source
153     directory and running:
154
155     .. code-block:: bat
156
157        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
158
159     This example assumes that Python is in your PATH variable, you
160     have built a Win32 Debug version of llvm with a standard out of
161     line build. You should not see any unexpected failures, but will
162     see many unsupported tests and expected failures.
163
164     A specific test or test directory can be run with:
165
166     .. code-block:: bat
167
168        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
169
170
171An Example Using the LLVM Tool Chain
172====================================
173
1741. First, create a simple C file, name it '``hello.c``':
175
176   .. code-block:: c
177
178      #include <stdio.h>
179      int main() {
180        printf("hello world\n");
181        return 0;
182      }
183
1842. Next, compile the C file into an LLVM bitcode file:
185
186   .. code-block:: bat
187
188      C:\..> clang -c hello.c -emit-llvm -o hello.bc
189
190   This will create the result file ``hello.bc`` which is the LLVM bitcode
191   that corresponds the compiled program and the library facilities that
192   it required.  You can execute this file directly using ``lli`` tool,
193   compile it to native assembly with the ``llc``, optimize or analyze it
194   further with the ``opt`` tool, etc.
195
196   Alternatively you can directly output an executable with clang with:
197
198   .. code-block:: bat
199
200      C:\..> clang hello.c -o hello.exe
201
202   The ``-o hello.exe`` is required because clang currently outputs ``a.out``
203   when neither ``-o`` nor ``-c`` are given.
204
2053. Run the program using the just-in-time compiler:
206
207   .. code-block:: bat
208
209      C:\..> lli hello.bc
210
2114. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code:
212
213   .. code-block:: bat
214
215      C:\..> llvm-dis < hello.bc | more
216
2175. Compile the program to object code using the LLC code generator:
218
219   .. code-block:: bat
220
221      C:\..> llc -filetype=obj hello.bc
222
2236. Link to binary using Microsoft link:
224
225   .. code-block:: bat
226
227      C:\..> link hello.obj -defaultlib:libcmt
228
2297. Execute the native code program:
230
231   .. code-block:: bat
232
233      C:\..> hello.exe
234
235
236Common Problems
237===============
238If you are having problems building or using LLVM, or if you have any other
239general questions about LLVM, please consult the :doc:`Frequently Asked Questions
240<FAQ>` page.
241
242
243Links
244=====
245This document is just an **introduction** to how to use LLVM to do some simple
246things... there are many more interesting and complicated things that you can
247do that aren't documented here (but we'll gladly accept a patch if you want to
248write something up!).  For more information about LLVM, check out:
249
250* `LLVM homepage <https://llvm.org/>`_
251* `LLVM doxygen tree <https://llvm.org/doxygen/>`_
252
253