xref: /aosp_15_r20/external/llvm/docs/GettingStartedVS.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker==================================================================
2*9880d681SAndroid Build Coastguard WorkerGetting Started with the LLVM System using Microsoft Visual Studio
3*9880d681SAndroid Build Coastguard Worker==================================================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard WorkerOverview
10*9880d681SAndroid Build Coastguard Worker========
11*9880d681SAndroid Build Coastguard WorkerWelcome to LLVM on Windows! This document only covers LLVM on Windows using
12*9880d681SAndroid Build Coastguard WorkerVisual Studio, not mingw or cygwin. In order to get started, you first need to
13*9880d681SAndroid Build Coastguard Workerknow some basic information.
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard WorkerThere are many different projects that compose LLVM. The first piece is the
16*9880d681SAndroid Build Coastguard WorkerLLVM suite. This contains all of the tools, libraries, and header files needed
17*9880d681SAndroid Build Coastguard Workerto use LLVM. It contains an assembler, disassembler, bitcode analyzer and
18*9880d681SAndroid Build Coastguard Workerbitcode optimizer. It also contains basic regression tests that can be used to
19*9880d681SAndroid Build Coastguard Workertest the LLVM tools and the Clang front end.
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard WorkerThe second piece is the `Clang <http://clang.llvm.org/>`_ front end.  This
22*9880d681SAndroid Build Coastguard Workercomponent compiles C, C++, Objective C, and Objective C++ code into LLVM
23*9880d681SAndroid Build Coastguard Workerbitcode. Clang typically uses LLVM libraries to optimize the bitcode and emit
24*9880d681SAndroid Build Coastguard Workermachine code. LLVM fully supports the COFF object file format, which is
25*9880d681SAndroid Build Coastguard Workercompatible with all other existing Windows toolchains.
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard WorkerThe last major part of LLVM, the execution Test Suite, does not run on Windows,
28*9880d681SAndroid Build Coastguard Workerand this document does not discuss it.
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard WorkerAdditional information about the LLVM directory structure and tool chain
31*9880d681SAndroid Build Coastguard Workercan be found on the main :doc:`GettingStarted` page.
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard WorkerRequirements
35*9880d681SAndroid Build Coastguard Worker============
36*9880d681SAndroid Build Coastguard WorkerBefore you begin to use the LLVM system, review the requirements given
37*9880d681SAndroid Build Coastguard Workerbelow.  This may save you some trouble by knowing ahead of time what hardware
38*9880d681SAndroid Build Coastguard Workerand software you will need.
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard WorkerHardware
41*9880d681SAndroid Build Coastguard Worker--------
42*9880d681SAndroid Build Coastguard WorkerAny system that can adequately run Visual Studio 2013 is fine. The LLVM
43*9880d681SAndroid Build Coastguard Workersource tree and object files, libraries and executables will consume
44*9880d681SAndroid Build Coastguard Workerapproximately 3GB.
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard WorkerSoftware
47*9880d681SAndroid Build Coastguard Worker--------
48*9880d681SAndroid Build Coastguard WorkerYou will need Visual Studio 2013 or higher, with the latest Update installed.
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard WorkerYou will also need the `CMake <http://www.cmake.org/>`_ build system since it
51*9880d681SAndroid Build Coastguard Workergenerates the project files you will use to build with. CMake 2.8.12.2 is the
52*9880d681SAndroid Build Coastguard Workerminimum required version for building with Visual Studio, though the latest
53*9880d681SAndroid Build Coastguard Workerversion of CMake is recommended.
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard WorkerIf you would like to run the LLVM tests you will need `Python
56*9880d681SAndroid Build Coastguard Worker<http://www.python.org/>`_. Version 2.7 and newer are known to work. You will
57*9880d681SAndroid Build Coastguard Workerneed `GnuWin32 <http://gnuwin32.sourceforge.net/>`_ tools, too.
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard WorkerDo not install the LLVM directory tree into a path containing spaces (e.g.
60*9880d681SAndroid Build Coastguard Worker``C:\Documents and Settings\...``) as the configure step will fail.
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard WorkerGetting Started
64*9880d681SAndroid Build Coastguard Worker===============
65*9880d681SAndroid Build Coastguard WorkerHere's the short story for getting up and running quickly with LLVM:
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker1. Read the documentation.
68*9880d681SAndroid Build Coastguard Worker2. Seriously, read the documentation.
69*9880d681SAndroid Build Coastguard Worker3. Remember that you were warned twice about reading the documentation.
70*9880d681SAndroid Build Coastguard Worker4. Get the Source Code
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker   * With the distributed files:
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker      1. ``cd <where-you-want-llvm-to-live>``
75*9880d681SAndroid Build Coastguard Worker      2. ``gunzip --stdout llvm-VERSION.tar.gz | tar -xvf -``
76*9880d681SAndroid Build Coastguard Worker         (*or use WinZip*)
77*9880d681SAndroid Build Coastguard Worker      3. ``cd llvm``
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker   * With anonymous Subversion access:
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker      1. ``cd <where-you-want-llvm-to-live>``
82*9880d681SAndroid Build Coastguard Worker      2. ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
83*9880d681SAndroid Build Coastguard Worker      3. ``cd llvm``
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker5. Use `CMake <http://www.cmake.org/>`_ to generate up-to-date project files:
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker   * Once CMake is installed then the simplest way is to just start the
88*9880d681SAndroid Build Coastguard Worker     CMake GUI, select the directory where you have LLVM extracted to, and
89*9880d681SAndroid Build Coastguard Worker     the default options should all be fine.  One option you may really
90*9880d681SAndroid Build Coastguard Worker     want to change, regardless of anything else, might be the
91*9880d681SAndroid Build Coastguard Worker     ``CMAKE_INSTALL_PREFIX`` setting to select a directory to INSTALL to
92*9880d681SAndroid Build Coastguard Worker     once compiling is complete, although installation is not mandatory for
93*9880d681SAndroid Build Coastguard Worker     using LLVM.  Another important option is ``LLVM_TARGETS_TO_BUILD``,
94*9880d681SAndroid Build Coastguard Worker     which controls the LLVM target architectures that are included on the
95*9880d681SAndroid Build Coastguard Worker     build.
96*9880d681SAndroid Build Coastguard Worker   * If CMake complains that it cannot find the compiler, make sure that
97*9880d681SAndroid Build Coastguard Worker     you have the Visual Studio C++ Tools installed, not just Visual Studio
98*9880d681SAndroid Build Coastguard Worker     itself (trying to create a C++ project in Visual Studio will generally
99*9880d681SAndroid Build Coastguard Worker     download the C++ tools if they haven't already been).
100*9880d681SAndroid Build Coastguard Worker   * See the :doc:`LLVM CMake guide <CMake>` for detailed information about
101*9880d681SAndroid Build Coastguard Worker     how to configure the LLVM build.
102*9880d681SAndroid Build Coastguard Worker   * CMake generates project files for all build types. To select a specific
103*9880d681SAndroid Build Coastguard Worker     build type, use the Configuration manager from the VS IDE or the
104*9880d681SAndroid Build Coastguard Worker     ``/property:Configuration`` command line option when using MSBuild.
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker6. Start Visual Studio
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker   * In the directory you created the project files will have an ``llvm.sln``
109*9880d681SAndroid Build Coastguard Worker     file, just double-click on that to open Visual Studio.
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker7. Build the LLVM Suite:
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker   * The projects may still be built individually, but to build them all do
114*9880d681SAndroid Build Coastguard Worker     not just select all of them in batch build (as some are meant as
115*9880d681SAndroid Build Coastguard Worker     configuration projects), but rather select and build just the
116*9880d681SAndroid Build Coastguard Worker     ``ALL_BUILD`` project to build everything, or the ``INSTALL`` project,
117*9880d681SAndroid Build Coastguard Worker     which first builds the ``ALL_BUILD`` project, then installs the LLVM
118*9880d681SAndroid Build Coastguard Worker     headers, libs, and other useful things to the directory set by the
119*9880d681SAndroid Build Coastguard Worker     ``CMAKE_INSTALL_PREFIX`` setting when you first configured CMake.
120*9880d681SAndroid Build Coastguard Worker   * The Fibonacci project is a sample program that uses the JIT. Modify the
121*9880d681SAndroid Build Coastguard Worker     project's debugging properties to provide a numeric command line argument
122*9880d681SAndroid Build Coastguard Worker     or run it from the command line.  The program will print the
123*9880d681SAndroid Build Coastguard Worker     corresponding fibonacci value.
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker8. Test LLVM in Visual Studio:
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker   * If ``%PATH%`` does not contain GnuWin32, you may specify
128*9880d681SAndroid Build Coastguard Worker     ``LLVM_LIT_TOOLS_DIR`` on CMake for the path to GnuWin32.
129*9880d681SAndroid Build Coastguard Worker   * You can run LLVM tests by merely building the project "check". The test
130*9880d681SAndroid Build Coastguard Worker     results will be shown in the VS output window.
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker9. Test LLVM on the command line:
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker   * The LLVM tests can be run by changing directory to the llvm source
135*9880d681SAndroid Build Coastguard Worker     directory and running:
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker     .. code-block:: bat
138*9880d681SAndroid Build Coastguard Worker
139*9880d681SAndroid Build Coastguard Worker        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
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker     This example assumes that Python is in your PATH variable, you
142*9880d681SAndroid Build Coastguard Worker     have built a Win32 Debug version of llvm with a standard out of
143*9880d681SAndroid Build Coastguard Worker     line build. You should not see any unexpected failures, but will
144*9880d681SAndroid Build Coastguard Worker     see many unsupported tests and expected failures.
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker     A specific test or test directory can be run with:
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker     .. code-block:: bat
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker        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
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard WorkerAn Example Using the LLVM Tool Chain
154*9880d681SAndroid Build Coastguard Worker====================================
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker1. First, create a simple C file, name it '``hello.c``':
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker   .. code-block:: c
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker      #include <stdio.h>
161*9880d681SAndroid Build Coastguard Worker      int main() {
162*9880d681SAndroid Build Coastguard Worker        printf("hello world\n");
163*9880d681SAndroid Build Coastguard Worker        return 0;
164*9880d681SAndroid Build Coastguard Worker      }
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker2. Next, compile the C file into an LLVM bitcode file:
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker   .. code-block:: bat
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker      C:\..> clang -c hello.c -emit-llvm -o hello.bc
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard Worker   This will create the result file ``hello.bc`` which is the LLVM bitcode
173*9880d681SAndroid Build Coastguard Worker   that corresponds the compiled program and the library facilities that
174*9880d681SAndroid Build Coastguard Worker   it required.  You can execute this file directly using ``lli`` tool,
175*9880d681SAndroid Build Coastguard Worker   compile it to native assembly with the ``llc``, optimize or analyze it
176*9880d681SAndroid Build Coastguard Worker   further with the ``opt`` tool, etc.
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker   Alternatively you can directly output an executable with clang with:
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker   .. code-block:: bat
181*9880d681SAndroid Build Coastguard Worker
182*9880d681SAndroid Build Coastguard Worker      C:\..> clang hello.c -o hello.exe
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker   The ``-o hello.exe`` is required because clang currently outputs ``a.out``
185*9880d681SAndroid Build Coastguard Worker   when neither ``-o`` nor ``-c`` are given.
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker3. Run the program using the just-in-time compiler:
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker   .. code-block:: bat
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker      C:\..> lli hello.bc
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker4. Use the ``llvm-dis`` utility to take a look at the LLVM assembly code:
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard Worker   .. code-block:: bat
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker      C:\..> llvm-dis < hello.bc | more
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Worker5. Compile the program to object code using the LLC code generator:
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker   .. code-block:: bat
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard Worker      C:\..> llc -filetype=obj hello.bc
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker6. Link to binary using Microsoft link:
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker   .. code-block:: bat
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker      C:\..> link hello.obj -defaultlib:libcmt
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard Worker7. Execute the native code program:
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard Worker   .. code-block:: bat
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker      C:\..> hello.exe
216*9880d681SAndroid Build Coastguard Worker
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard WorkerCommon Problems
219*9880d681SAndroid Build Coastguard Worker===============
220*9880d681SAndroid Build Coastguard WorkerIf you are having problems building or using LLVM, or if you have any other
221*9880d681SAndroid Build Coastguard Workergeneral questions about LLVM, please consult the :doc:`Frequently Asked Questions
222*9880d681SAndroid Build Coastguard Worker<FAQ>` page.
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard WorkerLinks
226*9880d681SAndroid Build Coastguard Worker=====
227*9880d681SAndroid Build Coastguard WorkerThis document is just an **introduction** to how to use LLVM to do some simple
228*9880d681SAndroid Build Coastguard Workerthings... there are many more interesting and complicated things that you can
229*9880d681SAndroid Build Coastguard Workerdo that aren't documented here (but we'll gladly accept a patch if you want to
230*9880d681SAndroid Build Coastguard Workerwrite something up!).  For more information about LLVM, check out:
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker* `LLVM homepage <http://llvm.org/>`_
233*9880d681SAndroid Build Coastguard Worker* `LLVM doxygen tree <http://llvm.org/doxygen/>`_
234*9880d681SAndroid Build Coastguard Worker
235