cmake: support list mode in Zephyr-sdk module package

Introduce list mode in Zephyr-sdk module package.

The list mode allows to list all Zephyr SDK's found in the system
without loading any of them.

Signature of the list mode is:
> find_package(Zephyr-sdk COMPONENTS LIST)

Will print valid Zephyr SDKs and their path, as well as defining the
following corresponding CMake lists:
- Zephyr-sdk      : List of Zephyr SDKs' version
- Zephyr-sdk_DIRS : List of Directories with a valid Zephyr SDK.
Each entry in Zephyr-sdk corresponds to the same entry index in the
Zephyr-sdk_DIRS list.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2024-08-16 12:28:53 +02:00 committed by Henrik Brix Andersen
commit cdd3e6fa84

View file

@ -4,6 +4,10 @@
# FindZephyr-sdk module for supporting module search mode of Zephyr SDK.
#
# It is possible to control the behavior of the Zephyr-SDK package using
# COMPONENTS.
# The Zephyr-SDK package supports the components:
# - LOAD: Load a Zephyr-SDK. This is the default behavior if no COMPONENTS is specified.
# Its purpose is to allow the find_package basic signature mode to lookup Zephyr
# SDK and based on user / environment settings of selected toolchain decide if
# the Zephyr SDK CMake package should be loaded.
@ -11,8 +15,7 @@
# It extends the Zephyr-sdk CMake package by providing more flexibility in when
# the Zephyr SDK is loaded and loads additional host tools from the Zephyr SDK.
#
# The module defines the following variables:
#
# The module defines the following variables when used in normal search and load mode:
# 'ZEPHYR_SDK_INSTALL_DIR'
# Install location of the Zephyr SDK
#
@ -22,6 +25,19 @@
# 'Zephyr-sdk_FOUND'
# True if the Zephyr SDK was found.
# - LIST: Will list all available Zephyr SDKs found in the system but not load
# any Sdk. This can be used to fetch available Zephyr-SDKs before doing
# an actual load.
# LIST component will define the following lists:
# - Zephyr-sdk : Version of a Zephyr-SDK
# - Zephyr-sdk_DIRS : Install dir of the Zephyr-SDK
# Each entry in Zephyr-SDK has a corresponding entry in Zephyr-SDK_DIRS.
# For example:
# index: Zephyr-sdk: Zephyr-sdk_DIRS:
# 0 0.15.0 /opt/zephyr-sdk-0.15.0
# 1 0.16.0 /home/<user>/zephyr-sdk-0.16.0
#
include(extensions)
# Set internal variables if set in environment.
@ -29,6 +45,10 @@ zephyr_get(ZEPHYR_TOOLCHAIN_VARIANT)
zephyr_get(ZEPHYR_SDK_INSTALL_DIR)
if("${Zephyr-sdk_FIND_COMPONENTS}" STREQUAL "")
set(Zephyr-sdk_FIND_COMPONENTS LOAD)
endif()
# Load Zephyr SDK Toolchain.
# There are three scenarios where Zephyr SDK should be looked up:
# 1) Zephyr specified as toolchain (ZEPHYR_SDK_INSTALL_DIR still used if defined)
@ -52,7 +72,7 @@ if(("zephyr" STREQUAL ${ZEPHYR_TOOLCHAIN_VARIANT}) OR
SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
if(DEFINED ZEPHYR_SDK_INSTALL_DIR)
if(DEFINED ZEPHYR_SDK_INSTALL_DIR AND LOAD IN_LIST Zephyr-sdk_FIND_COMPONENTS)
# The Zephyr SDK will automatically set the toolchain variant.
# To support Zephyr SDK tools (DTC, and other tools) with 3rd party toolchains
# then we keep track of current toolchain variant.
@ -80,20 +100,36 @@ if(("zephyr" STREQUAL ${ZEPHYR_TOOLCHAIN_VARIANT}) OR
find_package(Zephyr-sdk 0.0.0 EXACT QUIET CONFIG PATHS ${zephyr_sdk_search_paths})
# Remove duplicate entries and sort naturally in descending order.
set(zephyr_sdk_found_versions ${Zephyr-sdk_CONSIDERED_VERSIONS})
set(zephyr_sdk_found_configs ${Zephyr-sdk_CONSIDERED_CONFIGS})
foreach(version config IN ZIP_LISTS Zephyr-sdk_CONSIDERED_VERSIONS Zephyr-sdk_CONSIDERED_CONFIGS)
if(NOT DEFINED Zephyr-sdk-${version}_DIR)
set(Zephyr-sdk-${version}_DIR ${config})
endif()
endforeach()
list(REMOVE_DUPLICATES Zephyr-sdk_CONSIDERED_VERSIONS)
list(SORT Zephyr-sdk_CONSIDERED_VERSIONS COMPARE NATURAL ORDER DESCENDING)
if(LIST IN_LIST Zephyr-sdk_FIND_COMPONENTS)
set(Zephyr-sdk)
set(Zephyr-sdk_DIRS)
# Only list the Zephyr SDKs installed in the system.
foreach(version ${Zephyr-sdk_CONSIDERED_VERSIONS})
cmake_path(GET Zephyr-sdk-${version}_DIR PARENT_PATH dir)
cmake_path(GET dir PARENT_PATH dir)
list(APPEND Zephyr-sdk ${version})
list(APPEND Zephyr-sdk_DIRS ${dir})
if (NOT Zephyr-sdk_FIND_QUIETLY)
message(STATUS "Zephyr-sdk, version=${version}, dir=${dir}")
endif()
endforeach()
else()
# Loop over each found Zepher SDK version until one is found that is compatible.
foreach(zephyr_sdk_candidate ${Zephyr-sdk_CONSIDERED_VERSIONS})
if("${zephyr_sdk_candidate}" VERSION_GREATER_EQUAL "${Zephyr-sdk_FIND_VERSION}")
# Find the path for the current version being checked and get the directory
# of the Zephyr SDK so it can be checked.
list(FIND zephyr_sdk_found_versions ${zephyr_sdk_candidate} zephyr_sdk_current_index)
list(GET zephyr_sdk_found_configs ${zephyr_sdk_current_index} zephyr_sdk_current_check_path)
get_filename_component(zephyr_sdk_current_check_path ${zephyr_sdk_current_check_path} DIRECTORY)
cmake_path(GET Zephyr-sdk-${zephyr_sdk_candidate}_DIR PARENT_PATH zephyr_sdk_current_check_path)
cmake_path(GET zephyr_sdk_current_check_path PARENT_PATH zephyr_sdk_current_check_path)
# Then see if this version is compatible.
find_package(Zephyr-sdk ${Zephyr-sdk_FIND_VERSION} QUIET CONFIG PATHS ${zephyr_sdk_current_check_path} NO_DEFAULT_PATH)
@ -112,6 +148,7 @@ if(("zephyr" STREQUAL ${ZEPHYR_TOOLCHAIN_VARIANT}) OR
find_package(Zephyr-sdk ${Zephyr-sdk_FIND_VERSION} REQUIRED CONFIG PATHS ${zephyr_sdk_search_paths})
endif()
endif()
endif()
SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION ${CMAKE_FIND_PACKAGE_SORT_DIRECTION_CURRENT})
SET(CMAKE_FIND_PACKAGE_SORT_ORDER ${CMAKE_FIND_PACKAGE_SORT_ORDER_CURRENT})
@ -124,6 +161,7 @@ set(zephyr_sdk_found_configs)
set(zephyr_sdk_current_index)
set(zephyr_sdk_current_check_path)
if(LOAD IN_LIST Zephyr-sdk_FIND_COMPONENTS)
if(DEFINED ZEPHYR_SDK_INSTALL_DIR)
# Cache the Zephyr SDK install dir.
set(ZEPHYR_SDK_INSTALL_DIR ${ZEPHYR_SDK_INSTALL_DIR} CACHE PATH "Zephyr SDK install directory")
@ -136,3 +174,4 @@ if(Zephyr-sdk_FOUND)
message(STATUS "Found host-tools: zephyr ${SDK_VERSION} (${ZEPHYR_SDK_INSTALL_DIR})")
endif()
endif()
endif()