7 # CMAKE_PARSE_ARGUMENTS(<prefix> <options> <one_value_keywords>
8 # <multi_value_keywords> args...)
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.
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()
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
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.
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.
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.
39 # As an example here a my_install() macro, which takes similar arguments
40 # as the real install() command:
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} )
54 # Assume my_install() has been called like this:
58 # my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
62 # After the cmake_parse_arguments() call the macro will have set the
63 # following variables:
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"
77 # You can then continue and process these variables.
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.
86 #=============================================================================
87 # Copyright 2010 Alexander Neundorf <neundorf@kde.org>
89 # Distributed under the OSI-approved BSD License (the "License");
90 # see accompanying file Copyright.txt for details.
92 # This software is distributed WITHOUT ANY WARRANTY; without even the
93 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
94 # See the License for more information.
95 #=============================================================================
96 # (To distribute this file outside of CMake, substitute the full
97 # License text for the above reference.)
100 if(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
103 set(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)
106 function(CMAKE_PARSE_ARGUMENTS prefix _optionNames _singleArgNames _multiArgNames)
107 # first set all result variables to empty/FALSE
108 foreach(arg_name ${_singleArgNames} ${_multiArgNames})
109 set(${prefix}_${arg_name})
112 foreach(option ${_optionNames})
113 set(${prefix}_${option} FALSE)
116 set(${prefix}_UNPARSED_ARGUMENTS)
118 set(insideValues FALSE)
121 # now iterate over all arguments and fill the result variables
122 foreach(currentArg ${ARGN})
123 list(FIND _optionNames "${currentArg}" optionIndex) # ... then this marks the end of the arguments belonging to this keyword
124 list(FIND _singleArgNames "${currentArg}" singleArgIndex) # ... then this marks the end of the arguments belonging to this keyword
125 list(FIND _multiArgNames "${currentArg}" multiArgIndex) # ... then this marks the end of the arguments belonging to this keyword
127 if(${optionIndex} EQUAL -1 AND ${singleArgIndex} EQUAL -1 AND ${multiArgIndex} EQUAL -1)
129 if("${insideValues}" STREQUAL "SINGLE")
130 set(${prefix}_${currentArgName} ${currentArg})
131 set(insideValues FALSE)
132 elseif("${insideValues}" STREQUAL "MULTI")
133 list(APPEND ${prefix}_${currentArgName} ${currentArg})
136 list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
139 if(NOT ${optionIndex} EQUAL -1)
140 set(${prefix}_${currentArg} TRUE)
141 set(insideValues FALSE)
142 elseif(NOT ${singleArgIndex} EQUAL -1)
143 set(currentArgName ${currentArg})
144 set(${prefix}_${currentArgName})
145 set(insideValues "SINGLE")
146 elseif(NOT ${multiArgIndex} EQUAL -1)
147 set(currentArgName ${currentArg})
148 set(${prefix}_${currentArgName})
149 set(insideValues "MULTI")
155 # propagate the result variables to the caller:
156 foreach(arg_name ${_singleArgNames} ${_multiArgNames} ${_optionNames})
157 set(${prefix}_${arg_name} ${${prefix}_${arg_name}} PARENT_SCOPE)
159 set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)