cmake_parse_arguments.rst 4.5 KB

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