macro.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, the commands recorded in the macro are
  41. first modified by replacing formal parameters (``${arg1}``, ...)
  42. with the arguments passed, and then invoked as normal commands.
  43. In addition to referencing the formal parameters you can reference the
  44. values ``${ARGC}`` which will be set to the number of arguments passed
  45. into the function as well as ``${ARGV0}``, ``${ARGV1}``, ``${ARGV2}``,
  46. ... which will have the actual values of the arguments passed in.
  47. This facilitates creating macros with optional arguments.
  48. Furthermore, ``${ARGV}`` holds the list of all arguments given to the
  49. macro and ``${ARGN}`` holds the list of arguments past the last expected
  50. argument.
  51. Referencing to ``${ARGV#}`` arguments beyond ``${ARGC}`` have undefined
  52. behavior. Checking that ``${ARGC}`` is greater than ``#`` is the only
  53. way to ensure that ``${ARGV#}`` was passed to the function as an extra
  54. argument.
  55. .. _`Macro vs Function`:
  56. Macro vs Function
  57. ^^^^^^^^^^^^^^^^^
  58. The ``macro`` command is very similar to the :command:`function` command.
  59. Nonetheless, there are a few important differences.
  60. In a function, ``ARGN``, ``ARGC``, ``ARGV`` and ``ARGV0``, ``ARGV1``, ...
  61. are true variables in the usual CMake sense. In a macro, they are not,
  62. they are string replacements much like the C preprocessor would do
  63. with a macro. This has a number of consequences, as explained in
  64. the :ref:`Argument Caveats` section below.
  65. Another difference between macros and functions is the control flow.
  66. A function is executed by transferring control from the calling
  67. statement to the function body. A macro is executed as if the macro
  68. body were pasted in place of the calling statement. This has the
  69. consequence that a :command:`return()` in a macro body does not
  70. just terminate execution of the macro; rather, control is returned
  71. from the scope of the macro call. To avoid confusion, it is recommended
  72. to avoid :command:`return()` in macros altogether.
  73. Unlike a function, the :variable:`CMAKE_CURRENT_FUNCTION`,
  74. :variable:`CMAKE_CURRENT_FUNCTION_LIST_DIR`,
  75. :variable:`CMAKE_CURRENT_FUNCTION_LIST_FILE`,
  76. :variable:`CMAKE_CURRENT_FUNCTION_LIST_LINE` variables are not
  77. set for a macro.
  78. .. _`Argument Caveats`:
  79. Argument Caveats
  80. ^^^^^^^^^^^^^^^^
  81. Since ``ARGN``, ``ARGC``, ``ARGV``, ``ARGV0`` etc. are not variables,
  82. you will NOT be able to use commands like
  83. .. code-block:: cmake
  84. if(ARGV1) # ARGV1 is not a variable
  85. if(DEFINED ARGV2) # ARGV2 is not a variable
  86. if(ARGC GREATER 2) # ARGC is not a variable
  87. foreach(loop_var IN LISTS ARGN) # ARGN is not a variable
  88. In the first case, you can use ``if(${ARGV1})``. In the second and
  89. third case, the proper way to check if an optional variable was
  90. passed to the macro is to use ``if(${ARGC} GREATER 2)``. In the
  91. last case, you can use ``foreach(loop_var ${ARGN})`` but this will
  92. skip empty arguments. If you need to include them, you can use
  93. .. code-block:: cmake
  94. set(list_var "${ARGN}")
  95. foreach(loop_var IN LISTS list_var)
  96. Note that if you have a variable with the same name in the scope from
  97. which the macro is called, using unreferenced names will use the
  98. existing variable instead of the arguments. For example:
  99. .. code-block:: cmake
  100. macro(bar)
  101. foreach(arg IN LISTS ARGN)
  102. <commands>
  103. endforeach()
  104. endmacro()
  105. function(foo)
  106. bar(x y z)
  107. endfunction()
  108. foo(a b c)
  109. Will loop over ``a;b;c`` and not over ``x;y;z`` as one might have expected.
  110. If you want true CMake variables and/or better CMake scope control you
  111. should look at the function command.
  112. See Also
  113. ^^^^^^^^
  114. * :command:`cmake_parse_arguments`
  115. * :command:`endmacro`