Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit f4ab16c

Browse files
committed
Added utility-helper function to consume reserved arguments before parsing "unlimited" arguments, such as sources.
1 parent 4e3a9e3 commit f4ab16c

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

cmake/Platform/Libraries/LibrariesFinder.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ function(find_arduino_library _target_name _library_name _board_id)
3737

3838
if (parsed_args_HEADER_ONLY)
3939
add_arduino_header_only_library(${_target_name} ${_board_id} ${library_headers})
40+
4041
else ()
42+
4143
find_library_source_files("${library_path}" library_sources)
4244

4345
if (NOT library_sources)
@@ -48,8 +50,9 @@ function(find_arduino_library _target_name _library_name _board_id)
4850
else ()
4951
set(sources ${library_headers} ${library_sources})
5052

51-
add_arduino_library(${_target_name} ${_board_id} ${sources}
52-
LIB_PROPS_FILE ${library_properties_file})
53+
add_arduino_library(${_target_name} ${_board_id}
54+
LIB_PROPS_FILE ${library_properties_file}
55+
${sources})
5356
endif ()
5457

5558
endif ()

cmake/Platform/Utilities/CMakeArgumentsUtils.cmake

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
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

Comments
 (0)