cmake_parse_arguments.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. cmake_parse_arguments
  2. ---------------------
  3. Parse function or macro arguments.
  4. .. code-block:: cmake
  5. cmake_parse_arguments(<prefix> <options> <one_value_keywords>
  6. <multi_value_keywords> <args>...)
  7. cmake_parse_arguments(PARSE_ARGV <N> <prefix> <options>
  8. <one_value_keywords> <multi_value_keywords>)
  9. .. versionadded:: 3.5
  10. This command is implemented natively. Previously, it has been defined in the
  11. module :module:`CMakeParseArguments`.
  12. This command is for use in macros or functions.
  13. It processes the arguments given to that macro or function,
  14. and defines a set of variables which hold the values of the
  15. respective options.
  16. The first signature reads processes arguments passed in the ``<args>...``.
  17. This may be used in either a :command:`macro` or a :command:`function`.
  18. .. versionadded:: 3.7
  19. The ``PARSE_ARGV`` signature is only for use in a :command:`function`
  20. body. In this case the arguments that are parsed come from the
  21. ``ARGV#`` variables of the calling function. The parsing starts with
  22. the ``<N>``-th argument, where ``<N>`` is an unsigned integer.
  23. This allows for the values to have special characters like ``;`` in them.
  24. The ``<options>`` argument contains all options for the respective macro,
  25. i.e. keywords which can be used when calling the macro without any value
  26. following, like e.g. the ``OPTIONAL`` keyword of the :command:`install`
  27. command.
  28. The ``<one_value_keywords>`` argument contains all keywords for this macro
  29. which are followed by one value, like e.g. ``DESTINATION`` keyword of the
  30. :command:`install` command.
  31. The ``<multi_value_keywords>`` argument contains all keywords for this
  32. macro which can be followed by more than one value, like e.g. the
  33. ``TARGETS`` or ``FILES`` keywords of the :command:`install` command.
  34. .. versionchanged:: 3.5
  35. All keywords shall be unique. I.e. every keyword shall only be specified
  36. once in either ``<options>``, ``<one_value_keywords>`` or
  37. ``<multi_value_keywords>``. A warning will be emitted if uniqueness is
  38. violated.
  39. When done, ``cmake_parse_arguments`` will consider for each of the
  40. keywords listed in ``<options>``, ``<one_value_keywords>`` and
  41. ``<multi_value_keywords>`` a variable composed of the given ``<prefix>``
  42. followed by ``"_"`` and the name of the respective keyword. These
  43. variables will then hold the respective value from the argument list
  44. or be undefined if the associated option could not be found.
  45. For the ``<options>`` keywords, these will always be defined,
  46. to ``TRUE`` or ``FALSE``, whether the option is in the argument list or not.
  47. All remaining arguments are collected in a variable
  48. ``<prefix>_UNPARSED_ARGUMENTS`` that will be undefined if all arguments
  49. were recognized. This can be checked afterwards to see
  50. whether your macro was called with unrecognized parameters.
  51. .. versionadded:: 3.15
  52. ``<one_value_keywords>`` and ``<multi_value_keywords>`` that were given no
  53. values at all are collected in a variable
  54. ``<prefix>_KEYWORDS_MISSING_VALUES`` that will be undefined if all keywords
  55. received values. This can be checked to see if there were keywords without
  56. any values given.
  57. Consider the following example macro, ``my_install()``, which takes similar
  58. arguments to the real :command:`install` command:
  59. .. code-block:: cmake
  60. macro(my_install)
  61. set(options OPTIONAL FAST)
  62. set(oneValueArgs DESTINATION RENAME)
  63. set(multiValueArgs TARGETS CONFIGURATIONS)
  64. cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}"
  65. "${multiValueArgs}" ${ARGN} )
  66. # ...
  67. Assume ``my_install()`` has been called like this:
  68. .. code-block:: cmake
  69. my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub CONFIGURATIONS)
  70. After the ``cmake_parse_arguments`` call the macro will have set or undefined
  71. the following variables::
  72. MY_INSTALL_OPTIONAL = TRUE
  73. MY_INSTALL_FAST = FALSE # was not used in call to my_install
  74. MY_INSTALL_DESTINATION = "bin"
  75. MY_INSTALL_RENAME <UNDEFINED> # was not used
  76. MY_INSTALL_TARGETS = "foo;bar"
  77. MY_INSTALL_CONFIGURATIONS <UNDEFINED> # was not used
  78. MY_INSTALL_UNPARSED_ARGUMENTS = "blub" # nothing expected after "OPTIONAL"
  79. MY_INSTALL_KEYWORDS_MISSING_VALUES = "CONFIGURATIONS"
  80. # No value for "CONFIGURATIONS" given
  81. You can then continue and process these variables.
  82. Keywords terminate lists of values, e.g. if directly after a
  83. ``one_value_keyword`` another recognized keyword follows, this is
  84. interpreted as the beginning of the new option. E.g.
  85. ``my_install(TARGETS foo DESTINATION OPTIONAL)`` would result in
  86. ``MY_INSTALL_DESTINATION`` set to ``"OPTIONAL"``, but as ``OPTIONAL``
  87. is a keyword itself ``MY_INSTALL_DESTINATION`` will be empty (but added
  88. to ``MY_INSTALL_KEYWORDS_MISSING_VALUES``) and ``MY_INSTALL_OPTIONAL`` will
  89. therefore be set to ``TRUE``.
  90. See Also
  91. ^^^^^^^^
  92. * :command:`function`
  93. * :command:`macro`