xref: /openbsd-src/gnu/llvm/llvm/docs/CMakePrimer.rst (revision 73471bf04ceb096474c7f0fa83b1b65c70a787a1)
109467b48Spatrick============
209467b48SpatrickCMake Primer
309467b48Spatrick============
409467b48Spatrick
509467b48Spatrick.. contents::
609467b48Spatrick   :local:
709467b48Spatrick
809467b48Spatrick.. warning::
909467b48Spatrick   Disclaimer: This documentation is written by LLVM project contributors `not`
1009467b48Spatrick   anyone affiliated with the CMake project. This document may contain
1109467b48Spatrick   inaccurate terminology, phrasing, or technical details. It is provided with
1209467b48Spatrick   the best intentions.
1309467b48Spatrick
1409467b48Spatrick
1509467b48SpatrickIntroduction
1609467b48Spatrick============
1709467b48Spatrick
1809467b48SpatrickThe LLVM project and many of the core projects built on LLVM build using CMake.
1909467b48SpatrickThis document aims to provide a brief overview of CMake for developers modifying
2009467b48SpatrickLLVM projects or building their own projects on top of LLVM.
2109467b48Spatrick
2209467b48SpatrickThe official CMake language references is available in the cmake-language
2309467b48Spatrickmanpage and `cmake-language online documentation
2409467b48Spatrick<https://cmake.org/cmake/help/v3.4/manual/cmake-language.7.html>`_.
2509467b48Spatrick
2609467b48Spatrick10,000 ft View
2709467b48Spatrick==============
2809467b48Spatrick
2909467b48SpatrickCMake is a tool that reads script files in its own language that describe how a
3009467b48Spatricksoftware project builds. As CMake evaluates the scripts it constructs an
3109467b48Spatrickinternal representation of the software project. Once the scripts have been
3209467b48Spatrickfully processed, if there are no errors, CMake will generate build files to
3309467b48Spatrickactually build the project. CMake supports generating build files for a variety
3409467b48Spatrickof command line build tools as well as for popular IDEs.
3509467b48Spatrick
3609467b48SpatrickWhen a user runs CMake it performs a variety of checks similar to how autoconf
3709467b48Spatrickworked historically. During the checks and the evaluation of the build
3809467b48Spatrickdescription scripts CMake caches values into the CMakeCache. This is useful
3909467b48Spatrickbecause it allows the build system to skip long-running checks during
4009467b48Spatrickincremental development. CMake caching also has some drawbacks, but that will be
4109467b48Spatrickdiscussed later.
4209467b48Spatrick
4309467b48SpatrickScripting Overview
4409467b48Spatrick==================
4509467b48Spatrick
4609467b48SpatrickCMake's scripting language has a very simple grammar. Every language construct
4709467b48Spatrickis a command that matches the pattern _name_(_args_). Commands come in three
4809467b48Spatrickprimary types: language-defined (commands implemented in C++ in CMake), defined
4909467b48Spatrickfunctions, and defined macros. The CMake distribution also contains a suite of
5009467b48SpatrickCMake modules that contain definitions for useful functionality.
5109467b48Spatrick
5209467b48SpatrickThe example below is the full CMake build for building a C++ "Hello World"
5309467b48Spatrickprogram. The example uses only CMake language-defined functions.
5409467b48Spatrick
5509467b48Spatrick.. code-block:: cmake
5609467b48Spatrick
57*73471bf0Spatrick   cmake_minimum_required(VERSION 3.15)
5809467b48Spatrick   project(HelloWorld)
5909467b48Spatrick   add_executable(HelloWorld HelloWorld.cpp)
6009467b48Spatrick
6109467b48SpatrickThe CMake language provides control flow constructs in the form of foreach loops
6209467b48Spatrickand if blocks. To make the example above more complicated you could add an if
6309467b48Spatrickblock to define "APPLE" when targeting Apple platforms:
6409467b48Spatrick
6509467b48Spatrick.. code-block:: cmake
6609467b48Spatrick
67*73471bf0Spatrick   cmake_minimum_required(VERSION 3.15)
6809467b48Spatrick   project(HelloWorld)
6909467b48Spatrick   add_executable(HelloWorld HelloWorld.cpp)
7009467b48Spatrick   if(APPLE)
7109467b48Spatrick     target_compile_definitions(HelloWorld PUBLIC APPLE)
7209467b48Spatrick   endif()
7309467b48Spatrick
7409467b48SpatrickVariables, Types, and Scope
7509467b48Spatrick===========================
7609467b48Spatrick
7709467b48SpatrickDereferencing
7809467b48Spatrick-------------
7909467b48Spatrick
8009467b48SpatrickIn CMake variables are "stringly" typed. All variables are represented as
8109467b48Spatrickstrings throughout evaluation. Wrapping a variable in ``${}`` dereferences it
8209467b48Spatrickand results in a literal substitution of the name for the value. CMake refers to
8309467b48Spatrickthis as "variable evaluation" in their documentation. Dereferences are performed
8409467b48Spatrick*before* the command being called receives the arguments. This means
8509467b48Spatrickdereferencing a list results in multiple separate arguments being passed to the
8609467b48Spatrickcommand.
8709467b48Spatrick
8809467b48SpatrickVariable dereferences can be nested and be used to model complex data. For
8909467b48Spatrickexample:
9009467b48Spatrick
9109467b48Spatrick.. code-block:: cmake
9209467b48Spatrick
9309467b48Spatrick   set(var_name var1)
9409467b48Spatrick   set(${var_name} foo) # same as "set(var1 foo)"
9509467b48Spatrick   set(${${var_name}}_var bar) # same as "set(foo_var bar)"
9609467b48Spatrick
9709467b48SpatrickDereferencing an unset variable results in an empty expansion. It is a common
9809467b48Spatrickpattern in CMake to conditionally set variables knowing that it will be used in
9909467b48Spatrickcode paths that the variable isn't set. There are examples of this throughout
10009467b48Spatrickthe LLVM CMake build system.
10109467b48Spatrick
10209467b48SpatrickAn example of variable empty expansion is:
10309467b48Spatrick
10409467b48Spatrick.. code-block:: cmake
10509467b48Spatrick
10609467b48Spatrick   if(APPLE)
10709467b48Spatrick     set(extra_sources Apple.cpp)
10809467b48Spatrick   endif()
10909467b48Spatrick   add_executable(HelloWorld HelloWorld.cpp ${extra_sources})
11009467b48Spatrick
11109467b48SpatrickIn this example the ``extra_sources`` variable is only defined if you're
11209467b48Spatricktargeting an Apple platform. For all other targets the ``extra_sources`` will be
11309467b48Spatrickevaluated as empty before add_executable is given its arguments.
11409467b48Spatrick
11509467b48SpatrickLists
11609467b48Spatrick-----
11709467b48Spatrick
11809467b48SpatrickIn CMake lists are semi-colon delimited strings, and it is strongly advised that
11909467b48Spatrickyou avoid using semi-colons in lists; it doesn't go smoothly. A few examples of
12009467b48Spatrickdefining lists:
12109467b48Spatrick
12209467b48Spatrick.. code-block:: cmake
12309467b48Spatrick
12409467b48Spatrick   # Creates a list with members a, b, c, and d
12509467b48Spatrick   set(my_list a b c d)
12609467b48Spatrick   set(my_list "a;b;c;d")
12709467b48Spatrick
12809467b48Spatrick   # Creates a string "a b c d"
12909467b48Spatrick   set(my_string "a b c d")
13009467b48Spatrick
13109467b48SpatrickLists of Lists
13209467b48Spatrick--------------
13309467b48Spatrick
13409467b48SpatrickOne of the more complicated patterns in CMake is lists of lists. Because a list
13509467b48Spatrickcannot contain an element with a semi-colon to construct a list of lists you
13609467b48Spatrickmake a list of variable names that refer to other lists. For example:
13709467b48Spatrick
13809467b48Spatrick.. code-block:: cmake
13909467b48Spatrick
14009467b48Spatrick   set(list_of_lists a b c)
14109467b48Spatrick   set(a 1 2 3)
14209467b48Spatrick   set(b 4 5 6)
14309467b48Spatrick   set(c 7 8 9)
14409467b48Spatrick
14509467b48SpatrickWith this layout you can iterate through the list of lists printing each value
14609467b48Spatrickwith the following code:
14709467b48Spatrick
14809467b48Spatrick.. code-block:: cmake
14909467b48Spatrick
15009467b48Spatrick   foreach(list_name IN LISTS list_of_lists)
15109467b48Spatrick     foreach(value IN LISTS ${list_name})
15209467b48Spatrick       message(${value})
15309467b48Spatrick     endforeach()
15409467b48Spatrick   endforeach()
15509467b48Spatrick
15609467b48SpatrickYou'll notice that the inner foreach loop's list is doubly dereferenced. This is
15709467b48Spatrickbecause the first dereference turns ``list_name`` into the name of the sub-list
15809467b48Spatrick(a, b, or c in the example), then the second dereference is to get the value of
15909467b48Spatrickthe list.
16009467b48Spatrick
16109467b48SpatrickThis pattern is used throughout CMake, the most common example is the compiler
16209467b48Spatrickflags options, which CMake refers to using the following variable expansions:
16309467b48SpatrickCMAKE_${LANGUAGE}_FLAGS and CMAKE_${LANGUAGE}_FLAGS_${CMAKE_BUILD_TYPE}.
16409467b48Spatrick
16509467b48SpatrickOther Types
16609467b48Spatrick-----------
16709467b48Spatrick
16809467b48SpatrickVariables that are cached or specified on the command line can have types
16909467b48Spatrickassociated with them. The variable's type is used by CMake's UI tool to display
17009467b48Spatrickthe right input field. A variable's type generally doesn't impact evaluation,
17109467b48Spatrickhowever CMake does have special handling for some variables such as PATH.
17209467b48SpatrickYou can read more about the special handling in `CMake's set documentation
17309467b48Spatrick<https://cmake.org/cmake/help/v3.5/command/set.html#set-cache-entry>`_.
17409467b48Spatrick
17509467b48SpatrickScope
17609467b48Spatrick-----
17709467b48Spatrick
17809467b48SpatrickCMake inherently has a directory-based scoping. Setting a variable in a
17909467b48SpatrickCMakeLists file, will set the variable for that file, and all subdirectories.
18009467b48SpatrickVariables set in a CMake module that is included in a CMakeLists file will be
18109467b48Spatrickset in the scope they are included from, and all subdirectories.
18209467b48Spatrick
18309467b48SpatrickWhen a variable that is already set is set again in a subdirectory it overrides
18409467b48Spatrickthe value in that scope and any deeper subdirectories.
18509467b48Spatrick
18609467b48SpatrickThe CMake set command provides two scope-related options. PARENT_SCOPE sets a
18709467b48Spatrickvariable into the parent scope, and not the current scope. The CACHE option sets
18809467b48Spatrickthe variable in the CMakeCache, which results in it being set in all scopes. The
18909467b48SpatrickCACHE option will not set a variable that already exists in the CACHE unless the
19009467b48SpatrickFORCE option is specified.
19109467b48Spatrick
19209467b48SpatrickIn addition to directory-based scope, CMake functions also have their own scope.
19309467b48SpatrickThis means variables set inside functions do not bleed into the parent scope.
19409467b48SpatrickThis is not true of macros, and it is for this reason LLVM prefers functions
19509467b48Spatrickover macros whenever reasonable.
19609467b48Spatrick
19709467b48Spatrick.. note::
19809467b48Spatrick  Unlike C-based languages, CMake's loop and control flow blocks do not have
19909467b48Spatrick  their own scopes.
20009467b48Spatrick
20109467b48SpatrickControl Flow
20209467b48Spatrick============
20309467b48Spatrick
20409467b48SpatrickCMake features the same basic control flow constructs you would expect in any
20509467b48Spatrickscripting language, but there are a few quirks because, as with everything in
20609467b48SpatrickCMake, control flow constructs are commands.
20709467b48Spatrick
20809467b48SpatrickIf, ElseIf, Else
20909467b48Spatrick----------------
21009467b48Spatrick
21109467b48Spatrick.. note::
21209467b48Spatrick  For the full documentation on the CMake if command go
21309467b48Spatrick  `here <https://cmake.org/cmake/help/v3.4/command/if.html>`_. That resource is
21409467b48Spatrick  far more complete.
21509467b48Spatrick
21609467b48SpatrickIn general CMake if blocks work the way you'd expect:
21709467b48Spatrick
21809467b48Spatrick.. code-block:: cmake
21909467b48Spatrick
22009467b48Spatrick  if(<condition>)
22109467b48Spatrick    message("do stuff")
22209467b48Spatrick  elseif(<condition>)
22309467b48Spatrick    message("do other stuff")
22409467b48Spatrick  else()
22509467b48Spatrick    message("do other other stuff")
22609467b48Spatrick  endif()
22709467b48Spatrick
22809467b48SpatrickThe single most important thing to know about CMake's if blocks coming from a C
22909467b48Spatrickbackground is that they do not have their own scope. Variables set inside
23009467b48Spatrickconditional blocks persist after the ``endif()``.
23109467b48Spatrick
23209467b48SpatrickLoops
23309467b48Spatrick-----
23409467b48Spatrick
23509467b48SpatrickThe most common form of the CMake ``foreach`` block is:
23609467b48Spatrick
23709467b48Spatrick.. code-block:: cmake
23809467b48Spatrick
23909467b48Spatrick  foreach(var ...)
24009467b48Spatrick    message("do stuff")
24109467b48Spatrick  endforeach()
24209467b48Spatrick
24309467b48SpatrickThe variable argument portion of the ``foreach`` block can contain dereferenced
24409467b48Spatricklists, values to iterate, or a mix of both:
24509467b48Spatrick
24609467b48Spatrick.. code-block:: cmake
24709467b48Spatrick
24809467b48Spatrick  foreach(var foo bar baz)
24909467b48Spatrick    message(${var})
25009467b48Spatrick  endforeach()
25109467b48Spatrick  # prints:
25209467b48Spatrick  #  foo
25309467b48Spatrick  #  bar
25409467b48Spatrick  #  baz
25509467b48Spatrick
25609467b48Spatrick  set(my_list 1 2 3)
25709467b48Spatrick  foreach(var ${my_list})
25809467b48Spatrick    message(${var})
25909467b48Spatrick  endforeach()
26009467b48Spatrick  # prints:
26109467b48Spatrick  #  1
26209467b48Spatrick  #  2
26309467b48Spatrick  #  3
26409467b48Spatrick
26509467b48Spatrick  foreach(var ${my_list} out_of_bounds)
26609467b48Spatrick    message(${var})
26709467b48Spatrick  endforeach()
26809467b48Spatrick  # prints:
26909467b48Spatrick  #  1
27009467b48Spatrick  #  2
27109467b48Spatrick  #  3
27209467b48Spatrick  #  out_of_bounds
27309467b48Spatrick
27409467b48SpatrickThere is also a more modern CMake foreach syntax. The code below is equivalent
27509467b48Spatrickto the code above:
27609467b48Spatrick
27709467b48Spatrick.. code-block:: cmake
27809467b48Spatrick
27909467b48Spatrick  foreach(var IN ITEMS foo bar baz)
28009467b48Spatrick    message(${var})
28109467b48Spatrick  endforeach()
28209467b48Spatrick  # prints:
28309467b48Spatrick  #  foo
28409467b48Spatrick  #  bar
28509467b48Spatrick  #  baz
28609467b48Spatrick
28709467b48Spatrick  set(my_list 1 2 3)
28809467b48Spatrick  foreach(var IN LISTS my_list)
28909467b48Spatrick    message(${var})
29009467b48Spatrick  endforeach()
29109467b48Spatrick  # prints:
29209467b48Spatrick  #  1
29309467b48Spatrick  #  2
29409467b48Spatrick  #  3
29509467b48Spatrick
29609467b48Spatrick  foreach(var IN LISTS my_list ITEMS out_of_bounds)
29709467b48Spatrick    message(${var})
29809467b48Spatrick  endforeach()
29909467b48Spatrick  # prints:
30009467b48Spatrick  #  1
30109467b48Spatrick  #  2
30209467b48Spatrick  #  3
30309467b48Spatrick  #  out_of_bounds
30409467b48Spatrick
30509467b48SpatrickSimilar to the conditional statements, these generally behave how you would
30609467b48Spatrickexpect, and they do not have their own scope.
30709467b48Spatrick
30809467b48SpatrickCMake also supports ``while`` loops, although they are not widely used in LLVM.
30909467b48Spatrick
31009467b48SpatrickModules, Functions and Macros
31109467b48Spatrick=============================
31209467b48Spatrick
31309467b48SpatrickModules
31409467b48Spatrick-------
31509467b48Spatrick
31609467b48SpatrickModules are CMake's vehicle for enabling code reuse. CMake modules are just
31709467b48SpatrickCMake script files. They can contain code to execute on include as well as
31809467b48Spatrickdefinitions for commands.
31909467b48Spatrick
32009467b48SpatrickIn CMake macros and functions are universally referred to as commands, and they
32109467b48Spatrickare the primary method of defining code that can be called multiple times.
32209467b48Spatrick
32309467b48SpatrickIn LLVM we have several CMake modules that are included as part of our
32409467b48Spatrickdistribution for developers who don't build our project from source. Those
32509467b48Spatrickmodules are the fundamental pieces needed to build LLVM-based projects with
32609467b48SpatrickCMake. We also rely on modules as a way of organizing the build system's
32709467b48Spatrickfunctionality for maintainability and re-use within LLVM projects.
32809467b48Spatrick
32909467b48SpatrickArgument Handling
33009467b48Spatrick-----------------
33109467b48Spatrick
33209467b48SpatrickWhen defining a CMake command handling arguments is very useful. The examples
33309467b48Spatrickin this section will all use the CMake ``function`` block, but this all applies
33409467b48Spatrickto the ``macro`` block as well.
33509467b48Spatrick
336097a140dSpatrickCMake commands can have named arguments that are required at every call site. In
33709467b48Spatrickaddition, all commands will implicitly accept a variable number of extra
33809467b48Spatrickarguments (In C parlance, all commands are varargs functions). When a command is
33909467b48Spatrickinvoked with extra arguments (beyond the named ones) CMake will store the full
34009467b48Spatricklist of arguments (both named and unnamed) in a list named ``ARGV``, and the
34109467b48Spatricksublist of unnamed arguments in ``ARGN``. Below is a trivial example of
34209467b48Spatrickproviding a wrapper function for CMake's built in function ``add_dependencies``.
34309467b48Spatrick
34409467b48Spatrick.. code-block:: cmake
34509467b48Spatrick
34609467b48Spatrick   function(add_deps target)
34709467b48Spatrick     add_dependencies(${target} ${ARGN})
34809467b48Spatrick   endfunction()
34909467b48Spatrick
35009467b48SpatrickThis example defines a new macro named ``add_deps`` which takes a required first
35109467b48Spatrickargument, and just calls another function passing through the first argument and
35209467b48Spatrickall trailing arguments.
35309467b48Spatrick
35409467b48SpatrickCMake provides a module ``CMakeParseArguments`` which provides an implementation
35509467b48Spatrickof advanced argument parsing. We use this all over LLVM, and it is recommended
35609467b48Spatrickfor any function that has complex argument-based behaviors or optional
35709467b48Spatrickarguments. CMake's official documentation for the module is in the
35809467b48Spatrick``cmake-modules`` manpage, and is also available at the
35909467b48Spatrick`cmake-modules online documentation
36009467b48Spatrick<https://cmake.org/cmake/help/v3.4/module/CMakeParseArguments.html>`_.
36109467b48Spatrick
36209467b48Spatrick.. note::
36309467b48Spatrick  As of CMake 3.5 the cmake_parse_arguments command has become a native command
36409467b48Spatrick  and the CMakeParseArguments module is empty and only left around for
36509467b48Spatrick  compatibility.
36609467b48Spatrick
36709467b48SpatrickFunctions Vs Macros
36809467b48Spatrick-------------------
36909467b48Spatrick
37009467b48SpatrickFunctions and Macros look very similar in how they are used, but there is one
37109467b48Spatrickfundamental difference between the two. Functions have their own scope, and
37209467b48Spatrickmacros don't. This means variables set in macros will bleed out into the calling
37309467b48Spatrickscope. That makes macros suitable for defining very small bits of functionality
37409467b48Spatrickonly.
37509467b48Spatrick
37609467b48SpatrickThe other difference between CMake functions and macros is how arguments are
37709467b48Spatrickpassed. Arguments to macros are not set as variables, instead dereferences to
37809467b48Spatrickthe parameters are resolved across the macro before executing it. This can
37909467b48Spatrickresult in some unexpected behavior if using unreferenced variables. For example:
38009467b48Spatrick
38109467b48Spatrick.. code-block:: cmake
38209467b48Spatrick
38309467b48Spatrick   macro(print_list my_list)
38409467b48Spatrick     foreach(var IN LISTS my_list)
38509467b48Spatrick       message("${var}")
38609467b48Spatrick     endforeach()
38709467b48Spatrick   endmacro()
38809467b48Spatrick
38909467b48Spatrick   set(my_list a b c d)
39009467b48Spatrick   set(my_list_of_numbers 1 2 3 4)
39109467b48Spatrick   print_list(my_list_of_numbers)
39209467b48Spatrick   # prints:
39309467b48Spatrick   # a
39409467b48Spatrick   # b
39509467b48Spatrick   # c
39609467b48Spatrick   # d
39709467b48Spatrick
39809467b48SpatrickGenerally speaking this issue is uncommon because it requires using
39909467b48Spatricknon-dereferenced variables with names that overlap in the parent scope, but it
40009467b48Spatrickis important to be aware of because it can lead to subtle bugs.
40109467b48Spatrick
40209467b48SpatrickLLVM Project Wrappers
40309467b48Spatrick=====================
40409467b48Spatrick
40509467b48SpatrickLLVM projects provide lots of wrappers around critical CMake built-in commands.
40609467b48SpatrickWe use these wrappers to provide consistent behaviors across LLVM components
40709467b48Spatrickand to reduce code duplication.
40809467b48Spatrick
40909467b48SpatrickWe generally (but not always) follow the convention that commands prefaced with
41009467b48Spatrick``llvm_`` are intended to be used only as building blocks for other commands.
41109467b48SpatrickWrapper commands that are intended for direct use are generally named following
41209467b48Spatrickwith the project in the middle of the command name (i.e. ``add_llvm_executable``
41309467b48Spatrickis the wrapper for ``add_executable``). The LLVM ``add_*`` wrapper functions are
41409467b48Spatrickall defined in ``AddLLVM.cmake`` which is installed as part of the LLVM
41509467b48Spatrickdistribution. It can be included and used by any LLVM sub-project that requires
41609467b48SpatrickLLVM.
41709467b48Spatrick
41809467b48Spatrick.. note::
41909467b48Spatrick
42009467b48Spatrick   Not all LLVM projects require LLVM for all use cases. For example compiler-rt
42109467b48Spatrick   can be built without LLVM, and the compiler-rt sanitizer libraries are used
42209467b48Spatrick   with GCC.
42309467b48Spatrick
42409467b48SpatrickUseful Built-in Commands
42509467b48Spatrick========================
42609467b48Spatrick
42709467b48SpatrickCMake has a bunch of useful built-in commands. This document isn't going to
42809467b48Spatrickgo into details about them because The CMake project has excellent
42909467b48Spatrickdocumentation. To highlight a few useful functions see:
43009467b48Spatrick
43109467b48Spatrick* `add_custom_command <https://cmake.org/cmake/help/v3.4/command/add_custom_command.html>`_
43209467b48Spatrick* `add_custom_target <https://cmake.org/cmake/help/v3.4/command/add_custom_target.html>`_
43309467b48Spatrick* `file <https://cmake.org/cmake/help/v3.4/command/file.html>`_
43409467b48Spatrick* `list <https://cmake.org/cmake/help/v3.4/command/list.html>`_
43509467b48Spatrick* `math <https://cmake.org/cmake/help/v3.4/command/math.html>`_
43609467b48Spatrick* `string <https://cmake.org/cmake/help/v3.4/command/string.html>`_
43709467b48Spatrick
43809467b48SpatrickThe full documentation for CMake commands is in the ``cmake-commands`` manpage
43909467b48Spatrickand available on `CMake's website <https://cmake.org/cmake/help/v3.4/manual/cmake-commands.7.html>`_
440