llvm.org GIT mirror llvm / 9bb83e4
Add the PARSE_ARGUMENTS CMake macro to LLVM so I can use it to clean up add_clang_unittest's calling convention. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126031 91177308-0d34-0410-b5e6-96231b3b80d8 Jeffrey Yasskin 8 years ago
1 changed file(s) with 80 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 # Copied from http://www.itk.org/Wiki/CMakeMacroParseArguments under
1 # http://creativecommons.org/licenses/by/2.5/.
2 #
3 # The PARSE_ARGUMENTS macro will take the arguments of another macro and define
4 # several variables. The first argument to PARSE_ARGUMENTS is a prefix to put on
5 # all variables it creates. The second argument is a list of names, and the
6 # third argument is a list of options. Both of these lists should be quoted. The
7 # rest of PARSE_ARGUMENTS are arguments from another macro to be parsed.
8 #
9 # PARSE_ARGUMENTS(prefix arg_names options arg1 arg2...)
10 #
11 # For each item in options, PARSE_ARGUMENTS will create a variable with that
12 # name, prefixed with prefix_. So, for example, if prefix is MY_MACRO and
13 # options is OPTION1;OPTION2, then PARSE_ARGUMENTS will create the variables
14 # MY_MACRO_OPTION1 and MY_MACRO_OPTION2. These variables will be set to true if
15 # the option exists in the command line or false otherwise.
16 #
17 #For each item in arg_names, PARSE_ARGUMENTS will create a variable with that
18 #name, prefixed with prefix_. Each variable will be filled with the arguments
19 #that occur after the given arg_name is encountered up to the next arg_name or
20 #the end of the arguments. All options are removed from these
21 #lists. PARSE_ARGUMENTS also creates a prefix_DEFAULT_ARGS variable containing
22 #the list of all arguments up to the first arg_name encountered.
23 #
24 #Here is a simple, albeit impractical, example of using PARSE_ARGUMENTS that
25 #demonstrates its behavior.
26 #
27 # SET(arguments
28 # hello OPTION3 world
29 # LIST3 foo bar
30 # OPTION2
31 # LIST1 fuz baz
32 # )
33 #
34 # PARSE_ARGUMENTS(ARG "LIST1;LIST2;LIST3" "OPTION1;OPTION2;OPTION3" ${arguments})
35 #
36 # PARSE_ARGUMENTS creates 7 variables and sets them as follows:
37 # ARG_DEFAULT_ARGS: hello;world
38 # ARG_LIST1: fuz;baz
39 # ARG_LIST2:
40 # ARG_LIST3: foo;bar
41 # ARG_OPTION1: FALSE
42 # ARG_OPTION2: TRUE
43 # ARG_OPTION3: TRUE
44 #
45 # If you don't have any options, use an empty string in its place.
46 # PARSE_ARGUMENTS(ARG "LIST1;LIST2;LIST3" "" ${arguments})
47 # Likewise if you have no lists.
48 # PARSE_ARGUMENTS(ARG "" "OPTION1;OPTION2;OPTION3" ${arguments})
49
50 MACRO(PARSE_ARGUMENTS prefix arg_names option_names)
51 SET(DEFAULT_ARGS)
52 FOREACH(arg_name ${arg_names})
53 SET(${prefix}_${arg_name})
54 ENDFOREACH(arg_name)
55 FOREACH(option ${option_names})
56 SET(${prefix}_${option} FALSE)
57 ENDFOREACH(option)
58
59 SET(current_arg_name DEFAULT_ARGS)
60 SET(current_arg_list)
61 FOREACH(arg ${ARGN})
62 SET(larg_names ${arg_names})
63 LIST(FIND larg_names "${arg}" is_arg_name)
64 IF (is_arg_name GREATER -1)
65 SET(${prefix}_${current_arg_name} ${current_arg_list})
66 SET(current_arg_name ${arg})
67 SET(current_arg_list)
68 ELSE (is_arg_name GREATER -1)
69 SET(loption_names ${option_names})
70 LIST(FIND loption_names "${arg}" is_option)
71 IF (is_option GREATER -1)
72 SET(${prefix}_${arg} TRUE)
73 ELSE (is_option GREATER -1)
74 SET(current_arg_list ${current_arg_list} ${arg})
75 ENDIF (is_option GREATER -1)
76 ENDIF (is_arg_name GREATER -1)
77 ENDFOREACH(arg)
78 SET(${prefix}_${current_arg_name} ${current_arg_list})
79 ENDMACRO(PARSE_ARGUMENTS)