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

...
author anatofuz
date Thu, 19 Mar 2020 17:02:53 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 ===============================
anatofuz
parents:
diff changeset
2 Building a Distribution of LLVM
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 Introduction
anatofuz
parents:
diff changeset
9 ============
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 This document is geared toward people who want to build and package LLVM and any
anatofuz
parents:
diff changeset
12 combination of LLVM sub-project tools for distribution. This document covers
anatofuz
parents:
diff changeset
13 useful features of the LLVM build system as well as best practices and general
anatofuz
parents:
diff changeset
14 information about packaging LLVM.
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 If you are new to CMake you may find the :doc:`CMake` or :doc:`CMakePrimer`
anatofuz
parents:
diff changeset
17 documentation useful. Some of the things covered in this document are the inner
anatofuz
parents:
diff changeset
18 workings of the builds described in the :doc:`AdvancedBuilds` document.
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 General Distribution Guidance
anatofuz
parents:
diff changeset
21 =============================
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 When building a distribution of a compiler it is generally advised to perform a
anatofuz
parents:
diff changeset
24 bootstrap build of the compiler. That means building a "stage 1" compiler with
anatofuz
parents:
diff changeset
25 your host toolchain, then building the "stage 2" compiler using the "stage 1"
anatofuz
parents:
diff changeset
26 compiler. This is done so that the compiler you distribute benefits from all the
anatofuz
parents:
diff changeset
27 bug fixes, performance optimizations and general improvements provided by the
anatofuz
parents:
diff changeset
28 new compiler.
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 In deciding how to build your distribution there are a few trade-offs that you
anatofuz
parents:
diff changeset
31 will need to evaluate. The big two are:
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 #. Compile time of the distribution against performance of the built compiler
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 #. Binary size of the distribution against performance of the built compiler
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 The guidance for maximizing performance of the generated compiler is to use LTO,
anatofuz
parents:
diff changeset
38 PGO, and statically link everything. This will result in an overall larger
anatofuz
parents:
diff changeset
39 distribution, and it will take longer to generate, but it provides the most
anatofuz
parents:
diff changeset
40 opportunity for the compiler to optimize.
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 The guidance for minimizing distribution size is to dynamically link LLVM and
anatofuz
parents:
diff changeset
43 Clang libraries into the tools to reduce code duplication. This will come at a
anatofuz
parents:
diff changeset
44 substantial performance penalty to the generated binary both because it reduces
anatofuz
parents:
diff changeset
45 optimization opportunity, and because dynamic linking requires resolving symbols
anatofuz
parents:
diff changeset
46 at process launch time, which can be very slow for C++ code.
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 .. _shared_libs:
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 .. warning::
anatofuz
parents:
diff changeset
51 One very important note: Distributions should never be built using the
anatofuz
parents:
diff changeset
52 *BUILD_SHARED_LIBS* CMake option. That option exists for optimizing developer
anatofuz
parents:
diff changeset
53 workflow only. Due to design and implementation decisions, LLVM relies on
anatofuz
parents:
diff changeset
54 global data which can end up being duplicated across shared libraries
anatofuz
parents:
diff changeset
55 resulting in bugs. As such this is not a safe way to distribute LLVM or
anatofuz
parents:
diff changeset
56 LLVM-based tools.
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 The simplest example of building a distribution with reasonable performance is
anatofuz
parents:
diff changeset
59 captured in the DistributionExample CMake cache file located at
anatofuz
parents:
diff changeset
60 clang/cmake/caches/DistributionExample.cmake. The following command will perform
anatofuz
parents:
diff changeset
61 and install the distribution build:
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63 .. code-block:: console
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 $ cmake -G Ninja -C <path to clang>/cmake/caches/DistributionExample.cmake <path to LLVM source>
anatofuz
parents:
diff changeset
66 $ ninja stage2-distribution
anatofuz
parents:
diff changeset
67 $ ninja stage2-install-distribution
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 Difference between ``install`` and ``install-distribution``
anatofuz
parents:
diff changeset
70 -----------------------------------------------------------
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 One subtle but important thing to note is the difference between the ``install``
anatofuz
parents:
diff changeset
73 and ``install-distribution`` targets. The ``install`` target is expected to
anatofuz
parents:
diff changeset
74 install every part of LLVM that your build is configured to generate except the
anatofuz
parents:
diff changeset
75 LLVM testing tools. Alternatively the ``install-distribution`` target, which is
anatofuz
parents:
diff changeset
76 recommended for building distributions, only installs specific parts of LLVM as
anatofuz
parents:
diff changeset
77 specified at configuration time by *LLVM_DISTRIBUTION_COMPONENTS*.
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 Additionally by default the ``install`` target will install the LLVM testing
anatofuz
parents:
diff changeset
80 tools as the public tools. This can be changed well by setting
anatofuz
parents:
diff changeset
81 *LLVM_INSTALL_TOOLCHAIN_ONLY* to ``On``. The LLVM tools are intended for
anatofuz
parents:
diff changeset
82 development and testing of LLVM, and should only be included in distributions
anatofuz
parents:
diff changeset
83 that support LLVM development.
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 When building with *LLVM_DISTRIBUTION_COMPONENTS* the build system also
anatofuz
parents:
diff changeset
86 generates a ``distribution`` target which builds all the components specified in
anatofuz
parents:
diff changeset
87 the list. This is a convenience build target to allow building just the
anatofuz
parents:
diff changeset
88 distributed pieces without needing to build all configured targets.
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 Special Notes for Library-only Distributions
anatofuz
parents:
diff changeset
91 --------------------------------------------
anatofuz
parents:
diff changeset
92
anatofuz
parents:
diff changeset
93 One of the most powerful features of LLVM is its library-first design mentality
anatofuz
parents:
diff changeset
94 and the way you can compose a wide variety of tools using different portions of
anatofuz
parents:
diff changeset
95 LLVM. Even in this situation using *BUILD_SHARED_LIBS* is not supported. If you
anatofuz
parents:
diff changeset
96 want to distribute LLVM as a shared library for use in a tool, the recommended
anatofuz
parents:
diff changeset
97 method is using *LLVM_BUILD_LLVM_DYLIB*, and you can use *LLVM_DYLIB_COMPONENTS*
anatofuz
parents:
diff changeset
98 to configure which LLVM components are part of libLLVM.
anatofuz
parents:
diff changeset
99 Note: *LLVM_BUILD_LLVM_DYLIB* is not available on Windows.
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 Options for Optimizing LLVM
anatofuz
parents:
diff changeset
102 ===========================
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 There are four main build optimizations that our CMake build system supports.
anatofuz
parents:
diff changeset
105 When performing a bootstrap build it is not beneficial to do anything other than
anatofuz
parents:
diff changeset
106 setting *CMAKE_BUILD_TYPE* to ``Release`` for the stage-1 compiler. This is
anatofuz
parents:
diff changeset
107 because the more intensive optimizations are expensive to perform and the
anatofuz
parents:
diff changeset
108 stage-1 compiler is thrown away. All of the further options described should be
anatofuz
parents:
diff changeset
109 set on the stage-2 compiler either using a CMake cache file, or by prefixing the
anatofuz
parents:
diff changeset
110 option with *BOOTSTRAP_*.
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 The first and simplest to use is the compiler optimization level by setting the
anatofuz
parents:
diff changeset
113 *CMAKE_BUILD_TYPE* option. The main values of interest are ``Release`` or
anatofuz
parents:
diff changeset
114 ``RelWithDebInfo``. By default the ``Release`` option uses the ``-O3``
anatofuz
parents:
diff changeset
115 optimization level, and ``RelWithDebInfo`` uses ``-O2``. If you want to generate
anatofuz
parents:
diff changeset
116 debug information and use ``-O3`` you can override the
anatofuz
parents:
diff changeset
117 *CMAKE_<LANG>_FLAGS_RELWITHDEBINFO* option for C and CXX.
anatofuz
parents:
diff changeset
118 DistributionExample.cmake does this.
anatofuz
parents:
diff changeset
119
anatofuz
parents:
diff changeset
120 Another easy to use option is Link-Time-Optimization. You can set the
anatofuz
parents:
diff changeset
121 *LLVM_ENABLE_LTO* option on your stage-2 build to ``Thin`` or ``Full`` to enable
anatofuz
parents:
diff changeset
122 building LLVM with LTO. These options will significantly increase link time of
anatofuz
parents:
diff changeset
123 the binaries in the distribution, but it will create much faster binaries. This
anatofuz
parents:
diff changeset
124 option should not be used if your distribution includes static archives, as the
anatofuz
parents:
diff changeset
125 objects inside the archive will be LLVM bitcode, which is not portable.
anatofuz
parents:
diff changeset
126
anatofuz
parents:
diff changeset
127 The :doc:`AdvancedBuilds` documentation describes the built-in tooling for
anatofuz
parents:
diff changeset
128 generating LLVM profiling information to drive Profile-Guided-Optimization. The
anatofuz
parents:
diff changeset
129 in-tree profiling tests are very limited, and generating the profile takes a
anatofuz
parents:
diff changeset
130 significant amount of time, but it can result in a significant improvement in
anatofuz
parents:
diff changeset
131 the performance of the generated binaries.
anatofuz
parents:
diff changeset
132
anatofuz
parents:
diff changeset
133 In addition to PGO profiling we also have limited support in-tree for generating
anatofuz
parents:
diff changeset
134 linker order files. These files provide the linker with a suggested ordering for
anatofuz
parents:
diff changeset
135 functions in the final binary layout. This can measurably speed up clang by
anatofuz
parents:
diff changeset
136 physically grouping functions that are called temporally close to each other.
anatofuz
parents:
diff changeset
137 The current tooling is only available on Darwin systems with ``dtrace(1)``. It
anatofuz
parents:
diff changeset
138 is worth noting that dtrace is non-deterministic, and so the order file
anatofuz
parents:
diff changeset
139 generation using dtrace is also non-deterministic.
anatofuz
parents:
diff changeset
140
anatofuz
parents:
diff changeset
141 Options for Reducing Size
anatofuz
parents:
diff changeset
142 =========================
anatofuz
parents:
diff changeset
143
anatofuz
parents:
diff changeset
144 .. warning::
anatofuz
parents:
diff changeset
145 Any steps taken to reduce the binary size will come at a cost of runtime
anatofuz
parents:
diff changeset
146 performance in the generated binaries.
anatofuz
parents:
diff changeset
147
anatofuz
parents:
diff changeset
148 The simplest and least significant way to reduce binary size is to set the
anatofuz
parents:
diff changeset
149 *CMAKE_BUILD_TYPE* variable to ``MinSizeRel``, which will set the compiler
anatofuz
parents:
diff changeset
150 optimization level to ``-Os`` which optimizes for binary size. This will have
anatofuz
parents:
diff changeset
151 both the least benefit to size and the least impact on performance.
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 The most impactful way to reduce binary size is to dynamically link LLVM into
anatofuz
parents:
diff changeset
154 all the tools. This reduces code size by decreasing duplication of common code
anatofuz
parents:
diff changeset
155 between the LLVM-based tools. This can be done by setting the following two
anatofuz
parents:
diff changeset
156 CMake options to ``On``: *LLVM_BUILD_LLVM_DYLIB* and *LLVM_LINK_LLVM_DYLIB*.
anatofuz
parents:
diff changeset
157
anatofuz
parents:
diff changeset
158 .. warning::
anatofuz
parents:
diff changeset
159 Distributions should never be built using the *BUILD_SHARED_LIBS* CMake
anatofuz
parents:
diff changeset
160 option. (:ref:`See the warning above for more explanation <shared_libs>`.).
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 Relevant CMake Options
anatofuz
parents:
diff changeset
163 ======================
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 This section provides documentation of the CMake options that are intended to
anatofuz
parents:
diff changeset
166 help construct distributions. This is not an exhaustive list, and many
anatofuz
parents:
diff changeset
167 additional options are documented in the :doc:`CMake` page. Some key options
anatofuz
parents:
diff changeset
168 that are already documented include: *LLVM_TARGETS_TO_BUILD*,
anatofuz
parents:
diff changeset
169 *LLVM_ENABLE_PROJECTS*, *LLVM_BUILD_LLVM_DYLIB*, and *LLVM_LINK_LLVM_DYLIB*.
anatofuz
parents:
diff changeset
170
anatofuz
parents:
diff changeset
171 **LLVM_ENABLE_RUNTIMES**:STRING
anatofuz
parents:
diff changeset
172 When building a distribution that includes LLVM runtime projects (i.e. libcxx,
anatofuz
parents:
diff changeset
173 compiler-rt, libcxxabi, libunwind...), it is important to build those projects
anatofuz
parents:
diff changeset
174 with the just-built compiler.
anatofuz
parents:
diff changeset
175
anatofuz
parents:
diff changeset
176 **LLVM_DISTRIBUTION_COMPONENTS**:STRING
anatofuz
parents:
diff changeset
177 This variable can be set to a semi-colon separated list of LLVM build system
anatofuz
parents:
diff changeset
178 components to install. All LLVM-based tools are components, as well as most
anatofuz
parents:
diff changeset
179 of the libraries and runtimes. Component names match the names of the build
anatofuz
parents:
diff changeset
180 system targets.
anatofuz
parents:
diff changeset
181
anatofuz
parents:
diff changeset
182 **LLVM_RUNTIME_DISTRIBUTION_COMPONENTS**:STRING
anatofuz
parents:
diff changeset
183 This variable can be set to a semi-colon separated list of runtime library
anatofuz
parents:
diff changeset
184 components. This is used in conjunction with *LLVM_ENABLE_RUNTIMES* to specify
anatofuz
parents:
diff changeset
185 components of runtime libraries that you want to include in your distribution.
anatofuz
parents:
diff changeset
186 Just like with *LLVM_DISTRIBUTION_COMPONENTS*, component names match the names
anatofuz
parents:
diff changeset
187 of the build system targets.
anatofuz
parents:
diff changeset
188
anatofuz
parents:
diff changeset
189 **LLVM_DYLIB_COMPONENTS**:STRING
anatofuz
parents:
diff changeset
190 This variable can be set to a semi-colon separated name of LLVM library
anatofuz
parents:
diff changeset
191 components. LLVM library components are either library names with the LLVM
anatofuz
parents:
diff changeset
192 prefix removed (i.e. Support, Demangle...), LLVM target names, or special
anatofuz
parents:
diff changeset
193 purpose component names. The special purpose component names are:
anatofuz
parents:
diff changeset
194
anatofuz
parents:
diff changeset
195 #. ``all`` - All LLVM available component libraries
anatofuz
parents:
diff changeset
196 #. ``Native`` - The LLVM target for the Native system
anatofuz
parents:
diff changeset
197 #. ``AllTargetsAsmParsers`` - All the included target ASM parsers libraries
anatofuz
parents:
diff changeset
198 #. ``AllTargetsDescs`` - All the included target descriptions libraries
anatofuz
parents:
diff changeset
199 #. ``AllTargetsDisassemblers`` - All the included target dissassemblers libraries
anatofuz
parents:
diff changeset
200 #. ``AllTargetsInfos`` - All the included target info libraries
anatofuz
parents:
diff changeset
201
anatofuz
parents:
diff changeset
202 **LLVM_INSTALL_TOOLCHAIN_ONLY**:BOOL
anatofuz
parents:
diff changeset
203 This option defaults to ``Off``: when set to ``On`` it removes many of the
anatofuz
parents:
diff changeset
204 LLVM development and testing tools as well as component libraries from the
anatofuz
parents:
diff changeset
205 default ``install`` target. Including the development tools is not recommended
anatofuz
parents:
diff changeset
206 for distributions as many of the LLVM tools are only intended for development
anatofuz
parents:
diff changeset
207 and testing use.