GettingStartedVS.rst 8.9 KB

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