From 2d8b3c758c0574ef59a215182dad281359935317 Mon Sep 17 00:00:00 2001 From: Jonathon Penix Date: Tue, 27 Aug 2024 17:11:06 -0700 Subject: [PATCH 1/7] [nrf fromtree] cmake: linker: Use the same linker for cmake checks and final build Currently, the linker that is used when performing various cmake checks (check_c_compiler_flag, for example) may be different than the linker that will be used during the actual build. This happens as we currently specify '-fuse-ld' to force the appropriate linker a) after many such checks have already happened and b) in a way which is not automatically propagated to check_c_compiler_flag (and friends). As a result, the toolchain's default linker will generally be used for such checks regardless of which linker was selected in Zephyr. This can lead to a number of surprises when building Zephyr, particularly when building with clang. For example: - If the linker is misconfigured, where the build will fail can vary depending on whether the linker is the toolchain's default. When the configured linker happens to be the toolchain's default, the build (helpfully) fails quickly on the checks for a basic working toochain. When the configured linker isn't the default, the build won't fail until the final link steps. - The build can fail due to issues with a linker other than the one configured by the user in Zephyr. For example, LLVM toolchains without lld will generally fail to build Zephyr (the checks for a basic working toochain will fail) for targets where lld is the default in LLVM even if GNU ld is configured in Zephyr and would otherwise be used in the final build. - Flags which are only added if check_c_compiler_flag (or similar) succeeds may be unexpectedly omitted during the final build if the flag is supported in the configured linker but is unsupported in the toolchain's default linker (as check_c_compiler_flag will test using the default one). Note that this isn't limited to clang--even when we are building with Zephyr's SDK and force ld.bfd, we seem to use the 'ld' variant during the cmake checks (though this generally seems fairly harmless compared to mixing ld/lld or other proprietary linkers). To fix this, ensure the appropriate 'fuse-ld' is set early enough and in such a way that the same linker will be used throughout the entire build. Signed-off-by: Jonathon Penix Signed-off-by: Torsten Rasmussen (cherry picked from commit 9fe6c5e3fb38d8b0b62b173eeaff48334ecfb060) --- cmake/linker/ld/target.cmake | 15 ++++++++------- cmake/linker/lld/target.cmake | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmake/linker/ld/target.cmake b/cmake/linker/ld/target.cmake index ed29e88aa16..4382f156dcc 100644 --- a/cmake/linker/ld/target.cmake +++ b/cmake/linker/ld/target.cmake @@ -6,6 +6,14 @@ set(CMAKE_LINKER ${GNULD_LINKER}) set_ifndef(LINKERFLAGPREFIX -Wl) +if((${CMAKE_LINKER} STREQUAL "${CROSS_COMPILE}ld.bfd") OR + ${GNULD_LINKER_IS_BFD}) + # ld.bfd was found so let's explicitly use that for linking, see #32237 + list(APPEND TOOLCHAIN_LD_FLAGS -fuse-ld=bfd) + list(APPEND CMAKE_REQUIRED_FLAGS -fuse-ld=bfd) + string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") +endif() + if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "host") if(CONFIG_CPP_EXCEPTIONS AND LIBGCC_DIR) # When building with C++ Exceptions, it is important that crtbegin and crtend @@ -118,16 +126,9 @@ function(toolchain_ld_link_elf) ${ARGN} # input args to parse ) - if((${CMAKE_LINKER} STREQUAL "${CROSS_COMPILE}ld.bfd") OR - ${GNULD_LINKER_IS_BFD}) - # ld.bfd was found so let's explicitly use that for linking, see #32237 - set(use_linker "-fuse-ld=bfd") - endif() - target_link_libraries( ${TOOLCHAIN_LD_LINK_ELF_TARGET_ELF} ${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_PRE_SCRIPT} - ${use_linker} ${TOPT} ${TOOLCHAIN_LD_LINK_ELF_LINKER_SCRIPT} ${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_POST_SCRIPT} diff --git a/cmake/linker/lld/target.cmake b/cmake/linker/lld/target.cmake index a16529eeef7..9ce3313334f 100644 --- a/cmake/linker/lld/target.cmake +++ b/cmake/linker/lld/target.cmake @@ -6,6 +6,10 @@ set(CMAKE_LINKER ${LLVMLLD_LINKER}) set_ifndef(LINKERFLAGPREFIX -Wl) +list(APPEND TOOLCHAIN_LD_FLAGS -fuse-ld=lld) +list(APPEND CMAKE_REQUIRED_FLAGS -fuse-ld=lld) +string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + # Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen} # NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time macro(configure_linker_script linker_script_gen linker_pass_define) From fd22934cdc4c13e3cf99d9836032c5ca6f1682e5 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 6 Aug 2024 10:13:31 +0200 Subject: [PATCH 2/7] [nrf fromtree] cmake: improve Zephyr link phase Zephyr is a bare metal build where standard libs are disabled. This means that c and runtime libraries must manually be linked in. This has generally been handled by using CMake's link libraries handling but the issue with that is both de-duplication but also library link order. Standard libraries must be linked at last location to ensure symbols are always available, however this is not optimal with target_link_libraries() because this would ultimately require every library to know the c library to link with, which is not desired. Therefore, setup standard C and runtime library linking in linker CMake files for toolchains where this is required. This commit expands the principle introduced with toolchain abstraction, see PR#24851. This means that a toolchain implementation may specify standard C, runtime, C++, etc libraries, as well as their link order. Because a property approach is used, then Zephyr modules, such as the Picolibc module can adjust such properties. An optional `zephyr_linker_finalize()` macro is called at the end of Zephyr's CMakeList process and can be used by the toolchain implementation to define the final linker invocation. This aligns the linker handling flow to the principle introduced in PR#24851 and improves the flexibility and robustness of Zephyr build system. Signed-off-by: Torsten Rasmussen (cherry picked from commit 2e3873adde2386e0bc9acdbcdc8d389fdf4b9aee) --- CMakeLists.txt | 2 + cmake/compiler/clang/target.cmake | 4 +- cmake/compiler/gcc/target.cmake | 3 +- cmake/linker/armlink/linker_flags.cmake | 6 --- cmake/linker/armlink/linker_libraries.cmake | 8 ++++ cmake/linker/armlink/target.cmake | 18 ++++++++ cmake/linker/ld/linker_flags.cmake | 4 -- cmake/linker/ld/linker_libraries.cmake | 38 ++++++++++++++++ cmake/linker/ld/target.cmake | 46 ++++++++++++++------ cmake/linker/linker_libraries_template.cmake | 16 +++++++ cmake/linker/lld/linker_flags.cmake | 4 +- cmake/linker/lld/linker_libraries.cmake | 21 +++++++++ cmake/linker/lld/target.cmake | 22 +++++++++- cmake/linker/target_template.cmake | 13 ++++++ cmake/linker/xt-ld/target.cmake | 22 +++++++++- cmake/modules/FindTargetTools.cmake | 2 + cmake/modules/extensions.cmake | 33 ++++++++++++++ cmake/target_toolchain_flags.cmake | 2 + cmake/toolchain/llvm/generic.cmake | 2 +- lib/libc/newlib/CMakeLists.txt | 28 ------------ lib/libc/picolibc/CMakeLists.txt | 2 +- 21 files changed, 234 insertions(+), 62 deletions(-) delete mode 100644 cmake/linker/armlink/linker_flags.cmake create mode 100644 cmake/linker/armlink/linker_libraries.cmake create mode 100644 cmake/linker/ld/linker_libraries.cmake create mode 100644 cmake/linker/linker_libraries_template.cmake create mode 100644 cmake/linker/lld/linker_libraries.cmake create mode 100644 cmake/linker/target_template.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f3895a22cc..410aff11ab4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2233,6 +2233,8 @@ add_subdirectory_ifdef( cmake/makefile_exports ) +toolchain_linker_finalize() + yaml_context(EXISTS NAME build_info result) if(result) build_info(zephyr version VALUE ${PROJECT_VERSION_STR}) diff --git a/cmake/compiler/clang/target.cmake b/cmake/compiler/clang/target.cmake index 30e5da399af..d2d41e8b23c 100644 --- a/cmake/compiler/clang/target.cmake +++ b/cmake/compiler/clang/target.cmake @@ -76,8 +76,8 @@ if(NOT "${ARCH}" STREQUAL "posix") get_filename_component(RTLIB_NAME_WITH_PREFIX ${RTLIB_FILE_NAME} NAME_WLE) string(REPLACE lib "" RTLIB_NAME ${RTLIB_NAME_WITH_PREFIX}) - list(APPEND LIB_INCLUDE_DIR -L${RTLIB_DIR}) - list(APPEND TOOLCHAIN_LIBS ${RTLIB_NAME}) + set_property(TARGET linker PROPERTY lib_include_dir "-L${RTLIB_DIR}") + set_property(TARGET linker PROPERTY rt_library "-l${RTLIB_NAME}") list(APPEND CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags}) string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") diff --git a/cmake/compiler/gcc/target.cmake b/cmake/compiler/gcc/target.cmake index 5b1e5db1218..7e8ffc48173 100644 --- a/cmake/compiler/gcc/target.cmake +++ b/cmake/compiler/gcc/target.cmake @@ -109,8 +109,7 @@ get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY) assert_exists(LIBGCC_DIR) -LIST(APPEND LIB_INCLUDE_DIR "-L\"${LIBGCC_DIR}\"") -LIST(APPEND TOOLCHAIN_LIBS gcc) +set_linker_property(PROPERTY lib_include_dir "-L\"${LIBGCC_DIR}\"") # For CMake to be able to test if a compiler flag is supported by the # toolchain we need to give CMake the necessary flags to compile and diff --git a/cmake/linker/armlink/linker_flags.cmake b/cmake/linker/armlink/linker_flags.cmake deleted file mode 100644 index b0f93a7b9e7..00000000000 --- a/cmake/linker/armlink/linker_flags.cmake +++ /dev/null @@ -1,6 +0,0 @@ -# The ARMClang linker, armlink, requires a dedicated linking signature in -# order for Zephyr to control the map file. - -set(CMAKE_C_LINK_EXECUTABLE " -o ") -set(CMAKE_CXX_LINK_EXECUTABLE " -o ") -set(CMAKE_ASM_LINK_EXECUTABLE " -o ") diff --git a/cmake/linker/armlink/linker_libraries.cmake b/cmake/linker/armlink/linker_libraries.cmake new file mode 100644 index 00000000000..8374e7c3cd2 --- /dev/null +++ b/cmake/linker/armlink/linker_libraries.cmake @@ -0,0 +1,8 @@ +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: Apache-2.0 + +# Per default armclang (Arm Compiler 6) doesn't need explicit C library linking +# so we only need to set link order linking in case a custom C library is linked +# in, such as picolibc. +set_property(TARGET linker APPEND PROPERTY link_order_library "c;rt") diff --git a/cmake/linker/armlink/target.cmake b/cmake/linker/armlink/target.cmake index 9aa32ad0e6f..b2e1e867f90 100644 --- a/cmake/linker/armlink/target.cmake +++ b/cmake/linker/armlink/target.cmake @@ -108,5 +108,23 @@ function(toolchain_ld_link_elf) ) endfunction(toolchain_ld_link_elf) +# This function will generate the correct CMAKE_C_LINK_EXECUTABLE / CMAKE_CXX_LINK_EXECUTABLE +# rule to ensure that standard c and runtime libraries are correctly placed +# and the end of link invocation and doesn't appear in the middle of the link +# command invocation. +macro(toolchain_linker_finalize) + set(zephyr_std_libs) + get_property(link_order TARGET linker PROPERTY link_order_library) + foreach(lib ${link_order}) + get_property(link_flag TARGET linker PROPERTY ${lib}_library) + set(zephyr_std_libs "${zephyr_std_libs} ${link_flag}") + endforeach() + + set(common_link " ${zephyr_std_libs} -o ") + set(CMAKE_C_LINK_EXECUTABLE " ${common_link}") + set(CMAKE_CXX_LINK_EXECUTABLE " ${common_link}") + set(CMAKE_ASM_LINK_EXECUTABLE " ${common_link}") +endmacro() + include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake) include(${ZEPHYR_BASE}/cmake/linker/ld/target_configure.cmake) diff --git a/cmake/linker/ld/linker_flags.cmake b/cmake/linker/ld/linker_flags.cmake index 49bba260d3a..5063ddb46fb 100644 --- a/cmake/linker/ld/linker_flags.cmake +++ b/cmake/linker/ld/linker_flags.cmake @@ -7,10 +7,6 @@ check_set_linker_property(TARGET linker PROPERTY base ${LINKERFLAGPREFIX},--build-id=none ) -if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY AND NOT CONFIG_EXTERNAL_MODULE_LIBCPP) - set_property(TARGET linker PROPERTY cpp_base -lstdc++) -endif() - check_set_linker_property(TARGET linker PROPERTY baremetal -nostdlib -static diff --git a/cmake/linker/ld/linker_libraries.cmake b/cmake/linker/ld/linker_libraries.cmake new file mode 100644 index 00000000000..1305d682ab5 --- /dev/null +++ b/cmake/linker/ld/linker_libraries.cmake @@ -0,0 +1,38 @@ +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: Apache-2.0 + +# Do not specify default link libraries when targeting host (native build). +if(NOT CONFIG_NATIVE_BUILD) + set_linker_property(NO_CREATE PROPERTY c_library "-lc") + set_linker_property(NO_CREATE PROPERTY rt_library "-lgcc") + set_linker_property(NO_CREATE PROPERTY c++_library "-lstdc++") + set_linker_property(NO_CREATE PROPERTY math_library "-lm") + # Keeping default include dir empty. The linker will then select libraries + # from its default search path. The toolchain may adjust the value to a + # specific location, for example gcc infrastructure will set the value based + # on output from --print-libgcc-file-name. + set_linker_property(NO_CREATE PROPERTY lib_include_dir "") +endif() + +if(CONFIG_CPP + AND NOT CONFIG_MINIMAL_LIBCPP + AND NOT CONFIG_NATIVE_LIBRARY + # When new link principle is fully introduced, then the below condition can + # be removed, and instead the external module c++ should use: + # set_property(TARGET linker PROPERTY c++_library "") + AND NOT CONFIG_EXTERNAL_MODULE_LIBCPP +) + set_property(TARGET linker PROPERTY link_order_library "c++") +endif() + + +if(CONFIG_NEWLIB_LIBC AND CMAKE_C_COMPILER_ID STREQUAL "GNU") + # We are using c;rt;c (expands to '-lc -lgcc -lc') in code below. + # This is needed because when linking with newlib on aarch64, then libgcc has a + # link dependency to libc (strchr), but libc also has dependencies to libgcc. + # Lib C depends on libgcc. e.g. libc.a(lib_a-fvwrite.o) references __aeabi_idiv + set_property(TARGET linker APPEND PROPERTY link_order_library "math;c;rt;c") +else() + set_property(TARGET linker APPEND PROPERTY link_order_library "c;rt") +endif() diff --git a/cmake/linker/ld/target.cmake b/cmake/linker/ld/target.cmake index 4382f156dcc..c941d8ee89e 100644 --- a/cmake/linker/ld/target.cmake +++ b/cmake/linker/ld/target.cmake @@ -14,18 +14,6 @@ if((${CMAKE_LINKER} STREQUAL "${CROSS_COMPILE}ld.bfd") OR string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") endif() -if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "host") - if(CONFIG_CPP_EXCEPTIONS AND LIBGCC_DIR) - # When building with C++ Exceptions, it is important that crtbegin and crtend - # are linked at specific locations. - # The location is so important that we cannot let this be controlled by normal - # link libraries, instead we must control the link command specifically as - # part of toolchain. - set(CMAKE_CXX_LINK_EXECUTABLE - " ${LIBGCC_DIR}/crtbegin.o -o ${LIBGCC_DIR}/crtend.o") - endif() -endif() - # Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen} # NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time macro(configure_linker_script linker_script_gen linker_pass_define) @@ -139,14 +127,44 @@ function(toolchain_ld_link_elf) ${LINKERFLAGPREFIX},--no-whole-archive ${NO_WHOLE_ARCHIVE_LIBS} $ - ${LIB_INCLUDE_DIR} -L${PROJECT_BINARY_DIR} - ${TOOLCHAIN_LIBS} ${TOOLCHAIN_LD_LINK_ELF_DEPENDENCIES} ) endfunction(toolchain_ld_link_elf) +# Function for finalizing link setup after Zephyr configuration has completed. +# +# This function will generate the correct CMAKE_C_LINK_EXECUTABLE / CMAKE_CXX_LINK_EXECUTABLE +# rule to ensure that standard c and runtime libraries are correctly placed +# and the end of link invocation and doesn't appear in the middle of the link +# command invocation. +macro(toolchain_linker_finalize) + get_property(zephyr_std_libs TARGET linker PROPERTY lib_include_dir) + get_property(link_order TARGET linker PROPERTY link_order_library) + foreach(lib ${link_order}) + get_property(link_flag TARGET linker PROPERTY ${lib}_library) + list(APPEND zephyr_std_libs "${link_flag}") + endforeach() + string(REPLACE ";" " " zephyr_std_libs "${zephyr_std_libs}") + + set(link_libraries " -o ${zephyr_std_libs}") + set(common_link " ${link_libraries}") + + set(CMAKE_ASM_LINK_EXECUTABLE " ${common_link}") + set(CMAKE_C_LINK_EXECUTABLE " ${common_link}") + + set(cpp_link "${common_link}") + if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "host") + if(CONFIG_CPP_EXCEPTIONS AND LIBGCC_DIR) + # When building with C++ Exceptions, it is important that crtbegin and crtend + # are linked at specific locations. + set(cpp_link " ${LIBGCC_DIR}/crtbegin.o ${link_libraries} ${LIBGCC_DIR}/crtend.o") + endif() + endif() + set(CMAKE_CXX_LINK_EXECUTABLE " ${cpp_link}") +endmacro() + # Load toolchain_ld-family macros include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_relocation.cmake) include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_configure.cmake) diff --git a/cmake/linker/linker_libraries_template.cmake b/cmake/linker/linker_libraries_template.cmake new file mode 100644 index 00000000000..481ee5c473e --- /dev/null +++ b/cmake/linker/linker_libraries_template.cmake @@ -0,0 +1,16 @@ +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: Apache-2.0 + +# Linker flags for fixed linking with standard libraries, such as the C and runtime libraries. +# It is the responsibility of the linker infrastructure to use those properties to specify the +# correct placement of those libraries for correct link order. +# For example, GCC usually has the order: -lc -lgcc +# It is also possible to define extra libraries of the form `_library`, and then include +# Fixed library search path can be defined in the `lib_include_dir` property if needed. +# in the link_order_property. +# Usage example: +# set_linker_property(PROPERTY lib_include_dir "-L/path/to/libs") +# set_linker_property(PROPERTY c_library "-lc") +# set_linker_property(PROPERTY rt_library "-lgcc") +# set_linker_property(PROPERTY link_order_library "c;rt") diff --git a/cmake/linker/lld/linker_flags.cmake b/cmake/linker/lld/linker_flags.cmake index f6e873ad631..f11139aa1e2 100644 --- a/cmake/linker/lld/linker_flags.cmake +++ b/cmake/linker/lld/linker_flags.cmake @@ -1,4 +1,6 @@ # Copyright (c) 2022 Google LLC +# Copyright (c) 2024 Nordic Semiconductor +# # SPDX-License-Identifier: Apache-2.0 # Since lld is a drop in replacement for ld, we can just use ld's flags as a base @@ -6,7 +8,7 @@ include(${ZEPHYR_BASE}/cmake/linker/ld/linker_flags.cmake OPTIONAL) if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY AND NOT CONFIG_EXTERNAL_MODULE_LIBCPP) - set_property(TARGET linker PROPERTY cpp_base -lc++ ${LINKERFLAGPREFIX},-z,norelro) + set_property(TARGET linker PROPERTY cpp_base ${LINKERFLAGPREFIX},-z,norelro) endif() # Force LLVM to use built-in lld linker diff --git a/cmake/linker/lld/linker_libraries.cmake b/cmake/linker/lld/linker_libraries.cmake new file mode 100644 index 00000000000..2347898ad64 --- /dev/null +++ b/cmake/linker/lld/linker_libraries.cmake @@ -0,0 +1,21 @@ +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: Apache-2.0 + +set_linker_property(NO_CREATE TARGET linker PROPERTY c_library "-lc") +# Default per standard, will be populated by clang/target.cmake based on clang output. +set_linker_property(NO_CREATE TARGET linker PROPERTY rt_library "") +set_linker_property(TARGET linker PROPERTY c++_library "-lc++;-lc++abi") + +if(CONFIG_CPP + AND NOT CONFIG_MINIMAL_LIBCPP + AND NOT CONFIG_NATIVE_LIBRARY + # When new link principle is fully introduced, then the below condition can + # be removed, and instead the external module c++ should use: + # set_property(TARGET linker PROPERTY c++_library "") + AND NOT CONFIG_EXTERNAL_MODULE_LIBCPP +) + set_property(TARGET linker PROPERTY link_order_library "c++") +endif() + +set_property(TARGET linker APPEND PROPERTY link_order_library "c;rt") diff --git a/cmake/linker/lld/target.cmake b/cmake/linker/lld/target.cmake index 9ce3313334f..b6b96525e70 100644 --- a/cmake/linker/lld/target.cmake +++ b/cmake/linker/lld/target.cmake @@ -101,14 +101,32 @@ function(toolchain_ld_link_elf) ${LINKERFLAGPREFIX},--no-whole-archive ${NO_WHOLE_ARCHIVE_LIBS} $ - ${LIB_INCLUDE_DIR} -L${PROJECT_BINARY_DIR} - ${TOOLCHAIN_LIBS} ${TOOLCHAIN_LD_LINK_ELF_DEPENDENCIES} ) endfunction(toolchain_ld_link_elf) +# Function for finalizing link setup after Zephyr configuration has completed. +# +# This function will generate the correct CMAKE_C_LINK_EXECUTABLE / CMAKE_CXX_LINK_EXECUTABLE +# signature to ensure that standard c and runtime libraries are correctly placed +# and the end of link invocation and doesn't appear in the middle of the link +# command invocation. +macro(toolchain_linker_finalize) + get_property(zephyr_std_libs TARGET linker PROPERTY lib_include_dir) + get_property(link_order TARGET linker PROPERTY link_order_library) + foreach(lib ${link_order}) + get_property(link_flag TARGET linker PROPERTY ${lib}_library) + list(APPEND zephyr_std_libs "${link_flag}") + endforeach() + string(REPLACE ";" " " zephyr_std_libs "${zephyr_std_libs}") + + set(common_link " -o ${zephyr_std_libs}") + set(CMAKE_ASM_LINK_EXECUTABLE " ${common_link}") + set(CMAKE_C_LINK_EXECUTABLE " ${common_link}") + set(CMAKE_CXX_LINK_EXECUTABLE " ${common_link}") +endmacro() # Load toolchain_ld-family macros include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake) diff --git a/cmake/linker/target_template.cmake b/cmake/linker/target_template.cmake new file mode 100644 index 00000000000..efa27de6fb4 --- /dev/null +++ b/cmake/linker/target_template.cmake @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2024, Nordic Semiconductor ASA + +# Template file for optional Zephyr linker macros. +# +# This file will define optional linker macros for toolchains that are not +# defining these macros themselves. + +if(NOT COMMAND toolchain_linker_finalize) + macro(toolchain_linker_finalize) + endmacro() +endif() diff --git a/cmake/linker/xt-ld/target.cmake b/cmake/linker/xt-ld/target.cmake index 4f8e4fcfbdb..3546881cc71 100644 --- a/cmake/linker/xt-ld/target.cmake +++ b/cmake/linker/xt-ld/target.cmake @@ -129,7 +129,6 @@ function(toolchain_ld_link_elf) ${LINKERFLAGPREFIX},--no-whole-archive ${NO_WHOLE_ARCHIVE_LIBS} $ - ${LIB_INCLUDE_DIR} -L${PROJECT_BINARY_DIR} ${TOOLCHAIN_LIBS} @@ -137,6 +136,27 @@ function(toolchain_ld_link_elf) ) endfunction(toolchain_ld_link_elf) +# Function for finalizing link setup after Zephyr configuration has completed. +# +# This function will generate the correct CMAKE_C_LINK_EXECUTABLE / CMAKE_CXX_LINK_EXECUTABLE +# rule to ensure that standard c and runtime libraries are correctly placed +# and the end of link invocation and doesn't appear in the middle of the link +# command invocation. +macro(toolchain_linker_finalize) + get_property(zephyr_std_libs TARGET linker PROPERTY lib_include_dir) + get_property(link_order TARGET linker PROPERTY link_order_library) + foreach(lib ${link_order}) + get_property(link_flag TARGET linker PROPERTY ${lib}_library) + list(APPEND zephyr_std_libs "${link_flag}") + endforeach() + string(REPLACE ";" " " zephyr_std_libs "${zephyr_std_libs}") + + set(common_link " -o ${zephyr_std_libs}") + set(CMAKE_ASM_LINK_EXECUTABLE " ${common_link}") + set(CMAKE_C_LINK_EXECUTABLE " ${common_link}") + set(CMAKE_CXX_LINK_EXECUTABLE " ${common_link}") +endmacro() + # xt-ld is Xtensa's own version of binutils' ld. # So we can reuse most of the ld configurations. include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake) diff --git a/cmake/modules/FindTargetTools.cmake b/cmake/modules/FindTargetTools.cmake index a1fa4bf5c67..e35f577d697 100644 --- a/cmake/modules/FindTargetTools.cmake +++ b/cmake/modules/FindTargetTools.cmake @@ -105,5 +105,7 @@ include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/target.cmake OPTIONAL) include(${ZEPHYR_BASE}/cmake/bintools/bintools_template.cmake) include(${TOOLCHAIN_ROOT}/cmake/bintools/${BINTOOLS}/target.cmake OPTIONAL) +include(${TOOLCHAIN_ROOT}/cmake/linker/target_template.cmake) + set(TargetTools_FOUND TRUE) set(TARGETTOOLS_FOUND TRUE) diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index 23c30789b73..4773b563865 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -2410,6 +2410,39 @@ function(toolchain_parse_make_rule input_file include_files) set(${include_files} ${result} PARENT_SCOPE) endfunction() +# 'set_linker_property' is a function that sets the property for the linker +# property target used for toolchain abstraction. +# +# This function is similar in nature to the CMake set_property function, but +# with some additional extension flags for improved behavioral control. +# +# NO_CREATE: Flag to indicate that the property should only be set if not already +# defined with a value. +# APPEND: Flag indicated that the property should be appended to the existing +# value list for the property. +# TARGET: Name of target on which to add the property (commonly: linker) +# PROPERTY: Name of property with the value(s) following immediately after +# property name +function(set_linker_property) + set(options APPEND NO_CREATE) + set(single_args TARGET) + set(multi_args PROPERTY) + cmake_parse_arguments(LINKER_PROPERTY "${options}" "${single_args}" "${multi_args}" ${ARGN}) + + if(LINKER_PROPERTY_APPEND) + set(APPEND "APPEND") + endif() + + if(LINKER_PROPERTY_NO_CREATE) + list(GET LINKER_PROPERTY_PROPERTY 0 property_name) + get_target_property(var ${LINKER_PROPERTY_TARGET} ${property_name}) + if(NOT "${var}" STREQUAL "var-NOTFOUND") + return() + endif() + endif() + set_property(TARGET ${LINKER_PROPERTY_TARGET} ${APPEND} PROPERTY ${LINKER_PROPERTY_PROPERTY}) +endfunction() + # 'check_set_linker_property' is a function that check the provided linker # flag and only set the linker property if the check succeeds # diff --git a/cmake/target_toolchain_flags.cmake b/cmake/target_toolchain_flags.cmake index 1df7e5eddc6..716cc8f55e7 100644 --- a/cmake/target_toolchain_flags.cmake +++ b/cmake/target_toolchain_flags.cmake @@ -35,8 +35,10 @@ include(${CMAKE_CURRENT_LIST_DIR}/compiler/compiler_features.cmake) # a new toolchain. include(${CMAKE_CURRENT_LIST_DIR}/compiler/compiler_flags_template.cmake) include(${CMAKE_CURRENT_LIST_DIR}/linker/linker_flags_template.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/linker/linker_libraries_template.cmake) # Configure the toolchain flags based on what toolchain technology is used # (gcc, host-gcc etc.) include(${TOOLCHAIN_ROOT}/cmake/compiler/${COMPILER}/compiler_flags.cmake OPTIONAL) include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/linker_flags.cmake OPTIONAL) +include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/linker_libraries.cmake OPTIONAL) diff --git a/cmake/toolchain/llvm/generic.cmake b/cmake/toolchain/llvm/generic.cmake index 0b138c2868c..45474b7df30 100644 --- a/cmake/toolchain/llvm/generic.cmake +++ b/cmake/toolchain/llvm/generic.cmake @@ -19,4 +19,4 @@ set(BINTOOLS llvm) set(TOOLCHAIN_HAS_NEWLIB OFF CACHE BOOL "True if toolchain supports newlib") -message(STATUS "Found toolchain: host (clang/ld)") +message(STATUS "Found toolchain: llvm (clang/ld)") diff --git a/lib/libc/newlib/CMakeLists.txt b/lib/libc/newlib/CMakeLists.txt index de39b8ce213..d3dc448ecca 100644 --- a/lib/libc/newlib/CMakeLists.txt +++ b/lib/libc/newlib/CMakeLists.txt @@ -34,40 +34,12 @@ zephyr_compile_definitions(_ANSI_SOURCE) zephyr_compile_definitions(__LINUX_ERRNO_EXTENSIONS__) if(CMAKE_C_COMPILER_ID STREQUAL "GNU") - # We are using - # - ${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}c - # - ${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}gcc - # - c - # in code below. - # This is needed because when linking with newlib on aarch64, then libgcc has a - # link dependency to libc (strchr), but libc also has dependencies to libgcc. - # - # CMake is capable of handling circular link dependencies for CMake defined - # static libraries, which can be further controlled using LINK_INTERFACE_MULTIPLICITY. - # However, libc and libgcc are not regular CMake libraries, and is seen as linker - # flags by CMake, and thus symbol de-duplications will be performed. - # CMake link options cannot be used, as that will place those libs first on the - # linker invocation. -Wl,--start-group is problematic as the placement of -lc - # and -lgcc is not guaranteed in case later libraries are also using - # -lc / -libbgcc as interface linker flags. - # - # Thus, we resort to use `${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}` - # as this ensures the uniqueness and thus avoids symbol de-duplication which means - # libc will be followed by libgcc, which is finally followed by libc again. - - list(JOIN CMAKE_C_LINKER_WRAPPER_FLAG "" linker_wrapper_string) - zephyr_link_libraries( - m - "${linker_wrapper_string}${CMAKE_LINK_LIBRARY_FLAG}c" ${LIBC_LIBRARY_DIR_FLAG} # NB: Optional $<$:-u_printf_float> $<$:-u_scanf_float> - # Lib C depends on libgcc. e.g. libc.a(lib_a-fvwrite.o) references __aeabi_idiv - "${linker_wrapper_string}${CMAKE_LINK_LIBRARY_FLAG}gcc" ) endif() -zephyr_link_libraries(c) if(CONFIG_NEWLIB_LIBC_NANO) zephyr_link_libraries( diff --git a/lib/libc/picolibc/CMakeLists.txt b/lib/libc/picolibc/CMakeLists.txt index 87fb0d9d8ec..fcb4f9f2b57 100644 --- a/lib/libc/picolibc/CMakeLists.txt +++ b/lib/libc/picolibc/CMakeLists.txt @@ -17,7 +17,7 @@ if(NOT CONFIG_PICOLIBC_USE_MODULE) # Zephyr build (via the __ZEPHYR__ macro) to expose the Zephyr C API zephyr_compile_options(--specs=picolibc.specs) - zephyr_libc_link_libraries(--specs=picolibc.specs c -lgcc) + zephyr_libc_link_libraries(--specs=picolibc.specs) if(CONFIG_PICOLIBC_IO_FLOAT) zephyr_compile_definitions(PICOLIBC_DOUBLE_PRINTF_SCANF) zephyr_link_libraries(-DPICOLIBC_DOUBLE_PRINTF_SCANF) From 504fa6850dfde9b2fab136d7743712b2499aaa61 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 11 Sep 2024 08:51:47 +0200 Subject: [PATCH 3/7] [nrf fromtree] cmake: PROPERTY flag support on compile options and link libraries Extend zephyr_link_libraries to allow an optional value together with the `zephyr_link_libraries(PROPERTY [])`. This allow setting linker property combined with a value when linking Zephyr. The value will only be applied if the property is defined. Extend zephyr_compile_options to support the same PROPERTY flag that has been introduced for zephyr_link_libraries(). This remove the need for developers to write complex generator expressions for compiler flags and thus minimizes mistakes. The following syntax is now supported in addition to the existing syntax: `zephyr_compile_options(PROPERTY [])` Signed-off-by: Torsten Rasmussen (cherry picked from commit 718b726b3729d5a47ab79fda4d782b6f5ce197ad) --- cmake/modules/extensions.cmake | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index 4773b563865..1dc516460f4 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -109,16 +109,37 @@ endfunction() # https://cmake.org/cmake/help/latest/command/target_compile_options.html function(zephyr_compile_options) - target_compile_options(zephyr_interface INTERFACE ${ARGV}) + if(ARGV0 STREQUAL "PROPERTY") + set(property $) + set(property_defined $) + if(ARGC GREATER 3) + message(FATAL_ERROR "zephyr_compile_options(PROPERTY []) " + "called with too many arguments." + ) + elseif(ARGC EQUAL 3) + target_compile_options(zephyr_interface INTERFACE $<${property_defined}:${property}${ARGV2}>) + else() + target_compile_options(zephyr_interface INTERFACE ${property}) + endif() + else() + target_compile_options(zephyr_interface INTERFACE ${ARGV}) + endif() endfunction() # https://cmake.org/cmake/help/latest/command/target_link_libraries.html function(zephyr_link_libraries) if(ARGV0 STREQUAL "PROPERTY") - if(ARGC GREATER 2) - message(FATAL_ERROR "zephyr_link_libraries(PROPERTY ) only allows a single property.") + set(property $) + set(property_defined $) + if(ARGC GREATER 3) + message(FATAL_ERROR "zephyr_link_options(PROPERTY []) " + "called with too many arguments." + ) + elseif(ARGC EQUAL 3) + target_link_libraries(zephyr_interface INTERFACE $<${property_defined}:${property}${ARGV2}>) + else() + target_link_libraries(zephyr_interface INTERFACE ${property}) endif() - target_link_libraries(zephyr_interface INTERFACE $) else() target_link_libraries(zephyr_interface INTERFACE ${ARGV}) endif() From b8759b187d482a89113b5add56a5f0fff081e6c1 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 11 Sep 2024 10:18:13 +0200 Subject: [PATCH 4/7] [nrf fromtree] cmake: make TARGET optional in set_linker_property() The `check_set_linker_property()` and `set_linker_property()` takes a target argument. Make the target argument optional and use the target `linker` as default target. The function name `set_linker_property()` already implies that we are setting a property and the linker target. Remove the need to specify `TARGET linker` when using the default linker property target. Signed-off-by: Torsten Rasmussen (cherry picked from commit 102b3fc07836a137cf54bf39a626f3c332e72b71) --- cmake/modules/extensions.cmake | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index 1dc516460f4..921a457067d 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -2441,7 +2441,7 @@ endfunction() # defined with a value. # APPEND: Flag indicated that the property should be appended to the existing # value list for the property. -# TARGET: Name of target on which to add the property (commonly: linker) +# TARGET: Name of target on which to add the property (default: linker) # PROPERTY: Name of property with the value(s) following immediately after # property name function(set_linker_property) @@ -2454,6 +2454,10 @@ function(set_linker_property) set(APPEND "APPEND") endif() + if(NOT DEFINED LINKER_PROPERTY_TARGET) + set(LINKER_PROPERTY_TARGET "linker") + endif() + if(LINKER_PROPERTY_NO_CREATE) list(GET LINKER_PROPERTY_PROPERTY 0 property_name) get_target_property(var ${LINKER_PROPERTY_TARGET} ${property_name}) @@ -2473,7 +2477,7 @@ endfunction() # # APPEND: Flag indicated that the property should be appended to the existing # value list for the property. -# TARGET: Name of target on which to add the property (commonly: linker) +# TARGET: Name of target on which to add the property (default: linker) # PROPERTY: Name of property with the value(s) following immediately after # property name function(check_set_linker_property) @@ -2486,6 +2490,10 @@ function(check_set_linker_property) set(APPEND "APPEND") endif() + if(NOT DEFINED LINKER_PROPERTY_TARGET) + set(LINKER_PROPERTY_TARGET "linker") + endif() + list(GET LINKER_PROPERTY_PROPERTY 0 property) list(REMOVE_AT LINKER_PROPERTY_PROPERTY 0) From db054a11b952439abf30aa0f6899ee07fd736d4c Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Mon, 26 Aug 2024 14:57:15 +0200 Subject: [PATCH 5/7] [nrf fromtree] cmake: move specs compiler and linker flags to toolchain properties Moving specs argument to compiler and linker properties so that the compiler and linker in use can decide how the flags are mapped / handled for the compiler and linker in use. This avoids specifying `--specs=spec.picolibc` for clang which prints a warning about an unused argument. Signed-off-by: Torsten Rasmussen (cherry picked from commit 9d835fe464aff259cd1924e411a1be9c3f3e5787) --- cmake/compiler/clang/compiler_flags.cmake | 2 ++ cmake/compiler/compiler_flags_template.cmake | 4 ++++ cmake/compiler/gcc/compiler_flags.cmake | 2 ++ cmake/linker/ld/gcc/linker_flags.cmake | 2 ++ cmake/linker/linker_flags_template.cmake | 4 ++++ lib/libc/picolibc/CMakeLists.txt | 5 ++--- 6 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cmake/compiler/clang/compiler_flags.cmake b/cmake/compiler/clang/compiler_flags.cmake index acc9b629171..a64150eb58c 100644 --- a/cmake/compiler/clang/compiler_flags.cmake +++ b/cmake/compiler/clang/compiler_flags.cmake @@ -125,3 +125,5 @@ set_compiler_property(PROPERTY warning_error_coding_guideline ) set_compiler_property(PROPERTY no_global_merge "-mno-global-merge") + +set_compiler_property(PROPERTY specs) diff --git a/cmake/compiler/compiler_flags_template.cmake b/cmake/compiler/compiler_flags_template.cmake index c72e9b70f5d..5a4386f49be 100644 --- a/cmake/compiler/compiler_flags_template.cmake +++ b/cmake/compiler/compiler_flags_template.cmake @@ -139,3 +139,7 @@ set_compiler_property(PROPERTY warning_shadow_variables) # Compiler flags to avoid recognizing built-in functions set_compiler_property(PROPERTY no_builtin) set_compiler_property(PROPERTY no_builtin_malloc) + +# Compiler flag for defining specs. Used only by gcc, other compilers may keep +# this undefined. +set_compiler_property(PROPERTY specs) diff --git a/cmake/compiler/gcc/compiler_flags.cmake b/cmake/compiler/gcc/compiler_flags.cmake index e982f75aa78..c346a750af4 100644 --- a/cmake/compiler/gcc/compiler_flags.cmake +++ b/cmake/compiler/gcc/compiler_flags.cmake @@ -241,3 +241,5 @@ set_compiler_property(PROPERTY warning_shadow_variables -Wshadow) set_compiler_property(PROPERTY no_builtin -fno-builtin) set_compiler_property(PROPERTY no_builtin_malloc -fno-builtin-malloc) + +set_compiler_property(PROPERTY specs -specs=) diff --git a/cmake/linker/ld/gcc/linker_flags.cmake b/cmake/linker/ld/gcc/linker_flags.cmake index 4cde7d96ff3..c442c9c8921 100644 --- a/cmake/linker/ld/gcc/linker_flags.cmake +++ b/cmake/linker/ld/gcc/linker_flags.cmake @@ -15,3 +15,5 @@ add_link_options(-gdwarf-4) # Extra warnings options for twister run set_property(TARGET linker PROPERTY warnings_as_errors -Wl,--fatal-warnings) + +set_linker_property(PROPERTY specs -specs=) diff --git a/cmake/linker/linker_flags_template.cmake b/cmake/linker/linker_flags_template.cmake index 870c597be24..8b0b948b028 100644 --- a/cmake/linker/linker_flags_template.cmake +++ b/cmake/linker/linker_flags_template.cmake @@ -49,3 +49,7 @@ set_property(TARGET linker PROPERTY no_relax) # Linker flag for enabling relaxation of address optimization for jump calls. set_property(TARGET linker PROPERTY relax) + +# Linker flag for defining specs. Defined only by gcc, when gcc is used as +# front-end for ld. +set_compiler_property(PROPERTY specs) diff --git a/lib/libc/picolibc/CMakeLists.txt b/lib/libc/picolibc/CMakeLists.txt index fcb4f9f2b57..14af66b7b7d 100644 --- a/lib/libc/picolibc/CMakeLists.txt +++ b/lib/libc/picolibc/CMakeLists.txt @@ -15,9 +15,8 @@ if(NOT CONFIG_PICOLIBC_USE_MODULE) # Use picolibc provided with the toolchain. This requires a new enough # toolchain so that the version of picolibc supports auto-detecting a # Zephyr build (via the __ZEPHYR__ macro) to expose the Zephyr C API - - zephyr_compile_options(--specs=picolibc.specs) - zephyr_libc_link_libraries(--specs=picolibc.specs) + zephyr_compile_options(PROPERTY specs picolibc.specs) + zephyr_link_libraries(PROPERTY specs picolibc.specs) if(CONFIG_PICOLIBC_IO_FLOAT) zephyr_compile_definitions(PICOLIBC_DOUBLE_PRINTF_SCANF) zephyr_link_libraries(-DPICOLIBC_DOUBLE_PRINTF_SCANF) From 05deba9e3ee1af6d0e568087689370acdec0a857 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 12 Sep 2024 09:18:41 +0200 Subject: [PATCH 6/7] [nrf fromtree] manifest: update picolibc to support the new c_library property This commit updates picolibc module to remove the need for hard-coding linking with `-lgcc`. Instead it sets the c_library linker property and thereby allows the Zephyr toolchain infrastructure to properly handle the linking of C and runtime libraries. Signed-off-by: Torsten Rasmussen (cherry picked from commit b2eeef4ac9505d8b1d4cfbc51374a96b74064778) --- west.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/west.yml b/west.yml index 40c149d4dbd..4640623fdee 100644 --- a/west.yml +++ b/west.yml @@ -315,7 +315,7 @@ manifest: - debug - name: picolibc path: modules/lib/picolibc - revision: 06bde1fd7531b1f788f6e42b6f7b358c0fe4f814 + revision: 27746bbc246841852912fc3bb5b45094cd8a505a - name: segger revision: b011c45b585e097d95d9cf93edf4f2e01588d3cd path: modules/debug/segger From f040baa074ab86baa1a657ac89efd77c5925cb52 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 11 Sep 2024 23:39:50 +0200 Subject: [PATCH 7/7] [nrf fromtree] 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_=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 (cherry picked from commit 0274bcbee442441d3222747e536054cc911b6218) --- cmake/compiler/clang/target.cmake | 36 ++++++++++++++++++++++++++++++ cmake/toolchain/llvm/generic.cmake | 24 +++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/cmake/compiler/clang/target.cmake b/cmake/compiler/clang/target.cmake index d2d41e8b23c..62c39b0ec6f 100644 --- a/cmake/compiler/clang/target.cmake +++ b/cmake/compiler/clang/target.cmake @@ -64,6 +64,42 @@ if(NOT "${ARCH}" STREQUAL "posix") endif() endif() + # LLVM will use a default sysroot for selection of the C library. The default + # C library sysroot was defined at built time of clang/LLVM. + # + # For example, LLVM for Arm comes pre-built with Picolibc, and thus no flags + # are required for selecting Picolibc. + # + # Other clang/LLVM distributions may come with other pre-built C libraries. + # clang/LLVM supports using an alternative C library, either by direct linking, + # or by specifying '--sysroot '. + # + # LLVM for Arm provides a 'newlib.cfg' file for newlib C selection. + # Let us support this principle by looking for a dedicated 'newlib.cfg' or + # 'picolibc.cfg' and specify '--config .cfg' if such a file is found. + # If no cfg-file matching the chosen C implementation, then we assume that the + # chosen C implementation is identical to the default C library used be the + # toolchain. + if(CONFIG_NEWLIB_LIBC) + file(GLOB_RECURSE newlib_cfg ${LLVM_TOOLCHAIN_PATH}/newlib.cfg) + if(newlib_cfg) + list(GET newlib_cfg 0 newlib_cfg) + set_linker_property(PROPERTY c_library "--config=${newlib_cfg};-lc") + list(APPEND CMAKE_REQUIRED_FLAGS --config=${newlib_cfg}) + list(APPEND TOOLCHAIN_C_FLAGS --config=${newlib_cfg}) + endif() + endif() + + if(CONFIG_PICOLIBC) + file(GLOB_RECURSE picolibc_cfg ${LLVM_TOOLCHAIN_PATH}/picolibc.cfg) + if(picolibc_cfg) + list(GET picolibc_cfg 0 picolibc_cfg) + set_linker_property(PROPERTY c_library "--config=${picolibc_cfg};-lc") + list(APPEND CMAKE_REQUIRED_FLAGS --config=${picolibc_cfg}) + list(APPEND TOOLCHAIN_C_FLAGS --config=${picolibc_cfg}) + endif() + endif() + # This libgcc code is partially duplicated in compiler/*/target.cmake execute_process( COMMAND ${CMAKE_C_COMPILER} ${clang_target_flag} ${TOOLCHAIN_C_FLAGS} diff --git a/cmake/toolchain/llvm/generic.cmake b/cmake/toolchain/llvm/generic.cmake index 45474b7df30..f6d7878288d 100644 --- a/cmake/toolchain/llvm/generic.cmake +++ b/cmake/toolchain/llvm/generic.cmake @@ -17,6 +17,28 @@ set(LLVM_TOOLCHAIN_PATH ${CLANG_ROOT_DIR} CACHE PATH "clang install directory") set(COMPILER clang) set(BINTOOLS llvm) -set(TOOLCHAIN_HAS_NEWLIB OFF CACHE BOOL "True if toolchain supports newlib") +# 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_ 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_=' + +# 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)")