CMakeParseArguments.cmake 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #.rst:
  2. # CMakeParseArguments
  3. # -------------------
  4. #
  5. #
  6. #
  7. # CMAKE_PARSE_ARGUMENTS(<prefix> <options> <one_value_keywords>
  8. # <multi_value_keywords> args...)
  9. #
  10. # CMAKE_PARSE_ARGUMENTS() is intended to be used in macros or functions
  11. # for parsing the arguments given to that macro or function. It
  12. # processes the arguments and defines a set of variables which hold the
  13. # values of the respective options.
  14. #
  15. # The <options> argument contains all options for the respective macro,
  16. # i.e. keywords which can be used when calling the macro without any
  17. # value following, like e.g. the OPTIONAL keyword of the install()
  18. # command.
  19. #
  20. # The <one_value_keywords> argument contains all keywords for this macro
  21. # which are followed by one value, like e.g. DESTINATION keyword of the
  22. # install() command.
  23. #
  24. # The <multi_value_keywords> argument contains all keywords for this
  25. # macro which can be followed by more than one value, like e.g. the
  26. # TARGETS or FILES keywords of the install() command.
  27. #
  28. # When done, CMAKE_PARSE_ARGUMENTS() will have defined for each of the
  29. # keywords listed in <options>, <one_value_keywords> and
  30. # <multi_value_keywords> a variable composed of the given <prefix>
  31. # followed by "_" and the name of the respective keyword. These
  32. # variables will then hold the respective value from the argument list.
  33. # For the <options> keywords this will be TRUE or FALSE.
  34. #
  35. # All remaining arguments are collected in a variable
  36. # <prefix>_UNPARSED_ARGUMENTS, this can be checked afterwards to see
  37. # whether your macro was called with unrecognized parameters.
  38. #
  39. # As an example here a my_install() macro, which takes similar arguments
  40. # as the real install() command:
  41. #
  42. # ::
  43. #
  44. # function(MY_INSTALL)
  45. # set(options OPTIONAL FAST)
  46. # set(oneValueArgs DESTINATION RENAME)
  47. # set(multiValueArgs TARGETS CONFIGURATIONS)
  48. # cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  49. # ...
  50. #
  51. #
  52. #
  53. # Assume my_install() has been called like this:
  54. #
  55. # ::
  56. #
  57. # my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
  58. #
  59. #
  60. #
  61. # After the cmake_parse_arguments() call the macro will have set the
  62. # following variables:
  63. #
  64. # ::
  65. #
  66. # MY_INSTALL_OPTIONAL = TRUE
  67. # MY_INSTALL_FAST = FALSE (this option was not used when calling my_install()
  68. # MY_INSTALL_DESTINATION = "bin"
  69. # MY_INSTALL_RENAME = "" (was not used)
  70. # MY_INSTALL_TARGETS = "foo;bar"
  71. # MY_INSTALL_CONFIGURATIONS = "" (was not used)
  72. # MY_INSTALL_UNPARSED_ARGUMENTS = "blub" (no value expected after "OPTIONAL"
  73. #
  74. #
  75. #
  76. # You can then continue and process these variables.
  77. #
  78. # Keywords terminate lists of values, e.g. if directly after a
  79. # one_value_keyword another recognized keyword follows, this is
  80. # interpreted as the beginning of the new option. E.g.
  81. # my_install(TARGETS foo DESTINATION OPTIONAL) would result in
  82. # MY_INSTALL_DESTINATION set to "OPTIONAL", but MY_INSTALL_DESTINATION
  83. # would be empty and MY_INSTALL_OPTIONAL would be set to TRUE therefor.
  84. #=============================================================================
  85. # Copyright 2010 Alexander Neundorf <neundorf@kde.org>
  86. #
  87. # Distributed under the OSI-approved BSD License (the "License");
  88. # see accompanying file Copyright.txt for details.
  89. #
  90. # This software is distributed WITHOUT ANY WARRANTY; without even the
  91. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  92. # See the License for more information.
  93. #=============================================================================
  94. # (To distribute this file outside of CMake, substitute the full
  95. # License text for the above reference.)
  96. if(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
  97. return()
  98. endif()
  99. set(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)
  100. function(CMAKE_PARSE_ARGUMENTS prefix _optionNames _singleArgNames _multiArgNames)
  101. # first set all result variables to empty/FALSE
  102. foreach(arg_name ${_singleArgNames} ${_multiArgNames})
  103. set(${prefix}_${arg_name})
  104. endforeach()
  105. foreach(option ${_optionNames})
  106. set(${prefix}_${option} FALSE)
  107. endforeach()
  108. set(${prefix}_UNPARSED_ARGUMENTS)
  109. set(insideValues FALSE)
  110. set(currentArgName)
  111. # now iterate over all arguments and fill the result variables
  112. foreach(currentArg ${ARGN})
  113. list(FIND _optionNames "${currentArg}" optionIndex) # ... then this marks the end of the arguments belonging to this keyword
  114. list(FIND _singleArgNames "${currentArg}" singleArgIndex) # ... then this marks the end of the arguments belonging to this keyword
  115. list(FIND _multiArgNames "${currentArg}" multiArgIndex) # ... then this marks the end of the arguments belonging to this keyword
  116. if(${optionIndex} EQUAL -1 AND ${singleArgIndex} EQUAL -1 AND ${multiArgIndex} EQUAL -1)
  117. if(insideValues)
  118. if("${insideValues}" STREQUAL "SINGLE")
  119. set(${prefix}_${currentArgName} ${currentArg})
  120. set(insideValues FALSE)
  121. elseif("${insideValues}" STREQUAL "MULTI")
  122. list(APPEND ${prefix}_${currentArgName} ${currentArg})
  123. endif()
  124. else()
  125. list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
  126. endif()
  127. else()
  128. if(NOT ${optionIndex} EQUAL -1)
  129. set(${prefix}_${currentArg} TRUE)
  130. set(insideValues FALSE)
  131. elseif(NOT ${singleArgIndex} EQUAL -1)
  132. set(currentArgName ${currentArg})
  133. set(${prefix}_${currentArgName})
  134. set(insideValues "SINGLE")
  135. elseif(NOT ${multiArgIndex} EQUAL -1)
  136. set(currentArgName ${currentArg})
  137. set(${prefix}_${currentArgName})
  138. set(insideValues "MULTI")
  139. endif()
  140. endif()
  141. endforeach()
  142. # propagate the result variables to the caller:
  143. foreach(arg_name ${_singleArgNames} ${_multiArgNames} ${_optionNames})
  144. set(${prefix}_${arg_name} ${${prefix}_${arg_name}} PARENT_SCOPE)
  145. endforeach()
  146. set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)
  147. endfunction()