Skip to content

Commit 1a2b276

Browse files
tejlmandde-nordic
authored andcommitted
[nrf fromtree] cmake: create Zephyr scope functions for variables
zephyr_get() supports multiple scopes when returning variables. Variables are returned based on the scope's priority. To facilitate creation of more scopes in Zephyr for variable handling additional functions are introduced. - zephyr_create_scope(<scope>) creates a new scope - zephyr_set(<var> <val>... SCOPE <scope>) set the value of a variable in the specified scope. Signed-off-by: Torsten Rasmussen <[email protected]> (cherry picked from commit a6d9105) (cherry picked from commit b8f7f3a)
1 parent b02e3cc commit 1a2b276

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

cmake/modules/extensions.cmake

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,52 @@ function(zephyr_get variable)
26222622
endif()
26232623
endfunction(zephyr_get variable)
26242624

2625+
# Usage:
2626+
# zephyr_create_scope(<scope>)
2627+
#
2628+
# Create a new scope for creation of scoped variables.
2629+
#
2630+
# <scope>: Name of new scope.
2631+
#
2632+
function(zephyr_create_scope scope)
2633+
if(TARGET ${scope}_scope)
2634+
message(FATAL_ERROR "zephyr_create_scope(${scope}) already exists.")
2635+
endif()
2636+
2637+
add_custom_target(${scope}_scope)
2638+
endfunction()
2639+
2640+
# Usage:
2641+
# zephyr_set(<variable> <value> SCOPE <scope> [APPEND])
2642+
#
2643+
# Zephyr extension of CMake set which allows a variable to be set in a specific
2644+
# scope. The scope is used on later zephyr_get() invocation for precedence
2645+
# handling when a variable it set in multiple scopes.
2646+
#
2647+
# <variable> : Name of variable
2648+
# <value> : Value of variable, multiple values will create a list.
2649+
# The SCOPE argument identifies the end of value list.
2650+
# SCOPE <scope>: Name of scope for the variable
2651+
# APPEND : Append values to the already existing variable in <scope>
2652+
#
2653+
function(zephyr_set variable)
2654+
cmake_parse_arguments(SET_VAR "APPEND" "SCOPE" "" ${ARGN})
2655+
2656+
zephyr_check_arguments_required_all(zephyr_set SET_VAR SCOPE)
2657+
2658+
if(NOT TARGET ${SET_VAR_SCOPE}_scope)
2659+
message(FATAL_ERROR "zephyr_set(... SCOPE ${SET_VAR_SCOPE}) doesn't exists.")
2660+
endif()
2661+
2662+
if(SET_VAR_APPEND)
2663+
set(property_args APPEND)
2664+
endif()
2665+
2666+
set_property(TARGET ${SET_VAR_SCOPE}_scope ${property_args}
2667+
PROPERTY ${variable} ${SET_VAR_UNPARSED_ARGUMENTS}
2668+
)
2669+
endfunction()
2670+
26252671
# Usage:
26262672
# zephyr_check_cache(<variable> [REQUIRED])
26272673
#

0 commit comments

Comments
 (0)