macro.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(<name>)
  8. Defines a macro named ``<name>`` that takes arguments
  9. named ``<arg1>``, ...
  10. Commands listed after macro, but before the matching
  11. :command:`endmacro()`, are not invoked until the macro is invoked.
  12. When it is invoked, the commands recorded in the macro are first
  13. modified by replacing formal parameters (``${arg1}``, ...)
  14. with the arguments passed, and then invoked as normal commands.
  15. In addition to referencing the formal parameters you can reference the
  16. values ``${ARGC}`` which will be set to the number of arguments passed
  17. into the function as well as ``${ARGV0}``, ``${ARGV1}``, ``${ARGV2}``,
  18. ... which will have the actual values of the arguments passed in.
  19. This facilitates creating macros with optional arguments.
  20. Furthermore, ``${ARGV}`` holds the list of all arguments given to the
  21. macro and ``${ARGN}`` holds the list of arguments past the last expected
  22. argument.
  23. Referencing to ``${ARGV#}`` arguments beyond ``${ARGC}`` have undefined
  24. behavior. Checking that ``${ARGC}`` is greater than ``#`` is the only
  25. way to ensure that ``${ARGV#}`` was passed to the function as an extra
  26. argument.
  27. See the :command:`cmake_policy()` command documentation for the behavior
  28. of policies inside macros.
  29. Macro Argument Caveats
  30. ^^^^^^^^^^^^^^^^^^^^^^
  31. Note that the parameters to a macro and values such as ``ARGN`` are
  32. not variables in the usual CMake sense. They are string
  33. replacements much like the C preprocessor would do with a macro.
  34. Therefore you will NOT be able to use commands like
  35. .. code-block:: cmake
  36. if(ARGV1) # ARGV1 is not a variable
  37. if(DEFINED ARGV2) # ARGV2 is not a variable
  38. if(ARGC GREATER 2) # ARGC is not a variable
  39. foreach(loop_var IN LISTS ARGN) # ARGN is not a variable
  40. In the first case, you can use ``if(${ARGV1})``.
  41. In the second and third case, the proper way to check if an optional
  42. variable was passed to the macro is to use ``if(${ARGC} GREATER 2)``.
  43. In the last case, you can use ``foreach(loop_var ${ARGN})`` but this
  44. will skip empty arguments.
  45. If you need to include them, you can use
  46. .. code-block:: cmake
  47. set(list_var "${ARGN}")
  48. foreach(loop_var IN LISTS list_var)
  49. Note that if you have a variable with the same name in the scope from
  50. which the macro is called, using unreferenced names will use the
  51. existing variable instead of the arguments. For example:
  52. .. code-block:: cmake
  53. macro(_BAR)
  54. foreach(arg IN LISTS ARGN)
  55. <commands>
  56. endforeach()
  57. endmacro()
  58. function(_FOO)
  59. _bar(x y z)
  60. endfunction()
  61. _foo(a b c)
  62. Will loop over ``a;b;c`` and not over ``x;y;z`` as one might be expecting.
  63. If you want true CMake variables and/or better CMake scope control you
  64. should look at the function command.