CMakeParseArguments.cmake 5.6 KB

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