Skip to content

Commit 4e29a35

Browse files
tejlmandkartben
authored andcommitted
cmake: use GLOBAL property instead TARGET properties for scoping
Targets are not available in script mode. To support the Zephyr scoping feature used by snippets and yaml module then this commit moves from using custom targets to use GLOBAL properties for scopes. A scope property is prefixed with `<scope>:<property>` to avoid naming collisions. A `scope:<scope-name>` global property is used to track created scopes. Tracking valid scopes ensure that properties are only set on known scopes and thus catches typos / naming errors. Add zephyr_scope_exists() and zephyr_get_scoped() to abstract the implementation details of the scoped property retrieval and refactor current code to use them. Signed-off-by: Torsten Rasmussen <[email protected]> Signed-off-by: Luca Burelli <[email protected]>
1 parent 535c40d commit 4e29a35

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

cmake/modules/extensions.cmake

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3251,8 +3251,9 @@ function(zephyr_get variable)
32513251
set(sysbuild_global_${var})
32523252
endif()
32533253

3254-
if(TARGET snippets_scope)
3255-
get_property(snippets_${var} TARGET snippets_scope PROPERTY ${var})
3254+
zephyr_scope_exists(scope_defined snippets)
3255+
if(scope_defined)
3256+
zephyr_get_scoped(snippets_${var} snippets ${var})
32563257
endif()
32573258
endforeach()
32583259

@@ -3317,11 +3318,54 @@ endfunction(zephyr_get variable)
33173318
# <scope>: Name of new scope.
33183319
#
33193320
function(zephyr_create_scope scope)
3320-
if(TARGET ${scope}_scope)
3321+
zephyr_scope_exists(scope_defined ${scope})
3322+
if(scope_defined)
33213323
message(FATAL_ERROR "zephyr_create_scope(${scope}) already exists.")
33223324
endif()
33233325

3324-
add_custom_target(${scope}_scope)
3326+
set_property(GLOBAL PROPERTY scope:${scope} TRUE)
3327+
endfunction()
3328+
3329+
# Usage:
3330+
# zephyr_scope_exists(<result> <scope>)
3331+
#
3332+
# Check if <scope> exists.
3333+
#
3334+
# <result>: Variable to set with result.
3335+
# TRUE if scope exists, FALSE otherwise.
3336+
# <scope> : Name of scope.
3337+
#
3338+
function(zephyr_scope_exists result scope)
3339+
get_property(scope_defined GLOBAL PROPERTY scope:${scope})
3340+
if(scope_defined)
3341+
set(${result} TRUE PARENT_SCOPE)
3342+
else()
3343+
set(${result} FALSE PARENT_SCOPE)
3344+
endif()
3345+
endfunction()
3346+
3347+
# Usage:
3348+
# zephyr_get_scoped(<output> <scope> <var>)
3349+
#
3350+
# Get the current value of <var> in a specific <scope>, as defined by a
3351+
# previous zephyr_set() call. The value will be stored in the <output> var.
3352+
#
3353+
# <output> : Variable to store the value in
3354+
# <scope> : Scope for the variable look up
3355+
# <var> : Name to look up in the specific scope
3356+
#
3357+
function(zephyr_get_scoped output scope var)
3358+
zephyr_scope_exists(scope_defined ${scope})
3359+
if(NOT scope_defined)
3360+
message(FATAL_ERROR "zephyr_get_scoped(): scope ${scope} doesn't exists.")
3361+
endif()
3362+
3363+
get_property(value GLOBAL PROPERTY ${scope}_scope:${var})
3364+
if(DEFINED value)
3365+
set(${output} "${value}" PARENT_SCOPE)
3366+
else()
3367+
unset(${output} PARENT_SCOPE)
3368+
endif()
33253369
endfunction()
33263370

33273371
# Usage:
@@ -3342,16 +3386,17 @@ function(zephyr_set variable)
33423386

33433387
zephyr_check_arguments_required_all(zephyr_set SET_VAR SCOPE)
33443388

3345-
if(NOT TARGET ${SET_VAR_SCOPE}_scope)
3389+
zephyr_scope_exists(scope_defined ${SET_VAR_SCOPE})
3390+
if(NOT scope_defined)
33463391
message(FATAL_ERROR "zephyr_set(... SCOPE ${SET_VAR_SCOPE}) doesn't exists.")
33473392
endif()
33483393

33493394
if(SET_VAR_APPEND)
33503395
set(property_args APPEND)
33513396
endif()
33523397

3353-
set_property(TARGET ${SET_VAR_SCOPE}_scope ${property_args}
3354-
PROPERTY ${variable} ${SET_VAR_UNPARSED_ARGUMENTS}
3398+
set_property(GLOBAL ${property_args} PROPERTY
3399+
${SET_VAR_SCOPE}_scope:${variable} ${SET_VAR_UNPARSED_ARGUMENTS}
33553400
)
33563401
endfunction()
33573402

@@ -5886,16 +5931,11 @@ if(CMAKE_SCRIPT_MODE_FILE)
58865931
# This silence the error: 'set_target_properties command is not scriptable'
58875932
endfunction()
58885933

5889-
function(zephyr_set variable)
5890-
# This silence the error: zephyr_set(... SCOPE <scope>) doesn't exists.
5891-
endfunction()
5892-
58935934
# Build info creates a custom target for handling of build info.
58945935
# build_info is not needed in script mode but still called by Zephyr CMake
58955936
# modules. Therefore disable build_info(...) in when including
58965937
# extensions.cmake in script mode.
58975938
function(build_info)
5898-
# This silence the error: 'YAML context 'build_info' does not exist.'
5899-
# 'Remember to create a YAML context'
5939+
# This silence the error: 'Unknown CMake command "yaml_context"'
59005940
endfunction()
59015941
endif()

cmake/modules/yaml.cmake

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ function(yaml_context)
9393
)
9494
endif()
9595

96-
if(TARGET ${ARG_YAML_NAME}_scope)
96+
zephyr_scope_exists(scope_defined ${ARG_YAML_NAME})
97+
if(scope_defined)
9798
list(POP_FRONT ARG_YAML_UNPARSED_ARGUMENTS out-var)
9899
set(${out-var} TRUE PARENT_SCOPE)
99100
else()
@@ -183,7 +184,7 @@ function(yaml_get out_var)
183184
zephyr_check_arguments_required_all(${CMAKE_CURRENT_FUNCTION} ARG_YAML NAME KEY)
184185
internal_yaml_context_required(NAME ${ARG_YAML_NAME})
185186

186-
get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
187+
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
187188

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

227-
get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
228+
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
228229

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

265-
get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
266+
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
266267

267268
set(yaml_key_undefined ${ARG_YAML_KEY})
268269
foreach(k ${yaml_key_undefined})
@@ -335,7 +336,7 @@ function(yaml_remove)
335336
zephyr_check_arguments_required_all(${CMAKE_CURRENT_FUNCTION} ARG_YAML NAME KEY)
336337
internal_yaml_context_required(NAME ${ARG_YAML_NAME})
337338

338-
get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
339+
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
339340
string(JSON json_content REMOVE "${json_content}" ${ARG_YAML_KEY})
340341

341342
zephyr_set(JSON "${json_content}" SCOPE ${ARG_YAML_NAME})
@@ -359,18 +360,18 @@ function(yaml_save)
359360
zephyr_check_arguments_required(${CMAKE_CURRENT_FUNCTION} ARG_YAML NAME)
360361
internal_yaml_context_required(NAME ${ARG_YAML_NAME})
361362

362-
get_target_property(yaml_file ${ARG_YAML_NAME}_scope FILE)
363+
zephyr_get_scoped(yaml_file ${ARG_YAML_NAME} FILE)
363364
if(NOT yaml_file)
364365
zephyr_check_arguments_required(${CMAKE_CURRENT_FUNCTION} ARG_YAML FILE)
365366
endif()
366367

367-
get_property(json_content TARGET ${ARG_YAML_NAME}_scope PROPERTY JSON)
368+
zephyr_get_scoped(json_content ${ARG_YAML_NAME} JSON)
368369
to_yaml("${json_content}" 0 yaml_out)
369370

370371
if(DEFINED ARG_YAML_FILE)
371372
set(yaml_file ${ARG_YAML_FILE})
372373
else()
373-
get_property(yaml_file TARGET ${ARG_YAML_NAME}_scope PROPERTY FILE)
374+
zephyr_get_scoped(yaml_file ${ARG_YAML_NAME} FILE)
374375
endif()
375376
if(EXISTS ${yaml_file})
376377
FILE(RENAME ${yaml_file} ${yaml_file}.bak)

tests/cmake/zephyr_get/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ endfunction()
490490

491491

492492
function(test_snippets_scope)
493-
if(NOT TARGET snippets_scope)
493+
zephyr_scope_exists(snippets_defined snippets)
494+
if(NOT snippets_defined)
494495
zephyr_create_scope(snippets)
495496
endif()
496497

0 commit comments

Comments
 (0)