1+ #=============================================================================#
2+ # Consumes given arguments for reserved options and single-value options,
3+ # returning a new argument-list without them, no matter where they are positioned.
4+ # It allows easy parsing of arguments which are considered "unlimited",
5+ # limited only by multi-value options.
6+ # _args - Arguments to consume. Usually function's unparsed arguments - ${ARGN}.
7+ # _reserved_options - Reserved option arguments.
8+ # _reserved_single_values - Reserved single-value arguments.
9+ # _return_var - Name of a CMake variable that will hold the extraction result.
10+ # Returns - Original argument-list without reserved options and single-value options.
11+ #=============================================================================#
12+ function (_consume_reserved_arguments _args _reserved_options _reserved_single_values _return_var)
13+
14+ set (temp_arg_list ${_args} )
15+
16+ list (LENGTH _args args_length)
17+ decrement_integer(args_length 1) # We'll peform index iteration - It's always length-1
18+
19+ foreach (index RANGE ${args_length} )
20+
21+ list (GET _args ${index} arg)
22+
23+ if (${arg} IN_LIST _reserved_options)
24+ list (REMOVE_ITEM temp_arg_list ${arg} )
25+
26+ elseif (${arg} IN_LIST _reserved_single_values)
27+
28+ # Get the next index to remove as well - It's the option's/key's value
29+ set (next_index ${index} )
30+ increment_integer(next_index 1)
31+
32+ list (REMOVE_AT temp_arg_list ${index} ${next_index} )
33+
34+ endif ()
35+
36+ endforeach ()
37+
38+ set (${_return_var} ${temp_arg_list} PARENT_SCOPE)
39+
40+ endfunction ()
41+
142#=============================================================================#
243# Parses the given arguments for sources, stopping when all arguments have been read or
344# when at least one reserved argument/option has been encountered.
@@ -16,13 +57,15 @@ function(parse_sources_arguments _return_var _reserved_options _reserved_single_
1657 initialize_list(_reserved_single_values)
1758 initialize_list(_reserved_multi_values)
1859
60+ _consume_reserved_arguments("${_cmake_args} "
61+ "${_reserved_options} " "${_reserved_single_values} "
62+ consumed_args)
63+
1964 set (sources "" ) # Clear list because cmake preserves scope in nested functions
2065
21- foreach (arg ${_cmake_args } )
66+ foreach (arg ${consumed_args } )
2267
23- if (${arg} IN_LIST _reserved_options OR
24- ${arg} IN_LIST _reserved_single_values OR
25- ${arg} IN_LIST _reserved_multi_values)
68+ if (${arg} IN_LIST _reserved_multi_values)
2669 break ()
2770 else ()
2871 list (APPEND sources ${arg} )
0 commit comments