cmake_parse_arguments.rst 4.1 KB

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