From ebabbf66dbcc11cc19e21b990f64d8efe34d7cc1 Mon Sep 17 00:00:00 2001 From: Stephanos Ioannidis Date: Thu, 24 Oct 2024 19:35:56 +0900 Subject: [PATCH] cmake: Add LLVM toolchain support This commit adds the support file for the `zephyr-llvm` toolchain variant. Signed-off-by: Stephanos Ioannidis --- cmake/Zephyr-sdkConfig.cmake | 5 +++- cmake/zephyr/Kconfig | 6 ++-- cmake/zephyr/llvm/clang_compiler_rt.cfg | 4 +++ cmake/zephyr/llvm/generic.cmake | 11 +++++++ cmake/zephyr/llvm/target.cmake | 38 +++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 cmake/zephyr/llvm/clang_compiler_rt.cfg create mode 100644 cmake/zephyr/llvm/generic.cmake create mode 100644 cmake/zephyr/llvm/target.cmake diff --git a/cmake/Zephyr-sdkConfig.cmake b/cmake/Zephyr-sdkConfig.cmake index 806044b9..26c0481a 100644 --- a/cmake/Zephyr-sdkConfig.cmake +++ b/cmake/Zephyr-sdkConfig.cmake @@ -11,7 +11,10 @@ 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-gnu) + +if(NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT) + set(ZEPHYR_TOOLCHAIN_VARIANT zephyr-gnu) +endif() # Those are CMake package parameters. set(Zephyr-sdk_FOUND True) diff --git a/cmake/zephyr/Kconfig b/cmake/zephyr/Kconfig index 07158851..dc36034f 100644 --- a/cmake/zephyr/Kconfig +++ b/cmake/zephyr/Kconfig @@ -4,7 +4,8 @@ config TOOLCHAIN_ZEPHYR_0_17 def_bool y # FIXME: Newlib-nano is disabled for Xtensa targets due to the memset # bug causing crashes (see the GitHub issue #660). - select HAS_NEWLIB_LIBC_NANO if !XTENSA + select HAS_NEWLIB_LIBC_NANO if !XTENSA \ + && "$(ZEPHYR_TOOLCHAIN_VARIANT)" != "zephyr-llvm" config TOOLCHAIN_ZEPHYR_SUPPORTS_THREAD_LOCAL_STORAGE def_bool y @@ -16,6 +17,7 @@ config TOOLCHAIN_ZEPHYR_SUPPORTS_GNU_EXTENSIONS config PICOLIBC_SUPPORTED def_bool y - depends on "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "zephyr-gnu" + depends on "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "zephyr-gnu" \ + || "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "zephyr-llvm" help Zephyr SDK >=0.16 always supports Picolibc for C and C++ development. diff --git a/cmake/zephyr/llvm/clang_compiler_rt.cfg b/cmake/zephyr/llvm/clang_compiler_rt.cfg new file mode 100644 index 00000000..cd0d01ef --- /dev/null +++ b/cmake/zephyr/llvm/clang_compiler_rt.cfg @@ -0,0 +1,4 @@ +# Copyright (c) 2023 The ChromiumOS Authors +# SPDX-License-Identifier: Apache-2.0 + +--rtlib=compiler-rt diff --git a/cmake/zephyr/llvm/generic.cmake b/cmake/zephyr/llvm/generic.cmake new file mode 100644 index 00000000..c3f42fe1 --- /dev/null +++ b/cmake/zephyr/llvm/generic.cmake @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: Apache-2.0 + +set(LLVM_TOOLCHAIN_PATH ${ZEPHYR_SDK_INSTALL_DIR}/llvm) + +set(TOOLCHAIN_HOME ${ZEPHYR_SDK_INSTALL_DIR}/llvm/bin) + +set(COMPILER clang) +set(LINKER lld) +set(BINTOOLS llvm) + +set(TOOLCHAIN_HAS_PICOLIBC ON CACHE BOOL "True if toolchain supports picolibc") diff --git a/cmake/zephyr/llvm/target.cmake b/cmake/zephyr/llvm/target.cmake new file mode 100644 index 00000000..d7f36f30 --- /dev/null +++ b/cmake/zephyr/llvm/target.cmake @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: Apache-2.0 + +if("${ARCH}" STREQUAL "arm") + if(DEFINED CONFIG_ARMV8_M_MAINLINE) + # ARMv8-M mainline is ARMv7-M with additional features from ARMv8-M. + set(triple armv8m.main-none-eabi) + elseif(DEFINED CONFIG_ARMV8_M_BASELINE) + # ARMv8-M baseline is ARMv6-M with additional features from ARMv8-M. + set(triple armv8m.base-none-eabi) + elseif(DEFINED CONFIG_ARMV7_M_ARMV8_M_MAINLINE) + # ARMV7_M_ARMV8_M_MAINLINE means that ARMv7-M or backward compatible ARMv8-M + # processor is used. + set(triple armv7m-none-eabi) + elseif(DEFINED CONFIG_ARMV6_M_ARMV8_M_BASELINE) + # ARMV6_M_ARMV8_M_BASELINE means that ARMv6-M or ARMv8-M supporting the + # Baseline implementation processor is used. + set(triple armv6m-none-eabi) + else() + # Default ARM target supported by all processors. + set(triple arm-none-eabi) + endif() +elseif("${ARCH}" STREQUAL "arm64") + set(triple aarch64-none-elf) +# TODO: Add RISC-V target support. +endif() + +if(DEFINED triple) + set(CMAKE_C_COMPILER_TARGET ${triple}) + set(CMAKE_ASM_COMPILER_TARGET ${triple}) + set(CMAKE_CXX_COMPILER_TARGET ${triple}) + + unset(triple) +endif() + +list(APPEND TOOLCHAIN_C_FLAGS --config + ${ZEPHYR_SDK_INSTALL_DIR}/cmake/zephyr/llvm/clang_compiler_rt.cfg) +list(APPEND TOOLCHAIN_LD_FLAGS --config + ${ZEPHYR_SDK_INSTALL_DIR}/cmake/zephyr/llvm/clang_compiler_rt.cfg)