CMakeMacroParseArguments: Difference between revisions
No edit summary |
|||
Line 2: | Line 2: | ||
== Definition == | == Definition == | ||
There is a general convention for CMake commands that take optional flags and/or variable arguments. Optional flags are all caps and are added to the arguments to turn on. Variable arguments have an all caps identifier to determine where each variable argument list starts. The PARSE_ARGUMENTS macro, defined below, can be used by other macros to parse arguments defined in this way. Note that this macro relies on the [[CMakeMacroListOperations#LIST_CONTAINS | LIST_CONTAINS]] command. | |||
<pre> | |||
MACRO(PARSE_ARGUMENTS prefix arg_names option_names) | |||
SET(DEFAULT_ARGS) | |||
FOREACH(arg_name ${arg_names}) | |||
SET(${prefix}_${arg_name}) | |||
ENDFOREACH(arg_name) | |||
FOREACH(option ${option_names}) | |||
SET(${prefix}_${option} FALSE) | |||
ENDFOREACH(option) | |||
SET(current_arg_name DEFAULT_ARGS) | |||
SET(current_arg_list) | |||
FOREACH(arg ${ARGN}) | |||
LIST_CONTAINS(is_arg_name ${arg} ${arg_names}) | |||
IF (is_arg_name) | |||
SET(${prefix}_${current_arg_name} ${current_arg_list}) | |||
SET(current_arg_name ${arg}) | |||
SET(current_arg_list) | |||
ELSE (is_arg_name) | |||
LIST_CONTAINS(is_option ${arg} ${option_names}) | |||
IF (is_option) | |||
SET(${prefix}_${arg} TRUE) | |||
ELSE (is_option) | |||
SET(current_arg_list ${current_arg_list} ${arg}) | |||
ENDIF (is_option) | |||
ENDIF (is_arg_name) | |||
ENDFOREACH(arg) | |||
SET(${prefix}_${current_arg_name} ${current_arg_list}) | |||
ENDMACRO(PARSE_ARGUMENTS) | |||
</pre> | |||
The PARSE_ARGUMENTS macro will take the arguments of another macro and define several variables. The first argument to PARSE_ARGUMENTS is a prefix to put on all variables it creates. The second argument is a list of names, and the third argument is a list of options. Both of these lists should be quoted. The rest of PARSE_ARGUMENTS are arguments from another macro to be parsed. | |||
<tt> | |||
:PARSE_ARGUMENTS(''prefix'' ''arg_names'' ''options'' ''arg1'' ''arg2''...) | |||
</tt> | |||
For each item in ''options'', PARSE_ARGUMENTS will create a variable with that name, prefixed with <tt>''prefix''_</tt>. So, for example, if ''prefix'' is <tt>MY_MACRO</tt> and ''options'' is <tt>OPTION1;OPTION2</tt>, then PARSE_ARGUMENTS will create the variables MY_MACRO_OPTION1 and MY_MACRO_OPTION2. These variables will be set to true if the option exists in the command line or false otherwise. | |||
For each item in ''arg_names'', PARSE_ARGUMENTS will create a variable with that name, prefixed with <tt>''prefix''_</tt>. Each variable will be filled with the arguments that occur after the given arg_name is encountered up to the next arg_name or the end of the arguments. All options are removed from these lists. PARSE_ARGUMENTS also creates a <tt>''prefix''_DEFAULT_ARGS</tt> variable containing the list of all arguments up to the first arg_name encountered. | |||
== Usage == | == Usage == |
Revision as of 15:36, 2 March 2006
Definition
There is a general convention for CMake commands that take optional flags and/or variable arguments. Optional flags are all caps and are added to the arguments to turn on. Variable arguments have an all caps identifier to determine where each variable argument list starts. The PARSE_ARGUMENTS macro, defined below, can be used by other macros to parse arguments defined in this way. Note that this macro relies on the LIST_CONTAINS command.
MACRO(PARSE_ARGUMENTS prefix arg_names option_names) SET(DEFAULT_ARGS) FOREACH(arg_name ${arg_names}) SET(${prefix}_${arg_name}) ENDFOREACH(arg_name) FOREACH(option ${option_names}) SET(${prefix}_${option} FALSE) ENDFOREACH(option) SET(current_arg_name DEFAULT_ARGS) SET(current_arg_list) FOREACH(arg ${ARGN}) LIST_CONTAINS(is_arg_name ${arg} ${arg_names}) IF (is_arg_name) SET(${prefix}_${current_arg_name} ${current_arg_list}) SET(current_arg_name ${arg}) SET(current_arg_list) ELSE (is_arg_name) LIST_CONTAINS(is_option ${arg} ${option_names}) IF (is_option) SET(${prefix}_${arg} TRUE) ELSE (is_option) SET(current_arg_list ${current_arg_list} ${arg}) ENDIF (is_option) ENDIF (is_arg_name) ENDFOREACH(arg) SET(${prefix}_${current_arg_name} ${current_arg_list}) ENDMACRO(PARSE_ARGUMENTS)
The PARSE_ARGUMENTS macro will take the arguments of another macro and define several variables. The first argument to PARSE_ARGUMENTS is a prefix to put on all variables it creates. The second argument is a list of names, and the third argument is a list of options. Both of these lists should be quoted. The rest of PARSE_ARGUMENTS are arguments from another macro to be parsed.
- PARSE_ARGUMENTS(prefix arg_names options arg1 arg2...)
For each item in options, PARSE_ARGUMENTS will create a variable with that name, prefixed with prefix_. So, for example, if prefix is MY_MACRO and options is OPTION1;OPTION2, then PARSE_ARGUMENTS will create the variables MY_MACRO_OPTION1 and MY_MACRO_OPTION2. These variables will be set to true if the option exists in the command line or false otherwise.
For each item in arg_names, PARSE_ARGUMENTS will create a variable with that name, prefixed with prefix_. Each variable will be filled with the arguments that occur after the given arg_name is encountered up to the next arg_name or the end of the arguments. All options are removed from these lists. PARSE_ARGUMENTS also creates a prefix_DEFAULT_ARGS variable containing the list of all arguments up to the first arg_name encountered.
Usage
Example