diff --git a/cmake/modules/FindHostTools.cmake b/cmake/modules/FindHostTools.cmake new file mode 100644 index 0000000000..331eba908c --- /dev/null +++ b/cmake/modules/FindHostTools.cmake @@ -0,0 +1,125 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2022, Nordic Semiconductor ASA + +# FindHostTools module for locating a set of tools to use on the host for +# Zephyr development. +# +# This module will lookup the following tools for Zephyr development: +# +---------------------------------------------------------------+ +# | Tool | Required | Notes: | +# +---------------------------------------------------------------+ +# | Generic C-compiler | Yes | Pre-processing of devicetree | +# | Zephyr-sdk | | | +# | gperf | | | +# | openocd | | | +# | bossac | | | +# | imgtool | | | +# +---------------------------------------------------------------+ +# +# The module defines the following variables: +# +# 'CMAKE_C_COMPILER' +# Path to C compiler. +# Set to 'CMAKE_C_COMPILER-NOTFOUND' if no C compiler was found. +# +# 'GPERF' +# Path to gperf. +# Set to 'GPERF-NOTFOUND' if gperf was not found. +# +# 'OPENOCD' +# Path to openocd. +# Set to 'OPENOCD-NOTFOUND' if openocd was not found. +# +# 'BOSSAC' +# Path to bossac. +# Set to 'BOSSAC-NOTFOUND' if bossac was not found. +# +# 'IMGTOOL' +# Path to imgtool. +# Set to 'IMGTOOL-NOTFOUND' if imgtool was not found. +# +# 'HostTools_FOUND', 'HOSTTOOLS_FOUND' +# True if all required host tools were found. + +include(extensions) + +if(HostTools_FOUND) + return() +endif() + +# gperf is an optional dependency +find_program(GPERF gperf) + +# openocd is an optional dependency +find_program(OPENOCD openocd) + +# bossac is an optional dependency +find_program(BOSSAC bossac) + +# imgtool is an optional dependency (the build may also fall back to scripts/imgtool.py +# in the mcuboot repository if that's present in some cases) +find_program(IMGTOOL imgtool) + +# Keep XCC_USE_CLANG behaviour for a while. +if ("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "xcc" + AND "$ENV{XCC_USE_CLANG}" STREQUAL "1") + set(ZEPHYR_TOOLCHAIN_VARIANT xcc-clang) + message(STATUS "XCC_USE_CLANG is deprecated. Please set ZEPHYR_TOOLCHAIN_VARIANT to 'xcc-clang'") +endif() + +if(NOT ZEPHYR_TOOLCHAIN_VARIANT AND + (CROSS_COMPILE OR (DEFINED ENV{CROSS_COMPILE}))) + set(ZEPHYR_TOOLCHAIN_VARIANT cross-compile) +endif() + +find_package(Zephyr-sdk 0.15) + +# Pick host system's toolchain if we are targeting posix +if("${ARCH}" STREQUAL "posix") + if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "llvm") + set(ZEPHYR_TOOLCHAIN_VARIANT "host") + endif() +endif() + +# Prevent CMake from testing the toolchain +set(CMAKE_C_COMPILER_FORCED 1) +set(CMAKE_CXX_COMPILER_FORCED 1) + +if(NOT TOOLCHAIN_ROOT) + if(DEFINED ENV{TOOLCHAIN_ROOT}) + # Support for out-of-tree toolchain + set(TOOLCHAIN_ROOT $ENV{TOOLCHAIN_ROOT}) + else() + # Default toolchain cmake file + set(TOOLCHAIN_ROOT ${ZEPHYR_BASE}) + endif() +endif() +zephyr_file(APPLICATION_ROOT TOOLCHAIN_ROOT) + +# Host-tools don't unconditionally set TOOLCHAIN_HOME anymore, +# but in case Zephyr's SDK toolchain is used, set TOOLCHAIN_HOME +if("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "zephyr") + set(TOOLCHAIN_HOME ${HOST_TOOLS_HOME}) +endif() + +set(TOOLCHAIN_ROOT ${TOOLCHAIN_ROOT} CACHE STRING "Zephyr toolchain root" FORCE) +assert(TOOLCHAIN_ROOT "Zephyr toolchain root path invalid: please set the TOOLCHAIN_ROOT-variable") + +# Set cached ZEPHYR_TOOLCHAIN_VARIANT. +set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT} CACHE STRING "Zephyr toolchain variant") + +# Configure the toolchain based on what SDK/toolchain is in use. +include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/generic.cmake) + +# Configure the toolchain based on what toolchain technology is used +# (gcc, host-gcc etc.) +include(${TOOLCHAIN_ROOT}/cmake/compiler/${COMPILER}/generic.cmake OPTIONAL) +include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/generic.cmake OPTIONAL) +include(${TOOLCHAIN_ROOT}/cmake/bintools/${BINTOOLS}/generic.cmake OPTIONAL) + +# Optional folder for toolchains with may provide a Kconfig file for capabilities settings. +set_ifndef(TOOLCHAIN_KCONFIG_DIR ${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}) + +set(HostTools_FOUND TRUE) +set(HOSTTOOLS_FOUND TRUE) diff --git a/cmake/modules/FindZephyr-sdk.cmake b/cmake/modules/FindZephyr-sdk.cmake new file mode 100644 index 0000000000..e7eff17b49 --- /dev/null +++ b/cmake/modules/FindZephyr-sdk.cmake @@ -0,0 +1,90 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2022, Nordic Semiconductor ASA + +# FindZephyr-sdk module for supporting module search mode of Zephyr SDK. +# +# 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. +# +# 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: +# +# 'ZEPHYR_SDK_INSTALL_DIR' +# Install location of the Zephyr SDK +# +# 'ZEPHYR_TOOLCHAIN_VARIANT' +# Zephyr toolchain variant to use if not defined already. +# +# 'Zephyr-sdk_FOUND' +# True if the Zephyr SDK was found. + +# Set internal variables if set in environment. +if(NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT) + set(ZEPHYR_TOOLCHAIN_VARIANT $ENV{ZEPHYR_TOOLCHAIN_VARIANT}) +endif() + +if(NOT DEFINED ZEPHYR_SDK_INSTALL_DIR) + set(ZEPHYR_SDK_INSTALL_DIR $ENV{ZEPHYR_SDK_INSTALL_DIR}) +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) +# 2) No toolchain specified == Default to Zephyr toolchain +# Until we completely deprecate it +if(("zephyr" STREQUAL ${ZEPHYR_TOOLCHAIN_VARIANT}) OR + (NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT) OR + (DEFINED ZEPHYR_SDK_INSTALL_DIR)) + + # No toolchain was specified, so inform user that we will be searching. + if (NOT DEFINED ZEPHYR_SDK_INSTALL_DIR AND + NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT) + message(STATUS "ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK") + endif() + + # This ensure packages are sorted in descending order. + SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION_CURRENT ${CMAKE_FIND_PACKAGE_SORT_DIRECTION}) + SET(CMAKE_FIND_PACKAGE_SORT_ORDER_CURRENT ${CMAKE_FIND_PACKAGE_SORT_ORDER}) + SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC) + SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL) + + if(DEFINED ZEPHYR_SDK_INSTALL_DIR) + # 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. + set(ZEPHYR_CURRENT_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT}) + find_package(Zephyr-sdk ${Zephyr-sdk_FIND_VERSION} + REQUIRED QUIET CONFIG HINTS ${ZEPHYR_SDK_INSTALL_DIR} + ) + if(DEFINED ZEPHYR_CURRENT_TOOLCHAIN_VARIANT) + set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_CURRENT_TOOLCHAIN_VARIANT}) + endif() + else() + find_package(Zephyr-sdk ${Zephyr-sdk_FIND_VERSION} REQUIRED QUIET CONFIG PATHS + /usr + /usr/local + /opt + $ENV{HOME} + $ENV{HOME}/.local + $ENV{HOME}/.local/opt + $ENV{HOME}/bin) + 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}) +endif() + +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") +endif() + +if(Zephyr-sdk_FOUND) + include(${ZEPHYR_SDK_INSTALL_DIR}/cmake/zephyr/host-tools.cmake) + + message(STATUS "Found host-tools: zephyr ${SDK_VERSION} (${ZEPHYR_SDK_INSTALL_DIR})") +endif() diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index a641e0c68f..7d5ccc1209 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -5,7 +5,7 @@ include_guard(GLOBAL) include(extensions) include(python) include(boards) -include(generic_toolchain) +find_package(HostTools) find_package(Dtc 1.4.6) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/include/generated) diff --git a/cmake/modules/generic_toolchain.cmake b/cmake/modules/generic_toolchain.cmake deleted file mode 100644 index 821213e6d9..0000000000 --- a/cmake/modules/generic_toolchain.cmake +++ /dev/null @@ -1,44 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -include_guard(GLOBAL) - -include(extensions) -include(verify-toolchain) - -# Prevent CMake from testing the toolchain -set(CMAKE_C_COMPILER_FORCED 1) -set(CMAKE_CXX_COMPILER_FORCED 1) - -if(NOT TOOLCHAIN_ROOT) - if(DEFINED ENV{TOOLCHAIN_ROOT}) - # Support for out-of-tree toolchain - set(TOOLCHAIN_ROOT $ENV{TOOLCHAIN_ROOT}) - else() - # Default toolchain cmake file - set(TOOLCHAIN_ROOT ${ZEPHYR_BASE}) - endif() -endif() -zephyr_file(APPLICATION_ROOT TOOLCHAIN_ROOT) - -# Host-tools don't unconditionally set TOOLCHAIN_HOME anymore, -# but in case Zephyr's SDK toolchain is used, set TOOLCHAIN_HOME -if("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "zephyr") - set(TOOLCHAIN_HOME ${HOST_TOOLS_HOME}) -endif() - -set(TOOLCHAIN_ROOT ${TOOLCHAIN_ROOT} CACHE STRING "Zephyr toolchain root" FORCE) -assert(TOOLCHAIN_ROOT "Zephyr toolchain root path invalid: please set the TOOLCHAIN_ROOT-variable") - -# Set cached ZEPHYR_TOOLCHAIN_VARIANT. -set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT} CACHE STRING "Zephyr toolchain variant") - -# Configure the toolchain based on what SDK/toolchain is in use. -include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/generic.cmake) - -set_ifndef(TOOLCHAIN_KCONFIG_DIR ${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}) - -# Configure the toolchain based on what toolchain technology is used -# (gcc, host-gcc etc.) -include(${TOOLCHAIN_ROOT}/cmake/compiler/${COMPILER}/generic.cmake OPTIONAL) -include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/generic.cmake OPTIONAL) -include(${TOOLCHAIN_ROOT}/cmake/bintools/${BINTOOLS}/generic.cmake OPTIONAL) diff --git a/cmake/modules/host-tools.cmake b/cmake/modules/host-tools.cmake deleted file mode 100644 index afacef77f0..0000000000 --- a/cmake/modules/host-tools.cmake +++ /dev/null @@ -1,36 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -include_guard(GLOBAL) - -if(ZEPHYR_SDK_HOST_TOOLS) - include(${ZEPHYR_BASE}/cmake/toolchain/zephyr/host-tools.cmake) -endif() - -# gperf is an optional dependency -find_program( - GPERF - gperf - ) - -# openocd is an optional dependency -find_program( - OPENOCD - openocd - ) - -# bossac is an optional dependency -find_program( - BOSSAC - bossac - ) - -# imgtool is an optional dependency (the build may also fall back to -# scripts/imgtool.py in the mcuboot repository if that's present in -# some cases) -find_program( - IMGTOOL - imgtool - ) - -# TODO: Should we instead find one qemu binary for each ARCH? -# TODO: This will probably need to be re-organized when there exists more than one SDK. diff --git a/cmake/modules/verify-toolchain.cmake b/cmake/modules/verify-toolchain.cmake deleted file mode 100644 index 13d4b62729..0000000000 --- a/cmake/modules/verify-toolchain.cmake +++ /dev/null @@ -1,121 +0,0 @@ -# The purpose of this file is to verify that required variables has been -# defined for proper toolchain use. -# -# It also offers the possibility to verify that the selected toolchain matches -# a specific version. -# Currently only when using the Zephyr SDK the version is verified, but other -# other version verification for other toolchains can be added as needed. -# -# This file can also be executed in script mode so that it can be used in other -# places, such as python scripts. -# -# When invoked as a script with -P: -# cmake [options] -P verify-toolchain.cmake -# -# it takes the following arguments: -# FORMAT=json: Print the output as a json formatted string, useful for Python - -include_guard(GLOBAL) - -# This is the minimum required Zephyr SDK version. -set(TOOLCHAIN_ZEPHYR_MINIMUM_REQUIRED_VERSION 0.15) - -# Set internal variables if set in environment. -if(NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT) - set(ZEPHYR_TOOLCHAIN_VARIANT $ENV{ZEPHYR_TOOLCHAIN_VARIANT}) -endif() - -if(NOT DEFINED ZEPHYR_SDK_INSTALL_DIR) - set(ZEPHYR_SDK_INSTALL_DIR $ENV{ZEPHYR_SDK_INSTALL_DIR}) -endif() - -# Pick host system's toolchain if we are targeting posix -if("${ARCH}" STREQUAL "posix") - if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "llvm") - set(ZEPHYR_TOOLCHAIN_VARIANT "host") - endif() - return() -endif() - -# Keep XCC_USE_CLANG behaviour for a while. -if ("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "xcc" - AND "$ENV{XCC_USE_CLANG}" STREQUAL "1") - set(ZEPHYR_TOOLCHAIN_VARIANT xcc-clang) - message(STATUS "XCC_USE_CLANG is deprecated. Please set ZEPHYR_TOOLCHAIN_VARIANT to 'xcc-clang'") -endif() - -if(NOT ZEPHYR_TOOLCHAIN_VARIANT AND - (CROSS_COMPILE OR (DEFINED ENV{CROSS_COMPILE}))) - set(ZEPHYR_TOOLCHAIN_VARIANT cross-compile) -endif() - -# Verify 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) -# 2) No toolchain specified == Default to Zephyr toolchain -# Until we completely deprecate it -if(("zephyr" STREQUAL ${ZEPHYR_TOOLCHAIN_VARIANT}) OR - (NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT) OR - (DEFINED ZEPHYR_SDK_INSTALL_DIR)) - - # No toolchain was specified, so inform user that we will be searching. - if (NOT DEFINED ZEPHYR_SDK_INSTALL_DIR AND - NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT - AND NOT CMAKE_SCRIPT_MODE_FILE) - message(STATUS "ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK") - endif() - - # This ensure packages are sorted in descending order. - SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION_CURRENT ${CMAKE_FIND_PACKAGE_SORT_DIRECTION}) - SET(CMAKE_FIND_PACKAGE_SORT_ORDER_CURRENT ${CMAKE_FIND_PACKAGE_SORT_ORDER}) - SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC) - SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL) - - if(DEFINED ZEPHYR_SDK_INSTALL_DIR) - # 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. - set(ZEPHYR_CURRENT_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT}) - find_package(Zephyr-sdk ${TOOLCHAIN_ZEPHYR_MINIMUM_REQUIRED_VERSION} REQUIRED QUIET HINTS ${ZEPHYR_SDK_INSTALL_DIR}) - if(DEFINED ZEPHYR_CURRENT_TOOLCHAIN_VARIANT) - set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_CURRENT_TOOLCHAIN_VARIANT}) - endif() - else() - find_package(Zephyr-sdk ${TOOLCHAIN_ZEPHYR_MINIMUM_REQUIRED_VERSION} REQUIRED QUIET PATHS - /usr - /usr/local - /opt - $ENV{HOME} - $ENV{HOME}/.local - $ENV{HOME}/.local/opt - $ENV{HOME}/bin) - 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}) -endif() - -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") - # Use the Zephyr SDK host-tools. - set(ZEPHYR_SDK_HOST_TOOLS TRUE) -endif() - -if(CMAKE_SCRIPT_MODE_FILE) - if("${FORMAT}" STREQUAL "json") - set(json "{\"ZEPHYR_TOOLCHAIN_VARIANT\" : \"${ZEPHYR_TOOLCHAIN_VARIANT}\", ") - string(APPEND json "\"SDK_VERSION\": \"${SDK_VERSION}\", ") - string(APPEND json "\"ZEPHYR_SDK_INSTALL_DIR\" : \"${ZEPHYR_SDK_INSTALL_DIR}\"}") - message("${json}") - else() - message(STATUS "ZEPHYR_TOOLCHAIN_VARIANT: ${ZEPHYR_TOOLCHAIN_VARIANT}") - if(DEFINED SDK_VERSION) - message(STATUS "SDK_VERSION: ${SDK_VERSION}") - endif() - - if(DEFINED ZEPHYR_SDK_INSTALL_DIR) - message(STATUS "ZEPHYR_SDK_INSTALL_DIR : ${ZEPHYR_SDK_INSTALL_DIR}") - endif() - endif() -endif() diff --git a/cmake/modules/zephyr_default.cmake b/cmake/modules/zephyr_default.cmake index a54175bf4f..2360ffd6b5 100644 --- a/cmake/modules/zephyr_default.cmake +++ b/cmake/modules/zephyr_default.cmake @@ -76,27 +76,12 @@ list(APPEND zephyr_cmake_modules shields) list(APPEND zephyr_cmake_modules arch) list(APPEND zephyr_cmake_modules configuration_files) -list(APPEND zephyr_cmake_modules verify-toolchain) -list(APPEND zephyr_cmake_modules host-tools) - # Include board specific device-tree flags before parsing. set(pre_dt_board "\${BOARD_DIR}/pre_dt_board.cmake" OPTIONAL) list(APPEND zephyr_cmake_modules "\${pre_dt_board}") # DTS should be close to kconfig because CONFIG_ variables from # kconfig and dts should be available at the same time. -# -# The DT system uses a C preprocessor for it's code generation needs. -# This creates an awkward chicken-and-egg problem, because we don't -# always know exactly which toolchain the user needs until we know -# more about the target, e.g. after DT and Kconfig. -# -# To resolve this we find "some" C toolchain, configure it generically -# with the minimal amount of configuration needed to have it -# preprocess DT sources, and then, after we have finished processing -# both DT and Kconfig we complete the target-specific configuration, -# and possibly change the toolchain. -list(APPEND zephyr_cmake_modules generic_toolchain) list(APPEND zephyr_cmake_modules dts) list(APPEND zephyr_cmake_modules kconfig) list(APPEND zephyr_cmake_modules soc) diff --git a/cmake/toolchain/zephyr/host-tools.cmake b/cmake/toolchain/zephyr/host-tools.cmake deleted file mode 100644 index 1c21eab2f8..0000000000 --- a/cmake/toolchain/zephyr/host-tools.cmake +++ /dev/null @@ -1,5 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -include(${ZEPHYR_SDK_INSTALL_DIR}/cmake/zephyr/host-tools.cmake) - -message(STATUS "Found host-tools: zephyr ${SDK_VERSION} (${ZEPHYR_SDK_INSTALL_DIR})") diff --git a/cmake/verify-toolchain.cmake b/cmake/verify-toolchain.cmake new file mode 100644 index 0000000000..0c6f1771e5 --- /dev/null +++ b/cmake/verify-toolchain.cmake @@ -0,0 +1,46 @@ +# The purpose of this file is to verify that required variables has been +# defined for proper toolchain use. +# +# This file is intended to be executed in script mode so that it can be used in +# other tools, such as twister / python scripts. +# +# When invoked as a script with -P: +# cmake [options] -P verify-toolchain.cmake +# +# it takes the following arguments: +# FORMAT=json: Print the output as a json formatted string, useful for Python + +cmake_minimum_required(VERSION 3.20.0) + +if(NOT CMAKE_SCRIPT_MODE_FILE) + message(FATAL_ERROR "verify-toolchain.cmake is a script and must be invoked " + "as:\n 'cmake ... -P verify-toolchain.cmake'\n" + ) +endif() + +if("${FORMAT}" STREQUAL "json") + # If executing in script mode and output format is specified, then silence + # all other messages as only the specified output should be printed. + function(message) + endfunction() +endif() + +set(ZEPHYR_BASE ${CMAKE_CURRENT_LIST_DIR}/../) +list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/modules) +find_package(HostTools) + +if("${FORMAT}" STREQUAL "json") + set(json "{\"ZEPHYR_TOOLCHAIN_VARIANT\" : \"${ZEPHYR_TOOLCHAIN_VARIANT}\", ") + string(APPEND json "\"SDK_VERSION\": \"${SDK_VERSION}\", ") + string(APPEND json "\"ZEPHYR_SDK_INSTALL_DIR\" : \"${ZEPHYR_SDK_INSTALL_DIR}\"}") + _message("${json}") +else() + message(STATUS "ZEPHYR_TOOLCHAIN_VARIANT: ${ZEPHYR_TOOLCHAIN_VARIANT}") + if(DEFINED SDK_VERSION) + message(STATUS "SDK_VERSION: ${SDK_VERSION}") + endif() + + if(DEFINED ZEPHYR_SDK_INSTALL_DIR) + message(STATUS "ZEPHYR_SDK_INSTALL_DIR : ${ZEPHYR_SDK_INSTALL_DIR}") + endif() +endif() diff --git a/scripts/pylib/twister/twisterlib/environment.py b/scripts/pylib/twister/twisterlib/environment.py index 62e549eb8d..bc739b0ff6 100644 --- a/scripts/pylib/twister/twisterlib/environment.py +++ b/scripts/pylib/twister/twisterlib/environment.py @@ -748,7 +748,7 @@ class TwisterEnv: return results def get_toolchain(self): - toolchain_script = Path(ZEPHYR_BASE) / Path('cmake/modules/verify-toolchain.cmake') + toolchain_script = Path(ZEPHYR_BASE) / Path('cmake/verify-toolchain.cmake') result = self.run_cmake_script([toolchain_script, "FORMAT=json"]) try: