zephyr/cmake/toolchain/llvm/generic.cmake
Torsten Rasmussen 0274bcbee4 cmake: detect LLVM picolibc and newlib support
Newlib or Picolibc libraries for LLVM may be compiled or installed from
pre-built sources independently of LLVM itself.

This means that always indicating that TOOLCHAIN_HAS_NEWLIB=OFF and
TOOLCHAIN_HAS_PICOLIBC=OFF are wrong. But it could be just as wrong to
always indicate suport for newlib or picolibc.

Some pre-built LLVM toolchains are provided with default picolibc
support, such as LLVM for Arm embedded, but can also be used with newlib
be installing newlib add-on package.

Unfortunately it's not possible to query LLVM regarding newlib or
picolibc support.

Developers have the option of `-DTOOLCHAIN_HAS_<NEWLIB|PICOLIBC>=ON`,
but this is not widely known and cumbersome to do for each build.

An indication of newlib or picolibc support is the presence of library
specific headers, so to improve current situation we check for library
specific headers, and if those are present we assume support for the
library.

This commit improves the current support for LLVM in Zephyr when
cross-compiling, especially for users of LLVM for Arm embedded.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2024-10-04 16:34:35 +01:00

45 lines
2.0 KiB
CMake

# SPDX-License-Identifier: Apache-2.0
# Purpose of the generic.cmake is to define a generic C compiler which can be
# used for devicetree pre-processing and other pre-processing tasks which must
# be performed before the target can be determined.
# Todo: deprecate CLANG_ROOT_DIR
set_ifndef(LLVM_TOOLCHAIN_PATH "$ENV{CLANG_ROOT_DIR}")
zephyr_get(LLVM_TOOLCHAIN_PATH)
if(LLVM_TOOLCHAIN_PATH)
set(TOOLCHAIN_HOME ${LLVM_TOOLCHAIN_PATH}/bin/)
endif()
set(LLVM_TOOLCHAIN_PATH ${CLANG_ROOT_DIR} CACHE PATH "clang install directory")
set(COMPILER clang)
set(BINTOOLS llvm)
# LLVM is flexible, meaning that it can in principle always support newlib or picolibc.
# This is not decided by LLVM itself, but depends on libraries distributed with the installation.
# Also newlib or picolibc may be created as add-ons. Thus always stating that LLVM does not have
# newlib or picolibc would be wrong. Same with stating that LLVM has newlib or Picolibc.
# The best assumption for TOOLCHAIN_HAS_<NEWLIB|PICOLIBC> is to check for the presence of
# '_newlib_version.h' / 'picolibc' and have the default value set accordingly.
# This provides a best effort mechanism to allow developers to have the newlib C / Picolibc library
# selection available in Kconfig.
# Developers can manually indicate library support with '-DTOOLCHAIN_HAS_<NEWLIB|PICOLIBC>=<ON|OFF>'
# Support for newlib is indicated by the presence of '_newlib_version.h' in the toolchain path.
if(NOT LLVM_TOOLCHAIN_PATH STREQUAL "")
file(GLOB_RECURSE newlib_header ${LLVM_TOOLCHAIN_PATH}/_newlib_version.h)
if(newlib_header)
set(TOOLCHAIN_HAS_NEWLIB ON CACHE BOOL "True if toolchain supports newlib")
endif()
# Support for picolibc is indicated by the presence of 'picolibc.h' in the toolchain path.
file(GLOB_RECURSE picolibc_header ${LLVM_TOOLCHAIN_PATH}/picolibc.h)
if(picolibc_header)
set(TOOLCHAIN_HAS_PICOLIBC ON CACHE BOOL "True if toolchain supports picolibc")
endif()
endif()
message(STATUS "Found toolchain: llvm (clang/ld)")