macro.rst 4.6 KB

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