macro.rst 4.7 KB

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