AdvancedBuilds.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. =============================
  2. Advanced Build Configurations
  3. =============================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. `CMake <http://www.cmake.org/>`_ is a cross-platform build-generator tool. CMake
  9. does not build the project, it generates the files needed by your build tool
  10. (GNU make, Visual Studio, etc.) for building LLVM.
  11. If **you are a new contributor**, please start with the :doc:`GettingStarted` or
  12. :doc:`CMake` pages. This page is intended for users doing more complex builds.
  13. Many of the examples below are written assuming specific CMake Generators.
  14. Unless otherwise explicitly called out these commands should work with any CMake
  15. generator.
  16. Bootstrap Builds
  17. ================
  18. The Clang CMake build system supports bootstrap (aka multi-stage) builds. At a
  19. high level a multi-stage build is a chain of builds that pass data from one
  20. stage into the next. The most common and simple version of this is a traditional
  21. bootstrap build.
  22. In a simple two-stage bootstrap build, we build clang using the system compiler,
  23. then use that just-built clang to build clang again. In CMake this simplest form
  24. of a bootstrap build can be configured with a single option,
  25. CLANG_ENABLE_BOOTSTRAP.
  26. .. code-block:: console
  27. $ cmake -G Ninja -DCLANG_ENABLE_BOOTSTRAP=On <path to source>
  28. $ ninja stage2
  29. This command itself isn't terribly useful because it assumes default
  30. configurations for each stage. The next series of examples utilize CMake cache
  31. scripts to provide more complex options.
  32. By default, only a few CMake options will be passed between stages.
  33. The list, called _BOOTSTRAP_DEFAULT_PASSTHROUGH, is defined in clang/CMakeLists.txt.
  34. To force the passing of the variables between stages, use the -DCLANG_BOOTSTRAP_PASSTHROUGH
  35. CMake option, each variable separated by a ";". As example:
  36. .. code-block:: console
  37. $ cmake -G Ninja -DCLANG_ENABLE_BOOTSTRAP=On -DCLANG_BOOTSTRAP_PASSTHROUGH="CMAKE_INSTALL_PREFIX;CMAKE_VERBOSE_MAKEFILE" <path to source>
  38. $ ninja stage2
  39. CMake options starting by ``BOOTSTRAP_`` will be passed only to the stage2 build.
  40. This gives the opportunity to use Clang specific build flags.
  41. For example, the following CMake call will enabled '-fno-addrsig' only during
  42. the stage2 build for C and C++.
  43. .. code-block:: console
  44. $ cmake [..] -DBOOTSTRAP_CMAKE_CXX_FLAGS='-fno-addrsig' -DBOOTSTRAP_CMAKE_C_FLAGS='-fno-addrsig' [..]
  45. The clang build system refers to builds as stages. A stage1 build is a standard
  46. build using the compiler installed on the host, and a stage2 build is built
  47. using the stage1 compiler. This nomenclature holds up to more stages too. In
  48. general a stage*n* build is built using the output from stage*n-1*.
  49. Apple Clang Builds (A More Complex Bootstrap)
  50. =============================================
  51. Apple's Clang builds are a slightly more complicated example of the simple
  52. bootstrapping scenario. Apple Clang is built using a 2-stage build.
  53. The stage1 compiler is a host-only compiler with some options set. The stage1
  54. compiler is a balance of optimization vs build time because it is a throwaway.
  55. The stage2 compiler is the fully optimized compiler intended to ship to users.
  56. Setting up these compilers requires a lot of options. To simplify the
  57. configuration the Apple Clang build settings are contained in CMake Cache files.
  58. You can build an Apple Clang compiler using the following commands:
  59. .. code-block:: console
  60. $ cmake -G Ninja -C <path to clang>/cmake/caches/Apple-stage1.cmake <path to source>
  61. $ ninja stage2-distribution
  62. This CMake invocation configures the stage1 host compiler, and sets
  63. CLANG_BOOTSTRAP_CMAKE_ARGS to pass the Apple-stage2.cmake cache script to the
  64. stage2 configuration step.
  65. When you build the stage2-distribution target it builds the minimal stage1
  66. compiler and required tools, then configures and builds the stage2 compiler
  67. based on the settings in Apple-stage2.cmake.
  68. This pattern of using cache scripts to set complex settings, and specifically to
  69. make later stage builds include cache scripts is common in our more advanced
  70. build configurations.
  71. Multi-stage PGO
  72. ===============
  73. Profile-Guided Optimizations (PGO) is a really great way to optimize the code
  74. clang generates. Our multi-stage PGO builds are a workflow for generating PGO
  75. profiles that can be used to optimize clang.
  76. At a high level, the way PGO works is that you build an instrumented compiler,
  77. then you run the instrumented compiler against sample source files. While the
  78. instrumented compiler runs it will output a bunch of files containing
  79. performance counters (.profraw files). After generating all the profraw files
  80. you use llvm-profdata to merge the files into a single profdata file that you
  81. can feed into the LLVM_PROFDATA_FILE option.
  82. Our PGO.cmake cache script automates that whole process. You can use it by
  83. running:
  84. .. code-block:: console
  85. $ cmake -G Ninja -C <path_to_clang>/cmake/caches/PGO.cmake <source dir>
  86. $ ninja stage2-instrumented-generate-profdata
  87. If you let that run for a few hours or so, it will place a profdata file in your
  88. build directory. This takes a really long time because it builds clang twice,
  89. and you *must* have compiler-rt in your build tree.
  90. This process uses any source files under the perf-training directory as training
  91. data as long as the source files are marked up with LIT-style RUN lines.
  92. After it finishes you can use “find . -name clang.profdata” to find it, but it
  93. should be at a path something like:
  94. .. code-block:: console
  95. <build dir>/tools/clang/stage2-instrumented-bins/utils/perf-training/clang.profdata
  96. You can feed that file into the LLVM_PROFDATA_FILE option when you build your
  97. optimized compiler.
  98. The PGO came cache has a slightly different stage naming scheme than other
  99. multi-stage builds. It generates three stages; stage1, stage2-instrumented, and
  100. stage2. Both of the stage2 builds are built using the stage1 compiler.
  101. The PGO came cache generates the following additional targets:
  102. **stage2-instrumented**
  103. Builds a stage1 x86 compiler, runtime, and required tools (llvm-config,
  104. llvm-profdata) then uses that compiler to build an instrumented stage2 compiler.
  105. **stage2-instrumented-generate-profdata**
  106. Depends on "stage2-instrumented" and will use the instrumented compiler to
  107. generate profdata based on the training files in <clang>/utils/perf-training
  108. **stage2**
  109. Depends of "stage2-instrumented-generate-profdata" and will use the stage1
  110. compiler with the stage2 profdata to build a PGO-optimized compiler.
  111. **stage2-check-llvm**
  112. Depends on stage2 and runs check-llvm using the stage2 compiler.
  113. **stage2-check-clang**
  114. Depends on stage2 and runs check-clang using the stage2 compiler.
  115. **stage2-check-all**
  116. Depends on stage2 and runs check-all using the stage2 compiler.
  117. **stage2-test-suite**
  118. Depends on stage2 and runs the test-suite using the stage3 compiler (requires
  119. in-tree test-suite).
  120. 3-Stage Non-Determinism
  121. =======================
  122. In the ancient lore of compilers non-determinism is like the multi-headed hydra.
  123. Whenever its head pops up, terror and chaos ensue.
  124. Historically one of the tests to verify that a compiler was deterministic would
  125. be a three stage build. The idea of a three stage build is you take your sources
  126. and build a compiler (stage1), then use that compiler to rebuild the sources
  127. (stage2), then you use that compiler to rebuild the sources a third time
  128. (stage3) with an identical configuration to the stage2 build. At the end of
  129. this, you have a stage2 and stage3 compiler that should be bit-for-bit
  130. identical.
  131. You can perform one of these 3-stage builds with LLVM & clang using the
  132. following commands:
  133. .. code-block:: console
  134. $ cmake -G Ninja -C <path_to_clang>/cmake/caches/3-stage.cmake <source dir>
  135. $ ninja stage3
  136. After the build you can compare the stage2 & stage3 compilers. We have a bot
  137. setup `here <http://lab.llvm.org:8011/builders/clang-3stage-ubuntu>`_ that runs
  138. this build and compare configuration.