17330f729Sjoerg============ 27330f729SjoergCMake Primer 37330f729Sjoerg============ 47330f729Sjoerg 57330f729Sjoerg.. contents:: 67330f729Sjoerg :local: 77330f729Sjoerg 87330f729Sjoerg.. warning:: 97330f729Sjoerg Disclaimer: This documentation is written by LLVM project contributors `not` 107330f729Sjoerg anyone affiliated with the CMake project. This document may contain 117330f729Sjoerg inaccurate terminology, phrasing, or technical details. It is provided with 127330f729Sjoerg the best intentions. 137330f729Sjoerg 147330f729Sjoerg 157330f729SjoergIntroduction 167330f729Sjoerg============ 177330f729Sjoerg 187330f729SjoergThe LLVM project and many of the core projects built on LLVM build using CMake. 197330f729SjoergThis document aims to provide a brief overview of CMake for developers modifying 207330f729SjoergLLVM projects or building their own projects on top of LLVM. 217330f729Sjoerg 227330f729SjoergThe official CMake language references is available in the cmake-language 237330f729Sjoergmanpage and `cmake-language online documentation 247330f729Sjoerg<https://cmake.org/cmake/help/v3.4/manual/cmake-language.7.html>`_. 257330f729Sjoerg 267330f729Sjoerg10,000 ft View 277330f729Sjoerg============== 287330f729Sjoerg 297330f729SjoergCMake is a tool that reads script files in its own language that describe how a 307330f729Sjoergsoftware project builds. As CMake evaluates the scripts it constructs an 317330f729Sjoerginternal representation of the software project. Once the scripts have been 327330f729Sjoergfully processed, if there are no errors, CMake will generate build files to 337330f729Sjoergactually build the project. CMake supports generating build files for a variety 347330f729Sjoergof command line build tools as well as for popular IDEs. 357330f729Sjoerg 367330f729SjoergWhen a user runs CMake it performs a variety of checks similar to how autoconf 377330f729Sjoergworked historically. During the checks and the evaluation of the build 387330f729Sjoergdescription scripts CMake caches values into the CMakeCache. This is useful 397330f729Sjoergbecause it allows the build system to skip long-running checks during 407330f729Sjoergincremental development. CMake caching also has some drawbacks, but that will be 417330f729Sjoergdiscussed later. 427330f729Sjoerg 437330f729SjoergScripting Overview 447330f729Sjoerg================== 457330f729Sjoerg 467330f729SjoergCMake's scripting language has a very simple grammar. Every language construct 477330f729Sjoergis a command that matches the pattern _name_(_args_). Commands come in three 487330f729Sjoergprimary types: language-defined (commands implemented in C++ in CMake), defined 497330f729Sjoergfunctions, and defined macros. The CMake distribution also contains a suite of 507330f729SjoergCMake modules that contain definitions for useful functionality. 517330f729Sjoerg 527330f729SjoergThe example below is the full CMake build for building a C++ "Hello World" 537330f729Sjoergprogram. The example uses only CMake language-defined functions. 547330f729Sjoerg 557330f729Sjoerg.. code-block:: cmake 567330f729Sjoerg 57*82d56013Sjoerg cmake_minimum_required(VERSION 3.15) 587330f729Sjoerg project(HelloWorld) 597330f729Sjoerg add_executable(HelloWorld HelloWorld.cpp) 607330f729Sjoerg 617330f729SjoergThe CMake language provides control flow constructs in the form of foreach loops 627330f729Sjoergand if blocks. To make the example above more complicated you could add an if 637330f729Sjoergblock to define "APPLE" when targeting Apple platforms: 647330f729Sjoerg 657330f729Sjoerg.. code-block:: cmake 667330f729Sjoerg 67*82d56013Sjoerg cmake_minimum_required(VERSION 3.15) 687330f729Sjoerg project(HelloWorld) 697330f729Sjoerg add_executable(HelloWorld HelloWorld.cpp) 707330f729Sjoerg if(APPLE) 717330f729Sjoerg target_compile_definitions(HelloWorld PUBLIC APPLE) 727330f729Sjoerg endif() 737330f729Sjoerg 747330f729SjoergVariables, Types, and Scope 757330f729Sjoerg=========================== 767330f729Sjoerg 777330f729SjoergDereferencing 787330f729Sjoerg------------- 797330f729Sjoerg 807330f729SjoergIn CMake variables are "stringly" typed. All variables are represented as 817330f729Sjoergstrings throughout evaluation. Wrapping a variable in ``${}`` dereferences it 827330f729Sjoergand results in a literal substitution of the name for the value. CMake refers to 837330f729Sjoergthis as "variable evaluation" in their documentation. Dereferences are performed 847330f729Sjoerg*before* the command being called receives the arguments. This means 857330f729Sjoergdereferencing a list results in multiple separate arguments being passed to the 867330f729Sjoergcommand. 877330f729Sjoerg 887330f729SjoergVariable dereferences can be nested and be used to model complex data. For 897330f729Sjoergexample: 907330f729Sjoerg 917330f729Sjoerg.. code-block:: cmake 927330f729Sjoerg 937330f729Sjoerg set(var_name var1) 947330f729Sjoerg set(${var_name} foo) # same as "set(var1 foo)" 957330f729Sjoerg set(${${var_name}}_var bar) # same as "set(foo_var bar)" 967330f729Sjoerg 977330f729SjoergDereferencing an unset variable results in an empty expansion. It is a common 987330f729Sjoergpattern in CMake to conditionally set variables knowing that it will be used in 997330f729Sjoergcode paths that the variable isn't set. There are examples of this throughout 1007330f729Sjoergthe LLVM CMake build system. 1017330f729Sjoerg 1027330f729SjoergAn example of variable empty expansion is: 1037330f729Sjoerg 1047330f729Sjoerg.. code-block:: cmake 1057330f729Sjoerg 1067330f729Sjoerg if(APPLE) 1077330f729Sjoerg set(extra_sources Apple.cpp) 1087330f729Sjoerg endif() 1097330f729Sjoerg add_executable(HelloWorld HelloWorld.cpp ${extra_sources}) 1107330f729Sjoerg 1117330f729SjoergIn this example the ``extra_sources`` variable is only defined if you're 1127330f729Sjoergtargeting an Apple platform. For all other targets the ``extra_sources`` will be 1137330f729Sjoergevaluated as empty before add_executable is given its arguments. 1147330f729Sjoerg 1157330f729SjoergLists 1167330f729Sjoerg----- 1177330f729Sjoerg 1187330f729SjoergIn CMake lists are semi-colon delimited strings, and it is strongly advised that 1197330f729Sjoergyou avoid using semi-colons in lists; it doesn't go smoothly. A few examples of 1207330f729Sjoergdefining lists: 1217330f729Sjoerg 1227330f729Sjoerg.. code-block:: cmake 1237330f729Sjoerg 1247330f729Sjoerg # Creates a list with members a, b, c, and d 1257330f729Sjoerg set(my_list a b c d) 1267330f729Sjoerg set(my_list "a;b;c;d") 1277330f729Sjoerg 1287330f729Sjoerg # Creates a string "a b c d" 1297330f729Sjoerg set(my_string "a b c d") 1307330f729Sjoerg 1317330f729SjoergLists of Lists 1327330f729Sjoerg-------------- 1337330f729Sjoerg 1347330f729SjoergOne of the more complicated patterns in CMake is lists of lists. Because a list 1357330f729Sjoergcannot contain an element with a semi-colon to construct a list of lists you 1367330f729Sjoergmake a list of variable names that refer to other lists. For example: 1377330f729Sjoerg 1387330f729Sjoerg.. code-block:: cmake 1397330f729Sjoerg 1407330f729Sjoerg set(list_of_lists a b c) 1417330f729Sjoerg set(a 1 2 3) 1427330f729Sjoerg set(b 4 5 6) 1437330f729Sjoerg set(c 7 8 9) 1447330f729Sjoerg 1457330f729SjoergWith this layout you can iterate through the list of lists printing each value 1467330f729Sjoergwith the following code: 1477330f729Sjoerg 1487330f729Sjoerg.. code-block:: cmake 1497330f729Sjoerg 1507330f729Sjoerg foreach(list_name IN LISTS list_of_lists) 1517330f729Sjoerg foreach(value IN LISTS ${list_name}) 1527330f729Sjoerg message(${value}) 1537330f729Sjoerg endforeach() 1547330f729Sjoerg endforeach() 1557330f729Sjoerg 1567330f729SjoergYou'll notice that the inner foreach loop's list is doubly dereferenced. This is 1577330f729Sjoergbecause the first dereference turns ``list_name`` into the name of the sub-list 1587330f729Sjoerg(a, b, or c in the example), then the second dereference is to get the value of 1597330f729Sjoergthe list. 1607330f729Sjoerg 1617330f729SjoergThis pattern is used throughout CMake, the most common example is the compiler 1627330f729Sjoergflags options, which CMake refers to using the following variable expansions: 1637330f729SjoergCMAKE_${LANGUAGE}_FLAGS and CMAKE_${LANGUAGE}_FLAGS_${CMAKE_BUILD_TYPE}. 1647330f729Sjoerg 1657330f729SjoergOther Types 1667330f729Sjoerg----------- 1677330f729Sjoerg 1687330f729SjoergVariables that are cached or specified on the command line can have types 1697330f729Sjoergassociated with them. The variable's type is used by CMake's UI tool to display 1707330f729Sjoergthe right input field. A variable's type generally doesn't impact evaluation, 1717330f729Sjoerghowever CMake does have special handling for some variables such as PATH. 1727330f729SjoergYou can read more about the special handling in `CMake's set documentation 1737330f729Sjoerg<https://cmake.org/cmake/help/v3.5/command/set.html#set-cache-entry>`_. 1747330f729Sjoerg 1757330f729SjoergScope 1767330f729Sjoerg----- 1777330f729Sjoerg 1787330f729SjoergCMake inherently has a directory-based scoping. Setting a variable in a 1797330f729SjoergCMakeLists file, will set the variable for that file, and all subdirectories. 1807330f729SjoergVariables set in a CMake module that is included in a CMakeLists file will be 1817330f729Sjoergset in the scope they are included from, and all subdirectories. 1827330f729Sjoerg 1837330f729SjoergWhen a variable that is already set is set again in a subdirectory it overrides 1847330f729Sjoergthe value in that scope and any deeper subdirectories. 1857330f729Sjoerg 1867330f729SjoergThe CMake set command provides two scope-related options. PARENT_SCOPE sets a 1877330f729Sjoergvariable into the parent scope, and not the current scope. The CACHE option sets 1887330f729Sjoergthe variable in the CMakeCache, which results in it being set in all scopes. The 1897330f729SjoergCACHE option will not set a variable that already exists in the CACHE unless the 1907330f729SjoergFORCE option is specified. 1917330f729Sjoerg 1927330f729SjoergIn addition to directory-based scope, CMake functions also have their own scope. 1937330f729SjoergThis means variables set inside functions do not bleed into the parent scope. 1947330f729SjoergThis is not true of macros, and it is for this reason LLVM prefers functions 1957330f729Sjoergover macros whenever reasonable. 1967330f729Sjoerg 1977330f729Sjoerg.. note:: 1987330f729Sjoerg Unlike C-based languages, CMake's loop and control flow blocks do not have 1997330f729Sjoerg their own scopes. 2007330f729Sjoerg 2017330f729SjoergControl Flow 2027330f729Sjoerg============ 2037330f729Sjoerg 2047330f729SjoergCMake features the same basic control flow constructs you would expect in any 2057330f729Sjoergscripting language, but there are a few quirks because, as with everything in 2067330f729SjoergCMake, control flow constructs are commands. 2077330f729Sjoerg 2087330f729SjoergIf, ElseIf, Else 2097330f729Sjoerg---------------- 2107330f729Sjoerg 2117330f729Sjoerg.. note:: 2127330f729Sjoerg For the full documentation on the CMake if command go 2137330f729Sjoerg `here <https://cmake.org/cmake/help/v3.4/command/if.html>`_. That resource is 2147330f729Sjoerg far more complete. 2157330f729Sjoerg 2167330f729SjoergIn general CMake if blocks work the way you'd expect: 2177330f729Sjoerg 2187330f729Sjoerg.. code-block:: cmake 2197330f729Sjoerg 2207330f729Sjoerg if(<condition>) 2217330f729Sjoerg message("do stuff") 2227330f729Sjoerg elseif(<condition>) 2237330f729Sjoerg message("do other stuff") 2247330f729Sjoerg else() 2257330f729Sjoerg message("do other other stuff") 2267330f729Sjoerg endif() 2277330f729Sjoerg 2287330f729SjoergThe single most important thing to know about CMake's if blocks coming from a C 2297330f729Sjoergbackground is that they do not have their own scope. Variables set inside 2307330f729Sjoergconditional blocks persist after the ``endif()``. 2317330f729Sjoerg 2327330f729SjoergLoops 2337330f729Sjoerg----- 2347330f729Sjoerg 2357330f729SjoergThe most common form of the CMake ``foreach`` block is: 2367330f729Sjoerg 2377330f729Sjoerg.. code-block:: cmake 2387330f729Sjoerg 2397330f729Sjoerg foreach(var ...) 2407330f729Sjoerg message("do stuff") 2417330f729Sjoerg endforeach() 2427330f729Sjoerg 2437330f729SjoergThe variable argument portion of the ``foreach`` block can contain dereferenced 2447330f729Sjoerglists, values to iterate, or a mix of both: 2457330f729Sjoerg 2467330f729Sjoerg.. code-block:: cmake 2477330f729Sjoerg 2487330f729Sjoerg foreach(var foo bar baz) 2497330f729Sjoerg message(${var}) 2507330f729Sjoerg endforeach() 2517330f729Sjoerg # prints: 2527330f729Sjoerg # foo 2537330f729Sjoerg # bar 2547330f729Sjoerg # baz 2557330f729Sjoerg 2567330f729Sjoerg set(my_list 1 2 3) 2577330f729Sjoerg foreach(var ${my_list}) 2587330f729Sjoerg message(${var}) 2597330f729Sjoerg endforeach() 2607330f729Sjoerg # prints: 2617330f729Sjoerg # 1 2627330f729Sjoerg # 2 2637330f729Sjoerg # 3 2647330f729Sjoerg 2657330f729Sjoerg foreach(var ${my_list} out_of_bounds) 2667330f729Sjoerg message(${var}) 2677330f729Sjoerg endforeach() 2687330f729Sjoerg # prints: 2697330f729Sjoerg # 1 2707330f729Sjoerg # 2 2717330f729Sjoerg # 3 2727330f729Sjoerg # out_of_bounds 2737330f729Sjoerg 2747330f729SjoergThere is also a more modern CMake foreach syntax. The code below is equivalent 2757330f729Sjoergto the code above: 2767330f729Sjoerg 2777330f729Sjoerg.. code-block:: cmake 2787330f729Sjoerg 2797330f729Sjoerg foreach(var IN ITEMS foo bar baz) 2807330f729Sjoerg message(${var}) 2817330f729Sjoerg endforeach() 2827330f729Sjoerg # prints: 2837330f729Sjoerg # foo 2847330f729Sjoerg # bar 2857330f729Sjoerg # baz 2867330f729Sjoerg 2877330f729Sjoerg set(my_list 1 2 3) 2887330f729Sjoerg foreach(var IN LISTS my_list) 2897330f729Sjoerg message(${var}) 2907330f729Sjoerg endforeach() 2917330f729Sjoerg # prints: 2927330f729Sjoerg # 1 2937330f729Sjoerg # 2 2947330f729Sjoerg # 3 2957330f729Sjoerg 2967330f729Sjoerg foreach(var IN LISTS my_list ITEMS out_of_bounds) 2977330f729Sjoerg message(${var}) 2987330f729Sjoerg endforeach() 2997330f729Sjoerg # prints: 3007330f729Sjoerg # 1 3017330f729Sjoerg # 2 3027330f729Sjoerg # 3 3037330f729Sjoerg # out_of_bounds 3047330f729Sjoerg 3057330f729SjoergSimilar to the conditional statements, these generally behave how you would 3067330f729Sjoergexpect, and they do not have their own scope. 3077330f729Sjoerg 3087330f729SjoergCMake also supports ``while`` loops, although they are not widely used in LLVM. 3097330f729Sjoerg 3107330f729SjoergModules, Functions and Macros 3117330f729Sjoerg============================= 3127330f729Sjoerg 3137330f729SjoergModules 3147330f729Sjoerg------- 3157330f729Sjoerg 3167330f729SjoergModules are CMake's vehicle for enabling code reuse. CMake modules are just 3177330f729SjoergCMake script files. They can contain code to execute on include as well as 3187330f729Sjoergdefinitions for commands. 3197330f729Sjoerg 3207330f729SjoergIn CMake macros and functions are universally referred to as commands, and they 3217330f729Sjoergare the primary method of defining code that can be called multiple times. 3227330f729Sjoerg 3237330f729SjoergIn LLVM we have several CMake modules that are included as part of our 3247330f729Sjoergdistribution for developers who don't build our project from source. Those 3257330f729Sjoergmodules are the fundamental pieces needed to build LLVM-based projects with 3267330f729SjoergCMake. We also rely on modules as a way of organizing the build system's 3277330f729Sjoergfunctionality for maintainability and re-use within LLVM projects. 3287330f729Sjoerg 3297330f729SjoergArgument Handling 3307330f729Sjoerg----------------- 3317330f729Sjoerg 3327330f729SjoergWhen defining a CMake command handling arguments is very useful. The examples 3337330f729Sjoergin this section will all use the CMake ``function`` block, but this all applies 3347330f729Sjoergto the ``macro`` block as well. 3357330f729Sjoerg 336*82d56013SjoergCMake commands can have named arguments that are required at every call site. In 3377330f729Sjoergaddition, all commands will implicitly accept a variable number of extra 3387330f729Sjoergarguments (In C parlance, all commands are varargs functions). When a command is 3397330f729Sjoerginvoked with extra arguments (beyond the named ones) CMake will store the full 3407330f729Sjoerglist of arguments (both named and unnamed) in a list named ``ARGV``, and the 3417330f729Sjoergsublist of unnamed arguments in ``ARGN``. Below is a trivial example of 3427330f729Sjoergproviding a wrapper function for CMake's built in function ``add_dependencies``. 3437330f729Sjoerg 3447330f729Sjoerg.. code-block:: cmake 3457330f729Sjoerg 3467330f729Sjoerg function(add_deps target) 3477330f729Sjoerg add_dependencies(${target} ${ARGN}) 3487330f729Sjoerg endfunction() 3497330f729Sjoerg 3507330f729SjoergThis example defines a new macro named ``add_deps`` which takes a required first 3517330f729Sjoergargument, and just calls another function passing through the first argument and 3527330f729Sjoergall trailing arguments. 3537330f729Sjoerg 3547330f729SjoergCMake provides a module ``CMakeParseArguments`` which provides an implementation 3557330f729Sjoergof advanced argument parsing. We use this all over LLVM, and it is recommended 3567330f729Sjoergfor any function that has complex argument-based behaviors or optional 3577330f729Sjoergarguments. CMake's official documentation for the module is in the 3587330f729Sjoerg``cmake-modules`` manpage, and is also available at the 3597330f729Sjoerg`cmake-modules online documentation 3607330f729Sjoerg<https://cmake.org/cmake/help/v3.4/module/CMakeParseArguments.html>`_. 3617330f729Sjoerg 3627330f729Sjoerg.. note:: 3637330f729Sjoerg As of CMake 3.5 the cmake_parse_arguments command has become a native command 3647330f729Sjoerg and the CMakeParseArguments module is empty and only left around for 3657330f729Sjoerg compatibility. 3667330f729Sjoerg 3677330f729SjoergFunctions Vs Macros 3687330f729Sjoerg------------------- 3697330f729Sjoerg 3707330f729SjoergFunctions and Macros look very similar in how they are used, but there is one 3717330f729Sjoergfundamental difference between the two. Functions have their own scope, and 3727330f729Sjoergmacros don't. This means variables set in macros will bleed out into the calling 3737330f729Sjoergscope. That makes macros suitable for defining very small bits of functionality 3747330f729Sjoergonly. 3757330f729Sjoerg 3767330f729SjoergThe other difference between CMake functions and macros is how arguments are 3777330f729Sjoergpassed. Arguments to macros are not set as variables, instead dereferences to 3787330f729Sjoergthe parameters are resolved across the macro before executing it. This can 3797330f729Sjoergresult in some unexpected behavior if using unreferenced variables. For example: 3807330f729Sjoerg 3817330f729Sjoerg.. code-block:: cmake 3827330f729Sjoerg 3837330f729Sjoerg macro(print_list my_list) 3847330f729Sjoerg foreach(var IN LISTS my_list) 3857330f729Sjoerg message("${var}") 3867330f729Sjoerg endforeach() 3877330f729Sjoerg endmacro() 3887330f729Sjoerg 3897330f729Sjoerg set(my_list a b c d) 3907330f729Sjoerg set(my_list_of_numbers 1 2 3 4) 3917330f729Sjoerg print_list(my_list_of_numbers) 3927330f729Sjoerg # prints: 3937330f729Sjoerg # a 3947330f729Sjoerg # b 3957330f729Sjoerg # c 3967330f729Sjoerg # d 3977330f729Sjoerg 3987330f729SjoergGenerally speaking this issue is uncommon because it requires using 3997330f729Sjoergnon-dereferenced variables with names that overlap in the parent scope, but it 4007330f729Sjoergis important to be aware of because it can lead to subtle bugs. 4017330f729Sjoerg 4027330f729SjoergLLVM Project Wrappers 4037330f729Sjoerg===================== 4047330f729Sjoerg 4057330f729SjoergLLVM projects provide lots of wrappers around critical CMake built-in commands. 4067330f729SjoergWe use these wrappers to provide consistent behaviors across LLVM components 4077330f729Sjoergand to reduce code duplication. 4087330f729Sjoerg 4097330f729SjoergWe generally (but not always) follow the convention that commands prefaced with 4107330f729Sjoerg``llvm_`` are intended to be used only as building blocks for other commands. 4117330f729SjoergWrapper commands that are intended for direct use are generally named following 4127330f729Sjoergwith the project in the middle of the command name (i.e. ``add_llvm_executable`` 4137330f729Sjoergis the wrapper for ``add_executable``). The LLVM ``add_*`` wrapper functions are 4147330f729Sjoergall defined in ``AddLLVM.cmake`` which is installed as part of the LLVM 4157330f729Sjoergdistribution. It can be included and used by any LLVM sub-project that requires 4167330f729SjoergLLVM. 4177330f729Sjoerg 4187330f729Sjoerg.. note:: 4197330f729Sjoerg 4207330f729Sjoerg Not all LLVM projects require LLVM for all use cases. For example compiler-rt 4217330f729Sjoerg can be built without LLVM, and the compiler-rt sanitizer libraries are used 4227330f729Sjoerg with GCC. 4237330f729Sjoerg 4247330f729SjoergUseful Built-in Commands 4257330f729Sjoerg======================== 4267330f729Sjoerg 4277330f729SjoergCMake has a bunch of useful built-in commands. This document isn't going to 4287330f729Sjoerggo into details about them because The CMake project has excellent 4297330f729Sjoergdocumentation. To highlight a few useful functions see: 4307330f729Sjoerg 4317330f729Sjoerg* `add_custom_command <https://cmake.org/cmake/help/v3.4/command/add_custom_command.html>`_ 4327330f729Sjoerg* `add_custom_target <https://cmake.org/cmake/help/v3.4/command/add_custom_target.html>`_ 4337330f729Sjoerg* `file <https://cmake.org/cmake/help/v3.4/command/file.html>`_ 4347330f729Sjoerg* `list <https://cmake.org/cmake/help/v3.4/command/list.html>`_ 4357330f729Sjoerg* `math <https://cmake.org/cmake/help/v3.4/command/math.html>`_ 4367330f729Sjoerg* `string <https://cmake.org/cmake/help/v3.4/command/string.html>`_ 4377330f729Sjoerg 4387330f729SjoergThe full documentation for CMake commands is in the ``cmake-commands`` manpage 4397330f729Sjoergand available on `CMake's website <https://cmake.org/cmake/help/v3.4/manual/cmake-commands.7.html>`_ 440