Skip to content

Commit 26a9604

Browse files
committed
[nrf fromtree] cmake: move script mode handling from package helper to extensions.cmake
Move Zephyr CMake script mode handling from package_helper.cmake into extensions.cmake. This ensures that all Zephyr CMake script which includes extensions.cmake will have the same functions stubbed or mocked and thus does not need to replicate this behavior. Signed-off-by: Torsten Rasmussen <[email protected]> (cherry picked from commit daac2d5)
1 parent 6a2cc75 commit 26a9604

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

cmake/modules/extensions.cmake

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ include(CheckCXXCompilerFlag)
3838
# 7.1 llext_* configuration functions
3939
# 7.2 add_llext_* build control functions
4040
# 7.3 llext helper functions
41+
# 8. Script mode handling
4142

4243
########################################################
4344
# 1. Zephyr-aware extensions
@@ -5744,3 +5745,38 @@ function(llext_filter_zephyr_flags filter flags outvar)
57445745

57455746
set(${outvar} ${zephyr_filtered_flags} PARENT_SCOPE)
57465747
endfunction()
5748+
5749+
########################################################
5750+
# 8. Script mode handling
5751+
########################################################
5752+
#
5753+
# Certain features are not available when CMake is used in script mode.
5754+
# For example custom targets, and thus features related to custom targets, such
5755+
# as target properties are not available in script mode.
5756+
#
5757+
# This section defines behavior for functions whose default implementation does
5758+
# not work correctly in script mode.
5759+
#
5760+
# The script mode function can be a simple stub or a more complex solution
5761+
# depending on the exact use of the function in script mode.
5762+
#
5763+
# Current Zephyr CMake scripts which includes `extensions.cmake` in script mode
5764+
# are: package_helper.cmake, verify-toolchain.cmake
5765+
#
5766+
5767+
if(CMAKE_SCRIPT_MODE_FILE)
5768+
# add_custom_target and set_target_properties are not supported in script mode.
5769+
# However, Zephyr CMake functions like `zephyr_get()`, `zephyr_create_scope()`,
5770+
# llext functions creates or relies on custom CMake targets.
5771+
function(add_custom_target)
5772+
# This silence the error: 'add_custom_target command is not scriptable'
5773+
endfunction()
5774+
5775+
function(set_target_properties)
5776+
# This silence the error: 'set_target_properties command is not scriptable'
5777+
endfunction()
5778+
5779+
function(zephyr_set variable)
5780+
# This silence the error: zephyr_set(... SCOPE <scope>) doesn't exists.
5781+
endfunction()
5782+
endif()

cmake/package_helper.cmake

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,6 @@
4444

4545
cmake_minimum_required(VERSION 3.20.5)
4646

47-
# add_custom_target and set_target_properties are not supported in script mode.
48-
# However, several Zephyr CMake modules create custom target for user convenience
49-
# like menuconfig, boards, shields, etc.
50-
# As we are not generating a build system with this tool, only running part of
51-
# the modules, then we simply override those functions to allow running those
52-
# modules.
53-
function(add_custom_target)
54-
# This silence the error: 'add_custom_target command is not scriptable'
55-
endfunction()
56-
57-
function(set_target_properties)
58-
# This silence the error: 'set_target_properties command is not scriptable'
59-
endfunction()
60-
6147
# Find last `-B` and `-S` instances.
6248
foreach(i RANGE ${CMAKE_ARGC})
6349
if(CMAKE_ARGV${i} MATCHES "^-B(.*)")
@@ -111,16 +97,5 @@ if(NOT DEFINED MODULES)
11197
)
11298
endif()
11399

114-
# Loading Zephyr CMake extension commands, which allows us to overload Zephyr
115-
# scoping rules.
116-
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE} COMPONENTS extensions)
117-
118-
# Zephyr scoping creates custom targets for handling of properties.
119-
# However, custom targets cannot be used in CMake script mode.
120-
# Therefore disable zephyr_set(... SCOPE ...) in package helper as it is not needed.
121-
function(zephyr_set variable)
122-
# This silence the error: zephyr_set(... SCOPE <scope>) doesn't exists.
123-
endfunction()
124-
125100
string(REPLACE ";" "," MODULES "${MODULES}")
126101
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE} COMPONENTS zephyr_default:${MODULES})

0 commit comments

Comments
 (0)