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

Commit 83ca465

Browse files
committed
Added utility function to parse sources from cmake's unparsed arguments.
It allows functions to accept practically unlimited sources (or any other arbitrary value) as long as any reserved arguments/options aren't encountered. When at least one is encountered, processing stops and the already-parsed arguments are returned.
1 parent ee41ec7 commit 83ca465

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

cmake/Platform/Utilities/CMakeArgumentsUtils.cmake

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
#=============================================================================#
2+
# Parses the given arguments for sources, stopping when all arguments have been read or
3+
# when at least one reserved argument/option has been encountered.
4+
# _reserved_options - Reserved option arguments.
5+
# _reserved_single_values - Reserved single-value arguments.
6+
# _reserved_multi_values - Reserved multi-value arguments.
7+
# _cmake_args - Arguments to parse. Usually function's unparsed arguments - ${ARGN}.
8+
# _return_var - Name of a CMake variable that will hold the extraction result.
9+
# Returns - Parsed sources.
10+
#=============================================================================#
11+
function(parse_sources_arguments _return_var _reserved_options _reserved_single_values
12+
_reserved_multi_values _cmake_args)
13+
14+
# Prepare arguments for further inspection - Somewhat croocked logic due to CMake's limitations
15+
# If an argument is an empty string, populate it with a theoritcally impossible value.
16+
# just to have some value in it
17+
if ("${_reserved_options}" STREQUAL "")
18+
set(_reserved_options "+-*/")
19+
endif ()
20+
if ("${_reserved_single_values}" STREQUAL "")
21+
set(_reserved_single_values "+-*/")
22+
endif ()
23+
if ("${_reserved_multi_values}" STREQUAL "")
24+
set(_reserved_multi_values "+-*/")
25+
endif ()
26+
27+
set(sources "") # Clear list because cmake preserves scope in nested functions
28+
29+
foreach (arg ${_cmake_args})
30+
31+
if (${arg} IN_LIST _reserved_options OR
32+
${arg} IN_LIST _reserved_single_values OR
33+
${arg} IN_LIST _reserved_multi_values)
34+
break()
35+
else ()
36+
list(APPEND sources ${arg})
37+
endif ()
38+
39+
endforeach ()
40+
41+
set(${_return_var} ${sources} PARENT_SCOPE)
42+
43+
endfunction()
44+
145
#=============================================================================#
246
# Parses the given arguments for scope-controlling arguments, which are PUBLIC|INTERFACE|PRIVATE.
347
# If none is found, CMake generates an error, unless - The DEFAULT_SCOPE argument is passed.

0 commit comments

Comments
 (0)