macro.rst 2.7 KB

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