Customizing the build process
The CMake system uses a project description in the CMakeLists.txt
file. The top-level file is in the llvm
directory, llvm/CMakeLists.txt
. Other directories also have CMakeLists.txt
files, which are recursively included during the generation process.
Based on the information provided in the project description, CMake checks which compilers are installed, detects libraries and symbols, and creates the build system files, for example, build.ninja
or Makefile
(depending on the chosen generator). It is also possible to define reusable modules, for example, a function to detect whether LLVM is installed. These scripts are placed in the special cmake
directory (llvm/cmake
), which is searched automatically during the generation process.
The build process can be customized with the definition of CMake variables. The command-line option –D
is used to set a variable to a value. The variables are used in the CMake scripts. Variables defined by CMake itself are almost always prefixed with CMAKE_
and these variables can be used in all projects. Variables defined by LLVM are prefixed with LLVM_
but they can only be used if the project definition includes the use of LLVM.
Variables defined by CMake
Some variables are initialized with the value of environment variables. Most notable are CC
and CXX
, which define the C and C++ compilers to be used for building. CMake tries to locate a C and a C++ compiler automatically, using the current shell search path. It picks the first compiler found. If you have several compilers installed, for example, gcc and clang or different versions of clang, then this might not be the compiler you want for building LLVM.
Suppose you like to use clang17 as a C compiler and clang++17 as a C++ compiler. Then, you can invoke CMake in a Unix shell in the following way:
$ CC=clang17 CXX=clang++17 cmake –B build –S llvm
This sets the value of the environment variables only for the invocation of cmake
. If necessary, you can specify an absolute path for the compiler executables.
CC
is the default value of the CMAKE_C_COMPILER
CMake variable, and CXX
is the default value of the CMAKE_CXX_COMPILER
CMake variable. Instead of using the environment variables, you can set the CMake variables directly. This is equivalent to the preceding call:
$ cmake –DCMAKE_C_COMPILER=clang17 \ -DCMAKE_CXX_COMPILER=clang++17 –B build –S llvm
Other useful variables defined by CMake are as follows:
Variable name |
Purpose |
|
This is a path prefix that is prepended to every path during installation. The default is |
|
Different types of build require different settings. For example, a debug build needs to specify options to generate debug symbols and usually link against debug versions of system libraries. In contrast, a release build uses optimization flags and links against production versions of libraries. This variable is only used for build systems that can only handle one build type, for example, Ninja or Make. For IDE build systems, all variants are generated and you have to use the mechanism of the IDE to switch between build types. Possible values are as follows:
The default build type is taken from the |
|
These are extra flags used when compiling C and C++ source files. The initial values are taken from the |
|
This specifies additional directories that are searched for CMake modules. The specified directories are searched before the default ones. The value is a semicolon-separated list of directories. |
|
If the Python interpreter is not found or if the wrong one is picked in case you have installed multiple versions, you can set this variable to the path of the Python binary. This variable only has an effect if the Python module of CMake is included (which is the case for LLVM). |
Table 1.1 - Additional useful variables provided by CMake
CMake provides built-in help for variables. The --help-variable var
option prints help for the var
variable. For instance, you can type the following to get help for CMAKE_BUILD_TYPE
:
$ cmake --help-variable CMAKE_BUILD_TYPE
You can also list all variables with the following command:
$ cmake --help-variable-list
This list is very long. You may want to pipe the output to more
or a similar program.
Using LLVM-defined build configuration variables
The build configuration variables defined by LLVM work in the same way as those defined by CMake except that there is no built-in help. The most useful variables are found in the following tables, where they are divided into variables that are useful for first-time users installing LLVM, and also variables for more advanced LLVM users.
Variables useful for first-time users installing LLVM
Variable name |
Purpose |
|
LLVM supports code generation for different CPU architectures. By default, all these targets are built. Use this variable to specify the list of targets to build, separated by semicolons. The current targets are |
|
In addition to the official targets, the LLVM source tree also contains experimental targets. These targets are under development and often do not yet support the full functionality of a backend. The current list of experimental targets is |
|
This is a list of the projects you want to build, separated by semicolons. The source for the projects must be on the same level as the To build clang and bolt together with LLVM, you specify |
Table 1.2 - Useful variables for first-time LLVM users
Variables for advanced users of LLVM
|
If set to |
|
This enables some expensive checks that can really slow down compilation speed or consume large amounts of memory. The default value is |
|
LLVM tools such as |
|
LLVM automatically includes thread support if a threading library is detected (usually the |
|
The LLVM projects do not use C++ exception handling and therefore turn exception support off by default. This setting can be incompatible with other libraries your project is linking with. If needed, you can enable exception support by specifying |
|
LLVM uses a lightweight, self-build system for runtime type information. The generation of C++ RTTI is turned off by default. Like the exception handling support, this may be incompatible with other libraries. To turn generation of C++ RTTI on, you specify |
|
Compiling LLVM should generate no warning messages if possible. The option to print warning messages is therefore turned on by default. To turn it off, you specify |
|
The LLVM source should be C/C++ language standard-conforming; hence, pedantic checking of the source is enabled by default. If possible, compiler-specific extensions are also disabled. To reverse this setting, you specify |
|
If set to |
|
Usually, the tablegen tool is built with the same options as all other parts of LLVM. At the same time, tablegen is used to generate large parts of the code generator. As a result, tablegen is much slower in a debug build, increasing the compile time noticeably. If this option is set to |
|
If the build compiler is gcc or clang, then turning on this option will instruct the compiler to generate the DWARF debug information in a separate file. The reduced size of the object files reduces the link time of debug builds significantly. The default is |
Table 1.3 - Useful variables for advanced LLVM users
Note
LLVM defines many more CMake variables. You can find the complete list in the LLVM documentation about CMake https://releases.llvm.org/17.0.1/docs/CMake.html#llvm-specific-variables. The preceding list contains only the ones you are most likely to need.