Skip to content

Commit

Permalink
cmake: add script mode support to scope functions
Browse files Browse the repository at this point in the history
This patch adds support for scopes in CMake script mode, where the
concept of targets is not defined and therefore target properties are
not available.

To work around this limitation, the patch uses the SOURCE specifier to
emulate TARGET functionality in script mode. Since there is no if(SOURCE
...) equivalent in CMake language, a dummy property is created on the
virtual source file to support the zephyr_exists_scope() functionality.

Signed-off-by: Luca Burelli <[email protected]>
  • Loading branch information
pillo79 committed Dec 16, 2024
1 parent b14dc3b commit 4b914b7
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions cmake/modules/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3357,7 +3357,12 @@ function(zephyr_get_scoped output scope variable)
message(FATAL_ERROR "zephyr_get_scoped(): scope ${scope} doesn't exists.")
endif()

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

Expand Down Expand Up @@ -3388,7 +3393,12 @@ function(zephyr_set variable)
set(property_args APPEND)
endif()

set_property(TARGET ${SET_VAR_SCOPE}_scope ${property_args}
if(CMAKE_SCRIPT_MODE_FILE)
set(kind SOURCE)
else()
set(kind TARGET)
endif()
set_property(${kind} ${SET_VAR_SCOPE}_scope ${property_args}
PROPERTY ${variable} ${SET_VAR_UNPARSED_ARGUMENTS}
)
endfunction()
Expand Down Expand Up @@ -5909,10 +5919,6 @@ if(CMAKE_SCRIPT_MODE_FILE)
# This silence the error: 'set_target_properties command is not scriptable'
endfunction()

function(zephyr_set variable)
# This silence the error: zephyr_set(... SCOPE <scope>) doesn't exists.
endfunction()

# Build info creates a custom target for handling of build info.
# build_info is not needed in script mode but still called by Zephyr CMake
# modules. Therefore disable build_info(...) in when including
Expand All @@ -5921,4 +5927,27 @@ if(CMAKE_SCRIPT_MODE_FILE)
# This silence the error: 'YAML context 'build_info' does not exist.'
# 'Remember to create a YAML context'
endfunction()

# Script mode version, using SOURCE properties
function(zephyr_create_scope scope)
zephyr_exists_scope(scope_exists ${scope})
if(scope_exists)
message(FATAL_ERROR "zephyr_create_scope(${scope}) already exists.")
endif()

set_property(SOURCE ${scope}_scope PROPERTY ZEPHYR_SCOPE TRUE)
endfunction()

# Script mode version, using SOURCE properties
macro(zephyr_exists_scope output scope)
get_property(zephyr_exists_scope_value SOURCE ${scope}_scope PROPERTY ZEPHYR_SCOPE)
if(DEFINED zephyr_exists_scope_value)
unset(zephyr_exists_scope_value)
set(${output} TRUE)
else()
set(${output} FALSE)
endif()
endmacro()

# zephyr_get_scoped and zephyr_set support script mode as well
endif()

0 comments on commit 4b914b7

Please sign in to comment.