annotate llvm/docs/GettingStartedVS.rst @ 164:fdfabb438fbf

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