Skip to content

Commit 9da2766

Browse files
tejlmandnashif
authored andcommitted
cmake: add check_arguments_... macro to facilitate function writing
Add a new zephyr_check_arguments_required_allow_empty() macro for function argument validation. Zephyr already has a zephyr_check_arguments_required() for checking required arguments, like zephyr_check_arguments_required(foo_func <prefix> FOO BAR) which ensures that one of FOO or BAR are given in a call like: foo_func(BAR val) One limitation however, is that is some cases BAR may be allowed to be empty, so that it's still possible to know if FOO or BAR were supplied. In most case, BAR and FOO will have values following the keyword, like: foo_func(BAR my_bar_val) foo_func(FOO my_foo_val) but in cases where `my_bar_val` is a variable which may be empty, like: set(my_bar_val) foo_func(BAR ${my_bar_val}) # (expands to: foo_func(BAR) then BAR was actually supplied. To support functions where such empty expansion is allowed, then a new helper macro `zephyr_check_arguments_required_allow_empty()` has been implemented, as to be able to distinguish `foo_func()` from `foo_func(BAR)` when parsing arguments. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 2a6f151 commit 9da2766

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cmake/modules/extensions.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5168,6 +5168,24 @@ macro(zephyr_check_arguments_required function prefix)
51685168
set(check_defined)
51695169
endmacro()
51705170

5171+
#
5172+
# Helper macro for verifying that at least one of the required arguments has
5173+
# been provided by the caller. Arguments with empty values are allowed.
5174+
#
5175+
# A FATAL_ERROR will be raised if not one of the required arguments has been
5176+
# passed by the caller.
5177+
#
5178+
# Usage:
5179+
# zephyr_check_arguments_required_allow_empty(<function_name> <prefix> <arg1> [<arg2> ...])
5180+
#
5181+
macro(zephyr_check_arguments_required_allow_empty function prefix)
5182+
set(check_defined DEFINED)
5183+
set(allow_empty TRUE)
5184+
zephyr_check_flags_required(${function} ${prefix} ${ARGN})
5185+
set(allow_empty)
5186+
set(check_defined)
5187+
endmacro()
5188+
51715189
#
51725190
# Helper macro for verifying that at least one of the required flags has
51735191
# been provided by the caller.
@@ -5183,6 +5201,8 @@ macro(zephyr_check_flags_required function prefix)
51835201
foreach(required ${ARGN})
51845202
if(${check_defined} ${prefix}_${required})
51855203
set(required_found TRUE)
5204+
elseif("${allow_empty}" AND ${required} IN_LIST ${prefix}_KEYWORDS_MISSING_VALUES)
5205+
set(required_found TRUE)
51865206
endif()
51875207
endforeach()
51885208

0 commit comments

Comments
 (0)