Initial public release.
[OpenCLIPER] / cmake_modules / CMakeParseArguments.cmake
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 #=============================================================================
87 # Copyright 2010 Alexander Neundorf <neundorf@kde.org>
88 #
89 # Distributed under the OSI-approved BSD License (the "License");
90 # see accompanying file Copyright.txt for details.
91 #
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.)
98
99
100 if(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
101   return()
102 endif()
103 set(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)
104
105
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})
110   endforeach()
111
112   foreach(option ${_optionNames})
113     set(${prefix}_${option} FALSE)
114   endforeach()
115
116   set(${prefix}_UNPARSED_ARGUMENTS)
117
118   set(insideValues FALSE)
119   set(currentArgName)
120
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
126
127     if(${optionIndex} EQUAL -1  AND  ${singleArgIndex} EQUAL -1  AND  ${multiArgIndex} EQUAL -1)
128       if(insideValues)
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})
134         endif()
135       else()
136         list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
137       endif()
138     else()
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")
150       endif()
151     endif()
152
153   endforeach()
154
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)
158   endforeach()
159   set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)
160
161 endfunction()