cmake_parse_arguments.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 have defined 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. For the ``<options>`` keywords this will be ``TRUE`` or ``FALSE``.
  40. All remaining arguments are collected in a variable
  41. ``<prefix>_UNPARSED_ARGUMENTS``, this can be checked afterwards to see
  42. whether your macro was called with unrecognized parameters.
  43. As an example here a ``my_install()`` macro, which takes similar arguments
  44. as the real :command:`install` command:
  45. .. code-block:: cmake
  46. function(MY_INSTALL)
  47. set(options OPTIONAL FAST)
  48. set(oneValueArgs DESTINATION RENAME)
  49. set(multiValueArgs TARGETS CONFIGURATIONS)
  50. cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}"
  51. "${multiValueArgs}" ${ARGN} )
  52. # ...
  53. Assume ``my_install()`` has been called like this:
  54. .. code-block:: cmake
  55. my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
  56. After the ``cmake_parse_arguments`` call the macro will have set the
  57. following variables::
  58. MY_INSTALL_OPTIONAL = TRUE
  59. MY_INSTALL_FAST = FALSE (was not used in call to my_install)
  60. MY_INSTALL_DESTINATION = "bin"
  61. MY_INSTALL_RENAME = "" (was not used)
  62. MY_INSTALL_TARGETS = "foo;bar"
  63. MY_INSTALL_CONFIGURATIONS = "" (was not used)
  64. MY_INSTALL_UNPARSED_ARGUMENTS = "blub" (nothing expected after "OPTIONAL")
  65. You can then continue and process these variables.
  66. Keywords terminate lists of values, e.g. if directly after a
  67. one_value_keyword another recognized keyword follows, this is
  68. interpreted as the beginning of the new option. E.g.
  69. ``my_install(TARGETS foo DESTINATION OPTIONAL)`` would result in
  70. ``MY_INSTALL_DESTINATION`` set to ``"OPTIONAL"``, but as ``OPTIONAL``
  71. is a keyword itself ``MY_INSTALL_DESTINATION`` will be empty and
  72. ``MY_INSTALL_OPTIONAL`` will therefore be set to ``TRUE``.