1macro 2----- 3 4Start recording a macro for later invocation as a command 5 6.. code-block:: cmake 7 8 macro(<name> [<arg1> ...]) 9 <commands> 10 endmacro() 11 12Defines a macro named ``<name>`` that takes arguments named 13``<arg1>``, ... Commands listed after macro, but before the 14matching :command:`endmacro()`, are not executed until the macro 15is invoked. 16 17Per legacy, the :command:`endmacro` command admits an optional 18``<name>`` argument. If used, it must be a verbatim repeat of the 19argument of the opening ``macro`` command. 20 21See the :command:`cmake_policy()` command documentation for the behavior 22of policies inside macros. 23 24See the :ref:`Macro vs Function` section below for differences 25between CMake macros and :command:`functions <function>`. 26 27Invocation 28^^^^^^^^^^ 29 30The macro invocation is case-insensitive. A macro defined as 31 32.. code-block:: cmake 33 34 macro(foo) 35 <commands> 36 endmacro() 37 38can be invoked through any of 39 40.. code-block:: cmake 41 42 foo() 43 Foo() 44 FOO() 45 cmake_language(CALL foo) 46 47and so on. However, it is strongly recommended to stay with the 48case chosen in the macro definition. Typically macros use 49all-lowercase names. 50 51.. versionadded:: 3.18 52 The :command:`cmake_language(CALL ...)` command can also be used to 53 invoke the macro. 54 55Arguments 56^^^^^^^^^ 57 58When a macro is invoked, the commands recorded in the macro are 59first modified by replacing formal parameters (``${arg1}``, ...) 60with the arguments passed, and then invoked as normal commands. 61 62In addition to referencing the formal parameters you can reference the 63values ``${ARGC}`` which will be set to the number of arguments passed 64into the function as well as ``${ARGV0}``, ``${ARGV1}``, ``${ARGV2}``, 65... which will have the actual values of the arguments passed in. 66This facilitates creating macros with optional arguments. 67 68Furthermore, ``${ARGV}`` holds the list of all arguments given to the 69macro and ``${ARGN}`` holds the list of arguments past the last expected 70argument. 71Referencing to ``${ARGV#}`` arguments beyond ``${ARGC}`` have undefined 72behavior. Checking that ``${ARGC}`` is greater than ``#`` is the only 73way to ensure that ``${ARGV#}`` was passed to the function as an extra 74argument. 75 76.. _`Macro vs Function`: 77 78Macro vs Function 79^^^^^^^^^^^^^^^^^ 80 81The ``macro`` command is very similar to the :command:`function` command. 82Nonetheless, there are a few important differences. 83 84In a function, ``ARGN``, ``ARGC``, ``ARGV`` and ``ARGV0``, ``ARGV1``, ... 85are true variables in the usual CMake sense. In a macro, they are not, 86they are string replacements much like the C preprocessor would do 87with a macro. This has a number of consequences, as explained in 88the :ref:`Argument Caveats` section below. 89 90Another difference between macros and functions is the control flow. 91A function is executed by transferring control from the calling 92statement to the function body. A macro is executed as if the macro 93body were pasted in place of the calling statement. This has the 94consequence that a :command:`return()` in a macro body does not 95just terminate execution of the macro; rather, control is returned 96from the scope of the macro call. To avoid confusion, it is recommended 97to avoid :command:`return()` in macros altogether. 98 99Unlike a function, the :variable:`CMAKE_CURRENT_FUNCTION`, 100:variable:`CMAKE_CURRENT_FUNCTION_LIST_DIR`, 101:variable:`CMAKE_CURRENT_FUNCTION_LIST_FILE`, 102:variable:`CMAKE_CURRENT_FUNCTION_LIST_LINE` variables are not 103set for a macro. 104 105.. _`Argument Caveats`: 106 107Argument Caveats 108^^^^^^^^^^^^^^^^ 109 110Since ``ARGN``, ``ARGC``, ``ARGV``, ``ARGV0`` etc. are not variables, 111you will NOT be able to use commands like 112 113.. code-block:: cmake 114 115 if(ARGV1) # ARGV1 is not a variable 116 if(DEFINED ARGV2) # ARGV2 is not a variable 117 if(ARGC GREATER 2) # ARGC is not a variable 118 foreach(loop_var IN LISTS ARGN) # ARGN is not a variable 119 120In the first case, you can use ``if(${ARGV1})``. In the second and 121third case, the proper way to check if an optional variable was 122passed to the macro is to use ``if(${ARGC} GREATER 2)``. In the 123last case, you can use ``foreach(loop_var ${ARGN})`` but this will 124skip empty arguments. If you need to include them, you can use 125 126.. code-block:: cmake 127 128 set(list_var "${ARGN}") 129 foreach(loop_var IN LISTS list_var) 130 131Note that if you have a variable with the same name in the scope from 132which the macro is called, using unreferenced names will use the 133existing variable instead of the arguments. For example: 134 135.. code-block:: cmake 136 137 macro(bar) 138 foreach(arg IN LISTS ARGN) 139 <commands> 140 endforeach() 141 endmacro() 142 143 function(foo) 144 bar(x y z) 145 endfunction() 146 147 foo(a b c) 148 149Will loop over ``a;b;c`` and not over ``x;y;z`` as one might have expected. 150If you want true CMake variables and/or better CMake scope control you 151should look at the function command. 152