Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: add scope support to script mode #83045

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions cmake/modules/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3231,6 +3231,7 @@ function(zephyr_get variable)
# Keep current scope variables in internal variables.
# This is needed to properly handle cases where we want to check value against
# environment value or when appending with the MERGE operation.
zephyr_exists_scope(snippets_exists snippets)
foreach(var ${GET_VAR_VAR})
set(current_${var} ${${var}})
set(${var})
Expand All @@ -3251,10 +3252,11 @@ function(zephyr_get variable)
set(sysbuild_global_${var})
endif()

if(TARGET snippets_scope)
get_property(snippets_${var} TARGET snippets_scope PROPERTY ${var})
if(snippets_exists)
zephyr_get_scoped(snippets_${var} snippets ${var})
endif()
endforeach()
unset(snippets_exists)

set(${variable} "")
set(scopes "sysbuild_local;sysbuild_global;CACHE;snippets;ENV;current")
Expand Down Expand Up @@ -3317,13 +3319,54 @@ endfunction(zephyr_get variable)
# <scope>: Name of new scope.
#
function(zephyr_create_scope scope)
if(TARGET ${scope}_scope)
zephyr_exists_scope(scope_exists ${scope})
if(scope_exists)
message(FATAL_ERROR "zephyr_create_scope(${scope}) already exists.")
endif()

add_custom_target(${scope}_scope)
endfunction()

# Usage:
# zephyr_exists_scope(<output> <scope>)
#
# Checks if <scope> is defined. Sets <output> to TRUE or FALSE.
#
# <output> : Output variable containing the result of the check
# <scope> : Scope to look up
#
macro(zephyr_exists_scope output scope)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this as macro and not a function ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real need, will convert to set(... PARENT_SCOPE) instead.

if(TARGET ${scope}_scope)
set(${output} TRUE)
else()
set(${output} FALSE)
endif()
endmacro()

# Usage:
# zephyr_get_scoped(<output> <scope> <variable>)
#
# Get the current value of <variable> in a specific <scope>, as defined by a
# previous zephyr_set() call. The value will be stored in the <output> variable.
#
# <output> : Name of variable to store the value in
# <scope> : Scope for the variable look up
# <variable> : Name of variable to look up in the specific scope
#
function(zephyr_get_scoped output scope variable)
zephyr_exists_scope(scope_exists ${scope})
if(NOT scope_exists)
message(FATAL_ERROR "zephyr_get_scoped(): scope ${scope} doesn't exists.")
endif()

get_property(value TARGET ${scope}_scope PROPERTY ${variable})
if(DEFINED value)
set(${output} "${value}" PARENT_SCOPE)
else()
unset(${output} PARENT_SCOPE)
endif()
endfunction()

# Usage:
# zephyr_set(<variable> <value> SCOPE <scope> [APPEND])
#
Expand All @@ -3342,7 +3385,8 @@ function(zephyr_set variable)

zephyr_check_arguments_required_all(zephyr_set SET_VAR SCOPE)

if(NOT TARGET ${SET_VAR_SCOPE}_scope)
zephyr_exists_scope(scope_exists ${SET_VAR_SCOPE})
if(NOT scope_exists)
message(FATAL_ERROR "zephyr_set(... SCOPE ${SET_VAR_SCOPE}) doesn't exists.")
endif()

Expand Down
17 changes: 8 additions & 9 deletions cmake/modules/yaml.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ function(yaml_context)
)
endif()

if(TARGET ${ARG_YAML_NAME}_scope)
zephyr_exists_scope(scope_exists ${ARG_YAML_NAME})
if(scope_exists)
list(POP_FRONT ARG_YAML_UNPARSED_ARGUMENTS out-var)
set(${out-var} TRUE PARENT_SCOPE)
else()
Expand Down Expand Up @@ -183,7 +184,7 @@ function(yaml_get out_var)
zephyr_check_arguments_required_all(${CMAKE_CURRENT_FUNCTION} ARG_YAML NAME KEY)
internal_yaml_context_required(NAME ${ARG_YAML_NAME})

get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)

# We specify error variable to avoid a fatal error.
# If key is not found, then type becomes '-NOTFOUND' and value handling is done below.
Expand Down Expand Up @@ -224,7 +225,7 @@ function(yaml_length out_var)
zephyr_check_arguments_required_all(${CMAKE_CURRENT_FUNCTION} ARG_YAML NAME KEY)
internal_yaml_context_required(NAME ${ARG_YAML_NAME})

get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)

string(JSON type ERROR_VARIABLE error TYPE "${json_content}" ${ARG_YAML_KEY})
if(type STREQUAL ARRAY)
Expand Down Expand Up @@ -262,7 +263,7 @@ function(yaml_set)
zephyr_check_arguments_exclusive(${CMAKE_CURRENT_FUNCTION} ARG_YAML VALUE LIST)
internal_yaml_context_required(NAME ${ARG_YAML_NAME})

get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)

set(yaml_key_undefined ${ARG_YAML_KEY})
foreach(k ${yaml_key_undefined})
Expand Down Expand Up @@ -335,7 +336,7 @@ function(yaml_remove)
zephyr_check_arguments_required_all(${CMAKE_CURRENT_FUNCTION} ARG_YAML NAME KEY)
internal_yaml_context_required(NAME ${ARG_YAML_NAME})

get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
string(JSON json_content REMOVE "${json_content}" ${ARG_YAML_KEY})

zephyr_set(JSON "${json_content}" SCOPE ${ARG_YAML_NAME})
Expand All @@ -359,18 +360,16 @@ function(yaml_save)
zephyr_check_arguments_required(${CMAKE_CURRENT_FUNCTION} ARG_YAML NAME)
internal_yaml_context_required(NAME ${ARG_YAML_NAME})

get_target_property(yaml_file ${ARG_YAML_NAME}_scope FILE)
zephyr_get_scoped(yaml_file ${ARG_YAML_NAME} FILE)
if(NOT yaml_file)
zephyr_check_arguments_required(${CMAKE_CURRENT_FUNCTION} ARG_YAML FILE)
endif()

get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
to_yaml("${json_content}" 0 yaml_out)

if(DEFINED ARG_YAML_FILE)
set(yaml_file ${ARG_YAML_FILE})
else()
get_property(yaml_file TARGET ${ARG_YAML_NAME}_scope PROPERTY FILE)
endif()
if(EXISTS ${yaml_file})
FILE(RENAME ${yaml_file} ${yaml_file}.bak)
Expand Down