CMakeParseArguments.cmake 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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}"
  49. # "${multiValueArgs}" ${ARGN} )
  50. # ...
  51. #
  52. #
  53. #
  54. # Assume my_install() has been called like this:
  55. #
  56. # ::
  57. #
  58. # my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
  59. #
  60. #
  61. #
  62. # After the cmake_parse_arguments() call the macro will have set the
  63. # following variables:
  64. #
  65. # ::
  66. #
  67. # MY_INSTALL_OPTIONAL = TRUE
  68. # MY_INSTALL_FAST = FALSE (this option was not used when calling my_install()
  69. # MY_INSTALL_DESTINATION = "bin"
  70. # MY_INSTALL_RENAME = "" (was not used)
  71. # MY_INSTALL_TARGETS = "foo;bar"
  72. # MY_INSTALL_CONFIGURATIONS = "" (was not used)
  73. # MY_INSTALL_UNPARSED_ARGUMENTS = "blub" (no value expected after "OPTIONAL"
  74. #
  75. #
  76. #
  77. # You can then continue and process these variables.
  78. #
  79. # Keywords terminate lists of values, e.g. if directly after a
  80. # one_value_keyword another recognized keyword follows, this is
  81. # interpreted as the beginning of the new option. E.g.
  82. # my_install(TARGETS foo DESTINATION OPTIONAL) would result in
  83. # MY_INSTALL_DESTINATION set to "OPTIONAL", but MY_INSTALL_DESTINATION
  84. # would be empty and MY_INSTALL_OPTIONAL would be set to TRUE therefor.
  85. #=============================================================================
  86. # Copyright 2010 Alexander Neundorf <neundorf@kde.org>
  87. #
  88. # Distributed under the OSI-approved BSD License (the "License");
  89. # see accompanying file Copyright.txt for details.
  90. #
  91. # This software is distributed WITHOUT ANY WARRANTY; without even the
  92. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  93. # See the License for more information.
  94. #=============================================================================
  95. # (To distribute this file outside of CMake, substitute the full
  96. # License text for the above reference.)
  97. if(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
  98. return()
  99. endif()
  100. set(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)
  101. function(CMAKE_PARSE_ARGUMENTS prefix _optionNames _singleArgNames _multiArgNames)
  102. # first set all result variables to empty/FALSE
  103. foreach(arg_name ${_singleArgNames} ${_multiArgNames})
  104. set(${prefix}_${arg_name})
  105. endforeach()
  106. foreach(option ${_optionNames})
  107. set(${prefix}_${option} FALSE)
  108. endforeach()
  109. set(${prefix}_UNPARSED_ARGUMENTS)
  110. set(insideValues FALSE)
  111. set(currentArgName)
  112. # now iterate over all arguments and fill the result variables
  113. foreach(currentArg ${ARGN})
  114. list(FIND _optionNames "${currentArg}" optionIndex) # ... then this marks the end of the arguments belonging to this keyword
  115. list(FIND _singleArgNames "${currentArg}" singleArgIndex) # ... then this marks the end of the arguments belonging to this keyword
  116. list(FIND _multiArgNames "${currentArg}" multiArgIndex) # ... then this marks the end of the arguments belonging to this keyword
  117. if(${optionIndex} EQUAL -1 AND ${singleArgIndex} EQUAL -1 AND ${multiArgIndex} EQUAL -1)
  118. if(insideValues)
  119. if("${insideValues}" STREQUAL "SINGLE")
  120. set(${prefix}_${currentArgName} ${currentArg})
  121. set(insideValues FALSE)
  122. elseif("${insideValues}" STREQUAL "MULTI")
  123. list(APPEND ${prefix}_${currentArgName} ${currentArg})
  124. endif()
  125. else()
  126. list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
  127. endif()
  128. else()
  129. if(NOT ${optionIndex} EQUAL -1)
  130. set(${prefix}_${currentArg} TRUE)
  131. set(insideValues FALSE)
  132. elseif(NOT ${singleArgIndex} EQUAL -1)
  133. set(currentArgName ${currentArg})
  134. set(${prefix}_${currentArgName})
  135. set(insideValues "SINGLE")
  136. elseif(NOT ${multiArgIndex} EQUAL -1)
  137. set(currentArgName ${currentArg})
  138. set(${prefix}_${currentArgName})
  139. set(insideValues "MULTI")
  140. endif()
  141. endif()
  142. endforeach()
  143. # propagate the result variables to the caller:
  144. foreach(arg_name ${_singleArgNames} ${_multiArgNames} ${_optionNames})
  145. set(${prefix}_${arg_name} ${${prefix}_${arg_name}} PARENT_SCOPE)
  146. endforeach()
  147. set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)
  148. endfunction()