Projects.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ========================
  2. Creating an LLVM Project
  3. ========================
  4. .. contents::
  5. :local:
  6. Overview
  7. ========
  8. The LLVM build system is designed to facilitate the building of third party
  9. projects that use LLVM header files, libraries, and tools. In order to use
  10. these facilities, a ``Makefile`` from a project must do the following things:
  11. * Set ``make`` variables. There are several variables that a ``Makefile`` needs
  12. to set to use the LLVM build system:
  13. * ``PROJECT_NAME`` - The name by which your project is known.
  14. * ``LLVM_SRC_ROOT`` - The root of the LLVM source tree.
  15. * ``LLVM_OBJ_ROOT`` - The root of the LLVM object tree.
  16. * ``PROJ_SRC_ROOT`` - The root of the project's source tree.
  17. * ``PROJ_OBJ_ROOT`` - The root of the project's object tree.
  18. * ``PROJ_INSTALL_ROOT`` - The root installation directory.
  19. * ``LEVEL`` - The relative path from the current directory to the
  20. project's root ``($PROJ_OBJ_ROOT)``.
  21. * Include ``Makefile.config`` from ``$(LLVM_OBJ_ROOT)``.
  22. * Include ``Makefile.rules`` from ``$(LLVM_SRC_ROOT)``.
  23. There are two ways that you can set all of these variables:
  24. * You can write your own ``Makefiles`` which hard-code these values.
  25. * You can use the pre-made LLVM sample project. This sample project includes
  26. ``Makefiles``, a configure script that can be used to configure the location
  27. of LLVM, and the ability to support multiple object directories from a single
  28. source directory.
  29. If you want to devise your own build system, studying other projects and LLVM
  30. ``Makefiles`` will probably provide enough information on how to write your own
  31. ``Makefiles``.
  32. Source Tree Layout
  33. ==================
  34. In order to use the LLVM build system, you will want to organize your source
  35. code so that it can benefit from the build system's features. Mainly, you want
  36. your source tree layout to look similar to the LLVM source tree layout.
  37. Underneath your top level directory, you should have the following directories:
  38. **lib**
  39. This subdirectory should contain all of your library source code. For each
  40. library that you build, you will have one directory in **lib** that will
  41. contain that library's source code.
  42. Libraries can be object files, archives, or dynamic libraries. The **lib**
  43. directory is just a convenient place for libraries as it places them all in
  44. a directory from which they can be linked later.
  45. **include**
  46. This subdirectory should contain any header files that are global to your
  47. project. By global, we mean that they are used by more than one library or
  48. executable of your project.
  49. By placing your header files in **include**, they will be found
  50. automatically by the LLVM build system. For example, if you have a file
  51. **include/jazz/note.h**, then your source files can include it simply with
  52. **#include "jazz/note.h"**.
  53. **tools**
  54. This subdirectory should contain all of your source code for executables.
  55. For each program that you build, you will have one directory in **tools**
  56. that will contain that program's source code.
  57. **test**
  58. This subdirectory should contain tests that verify that your code works
  59. correctly. Automated tests are especially useful.
  60. Currently, the LLVM build system provides basic support for tests. The LLVM
  61. system provides the following:
  62. * LLVM contains regression tests in ``llvm/test``. These tests are run by the
  63. :doc:`Lit <CommandGuide/lit>` testing tool. This test procedure uses ``RUN``
  64. lines in the actual test case to determine how to run the test. See the
  65. :doc:`TestingGuide` for more details.
  66. * LLVM contains an optional package called ``llvm-test``, which provides
  67. benchmarks and programs that are known to compile with the Clang front
  68. end. You can use these programs to test your code, gather statistical
  69. information, and compare it to the current LLVM performance statistics.
  70. Currently, there is no way to hook your tests directly into the ``llvm/test``
  71. testing harness. You will simply need to find a way to use the source
  72. provided within that directory on your own.
  73. Typically, you will want to build your **lib** directory first followed by your
  74. **tools** directory.
  75. Writing LLVM Style Makefiles
  76. ============================
  77. The LLVM build system provides a convenient way to build libraries and
  78. executables. Most of your project Makefiles will only need to define a few
  79. variables. Below is a list of the variables one can set and what they can
  80. do:
  81. Required Variables
  82. ------------------
  83. ``LEVEL``
  84. This variable is the relative path from this ``Makefile`` to the top
  85. directory of your project's source code. For example, if your source code
  86. is in ``/tmp/src``, then the ``Makefile`` in ``/tmp/src/jump/high``
  87. would set ``LEVEL`` to ``"../.."``.
  88. Variables for Building Subdirectories
  89. -------------------------------------
  90. ``DIRS``
  91. This is a space separated list of subdirectories that should be built. They
  92. will be built, one at a time, in the order specified.
  93. ``PARALLEL_DIRS``
  94. This is a list of directories that can be built in parallel. These will be
  95. built after the directories in DIRS have been built.
  96. ``OPTIONAL_DIRS``
  97. This is a list of directories that can be built if they exist, but will not
  98. cause an error if they do not exist. They are built serially in the order
  99. in which they are listed.
  100. Variables for Building Libraries
  101. --------------------------------
  102. ``LIBRARYNAME``
  103. This variable contains the base name of the library that will be built. For
  104. example, to build a library named ``libsample.a``, ``LIBRARYNAME`` should
  105. be set to ``sample``.
  106. ``BUILD_ARCHIVE``
  107. By default, a library is a ``.o`` file that is linked directly into a
  108. program. To build an archive (also known as a static library), set the
  109. ``BUILD_ARCHIVE`` variable.
  110. ``SHARED_LIBRARY``
  111. If ``SHARED_LIBRARY`` is defined in your Makefile, a shared (or dynamic)
  112. library will be built.
  113. Variables for Building Programs
  114. -------------------------------
  115. ``TOOLNAME``
  116. This variable contains the name of the program that will be built. For
  117. example, to build an executable named ``sample``, ``TOOLNAME`` should be set
  118. to ``sample``.
  119. ``USEDLIBS``
  120. This variable holds a space separated list of libraries that should be
  121. linked into the program. These libraries must be libraries that come from
  122. your **lib** directory. The libraries must be specified without their
  123. ``lib`` prefix. For example, to link ``libsample.a``, you would set
  124. ``USEDLIBS`` to ``sample.a``.
  125. Note that this works only for statically linked libraries.
  126. ``LLVMLIBS``
  127. This variable holds a space separated list of libraries that should be
  128. linked into the program. These libraries must be LLVM libraries. The
  129. libraries must be specified without their ``lib`` prefix. For example, to
  130. link with a driver that performs an IR transformation you might set
  131. ``LLVMLIBS`` to this minimal set of libraries ``LLVMSupport.a LLVMCore.a
  132. LLVMBitReader.a LLVMAsmParser.a LLVMAnalysis.a LLVMTransformUtils.a
  133. LLVMScalarOpts.a LLVMTarget.a``.
  134. Note that this works only for statically linked libraries. LLVM is split
  135. into a large number of static libraries, and the list of libraries you
  136. require may be much longer than the list above. To see a full list of
  137. libraries use: ``llvm-config --libs all``. Using ``LINK_COMPONENTS`` as
  138. described below, obviates the need to set ``LLVMLIBS``.
  139. ``LINK_COMPONENTS``
  140. This variable holds a space separated list of components that the LLVM
  141. ``Makefiles`` pass to the ``llvm-config`` tool to generate a link line for
  142. the program. For example, to link with all LLVM libraries use
  143. ``LINK_COMPONENTS = all``.
  144. ``LIBS``
  145. To link dynamic libraries, add ``-l<library base name>`` to the ``LIBS``
  146. variable. The LLVM build system will look in the same places for dynamic
  147. libraries as it does for static libraries.
  148. For example, to link ``libsample.so``, you would have the following line in
  149. your ``Makefile``:
  150. .. code-block:: makefile
  151. LIBS += -lsample
  152. Note that ``LIBS`` must occur in the Makefile after the inclusion of
  153. ``Makefile.common``.
  154. Miscellaneous Variables
  155. -----------------------
  156. ``CFLAGS`` & ``CPPFLAGS``
  157. This variable can be used to add options to the C and C++ compiler,
  158. respectively. It is typically used to add options that tell the compiler
  159. the location of additional directories to search for header files.
  160. It is highly suggested that you append to ``CFLAGS`` and ``CPPFLAGS`` as
  161. opposed to overwriting them. The master ``Makefiles`` may already have
  162. useful options in them that you may not want to overwrite.
  163. Placement of Object Code
  164. ========================
  165. The final location of built libraries and executables will depend upon whether
  166. you do a ``Debug``, ``Release``, or ``Profile`` build.
  167. Libraries
  168. All libraries (static and dynamic) will be stored in
  169. ``PROJ_OBJ_ROOT/<type>/lib``, where *type* is ``Debug``, ``Release``, or
  170. ``Profile`` for a debug, optimized, or profiled build, respectively.
  171. Executables
  172. All executables will be stored in ``PROJ_OBJ_ROOT/<type>/bin``, where *type*
  173. is ``Debug``, ``Release``, or ``Profile`` for a debug, optimized, or
  174. profiled build, respectively.
  175. Further Help
  176. ============
  177. If you have any questions or need any help creating an LLVM project, the LLVM
  178. team would be more than happy to help. You can always post your questions to
  179. the `LLVM Developers Mailing List
  180. <http://lists.llvm.org/pipermail/llvm-dev/>`_.