forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmake: detection and using a not registered Zephyr repository package
This commit adds the possibility of using a Zephyr repository package base even when it has not been exported to CMake package registry. It also introduces the possibility of locating and using Zephyr CMake config package correctly when invoking CMake on an out-of-tree project. Signed-off-by: Torsten Rasmussen <[email protected]>
- Loading branch information
1 parent
3dd96d8
commit 555eb9b
Showing
3 changed files
with
226 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# The purpose of this file is to provide search mechanism for locating Zephyr in-work-tree package | ||
# even when they are not installed into CMake package system | ||
# Linux/MacOS: ~/.cmake/packages | ||
# Windows: Registry database | ||
|
||
# Relative directory of worktree project dir as seen from Zephyr package file | ||
set(PROJECT_WORKTREE_RELATIVE_DIR "../../../../..") | ||
|
||
# Relative directory of Zephyr dir as seen from Zephyr package file | ||
set(ZEPHYR_RELATIVE_DIR "../../../..") | ||
|
||
# This macro returns a list of parent folders to use for later searches. | ||
macro(get_search_paths START_PATH SEARCH_PATHS) | ||
get_filename_component(SEARCH_PATH ${START_PATH} DIRECTORY) | ||
while(NOT (SEARCH_PATH STREQUAL "/")) | ||
list(APPEND SEARCH_PATHS ${SEARCH_PATH}/zephyr) | ||
list(APPEND SEARCH_PATHS ${SEARCH_PATH}) | ||
get_filename_component(SEARCH_PATH ${SEARCH_PATH} DIRECTORY) | ||
endwhile() | ||
endmacro() | ||
|
||
# This macro can check for additional Zephyr package that has a better match | ||
# Options: | ||
# - PROJECT_WORKTREE_DIR: Search for projects in specified worktree. | ||
# - SEARCH_PARENTS : Search parent folder of current source file (application) to locate in-project-tree Zephyr candidates. | ||
# - CHECK_ONLY : Only set PACKAGE_VERSION_COMPATIBLE to false if a better candidate is found, default is to also include the found candidate. | ||
# - VERSION_CHECK : This is the version check stage by CMake find package | ||
macro(check_zephyr_package) | ||
set(options CHECK_ONLY INCLUDE_FILE SEARCH_PARENTS VERSION_CHECK) | ||
set(single_args PROJECT_WORKTREE_DIR) | ||
cmake_parse_arguments(CHECK_ZEPHYR_PACKAGE "${options}" "${single_args}" "" ${ARGN}) | ||
|
||
if(CHECK_ZEPHYR_PACKAGE_PROJECT_WORKTREE_DIR) | ||
set(SEARCH_SETTINGS PATHS ${CHECK_ZEPHYR_PACKAGE_PROJECT_WORKTREE_DIR}/zephyr ${CHECK_ZEPHYR_PACKAGE_PROJECT_WORKTREE_DIR} NO_DEFAULT_PATH) | ||
endif() | ||
|
||
if(CHECK_ZEPHYR_PACKAGE_SEARCH_PARENTS) | ||
get_search_paths(${CMAKE_CURRENT_SOURCE_DIR} SEARCH_PATHS) | ||
set(SEARCH_SETTINGS PATHS ${SEARCH_PATHS} NO_DEFAULT_PATH) | ||
endif() | ||
|
||
# Searching for version zero means there will be no match, but we obtain | ||
# a list of all potential Zephyr candidates in the tree to consider. | ||
find_package(Zephyr 0.0.0 EXACT QUIET ${SEARCH_SETTINGS}) | ||
|
||
# The find package will also find ourself when searching in-tree, so to avoid re-including | ||
# this file, it is removed from the list along with any duplicates. | ||
list(REMOVE_ITEM Zephyr_CONSIDERED_CONFIGS ${CMAKE_CURRENT_LIST_DIR}/ZephyrConfig.cmake) | ||
list(REMOVE_DUPLICATES Zephyr_CONSIDERED_CONFIGS) | ||
|
||
foreach(ZEPHYR_CANDIDATE ${Zephyr_CONSIDERED_CONFIGS}) | ||
if(CHECK_ZEPHYR_PACKAGE_PROJECT_WORKTREE_DIR) | ||
# Check is done in project tree already, thus check only for pure Zephyr candidates. | ||
get_filename_component(CANDIDATE_DIR ${ZEPHYR_CANDIDATE}/${ZEPHYR_RELATIVE_DIR} ABSOLUTE) | ||
else() | ||
get_filename_component(CANDIDATE_DIR ${ZEPHYR_CANDIDATE}/${PROJECT_WORKTREE_RELATIVE_DIR} ABSOLUTE) | ||
endif() | ||
|
||
string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "${CANDIDATE_DIR}/" COMMON_INDEX) | ||
if (COMMON_INDEX EQUAL 0) | ||
if(CHECK_ZEPHYR_PACKAGE_CHECK_ONLY) | ||
# A better candidate exists, thus return | ||
set(PACKAGE_VERSION_COMPATIBLE FALSE) | ||
return() | ||
else() | ||
# A better candidate exists, thus return | ||
if(CHECK_ZEPHYR_PACKAGE_VERSION_CHECK) | ||
string(REGEX REPLACE "\.cmake$" "Version.cmake" ZEPHYR_VERSION_CANDIDATE ${ZEPHYR_CANDIDATE}) | ||
include(${ZEPHYR_VERSION_CANDIDATE} NO_POLICY_SCOPE) | ||
return() | ||
else() | ||
include(${ZEPHYR_CANDIDATE} NO_POLICY_SCOPE) | ||
return() | ||
endif() | ||
endif() | ||
endif() | ||
endforeach() | ||
endmacro() |