cmake_parse_arguments.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. The ``<options>`` argument contains all options for the respective macro,
  11. i.e. keywords which can be used when calling the macro without any value
  12. following, like e.g. the ``OPTIONAL`` keyword of the :command:`install`
  13. command.
  14. The ``<one_value_keywords>`` argument contains all keywords for this macro
  15. which are followed by one value, like e.g. ``DESTINATION`` keyword of the
  16. :command:`install` command.
  17. The ``<multi_value_keywords>`` argument contains all keywords for this
  18. macro which can be followed by more than one value, like e.g. the
  19. ``TARGETS`` or ``FILES`` keywords of the :command:`install` command.
  20. .. note::
  21. All keywords shall be unique. I.e. every keyword shall only be specified
  22. once in either ``<options>``, ``<one_value_keywords>`` or
  23. ``<multi_value_keywords>``. A warning will be emitted if uniqueness is
  24. violated.
  25. When done, ``cmake_parse_arguments`` will have defined for each of the
  26. keywords listed in ``<options>``, ``<one_value_keywords>`` and
  27. ``<multi_value_keywords>`` a variable composed of the given ``<prefix>``
  28. followed by ``"_"`` and the name of the respective keyword. These
  29. variables will then hold the respective value from the argument list.
  30. For the ``<options>`` keywords this will be ``TRUE`` or ``FALSE``.
  31. All remaining arguments are collected in a variable
  32. ``<prefix>_UNPARSED_ARGUMENTS``, this can be checked afterwards to see
  33. whether your macro was called with unrecognized parameters.
  34. As an example here a ``my_install()`` macro, which takes similar arguments
  35. as the real :command:`install` command:
  36. .. code-block:: cmake
  37. function(MY_INSTALL)
  38. set(options OPTIONAL FAST)
  39. set(oneValueArgs DESTINATION RENAME)
  40. set(multiValueArgs TARGETS CONFIGURATIONS)
  41. cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}"
  42. "${multiValueArgs}" ${ARGN} )
  43. # ...
  44. Assume ``my_install()`` has been called like this:
  45. .. code-block:: cmake
  46. my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
  47. After the ``cmake_parse_arguments`` call the macro will have set the
  48. following variables::
  49. MY_INSTALL_OPTIONAL = TRUE
  50. MY_INSTALL_FAST = FALSE (was not used in call to my_install)
  51. MY_INSTALL_DESTINATION = "bin"
  52. MY_INSTALL_RENAME = "" (was not used)
  53. MY_INSTALL_TARGETS = "foo;bar"
  54. MY_INSTALL_CONFIGURATIONS = "" (was not used)
  55. MY_INSTALL_UNPARSED_ARGUMENTS = "blub" (nothing expected after "OPTIONAL")
  56. You can then continue and process these variables.
  57. Keywords terminate lists of values, e.g. if directly after a
  58. one_value_keyword another recognized keyword follows, this is
  59. interpreted as the beginning of the new option. E.g.
  60. ``my_install(TARGETS foo DESTINATION OPTIONAL)`` would result in
  61. ``MY_INSTALL_DESTINATION`` set to ``"OPTIONAL"``, but as ``OPTIONAL``
  62. is a keyword itself ``MY_INSTALL_DESTINATION`` will be empty and
  63. ``MY_INSTALL_OPTIONAL`` will therefore be set to ``TRUE``.