xref: /llvm-project/llvm/docs/GettingStartedVS.rst (revision 33f4a77d9218c1a2d5c994a8a8398479731612dc)
1==================================================================
2Getting Started with the LLVM System using Microsoft Visual Studio
3==================================================================
4
5
6.. contents::
7   :local:
8
9
10Overview
11========
12Welcome to LLVM on Windows! This document only covers LLVM on Windows using
13Visual Studio, not WSL, mingw or cygwin. In order to get started, you first need
14to know some basic information.
15
16There are many different projects that compose LLVM. The first piece is the
17LLVM suite. This contains all of the tools, libraries, and header files needed
18to use LLVM. It contains an assembler, disassembler, bitcode analyzer and
19bitcode optimizer. It also contains basic regression tests that can be used to
20test the LLVM tools and the Clang front end.
21
22The second piece is the `Clang <https://clang.llvm.org/>`_ front end.  This
23component compiles C, C++, Objective C, and Objective C++ code into LLVM
24bitcode. Clang typically uses LLVM libraries to optimize the bitcode and emit
25machine code. LLVM fully supports the COFF object file format, which is
26compatible with all other existing Windows toolchains.
27
28There are more LLVM projects which this document does not discuss.
29
30
31Requirements
32============
33Before you begin to use the LLVM system, review the requirements given
34below.  This may save you some trouble by knowing ahead of time what hardware
35and software you will need.
36
37Hardware
38--------
39Any system that can adequately run Visual Studio 2019 is fine. The LLVM
40source tree including the git index consumes approximately 3GB.
41Object files, libraries and executables consume approximately 5GB in
42Release mode and much more in Debug mode. SSD drive and >16GB RAM are
43recommended.
44
45
46Software
47--------
48You will need `Visual Studio <https://visualstudio.microsoft.com/>`_ 2019 or
49later, with the latest Update installed. Visual Studio Community Edition
50suffices.
51
52You will also need the `CMake <http://www.cmake.org/>`_ build system since it
53generates the project files you will use to build with. CMake is bundled with
54Visual Studio 2019 so separate installation is not required. If you do install
55CMake separately, Visual Studio 2022 will require CMake Version 3.21 or later.
56
57If you would like to run the LLVM tests you will need `Python
58<http://www.python.org/>`_. Version 3.8 and newer are known to work. You can
59install Python with Visual Studio 2019, from the Microsoft store or from
60the `Python web site <http://www.python.org/>`_. We recommend the latter since it
61allows you to adjust installation options.
62
63You will need `Git for Windows <https://git-scm.com/>`_ with bash tools, too.
64Git for Windows is also bundled with Visual Studio 2019.
65
66
67Getting Started
68===============
69Here's the short story for getting up and running quickly with LLVM.
70These instruction were tested with Visual Studio 2019 and Python 3.9.6:
71
721. Download and install `Visual Studio <https://visualstudio.microsoft.com/>`_.
732. In the Visual Studio installer, Workloads tab, select the
74   **Desktop development with C++** workload. Under Individual components tab,
75   select **Git for Windows**.
763. Complete the Visual Studio installation.
774. Download and install the latest `Python 3 release <http://www.python.org/>`_.
785. In the first install screen, select both **Install launcher for all users**
79   and **Add Python to the PATH**. This will allow installing psutil for all
80   users for the regression tests and make Python available from the command
81   line.
826. In the second install screen, select (again) **Install for all users** and
83   if you want to develop `lldb <https://lldb.llvm.org/>`_, selecting
84   **Download debug binaries** is useful.
857. Complete the Python installation.
868. Run a "Developer Command Prompt for VS 2019" **as administrator**. This command
87    prompt provides correct path and environment variables to Visual Studio and
88    the installed tools.
899. In the terminal window, type the commands:
90
91   .. code-block:: bat
92
93     c:
94     cd \
95
96  You may install the llvm sources in other location than ``c:\llvm`` but do not
97  install into a path containing spaces (e.g. ``c:\Documents and Settings\...``)
98  as it will fail.
99
10010. Register the Microsoft Debug Interface Access (DIA) DLLs
101
102    .. code-block:: bat
103
104     regsvr32 "%VSINSTALLDIR%\DIA SDK\bin\msdia140.dll"
105     regsvr32 "%VSINSTALLDIR%\DIA SDK\bin\amd64\msdia140.dll"
106
107 The DIA library is required for LLVM PDB tests and
108 `LLDB development <https://lldb.llvm.org/resources/build.html>`_.
109
11011. Install psutil and obtain LLVM source code:
111
112    .. code-block:: bat
113
114     pip install psutil
115     git clone https://github.com/llvm/llvm-project.git llvm
116
117 Instead of ``git clone`` you may download a compressed source distribution
118 from the `releases page <https://github.com/llvm/llvm-project/releases>`_.
119 Select the last link: ``Source code (zip)`` and unpack the downloaded file using
120 Windows Explorer built-in zip support or any other unzip tool.
121
12212. Finally, configure LLVM using CMake:
123
124    .. code-block:: bat
125
126       cmake -S llvm\llvm -B build -DLLVM_ENABLE_PROJECTS=clang -DLLVM_TARGETS_TO_BUILD=X86 -Thost=x64
127       exit
128
129   ``LLVM_ENABLE_PROJECTS`` specifies any additional LLVM projects you want to
130   build while ``LLVM_TARGETS_TO_BUILD`` selects the compiler targets. If
131   ``LLVM_TARGETS_TO_BUILD`` is omitted by default all targets are built
132   slowing compilation and using more disk space.
133   See the :doc:`LLVM CMake guide <CMake>` for detailed information about
134   how to configure the LLVM build.
135
136   The ``cmake`` command line tool is bundled with Visual Studio but its GUI is
137   not. You may install `CMake <http://www.cmake.org/>`_ to use its GUI to change
138   CMake variables or modify the above command line.
139
140   * Once CMake is installed then the simplest way is to just start the
141     CMake GUI, select the directory where you have LLVM extracted to, and
142     the default options should all be fine.  One option you may really
143     want to change, regardless of anything else, might be the
144     ``CMAKE_INSTALL_PREFIX`` setting to select a directory to INSTALL to
145     once compiling is complete, although installation is not mandatory for
146     using LLVM.  Another important option is ``LLVM_TARGETS_TO_BUILD``,
147     which controls the LLVM target architectures that are included on the
148     build.
149   * CMake generates project files for all build types. To select a specific
150     build type, use the Configuration manager from the VS IDE or the
151     ``/property:Configuration`` command line option when using MSBuild.
152   * By default, the Visual Studio project files generated by CMake use the
153     32-bit toolset. If you are developing on a 64-bit version of Windows and
154     want to use the 64-bit toolset, pass the ``-Thost=x64`` flag when
155     generating the Visual Studio solution. This requires CMake 3.8.0 or later.
156
15713. Start Visual Studio and select configuration:
158
159   In the directory you created the project files will have an ``llvm.sln``
160   file, just double-click on that to open Visual Studio. The default Visual
161   Studio configuration is **Debug** which is slow and generates a huge amount
162   of debug information on disk. For now, we recommend selecting **Release**
163   configuration for the LLVM project which will build the fastest or
164   **RelWithDebInfo** which is also several time larger than Release.
165   Another technique is to build all of LLVM in Release mode and change
166   compiler flags, disabling optimization and enabling debug information, only
167   for specific libraries or source files you actually need to debug.
168
16914. Test LLVM in Visual Studio:
170
171   You can run LLVM tests by merely building the project "check-all". The test
172   results will be shown in the VS output window. Once the build succeeds, you
173   have verified a working LLVM development environment!
174
175   You should not see any unexpected failures, but will see many unsupported
176   tests and expected failures:
177
178   ::
179
180    114>Testing Time: 1124.66s
181    114>  Skipped          :    39
182    114>  Unsupported      : 21649
183    114>  Passed           : 51615
184    114>  Expectedly Failed:    93
185    ========== Build: 114 succeeded, 0 failed, 321 up-to-date, 0 skipped ==========``
186
187Alternatives to manual installation
188===================================
189Instead of the steps above, to simplify the installation procedure you can use
190`Chocolatey <https://chocolatey.org/>`_ as package manager.
191After the `installation <https://chocolatey.org/install>`_ of Chocolatey,
192run these commands in an admin shell to install the required tools:
193
194.. code-block:: bat
195
196   choco install -y git cmake python3
197   pip3 install psutil
198
199There is also a Windows
200`Dockerfile <https://github.com/llvm/llvm-zorg/blob/main/buildbot/google/docker/windows-base-vscode2019/Dockerfile>`_
201with the entire build tool chain. This can be used to test the build with a
202tool chain different from your host installation or to create build servers.
203
204Next steps
205==========
2061. Read the documentation.
2072. Seriously, read the documentation.
2083. Remember that you were warned twice about reading the documentation.
209
210Test LLVM on the command line:
211------------------------------
212The LLVM tests can be run by changing directory to the llvm source
213directory and running:
214
215.. code-block:: bat
216
217  c:\llvm> python ..\build\Release\bin\llvm-lit.py llvm\test
218
219This example assumes that Python is in your PATH variable, which would be
220after **Add Python to the PATH** was selected during Python installation.
221If you had opened a command window prior to Python installation, you would
222have to close and reopen it to get the updated PATH.
223
224A specific test or test directory can be run with:
225
226.. code-block:: bat
227
228  c:\llvm> python ..\build\Release\bin\llvm-lit.py llvm\test\Transforms\Util
229
230Build the LLVM Suite:
231---------------------
232* The projects may still be built individually, but to build them all do
233  not just select all of them in batch build (as some are meant as
234  configuration projects), but rather select and build just the
235  ``ALL_BUILD`` project to build everything, or the ``INSTALL`` project,
236  which first builds the ``ALL_BUILD`` project, then installs the LLVM
237  headers, libs, and other useful things to the directory set by the
238  ``CMAKE_INSTALL_PREFIX`` setting when you first configured CMake.
239* The Fibonacci project is a sample program that uses the JIT. Modify the
240  project's debugging properties to provide a numeric command line argument
241  or run it from the command line.  The program will print the
242  corresponding fibonacci value.
243
244
245Links
246=====
247This document is just an **introduction** to how to use LLVM to do some simple
248things... there are many more interesting and complicated things that you can
249do that aren't documented here (but we'll gladly accept a patch if you want to
250write something up!).  For more information about LLVM, check out:
251
252* `LLVM homepage <https://llvm.org/>`_
253* `LLVM doxygen tree <https://llvm.org/doxygen/>`_
254* Additional information about the LLVM directory structure and tool chain
255  can be found on the main :doc:`GettingStarted` page.
256* If you are having problems building or using LLVM, or if you have any other
257  general questions about LLVM, please consult the
258  :doc:`Frequently Asked Questions <FAQ>` page.
259