From f96ee77c7cbf08b49651368f3f1b7ae9b52ac9e7 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 1 Mar 2022 10:32:23 +0100 Subject: [PATCH] cmake: function to update Zephyr_DIR when loading old Zephyr packages Fixes: #43094 This commit introduces a function which updates Zephyr_DIR to point to the directory of the Zephyr package being loaded. For Zephyr 3.0 and earlier, the Zephyr_DIR might in some cases be `Zephyr_DIR-NOTFOUND` or pointing to the Zephyr package including the boilerplate code instead of the Zephyr package of the included boilerplate code. This code ensures that when a package is loaded then Zephyr_DIR will point correctly. This ensures that when Zephyr releases <=3.0 is loaded, then Zephyr_DIR will point correctly, see more in #43094. Old style Zephyr package will in some cases load boilerplate.cmake directly so to ensure proper behavior, restrict boilerplate uses of `find_package(Zephyr)` to not use default search path, but allow only the current Zephyr. Of the same reason, only print warning if Zephyr_DIR is not defined as this indicates old style inclusion. Signed-off-by: Torsten Rasmussen --- cmake/app/boilerplate.cmake | 13 +++++++++---- .../cmake/zephyr_package_search.cmake | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cmake/app/boilerplate.cmake b/cmake/app/boilerplate.cmake index 72aba79600a..0e09c98b621 100644 --- a/cmake/app/boilerplate.cmake +++ b/cmake/app/boilerplate.cmake @@ -13,8 +13,13 @@ # inside the Zephyr repository. # # Loading of this file directly is deprecated and only kept for backward compatibility. -message(WARNING "Loading of Zephyr boilerplate.cmake directly is deprecated, " - "please use 'find_package(Zephyr)'" -) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +if(NOT DEFINED Zephyr_DIR) + # When `find_package(Zephyr)` is used then `Zephyr_DIR` is defined, else + # old style inclusion is used. Warning is only printed in first invocation. + message(WARNING "Loading of Zephyr boilerplate.cmake directly is deprecated, " + "please use 'find_package(Zephyr)'" + ) +endif() + +find_package(Zephyr REQUIRED PATHS ${CMAKE_CURRENT_LIST_DIR}/../.. NO_DEFAULT_PATH) diff --git a/share/zephyr-package/cmake/zephyr_package_search.cmake b/share/zephyr-package/cmake/zephyr_package_search.cmake index 8497ef98484..bcd58217ba6 100644 --- a/share/zephyr-package/cmake/zephyr_package_search.cmake +++ b/share/zephyr-package/cmake/zephyr_package_search.cmake @@ -9,6 +9,21 @@ set(WORKSPACE_RELATIVE_DIR "../../../../..") # Relative directory of Zephyr dir as seen from Zephyr package file set(ZEPHYR_RELATIVE_DIR "../../../..") +# This function updates Zephyr_DIR to the point to the candidate dir. +# For Zephyr 3.0 and earlier, the Zephyr_DIR might in some cases be +# `Zephyr_DIR-NOTFOUND` or pointing to the Zephyr package including the +# boilerplate code instead of the Zephyr package of the included boilerplate. +# This code ensures that when Zephyr releases <=3.0 is loaded, then Zephyr_DIR +# will point correctly, see also #43094 which relates to this. +function(set_zephyr_dir zephyr_candidate) + get_filename_component(zephyr_candidate_dir "${zephyr_candidate}" DIRECTORY) + if(NOT "${zephyr_candidate_dir}" STREQUAL "${Zephyr_DIR}") + set(Zephyr_DIR ${zephyr_candidate_dir} CACHE PATH + "The directory containing a CMake configuration file for Zephyr." FORCE + ) + endif() +endfunction() + # This macro returns a list of parent folders to use for later searches. macro(get_search_paths START_PATH SEARCH_PATHS PREFERENCE_LIST) get_filename_component(SEARCH_PATH ${START_PATH} DIRECTORY) @@ -79,6 +94,7 @@ macro(check_zephyr_package) return() else() include(${ZEPHYR_CANDIDATE} NO_POLICY_SCOPE) + set_zephyr_dir(${ZEPHYR_CANDIDATE}) return() endif() endif() @@ -99,6 +115,7 @@ macro(check_zephyr_package) return() else() include(${ZEPHYR_CANDIDATE} NO_POLICY_SCOPE) + set_zephyr_dir(${ZEPHYR_CANDIDATE}) return() endif() endif()