1 # CMAKE_PARSE_ARGUMENTS(<prefix> <options> <one_value_keywords> <multi_value_keywords> args...)
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.
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.
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
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.
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.
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.
31 # As an example here a my_install() macro, which takes similar arguments as the
32 # real install() command:
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} )
41 # Assume my_install() has been called like this:
42 # my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
44 # After the cmake_parse_arguments() call the macro will have set the following
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"
54 # You can the continue and process these variables.
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
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.
63 #=============================================================================
64 # Copyright 2010 Alexander Neundorf <neundorf@kde.org>
66 # Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
67 # All rights reserved.
69 # Redistribution and use in source and binary forms, with or without
70 # modification, are permitted provided that the following conditions
73 # * Redistributions of source code must retain the above copyright
74 # notice, this list of conditions and the following disclaimer.
76 # * Redistributions in binary form must reproduce the above copyright
77 # notice, this list of conditions and the following disclaimer in the
78 # documentation and/or other materials provided with the distribution.
80 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
81 # nor the names of their contributors may be used to endorse or promote
82 # products derived from this software without specific prior written
85 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
86 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
87 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
88 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
89 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
90 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
91 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
92 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
93 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
94 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
95 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 #=============================================================================
99 if(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
102 set(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)
105 function(CMAKE_PARSE_ARGUMENTS prefix _optionNames _singleArgNames _multiArgNames)
106 # first set all result variables to empty/FALSE
107 foreach(arg_name ${_singleArgNames} ${_multiArgNames})
108 set(${prefix}_${arg_name})
111 foreach(option ${_optionNames})
112 set(${prefix}_${option} FALSE)
115 set(${prefix}_UNPARSED_ARGUMENTS)
117 set(insideValues FALSE)
120 # now iterate over all arguments and fill the result variables
121 foreach(currentArg ${ARGN})
122 list(FIND _optionNames "${currentArg}" optionIndex) # ... then this marks the end of the arguments belonging to this keyword
123 list(FIND _singleArgNames "${currentArg}" singleArgIndex) # ... then this marks the end of the arguments belonging to this keyword
124 list(FIND _multiArgNames "${currentArg}" multiArgIndex) # ... then this marks the end of the arguments belonging to this keyword
126 if(${optionIndex} EQUAL -1 AND ${singleArgIndex} EQUAL -1 AND ${multiArgIndex} EQUAL -1)
128 if("${insideValues}" STREQUAL "SINGLE")
129 set(${prefix}_${currentArgName} ${currentArg})
130 set(insideValues FALSE)
131 elseif("${insideValues}" STREQUAL "MULTI")
132 list(APPEND ${prefix}_${currentArgName} ${currentArg})
135 list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
138 if(NOT ${optionIndex} EQUAL -1)
139 set(${prefix}_${currentArg} TRUE)
140 set(insideValues FALSE)
141 elseif(NOT ${singleArgIndex} EQUAL -1)
142 set(currentArgName ${currentArg})
143 set(${prefix}_${currentArgName})
144 set(insideValues "SINGLE")
145 elseif(NOT ${multiArgIndex} EQUAL -1)
146 set(currentArgName ${currentArg})
147 set(${prefix}_${currentArgName})
148 set(insideValues "MULTI")
152 endforeach(currentArg)
154 # propagate the result variables to the caller:
155 foreach(arg_name ${_singleArgNames} ${_multiArgNames} ${_optionNames})
156 set(${prefix}_${arg_name} ${${prefix}_${arg_name}} PARENT_SCOPE)
158 set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)
160 endfunction(CMAKE_PARSE_ARGUMENTS _options _singleArgs _multiArgs)