From daac2d55ab13039d68548dec030073ffcee6a71e Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Mon, 30 Sep 2024 21:53:51 +0200 Subject: [PATCH] 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 --- cmake/modules/extensions.cmake | 36 ++++++++++++++++++++++++++++++++++ cmake/package_helper.cmake | 25 ----------------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index 2ac8c20058b..e45b4c60237 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -38,6 +38,7 @@ include(CheckCXXCompilerFlag) # 7.1 llext_* configuration functions # 7.2 add_llext_* build control functions # 7.3 llext helper functions +# 8. Script mode handling ######################################################## # 1. Zephyr-aware extensions @@ -5759,3 +5760,38 @@ function(llext_filter_zephyr_flags filter flags outvar) set(${outvar} ${zephyr_filtered_flags} PARENT_SCOPE) endfunction() + +######################################################## +# 8. Script mode handling +######################################################## +# +# Certain features are not available when CMake is used in script mode. +# For example custom targets, and thus features related to custom targets, such +# as target properties are not available in script mode. +# +# This section defines behavior for functions whose default implementation does +# not work correctly in script mode. +# +# The script mode function can be a simple stub or a more complex solution +# depending on the exact use of the function in script mode. +# +# Current Zephyr CMake scripts which includes `extensions.cmake` in script mode +# are: package_helper.cmake, verify-toolchain.cmake +# + +if(CMAKE_SCRIPT_MODE_FILE) + # add_custom_target and set_target_properties are not supported in script mode. + # However, Zephyr CMake functions like `zephyr_get()`, `zephyr_create_scope()`, + # llext functions creates or relies on custom CMake targets. + function(add_custom_target) + # This silence the error: 'add_custom_target command is not scriptable' + endfunction() + + function(set_target_properties) + # This silence the error: 'set_target_properties command is not scriptable' + endfunction() + + function(zephyr_set variable) + # This silence the error: zephyr_set(... SCOPE ) doesn't exists. + endfunction() +endif() diff --git a/cmake/package_helper.cmake b/cmake/package_helper.cmake index 55412b94110..886dbfff71d 100644 --- a/cmake/package_helper.cmake +++ b/cmake/package_helper.cmake @@ -44,20 +44,6 @@ cmake_minimum_required(VERSION 3.20.5) -# add_custom_target and set_target_properties are not supported in script mode. -# However, several Zephyr CMake modules create custom target for user convenience -# like menuconfig, boards, shields, etc. -# As we are not generating a build system with this tool, only running part of -# the modules, then we simply override those functions to allow running those -# modules. -function(add_custom_target) - # This silence the error: 'add_custom_target command is not scriptable' -endfunction() - -function(set_target_properties) - # This silence the error: 'set_target_properties command is not scriptable' -endfunction() - # Find last `-B` and `-S` instances. foreach(i RANGE ${CMAKE_ARGC}) if(CMAKE_ARGV${i} MATCHES "^-B(.*)") @@ -111,16 +97,5 @@ if(NOT DEFINED MODULES) ) endif() -# Loading Zephyr CMake extension commands, which allows us to overload Zephyr -# scoping rules. -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE} COMPONENTS extensions) - -# Zephyr scoping creates custom targets for handling of properties. -# However, custom targets cannot be used in CMake script mode. -# Therefore disable zephyr_set(... SCOPE ...) in package helper as it is not needed. -function(zephyr_set variable) - # This silence the error: zephyr_set(... SCOPE ) doesn't exists. -endfunction() - string(REPLACE ";" "," MODULES "${MODULES}") find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE} COMPONENTS zephyr_default:${MODULES})