Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: introduce gnu and llvm detection when Zephyr toolchain is unset. #853

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading