cmake_parse_arguments.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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 function
  25. or macro. These are keywords that have no value following them, like the
  26. ``OPTIONAL`` keyword of the :command:`install` command.
  27. The ``<one_value_keywords>`` argument contains all keywords for this function
  28. or macro which are followed by one value, like the ``DESTINATION`` keyword of
  29. the :command:`install` command.
  30. The ``<multi_value_keywords>`` argument contains all keywords for this
  31. function or macro which can be followed by more than one value, like the
  32. ``TARGETS`` or ``FILES`` keywords of the :command:`install` command.
  33. .. versionchanged:: 3.5
  34. All keywords must be unique. Each keyword can only be specified
  35. once in any of the ``<options>``, ``<one_value_keywords>``, or
  36. ``<multi_value_keywords>``. A warning will be emitted if uniqueness is
  37. violated.
  38. When done, ``cmake_parse_arguments`` will consider for each of the
  39. keywords listed in ``<options>``, ``<one_value_keywords>``, and
  40. ``<multi_value_keywords>``, a variable composed of the given ``<prefix>``
  41. followed by ``"_"`` and the name of the respective keyword. For
  42. ``<one_value_keywords>`` and ``<multi_value_keywords>``, these variables
  43. will then hold the respective value(s) from the argument list, or be undefined
  44. if the associated keyword was not given (policy :policy:`CMP0174` can also
  45. affect the behavior for ``<one_value_keywords>``). For the ``<options>``
  46. keywords, these variables will always be defined, and they will be set to
  47. ``TRUE`` if the keyword is present, or ``FALSE`` if it is not.
  48. All remaining arguments are collected in a variable
  49. ``<prefix>_UNPARSED_ARGUMENTS`` that will be undefined if all arguments
  50. were recognized. This can be checked afterwards to see
  51. whether your macro or function was called with unrecognized parameters.
  52. .. versionadded:: 3.15
  53. ``<one_value_keywords>`` and ``<multi_value_keywords>`` that were given no
  54. values at all are collected in a variable
  55. ``<prefix>_KEYWORDS_MISSING_VALUES`` that will be undefined if all keywords
  56. received values. This can be checked to see if there were keywords without
  57. any values given.
  58. .. versionchanged:: 3.31
  59. If a ``<one_value_keyword>`` is followed by an empty string as its value,
  60. policy :policy:`CMP0174` controls whether a corresponding
  61. ``<prefix>_<keyword>`` variable is defined or not.
  62. Choose a ``<prefix>`` carefully to avoid clashing with existing variable names.
  63. When used inside a function, it is usually suitable to use the prefix ``arg``.
  64. There is a very strong convention that all keywords are fully uppercase, so
  65. this prefix results in variables of the form ``arg_SOME_KEYWORD``. This makes
  66. the code more readable, and it minimizes the chance of clashing with cache
  67. variables, which also have a strong convention of being all uppercase.
  68. .. code-block:: cmake
  69. function(my_install)
  70. set(options OPTIONAL FAST)
  71. set(oneValueArgs DESTINATION RENAME)
  72. set(multiValueArgs TARGETS CONFIGURATIONS)
  73. cmake_parse_arguments(PARSE_ARGV 0 arg
  74. "${options}" "${oneValueArgs}" "${multiValueArgs}"
  75. )
  76. # The above will set or unset variables with the following names:
  77. # arg_OPTIONAL
  78. # arg_FAST
  79. # arg_DESTINATION
  80. # arg_RENAME
  81. # arg_TARGETS
  82. # arg_CONFIGURATIONS
  83. #
  84. # The following will also be set or unset:
  85. # arg_UNPARSED_ARGUMENTS
  86. # arg_KEYWORDS_MISSING_VALUES
  87. When used inside a macro, ``arg`` might not be a suitable prefix because the
  88. code will affect the calling scope. If another macro also called in the same
  89. scope were to use ``arg`` in its own call to ``cmake_parse_arguments()``,
  90. and if there are any common keywords between the two macros, the later call's
  91. variables can overwrite or remove those of the earlier macro's call.
  92. Therefore, it is advisable to incorporate something unique from the macro name
  93. in the ``<prefix>``, such as ``arg_lowercase_macro_name``.
  94. .. code-block:: cmake
  95. macro(my_install)
  96. set(options OPTIONAL FAST)
  97. set(oneValueArgs DESTINATION RENAME)
  98. set(multiValueArgs TARGETS CONFIGURATIONS)
  99. cmake_parse_arguments(arg_my_install
  100. "${options}" "${oneValueArgs}" "${multiValueArgs}"
  101. ${ARGN}
  102. )
  103. # ...
  104. endmacro()
  105. macro(my_special_install)
  106. # NOTE: Has the same keywords as my_install()
  107. set(options OPTIONAL FAST)
  108. set(oneValueArgs DESTINATION RENAME)
  109. set(multiValueArgs TARGETS CONFIGURATIONS)
  110. cmake_parse_arguments(arg_my_special_install
  111. "${options}" "${oneValueArgs}" "${multiValueArgs}"
  112. ${ARGN}
  113. )
  114. # ...
  115. endmacro()
  116. Suppose the above macros are called one after the other, like so:
  117. .. code-block:: cmake
  118. my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub CONFIGURATIONS)
  119. my_special_install(TARGETS barry DESTINATION sbin RENAME FAST)
  120. After these two calls, the following describes the variables that will be
  121. set or unset::
  122. arg_my_install_OPTIONAL = TRUE
  123. arg_my_install_FAST = FALSE # was not present in call to my_install
  124. arg_my_install_DESTINATION = "bin"
  125. arg_my_install_RENAME <UNSET> # was not present
  126. arg_my_install_TARGETS = "foo;bar"
  127. arg_my_install_CONFIGURATIONS <UNSET> # was not present
  128. arg_my_install_UNPARSED_ARGUMENTS = "blub" # nothing expected after "OPTIONAL"
  129. arg_my_install_KEYWORDS_MISSING_VALUES = "CONFIGURATIONS" # value was missing
  130. arg_my_special_install_OPTIONAL = FALSE # was not present
  131. arg_my_special_install_FAST = TRUE
  132. arg_my_special_install_DESTINATION = "sbin"
  133. arg_my_special_install_RENAME <UNSET> # value was missing
  134. arg_my_special_install_TARGETS = "barry"
  135. arg_my_special_install_CONFIGURATIONS <UNSET> # was not present
  136. arg_my_special_install_UNPARSED_ARGUMENTS <UNSET>
  137. arg_my_special_install_KEYWORDS_MISSING_VALUES = "RENAME"
  138. Keywords terminate lists of values. If a keyword is given directly after a
  139. ``<one_value_keyword>``, that preceding ``<one_value_keyword>`` receives no
  140. value and the keyword is added to the ``<prefix>_KEYWORDS_MISSING_VALUES``
  141. variable. In the above example, the call to ``my_special_install()`` contains
  142. the ``RENAME`` keyword immediately followed by the ``FAST`` keyword.
  143. In this case, ``FAST`` terminates processing of the ``RENAME`` keyword.
  144. ``arg_my_special_install_FAST`` is set to ``TRUE``,
  145. ``arg_my_special_install_RENAME`` is unset, and
  146. ``arg_my_special_install_KEYWORDS_MISSING_VALUES`` contains the value
  147. ``RENAME``.
  148. See Also
  149. ^^^^^^^^
  150. * :command:`function`
  151. * :command:`macro`