From 100befc70c74f7ec83dd8ac3171ee18eeddb4dbd Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 28 May 2024 11:47:41 +0200 Subject: [PATCH] [nrf fromtree] sysbuild: introduce sysbuild_root for root adjustment Fixes: #73066 Introduce sysbuild_root CMake module similar to the Zephyr root CMake module. The sysbuild_root CMake module works similar to existing root module, but with the difference that root paths are adjusted relative to APP_DIR which is the main image, instead of relative to sysbuild. Signed-off-by: Torsten Rasmussen (cherry picked from commit 24acc7d43ddeb55d036b293c4a0ca37a8d5eb4c2) --- cmake/modules/extensions.cmake | 12 +++- cmake/modules/root.cmake | 1 + .../cmake/modules/sysbuild_default.cmake | 2 +- .../cmake/modules/sysbuild_extensions.cmake | 10 --- .../cmake/modules/sysbuild_root.cmake | 63 +++++++++++++++++++ 5 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 share/sysbuild/cmake/modules/sysbuild_root.cmake diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index 20dc01cbb38..52bb28b0b47 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -2534,6 +2534,8 @@ endfunction() # to absolute path, relative from `APPLICATION_SOURCE_DIR` # Issue an error for any relative path not specified # by user with `-D` +# BASE_DIR : convert paths relative to +# instead of `APPLICATION_SOURCE_DIR` # # returns an updated list of absolute paths # @@ -2587,7 +2589,7 @@ Please provide one of following: APPLICATION_ROOT, CONF_FILES") endif() if(${ARGV0} STREQUAL APPLICATION_ROOT) - set(single_args APPLICATION_ROOT) + set(single_args APPLICATION_ROOT BASE_DIR) elseif(${ARGV0} STREQUAL CONF_FILES) set(options QUALIFIERS REQUIRED) set(single_args BOARD BOARD_REVISION BOARD_QUALIFIERS DTS KCONF DEFCONFIG BUILD SUFFIX) @@ -2600,6 +2602,10 @@ Please provide one of following: APPLICATION_ROOT, CONF_FILES") endif() if(ZFILE_APPLICATION_ROOT) + if(NOT DEFINED ZFILE_BASE_DIR) + set(ZFILE_BASE_DIR ${APPLICATION_SOURCE_DIR}) + endif() + # Note: user can do: `-D=` and app can at same # time specify `list(APPEND )` # Thus need to check and update only CACHED variables (-D). @@ -2610,7 +2616,7 @@ Please provide one of following: APPLICATION_ROOT, CONF_FILES") # path from `APPLICATION_SOURCE_DIR`. if(NOT IS_ABSOLUTE ${path}) list(FIND ${ZFILE_APPLICATION_ROOT} ${path} index) - cmake_path(ABSOLUTE_PATH path BASE_DIRECTORY ${APPLICATION_SOURCE_DIR} NORMALIZE) + cmake_path(ABSOLUTE_PATH path BASE_DIRECTORY ${ZFILE_BASE_DIR} NORMALIZE) if(NOT ${index} LESS 0) list(REMOVE_AT ${ZFILE_APPLICATION_ROOT} ${index}) list(INSERT ${ZFILE_APPLICATION_ROOT} ${index} ${path}) @@ -2630,7 +2636,7 @@ Relative paths are only allowed with `-D${ARGV1}=`") endif() endforeach() - list(REMOVE_DUPLICATES ZFILE_APPLICATION_ROOT) + list(REMOVE_DUPLICATES ${ZFILE_APPLICATION_ROOT}) # This updates the provided argument in parent scope (callers scope) set(${ZFILE_APPLICATION_ROOT} ${${ZFILE_APPLICATION_ROOT}} PARENT_SCOPE) endif() diff --git a/cmake/modules/root.cmake b/cmake/modules/root.cmake index 0f4d5e1437b..e14b853831f 100644 --- a/cmake/modules/root.cmake +++ b/cmake/modules/root.cmake @@ -12,6 +12,7 @@ # - SOC_ROOT: CMake list of SoC roots containing SoC implementations # - BOARD_ROOT: CMake list of board roots containing board and shield implementations # - MODULE_EXT_ROOT: CMake list of module external roots containing module glue code +# - SCA_ROOT: CMake list of SCA roots containing static code analysis integration code # # If a root is defined it will check the list of paths in the root and convert # any relative path to absolute path and update the root list. diff --git a/share/sysbuild/cmake/modules/sysbuild_default.cmake b/share/sysbuild/cmake/modules/sysbuild_default.cmake index 6bcf714de95..66ffb7374b3 100644 --- a/share/sysbuild/cmake/modules/sysbuild_default.cmake +++ b/share/sysbuild/cmake/modules/sysbuild_default.cmake @@ -9,7 +9,7 @@ include(extensions) include(sysbuild_extensions) include(python) include(west) -include(root) +include(sysbuild_root) include(zephyr_module) include(boards) include(shields) diff --git a/share/sysbuild/cmake/modules/sysbuild_extensions.cmake b/share/sysbuild/cmake/modules/sysbuild_extensions.cmake index 28d08cc5857..c20fcdb1e51 100644 --- a/share/sysbuild/cmake/modules/sysbuild_extensions.cmake +++ b/share/sysbuild/cmake/modules/sysbuild_extensions.cmake @@ -460,16 +460,6 @@ function(ExternalZephyrProject_Cmake) PROPERTY IMAGE_CONF_SCRIPT ) - # Update ROOT variables with relative paths to use absolute paths based on - # the source application directory. - foreach(type MODULE_EXT BOARD SOC ARCH SCA) - if(DEFINED CACHE{${type}_ROOT} AND NOT IS_ABSOLUTE $CACHE{${type}_ROOT}) - set(rel_path $CACHE{${type}_ROOT}) - cmake_path(ABSOLUTE_PATH rel_path BASE_DIRECTORY "${APP_DIR}" NORMALIZE OUTPUT_VARIABLE abs_path) - set(${type}_ROOT ${abs_path} CACHE PATH "Sysbuild adjusted absolute path" FORCE) - endif() - endforeach() - sysbuild_cache(CREATE APPLICATION ${ZCMAKE_APPLICATION}) foreach(script ${${ZCMAKE_APPLICATION}_CONF_SCRIPT}) diff --git a/share/sysbuild/cmake/modules/sysbuild_root.cmake b/share/sysbuild/cmake/modules/sysbuild_root.cmake new file mode 100644 index 00000000000..ca9f37238cf --- /dev/null +++ b/share/sysbuild/cmake/modules/sysbuild_root.cmake @@ -0,0 +1,63 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2024, Nordic Semiconductor ASA + +# Convert Zephyr roots to absolute paths to be used by sysbuild. +# +# This CMake module will convert all relative paths in existing ROOT lists to +# absolute path relative from APP_DIR. +# +# Optional variables: +# - ARCH_ROOT: CMake list of arch roots containing arch implementations +# - SOC_ROOT: CMake list of SoC roots containing SoC implementations +# - BOARD_ROOT: CMake list of board roots containing board and shield implementations +# - MODULE_EXT_ROOT: CMake list of module external roots containing module glue code +# - SCA_ROOT: CMake list of SCA roots containing static code analysis integration code +# +# If a root is defined it will check the list of paths in the root and convert +# any relative path to absolute path and update the root list. +# If a root is undefined it will still be undefined when this module has loaded. +# +# Converted paths are placed in the CMake cache so that they are propagated +# correctly to image builds. + +include_guard(GLOBAL) + +include(extensions) + +# Merge in variables from other sources +zephyr_get(MODULE_EXT_ROOT MERGE) +zephyr_get(BOARD_ROOT MERGE) +zephyr_get(SOC_ROOT MERGE) +zephyr_get(ARCH_ROOT MERGE) +zephyr_get(SCA_ROOT MERGE) + +# Convert paths to absolute, relative from APP_DIR +zephyr_file(APPLICATION_ROOT MODULE_EXT_ROOT BASE_DIR ${APP_DIR}) +zephyr_file(APPLICATION_ROOT BOARD_ROOT BASE_DIR ${APP_DIR}) +zephyr_file(APPLICATION_ROOT SOC_ROOT BASE_DIR ${APP_DIR}) +zephyr_file(APPLICATION_ROOT ARCH_ROOT BASE_DIR ${APP_DIR}) +zephyr_file(APPLICATION_ROOT SCA_ROOT BASE_DIR ${APP_DIR}) + +# Sysbuild must ensure any locally defined variables in sysbuild/CMakeLists.txt +# have been added to the cache in order for the settings to propagate to images. +# note: zephyr_file has removed any list duplicates +if(DEFINED MODULE_EXT_ROOT) + set(MODULE_EXT_ROOT ${MODULE_EXT_ROOT} CACHE PATH "Sysbuild adjusted MODULE_EXT_ROOT" FORCE) +endif() + +if(DEFINED BOARD_ROOT) + set(BOARD_ROOT ${BOARD_ROOT} CACHE PATH "Sysbuild adjusted BOARD_ROOT" FORCE) +endif() + +if(DEFINED SOC_ROOT) + set(SOC_ROOT ${SOC_ROOT} CACHE PATH "Sysbuild adjusted SOC_ROOT" FORCE) +endif() + +if(DEFINED ARCH_ROOT) + set(ARCH_ROOT ${ARCH_ROOT} CACHE PATH "Sysbuild adjusted ARCH_ROOT" FORCE) +endif() + +if(DEFINED SCA_ROOT) + set(SCA_ROOT ${SCA_ROOT} CACHE PATH "Sysbuild adjusted SCA_ROOT" FORCE) +endif()