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 <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2022-03-01 10:32:23 +01:00 committed by Carles Cufí
parent 0a64b08062
commit f96ee77c7c
2 changed files with 26 additions and 4 deletions

View file

@ -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)

View file

@ -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()