macro.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. Invocation
  18. ^^^^^^^^^^
  19. The macro invocation is case-insensitive. A macro defined as
  20. .. code-block:: cmake
  21. macro(foo)
  22. <commands>
  23. endmacro()
  24. can be invoked through any of
  25. .. code-block:: cmake
  26. foo()
  27. Foo()
  28. FOO()
  29. and so on. However, it is strongly recommended to stay with the
  30. case chosen in the macro definition. Typically macros use
  31. all-lowercase names.
  32. Arguments
  33. ^^^^^^^^^
  34. When a macro is invoked, the commands recorded in the macro are
  35. first modified by replacing formal parameters (``${arg1}``, ...)
  36. with the arguments passed, and then invoked as normal commands.
  37. In addition to referencing the formal parameters you can reference the
  38. values ``${ARGC}`` which will be set to the number of arguments passed
  39. into the function as well as ``${ARGV0}``, ``${ARGV1}``, ``${ARGV2}``,
  40. ... which will have the actual values of the arguments passed in.
  41. This facilitates creating macros with optional arguments.
  42. Furthermore, ``${ARGV}`` holds the list of all arguments given to the
  43. macro and ``${ARGN}`` holds the list of arguments past the last expected
  44. argument.
  45. Referencing to ``${ARGV#}`` arguments beyond ``${ARGC}`` have undefined
  46. behavior. Checking that ``${ARGC}`` is greater than ``#`` is the only
  47. way to ensure that ``${ARGV#}`` was passed to the function as an extra
  48. argument.
  49. Argument Caveats
  50. ^^^^^^^^^^^^^^^^
  51. Note that the parameters to a macro and values such as ``ARGN`` are
  52. not variables in the usual CMake sense. They are string
  53. replacements much like the C preprocessor would do with a macro.
  54. Therefore you will NOT be able to use commands like
  55. .. code-block:: cmake
  56. if(ARGV1) # ARGV1 is not a variable
  57. if(DEFINED ARGV2) # ARGV2 is not a variable
  58. if(ARGC GREATER 2) # ARGC is not a variable
  59. foreach(loop_var IN LISTS ARGN) # ARGN is not a variable
  60. In the first case, you can use ``if(${ARGV1})``.
  61. In the second and third case, the proper way to check if an optional
  62. variable was passed to the macro is to use ``if(${ARGC} GREATER 2)``.
  63. In the last case, you can use ``foreach(loop_var ${ARGN})`` but this
  64. will skip empty arguments.
  65. If you need to include them, you can use
  66. .. code-block:: cmake
  67. set(list_var "${ARGN}")
  68. foreach(loop_var IN LISTS list_var)
  69. Note that if you have a variable with the same name in the scope from
  70. which the macro is called, using unreferenced names will use the
  71. existing variable instead of the arguments. For example:
  72. .. code-block:: cmake
  73. macro(_BAR)
  74. foreach(arg IN LISTS ARGN)
  75. <commands>
  76. endforeach()
  77. endmacro()
  78. function(_FOO)
  79. _bar(x y z)
  80. endfunction()
  81. _foo(a b c)
  82. Will loop over ``a;b;c`` and not over ``x;y;z`` as one might be expecting.
  83. If you want true CMake variables and/or better CMake scope control you
  84. should look at the function command.