cmake_parse_arguments.rst 4.6 KB

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