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