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