Imported Upstream version 0.7.4
[platform/upstream/libsolv.git] / cmake / modules / _CMakeParseArguments.cmake
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 the 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 #=============================================================================
64 # Copyright 2010 Alexander Neundorf <neundorf@kde.org>
65 #
66 # Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
67 # All rights reserved.
68 #
69 # Redistribution and use in source and binary forms, with or without
70 # modification, are permitted provided that the following conditions
71 # are met:
72 #
73 # * Redistributions of source code must retain the above copyright
74 #   notice, this list of conditions and the following disclaimer.
75 #
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.
79 #
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
83 #   permission.
84 #
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 #=============================================================================
97
98
99 if(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
100   return()
101 endif()
102 set(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)
103
104
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})
109   endforeach(arg_name)
110
111   foreach(option ${_optionNames})
112     set(${prefix}_${option} FALSE)
113   endforeach(option)
114
115   set(${prefix}_UNPARSED_ARGUMENTS)
116
117   set(insideValues FALSE)
118   set(currentArgName)
119
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
125
126     if(${optionIndex} EQUAL -1  AND  ${singleArgIndex} EQUAL -1  AND  ${multiArgIndex} EQUAL -1)
127       if(insideValues)
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})
133         endif()
134       else(insideValues)
135         list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
136       endif(insideValues)
137     else()
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")
149       endif()
150     endif()
151
152   endforeach(currentArg)
153
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)
157   endforeach(arg_name)
158   set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)
159
160 endfunction(CMAKE_PARSE_ARGUMENTS _options _singleArgs _multiArgs)