Skip to content

Commit

Permalink
cmake: introduce gnu and llvm detection when Zephyr toolchain is unset
Browse files Browse the repository at this point in the history
Zephyr SDK v0.18.0 provides both GNU and LLVM based toolchain.

Users may install one or both toolchains.

If ZEPHYR_TOOLCHAIN_VARIANT is not defined, then we lookup first GNU
based toolchain, and if found, use this toolchain.
Else lookup LLVM based toolchain and use that one.

Signed-off-by: Torsten Rasmussen <[email protected]>
  • Loading branch information
tejlmand committed Feb 17, 2025
1 parent fff0928 commit a673fd0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
24 changes: 22 additions & 2 deletions cmake/Zephyr-sdkConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,28 @@ set(SDK_MAJOR_MINOR_MICRO ${SDK_VERSION})

get_filename_component(ZEPHYR_SDK_INSTALL_DIR ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
set(ZEPHYR_SDK_INSTALL_DIR ${ZEPHYR_SDK_INSTALL_DIR})
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr)

if(ZEPHYR_TOOLCHAIN_VARIANT STREQUAL "zephyr")
set(ZEPHYR_TOOLCHAIN_VARIANT "zephyr-gnu")
endif()

if(NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT)
file(GLOB use_gnu "${ZEPHYR_SDK_INSTALL_DIR}/gnu/*/bin/*zephyr-*-gcc")
if(use_gnu)
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr-gnu)
elseif(EXISTS "${ZEPHYR_SDK_INSTALL_DIR}/llvm/bin/clang")
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr-llvm)
else()
# We are in a Zephyr-sdk but gnu nor llvm was properly detected.
# Warn the user and set toolchain to zephyr-gnu.
message(WARNING
"Zephyr SDK found in ${ZEPHYR_SDK_INSTALL_DIR} but gcc nor clang was properly detected.\n"
"Please check you Zephyr SDK installation. Defaulting to 'zephyr-gnu'"
)
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr-gnu)
endif()
endif()

# Those are CMake package parameters.
set(Zephyr-sdk_FOUND True)
set(Zephyr-sdk_DIR ${ZEPHYR_SDK_INSTALL_DIR})
set(Zephyr-sdk_dir ${ZEPHYR_SDK_INSTALL_DIR})
36 changes: 21 additions & 15 deletions cmake/Zephyr-sdkConfigVersion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ string(STRIP ${SDK_VERSION} PACKAGE_VERSION)
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
# Currently, this Zephyr SDK is expected to work with any Zephyr project
# requiring this version or any older version.
#
# In case this version of Zephyr SDK is no longer backwards compatible with
# previous versions of Zephyr SDK, this is the place to test if the caller
# requested an older (incompatible) revision.
# For example, imagine caller requests Zephyr SDK v0.10, and this Zephyr SDK
# is revision v0.11, then the below snippet can be activated to ensure that
# this Zephyr SDK (v0.11) is marked as not compatible if caller requested an
# older version, like v0.10
# set(ZEPHYR_SDK_MINIMUM_COMPATIBLE_VERSION 0.11)
# if(PACKAGE_FIND_VERSION VERSION_LESS ZEPHYR_SDK_MINIMUM_COMPATIBLE_VERSION)
# set(PACKAGE_VERSION_COMPATIBLE FALSE)
# return()
# endif()
# This Zephyr SDK will not work with Zephyr projects requiring Zephyr SDK <0.18.x
set(ZEPHYR_SDK_MINIMUM_COMPATIBLE_VERSION 0.18)
if(PACKAGE_FIND_VERSION VERSION_LESS ZEPHYR_SDK_MINIMUM_COMPATIBLE_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
return()
endif()

# Basic validation if user requests 'zephyr-gnu' ('zephyr') or 'zephyr-llvm'.
# We must ensure gcc is available, likewise with clang.
if(ZEPHYR_TOOLCHAIN_VARIANT STREQUAL "zephyr-gnu" OR ZEPHYR_TOOLCHAIN_VARIANT STREQUAL "zephyr")
file(GLOB has_gnu "${CMAKE_CURRENT_LIST_DIR}/../gnu/*/bin/*zephyr-*-gcc")
if(NOT has_gnu)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
return()
endif()
elseif(ZEPHYR_TOOLCHAIN_VARIANT STREQUAL "zephyr-llvm")
if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/../llvm/bin/clang")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
return()
endif()
endif()

set(PACKAGE_VERSION_COMPATIBLE TRUE)
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
Expand Down

0 comments on commit a673fd0

Please sign in to comment.