Skip to content

Commit

Permalink
cmake: add reverse option to zephyr_get(... MERGE)
Browse files Browse the repository at this point in the history
Adding the possibility to have the list returned in reversed order when
using `zephyr_get(... MERGE).

`zephyr_get(... MERGE)` creates a list which populates the content based
on variable settings in the following scopes, in this order:
sysbuild, cache, environment, local.

This works well for lists where content first in list has highest
precedence, such as ROOTs settings.
However, for settings where the value last in the list will overwrite
values earlier in the list, we want the list to be reversed, examples
of such can be CONF_FILE, OVERLAY_CONFIG, DTC_OVERLAY_FILE, where the
content of the file last in the list will overrule the content from an
earlier file.
So to ensure that a DTC_OVERLAY_FILE defined as cache takes precedence
over an env or local scope variable the possibility of reversing the
list must be available.

Signed-off-by: Torsten Rasmussen <[email protected]>
  • Loading branch information
tejlmand authored and fabiobaltieri committed May 25, 2023
1 parent 39b50ee commit fd526cc
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmake/modules/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2493,10 +2493,13 @@ function(zephyr_var_name variable scope out)
endfunction()

# Usage:
# zephyr_get(<variable> [MERGE] [SYSBUILD [LOCAL|GLOBAL]] [VAR <var1> ...])
# zephyr_get(<variable> [MERGE [REVERSE]] [SYSBUILD [LOCAL|GLOBAL]] [VAR <var1> ...])
#
# Return the value of <variable> as local scoped variable of same name. If MERGE
# is supplied, will return a list of found items.
# is supplied, will return a list of found items. If REVERSE is supplied
# together with MERGE, the order of the list will be reversed before being
# returned. Reverse will happen before the list is returned and hence it will
# not change the order of precedence in which the list itself is constructed.
#
# VAR can be used either to store the result in a variable with a different
# name, or to look for values from multiple variables.
Expand Down Expand Up @@ -2526,7 +2529,7 @@ endfunction()
# using `-DZEPHYR_TOOLCHAIN_VARIANT=<val>`, then the value from the cache is
# returned.
function(zephyr_get variable)
cmake_parse_arguments(GET_VAR "MERGE" "SYSBUILD" "VAR" ${ARGN})
cmake_parse_arguments(GET_VAR "MERGE;REVERSE" "SYSBUILD" "VAR" ${ARGN})

if(DEFINED GET_VAR_SYSBUILD)
if(NOT ("${GET_VAR_SYSBUILD}" STREQUAL "GLOBAL" OR
Expand All @@ -2538,6 +2541,10 @@ function(zephyr_get variable)
set(GET_VAR_SYSBUILD "GLOBAL")
endif()

if(GET_VAR_REVERSE AND NOT GET_VAR_MERGE)
message(FATAL_ERROR "zephyr_get(... REVERSE) missing a required argument: MERGE")
endif()

if(NOT DEFINED GET_VAR_VAR)
set(GET_VAR_VAR ${variable})
endif()
Expand All @@ -2564,6 +2571,9 @@ function(zephyr_get variable)
endforeach()

set(scopes "sysbuild;CACHE;ENV;current")
if(GET_VAR_REVERSE)
list(REVERSE scopes)
endif()
foreach(scope IN LISTS scopes)
foreach(var ${GET_VAR_VAR})
zephyr_var_name("${var}" "${scope}" expansion_var)
Expand Down Expand Up @@ -2601,7 +2611,13 @@ function(zephyr_get variable)
endforeach()

if(GET_VAR_MERGE)
list(REMOVE_DUPLICATES ${variable})
if(GET_VAR_REVERSE)
list(REVERSE ${variable})
list(REMOVE_DUPLICATES ${variable})
list(REVERSE ${variable})
else()
list(REMOVE_DUPLICATES ${variable})
endif()
set(${variable} ${${variable}} PARENT_SCOPE)
endif()
endfunction(zephyr_get variable)
Expand Down

0 comments on commit fd526cc

Please sign in to comment.