macro.rst 4.8 KB

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