From 07f81d2c3995558908e823ec4f61e9e72d903a16 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 4 Dec 2024 13:30:44 +0100 Subject: [PATCH 01/16] Revert "[nrf fromtree] cmake: update build_info() calls to use PATH argument" This reverts commit 18ba09b105d2c95c79432df84455f640dfa211c9. Signed-off-by: Torsten Rasmussen --- cmake/modules/FindHostTools.cmake | 2 +- cmake/modules/dts.cmake | 10 +++++----- cmake/modules/kconfig.cmake | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmake/modules/FindHostTools.cmake b/cmake/modules/FindHostTools.cmake index 1dfce4d3d4a..876a86934b1 100644 --- a/cmake/modules/FindHostTools.cmake +++ b/cmake/modules/FindHostTools.cmake @@ -116,4 +116,4 @@ set(HostTools_FOUND TRUE) set(HOSTTOOLS_FOUND TRUE) build_info(toolchain name VALUE ${ZEPHYR_TOOLCHAIN_VARIANT}) string(TOUPPER ${ZEPHYR_TOOLCHAIN_VARIANT} zephyr_toolchain_variant_upper) -build_info(toolchain path PATH "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}") +build_info(toolchain path VALUE "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}") diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index eb7869548fe..b029dd5be3e 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -182,7 +182,7 @@ set(dts_files if(DTC_OVERLAY_FILE) zephyr_list(TRANSFORM DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE DTC_OVERLAY_FILE_AS_LIST) - build_info(devicetree user-files PATH ${DTC_OVERLAY_FILE_AS_LIST}) + build_info(devicetree user-files VALUE ${DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${DTC_OVERLAY_FILE_AS_LIST} @@ -192,7 +192,7 @@ endif() if(EXTRA_DTC_OVERLAY_FILE) zephyr_list(TRANSFORM EXTRA_DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE EXTRA_DTC_OVERLAY_FILE_AS_LIST) - build_info(devicetree extra-user-files PATH ${EXTRA_DTC_OVERLAY_FILE_AS_LIST}) + build_info(devicetree extra-user-files VALUE ${EXTRA_DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${EXTRA_DTC_OVERLAY_FILE_AS_LIST} @@ -399,6 +399,6 @@ elseif(stderr) endif() endif(DTC) -build_info(devicetree files PATH ${dts_files}) -build_info(devicetree include-dirs PATH ${DTS_ROOT_SYSTEM_INCLUDE_DIRS}) -build_info(devicetree bindings-dirs PATH ${DTS_ROOT_BINDINGS}) +build_info(devicetree files VALUE ${dts_files}) +build_info(devicetree include-dirs VALUE ${DTS_ROOT_SYSTEM_INCLUDE_DIRS}) +build_info(devicetree bindings-dirs VALUE ${DTS_ROOT_BINDINGS}) diff --git a/cmake/modules/kconfig.cmake b/cmake/modules/kconfig.cmake index 97086cdf19d..43e73511768 100644 --- a/cmake/modules/kconfig.cmake +++ b/cmake/modules/kconfig.cmake @@ -98,13 +98,13 @@ set(PARSED_KCONFIG_SOURCES_TXT ${PROJECT_BINARY_DIR}/kconfig/sources.txt) if(CONF_FILE) string(CONFIGURE "${CONF_FILE}" CONF_FILE_EXPANDED) string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE_EXPANDED}") - build_info(kconfig user-files PATH ${CONF_FILE_AS_LIST}) + build_info(kconfig user-files VALUE ${CONF_FILE_AS_LIST}) endif() if(EXTRA_CONF_FILE) string(CONFIGURE "${EXTRA_CONF_FILE}" EXTRA_CONF_FILE_EXPANDED) string(REPLACE " " ";" EXTRA_CONF_FILE_AS_LIST "${EXTRA_CONF_FILE_EXPANDED}") - build_info(kconfig extra-user-files PATH ${EXTRA_CONF_FILE_AS_LIST}) + build_info(kconfig extra-user-files VALUE ${EXTRA_CONF_FILE_AS_LIST}) endif() zephyr_file(CONF_FILES ${BOARD_EXTENSION_DIRS} KCONF board_extension_conf_files SUFFIX ${FILE_SUFFIX}) @@ -360,7 +360,7 @@ endif() if(CREATE_NEW_DOTCONFIG) set(input_configs_flags --handwritten-input-configs) set(input_configs ${merge_config_files} ${FORCED_CONF_FILE}) - build_info(kconfig files PATH ${input_configs}) + build_info(kconfig files VALUE ${input_configs}) else() set(input_configs ${DOTCONFIG} ${FORCED_CONF_FILE}) endif() From ac636d66b7298f3f3fbc5fa3721b07a6e42f4e3a Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 4 Dec 2024 13:30:46 +0100 Subject: [PATCH 02/16] Revert "[nrf fromtree] cmake: nordic: save build information regarding SVD file used." This reverts commit 5767e2e5e2e342943188e53f2d61001eae0f1769. Signed-off-by: Torsten Rasmussen --- modules/hal_nordic/nrfx/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/hal_nordic/nrfx/CMakeLists.txt b/modules/hal_nordic/nrfx/CMakeLists.txt index 1665c122c9f..3863fa177ed 100644 --- a/modules/hal_nordic/nrfx/CMakeLists.txt +++ b/modules/hal_nordic/nrfx/CMakeLists.txt @@ -242,4 +242,3 @@ mdk_svd_ifdef(CONFIG_SOC_NRF9160 nrf9160.svd) mdk_svd_ifdef(CONFIG_SOC_NRF9230_ENGB_CPUAPP nrf9230_engb_application.svd) mdk_svd_ifdef(CONFIG_SOC_NRF9230_ENGB_CPUPPR nrf9230_engb_ppr.svd) mdk_svd_ifdef(CONFIG_SOC_NRF9230_ENGB_CPURAD nrf9230_engb_radiocore.svd) -build_info(vendor-specific nordic svdfile VALUE ${SOC_SVD_FILE}) From b18c567248d47e920e0da7bb7fc7cc85ae0f35de Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 4 Dec 2024 13:30:46 +0100 Subject: [PATCH 03/16] Revert "[nrf fromtree] cmake: support build info in Zephyr" This reverts commit 94604d6b81cd855edd25afd19e21dd110d984fe0. Signed-off-by: Torsten Rasmussen --- CMakeLists.txt | 7 ------- cmake/modules/FindHostTools.cmake | 3 --- cmake/modules/boards.cmake | 4 ---- cmake/modules/configuration_files.cmake | 2 -- cmake/modules/dts.cmake | 6 ------ cmake/modules/kconfig.cmake | 3 --- share/sysbuild/CMakeLists.txt | 7 ------- 7 files changed, 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f3895a22cc..952acccbba8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2232,10 +2232,3 @@ add_subdirectory_ifdef( CONFIG_MAKEFILE_EXPORTS cmake/makefile_exports ) - -yaml_context(EXISTS NAME build_info result) -if(result) - build_info(zephyr version VALUE ${PROJECT_VERSION_STR}) - build_info(zephyr zephyr-base VALUE ${ZEPHYR_BASE}) - yaml_save(NAME build_info) -endif() diff --git a/cmake/modules/FindHostTools.cmake b/cmake/modules/FindHostTools.cmake index 876a86934b1..8d3c9eccaaf 100644 --- a/cmake/modules/FindHostTools.cmake +++ b/cmake/modules/FindHostTools.cmake @@ -114,6 +114,3 @@ set_ifndef(TOOLCHAIN_KCONFIG_DIR ${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOL set(HostTools_FOUND TRUE) set(HOSTTOOLS_FOUND TRUE) -build_info(toolchain name VALUE ${ZEPHYR_TOOLCHAIN_VARIANT}) -string(TOUPPER ${ZEPHYR_TOOLCHAIN_VARIANT} zephyr_toolchain_variant_upper) -build_info(toolchain path VALUE "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}") diff --git a/cmake/modules/boards.cmake b/cmake/modules/boards.cmake index 2b78845482a..d13e0a16f2d 100644 --- a/cmake/modules/boards.cmake +++ b/cmake/modules/boards.cmake @@ -353,7 +353,3 @@ if(BOARD_EXTENSIONS) list(APPEND BOARD_EXTENSION_DIRS ${board_extension_dir}) endforeach() endif() -build_info(board name VALUE ${BOARD}) -string(REGEX REPLACE "^/" "" qualifiers "${BOARD_QUALIFIERS}") -build_info(board qualifiers VALUE ${qualifiers}) -build_info(board revision VALUE ${BOARD_REVISION}) diff --git a/cmake/modules/configuration_files.cmake b/cmake/modules/configuration_files.cmake index ff7a172445c..84af8c5b635 100644 --- a/cmake/modules/configuration_files.cmake +++ b/cmake/modules/configuration_files.cmake @@ -99,5 +99,3 @@ zephyr_boilerplate_watch(DTC_OVERLAY_FILE) zephyr_get(EXTRA_CONF_FILE SYSBUILD LOCAL VAR EXTRA_CONF_FILE OVERLAY_CONFIG MERGE REVERSE) zephyr_get(EXTRA_DTC_OVERLAY_FILE SYSBUILD LOCAL MERGE REVERSE) zephyr_get(DTS_EXTRA_CPPFLAGS SYSBUILD LOCAL MERGE REVERSE) -build_info(application source-dir VALUE ${APPLICATION_SOURCE_DIR}) -build_info(application configuration-dir VALUE ${APPLICATION_CONFIG_DIR}) diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index b029dd5be3e..42e822c6c91 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -182,7 +182,6 @@ set(dts_files if(DTC_OVERLAY_FILE) zephyr_list(TRANSFORM DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE DTC_OVERLAY_FILE_AS_LIST) - build_info(devicetree user-files VALUE ${DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${DTC_OVERLAY_FILE_AS_LIST} @@ -192,7 +191,6 @@ endif() if(EXTRA_DTC_OVERLAY_FILE) zephyr_list(TRANSFORM EXTRA_DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE EXTRA_DTC_OVERLAY_FILE_AS_LIST) - build_info(devicetree extra-user-files VALUE ${EXTRA_DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${EXTRA_DTC_OVERLAY_FILE_AS_LIST} @@ -398,7 +396,3 @@ elseif(stderr) message(WARNING "dtc raised one or more warnings:\n${stderr}") endif() endif(DTC) - -build_info(devicetree files VALUE ${dts_files}) -build_info(devicetree include-dirs VALUE ${DTS_ROOT_SYSTEM_INCLUDE_DIRS}) -build_info(devicetree bindings-dirs VALUE ${DTS_ROOT_BINDINGS}) diff --git a/cmake/modules/kconfig.cmake b/cmake/modules/kconfig.cmake index 43e73511768..1e156bff470 100644 --- a/cmake/modules/kconfig.cmake +++ b/cmake/modules/kconfig.cmake @@ -98,13 +98,11 @@ set(PARSED_KCONFIG_SOURCES_TXT ${PROJECT_BINARY_DIR}/kconfig/sources.txt) if(CONF_FILE) string(CONFIGURE "${CONF_FILE}" CONF_FILE_EXPANDED) string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE_EXPANDED}") - build_info(kconfig user-files VALUE ${CONF_FILE_AS_LIST}) endif() if(EXTRA_CONF_FILE) string(CONFIGURE "${EXTRA_CONF_FILE}" EXTRA_CONF_FILE_EXPANDED) string(REPLACE " " ";" EXTRA_CONF_FILE_AS_LIST "${EXTRA_CONF_FILE_EXPANDED}") - build_info(kconfig extra-user-files VALUE ${EXTRA_CONF_FILE_AS_LIST}) endif() zephyr_file(CONF_FILES ${BOARD_EXTENSION_DIRS} KCONF board_extension_conf_files SUFFIX ${FILE_SUFFIX}) @@ -360,7 +358,6 @@ endif() if(CREATE_NEW_DOTCONFIG) set(input_configs_flags --handwritten-input-configs) set(input_configs ${merge_config_files} ${FORCED_CONF_FILE}) - build_info(kconfig files VALUE ${input_configs}) else() set(input_configs ${DOTCONFIG} ${FORCED_CONF_FILE}) endif() diff --git a/share/sysbuild/CMakeLists.txt b/share/sysbuild/CMakeLists.txt index f2fbb4b2718..8370490fb94 100644 --- a/share/sysbuild/CMakeLists.txt +++ b/share/sysbuild/CMakeLists.txt @@ -25,10 +25,3 @@ if(EXISTS ${APP_DIR}/sysbuild/CMakeLists.txt) else() add_subdirectory(template _sysbuild) endif() - -build_info(sysbuild VALUE true) -build_info(application source-dir VALUE ${CMAKE_CURRENT_SOURCE_DIR}) -yaml_context(EXISTS NAME build_info result) -if(result) - yaml_save(NAME build_info) -endif() From ce9d8d329702fac22c068e519033b6954b36a5a4 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 26 Sep 2024 11:29:55 -0700 Subject: [PATCH 04/16] [nrf fromtree] picolibc: Update module to version 1.8.8 Switch to released version with all of the Zephyr build fixes Signed-off-by: Keith Packard (cherry picked from commit d5b653961487ce516dc0eefca65d19dfc72a1497) --- west.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/west.yml b/west.yml index 11cd720a905..4ca8aa06f6a 100644 --- a/west.yml +++ b/west.yml @@ -318,7 +318,7 @@ manifest: - debug - name: picolibc path: modules/lib/picolibc - revision: 06bde1fd7531b1f788f6e42b6f7b358c0fe4f814 + revision: e15656f7682500fc8953e1a2008f7b1ae4f65ce8 - name: segger revision: b011c45b585e097d95d9cf93edf4f2e01588d3cd path: modules/debug/segger From 0747c5c53279bfa85f79c26cb3e15f579e8bd033 Mon Sep 17 00:00:00 2001 From: Jonathon Penix Date: Tue, 27 Aug 2024 17:11:06 -0700 Subject: [PATCH 05/16] [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 2b27d6421afa20738d04932bc6f9fd774ca0b8fe Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 6 Aug 2024 10:13:31 +0200 Subject: [PATCH 06/16] [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 952acccbba8..cbd6ac7bb1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2232,3 +2232,5 @@ add_subdirectory_ifdef( CONFIG_MAKEFILE_EXPORTS cmake/makefile_exports ) + +toolchain_linker_finalize() 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 9a8b56b52f4777623efe97134be8630a742862f8 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 11 Sep 2024 08:51:47 +0200 Subject: [PATCH 07/16] [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 497a0be5cb333b85aa3e3f0b9b3025d727599445 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 11 Sep 2024 10:18:13 +0200 Subject: [PATCH 08/16] [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 e338119fb232f5b5ba1f360abe1b8533dd0de39b Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Mon, 26 Aug 2024 14:57:15 +0200 Subject: [PATCH 09/16] [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 d915f0641da5d41bad2e8eaeb9049c9421a2a134 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 12 Sep 2024 09:18:41 +0200 Subject: [PATCH 10/16] [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 4ca8aa06f6a..fadcb7d7daf 100644 --- a/west.yml +++ b/west.yml @@ -318,7 +318,7 @@ manifest: - debug - name: picolibc path: modules/lib/picolibc - revision: e15656f7682500fc8953e1a2008f7b1ae4f65ce8 + revision: 27746bbc246841852912fc3bb5b45094cd8a505a - name: segger revision: b011c45b585e097d95d9cf93edf4f2e01588d3cd path: modules/debug/segger From 32ebd10dc25fb34465ffc3d03cf69876b8b98d46 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 11 Sep 2024 23:39:50 +0200 Subject: [PATCH 11/16] [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)") From f49c1531e18d567944bd8ff2e93aece4d9c80e23 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Fri, 9 Aug 2024 16:43:20 +0200 Subject: [PATCH 12/16] [nrf fromtree] cmake: support build info in Zephyr Store informations regarding the current Zephyr build. The following informations are stored during CMake configure: - Board information - Application source directory - Application configuration directory - Toolchain information - Devicetree files - Kconfig config files - Zephyr version Signed-off-by: Torsten Rasmussen (cherry picked from commit 09faf537dd160ee64d4b159140f386452624f08b) --- CMakeLists.txt | 7 +++++++ cmake/modules/FindHostTools.cmake | 3 +++ cmake/modules/boards.cmake | 4 ++++ cmake/modules/configuration_files.cmake | 2 ++ cmake/modules/dts.cmake | 6 ++++++ cmake/modules/kconfig.cmake | 3 +++ share/sysbuild/CMakeLists.txt | 7 +++++++ 7 files changed, 32 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index cbd6ac7bb1d..410aff11ab4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2234,3 +2234,10 @@ add_subdirectory_ifdef( ) toolchain_linker_finalize() + +yaml_context(EXISTS NAME build_info result) +if(result) + build_info(zephyr version VALUE ${PROJECT_VERSION_STR}) + build_info(zephyr zephyr-base VALUE ${ZEPHYR_BASE}) + yaml_save(NAME build_info) +endif() diff --git a/cmake/modules/FindHostTools.cmake b/cmake/modules/FindHostTools.cmake index 8d3c9eccaaf..876a86934b1 100644 --- a/cmake/modules/FindHostTools.cmake +++ b/cmake/modules/FindHostTools.cmake @@ -114,3 +114,6 @@ set_ifndef(TOOLCHAIN_KCONFIG_DIR ${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOL set(HostTools_FOUND TRUE) set(HOSTTOOLS_FOUND TRUE) +build_info(toolchain name VALUE ${ZEPHYR_TOOLCHAIN_VARIANT}) +string(TOUPPER ${ZEPHYR_TOOLCHAIN_VARIANT} zephyr_toolchain_variant_upper) +build_info(toolchain path VALUE "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}") diff --git a/cmake/modules/boards.cmake b/cmake/modules/boards.cmake index d13e0a16f2d..2b78845482a 100644 --- a/cmake/modules/boards.cmake +++ b/cmake/modules/boards.cmake @@ -353,3 +353,7 @@ if(BOARD_EXTENSIONS) list(APPEND BOARD_EXTENSION_DIRS ${board_extension_dir}) endforeach() endif() +build_info(board name VALUE ${BOARD}) +string(REGEX REPLACE "^/" "" qualifiers "${BOARD_QUALIFIERS}") +build_info(board qualifiers VALUE ${qualifiers}) +build_info(board revision VALUE ${BOARD_REVISION}) diff --git a/cmake/modules/configuration_files.cmake b/cmake/modules/configuration_files.cmake index 84af8c5b635..ff7a172445c 100644 --- a/cmake/modules/configuration_files.cmake +++ b/cmake/modules/configuration_files.cmake @@ -99,3 +99,5 @@ zephyr_boilerplate_watch(DTC_OVERLAY_FILE) zephyr_get(EXTRA_CONF_FILE SYSBUILD LOCAL VAR EXTRA_CONF_FILE OVERLAY_CONFIG MERGE REVERSE) zephyr_get(EXTRA_DTC_OVERLAY_FILE SYSBUILD LOCAL MERGE REVERSE) zephyr_get(DTS_EXTRA_CPPFLAGS SYSBUILD LOCAL MERGE REVERSE) +build_info(application source-dir VALUE ${APPLICATION_SOURCE_DIR}) +build_info(application configuration-dir VALUE ${APPLICATION_CONFIG_DIR}) diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index 42e822c6c91..b029dd5be3e 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -182,6 +182,7 @@ set(dts_files if(DTC_OVERLAY_FILE) zephyr_list(TRANSFORM DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE DTC_OVERLAY_FILE_AS_LIST) + build_info(devicetree user-files VALUE ${DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${DTC_OVERLAY_FILE_AS_LIST} @@ -191,6 +192,7 @@ endif() if(EXTRA_DTC_OVERLAY_FILE) zephyr_list(TRANSFORM EXTRA_DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE EXTRA_DTC_OVERLAY_FILE_AS_LIST) + build_info(devicetree extra-user-files VALUE ${EXTRA_DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${EXTRA_DTC_OVERLAY_FILE_AS_LIST} @@ -396,3 +398,7 @@ elseif(stderr) message(WARNING "dtc raised one or more warnings:\n${stderr}") endif() endif(DTC) + +build_info(devicetree files VALUE ${dts_files}) +build_info(devicetree include-dirs VALUE ${DTS_ROOT_SYSTEM_INCLUDE_DIRS}) +build_info(devicetree bindings-dirs VALUE ${DTS_ROOT_BINDINGS}) diff --git a/cmake/modules/kconfig.cmake b/cmake/modules/kconfig.cmake index 1e156bff470..43e73511768 100644 --- a/cmake/modules/kconfig.cmake +++ b/cmake/modules/kconfig.cmake @@ -98,11 +98,13 @@ set(PARSED_KCONFIG_SOURCES_TXT ${PROJECT_BINARY_DIR}/kconfig/sources.txt) if(CONF_FILE) string(CONFIGURE "${CONF_FILE}" CONF_FILE_EXPANDED) string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE_EXPANDED}") + build_info(kconfig user-files VALUE ${CONF_FILE_AS_LIST}) endif() if(EXTRA_CONF_FILE) string(CONFIGURE "${EXTRA_CONF_FILE}" EXTRA_CONF_FILE_EXPANDED) string(REPLACE " " ";" EXTRA_CONF_FILE_AS_LIST "${EXTRA_CONF_FILE_EXPANDED}") + build_info(kconfig extra-user-files VALUE ${EXTRA_CONF_FILE_AS_LIST}) endif() zephyr_file(CONF_FILES ${BOARD_EXTENSION_DIRS} KCONF board_extension_conf_files SUFFIX ${FILE_SUFFIX}) @@ -358,6 +360,7 @@ endif() if(CREATE_NEW_DOTCONFIG) set(input_configs_flags --handwritten-input-configs) set(input_configs ${merge_config_files} ${FORCED_CONF_FILE}) + build_info(kconfig files VALUE ${input_configs}) else() set(input_configs ${DOTCONFIG} ${FORCED_CONF_FILE}) endif() diff --git a/share/sysbuild/CMakeLists.txt b/share/sysbuild/CMakeLists.txt index 8370490fb94..f2fbb4b2718 100644 --- a/share/sysbuild/CMakeLists.txt +++ b/share/sysbuild/CMakeLists.txt @@ -25,3 +25,10 @@ if(EXISTS ${APP_DIR}/sysbuild/CMakeLists.txt) else() add_subdirectory(template _sysbuild) endif() + +build_info(sysbuild VALUE true) +build_info(application source-dir VALUE ${CMAKE_CURRENT_SOURCE_DIR}) +yaml_context(EXISTS NAME build_info result) +if(result) + yaml_save(NAME build_info) +endif() From d552044f7214e5bb1687ce1d3e3bc9bd6b3d91a0 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 22 Aug 2024 16:07:31 +0200 Subject: [PATCH 13/16] [nrf fromtree] cmake: nordic: save build information regarding SVD file used. Save information regarding SVD file in use in vendor-specific section of the build info file. Information is stored under Nordic section. Signed-off-by: Torsten Rasmussen (cherry picked from commit 46a3e61bc7c04e2cd609f64804b34ff4d29628a0) --- modules/hal_nordic/nrfx/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/hal_nordic/nrfx/CMakeLists.txt b/modules/hal_nordic/nrfx/CMakeLists.txt index 3863fa177ed..1665c122c9f 100644 --- a/modules/hal_nordic/nrfx/CMakeLists.txt +++ b/modules/hal_nordic/nrfx/CMakeLists.txt @@ -242,3 +242,4 @@ mdk_svd_ifdef(CONFIG_SOC_NRF9160 nrf9160.svd) mdk_svd_ifdef(CONFIG_SOC_NRF9230_ENGB_CPUAPP nrf9230_engb_application.svd) mdk_svd_ifdef(CONFIG_SOC_NRF9230_ENGB_CPUPPR nrf9230_engb_ppr.svd) mdk_svd_ifdef(CONFIG_SOC_NRF9230_ENGB_CPURAD nrf9230_engb_radiocore.svd) +build_info(vendor-specific nordic svdfile VALUE ${SOC_SVD_FILE}) From c87e19d7616e5fc3d0e60849899561bffaca261a Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 10 Oct 2024 11:59:53 +0200 Subject: [PATCH 14/16] [nrf fromtree] cmake: update build_info() calls to use PATH argument Update build_info() calls to use `PATH` argument when values passed to `build_info()` are user specified and thereby might use native path separator, such as a single `\`. Signed-off-by: Torsten Rasmussen (cherry picked from commit 2c1eae216d568cfa018b9d6741c57afdc26acf15) --- cmake/modules/FindHostTools.cmake | 2 +- cmake/modules/dts.cmake | 10 +++++----- cmake/modules/kconfig.cmake | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmake/modules/FindHostTools.cmake b/cmake/modules/FindHostTools.cmake index 876a86934b1..1dfce4d3d4a 100644 --- a/cmake/modules/FindHostTools.cmake +++ b/cmake/modules/FindHostTools.cmake @@ -116,4 +116,4 @@ set(HostTools_FOUND TRUE) set(HOSTTOOLS_FOUND TRUE) build_info(toolchain name VALUE ${ZEPHYR_TOOLCHAIN_VARIANT}) string(TOUPPER ${ZEPHYR_TOOLCHAIN_VARIANT} zephyr_toolchain_variant_upper) -build_info(toolchain path VALUE "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}") +build_info(toolchain path PATH "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}") diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index b029dd5be3e..eb7869548fe 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -182,7 +182,7 @@ set(dts_files if(DTC_OVERLAY_FILE) zephyr_list(TRANSFORM DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE DTC_OVERLAY_FILE_AS_LIST) - build_info(devicetree user-files VALUE ${DTC_OVERLAY_FILE_AS_LIST}) + build_info(devicetree user-files PATH ${DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${DTC_OVERLAY_FILE_AS_LIST} @@ -192,7 +192,7 @@ endif() if(EXTRA_DTC_OVERLAY_FILE) zephyr_list(TRANSFORM EXTRA_DTC_OVERLAY_FILE NORMALIZE_PATHS OUTPUT_VARIABLE EXTRA_DTC_OVERLAY_FILE_AS_LIST) - build_info(devicetree extra-user-files VALUE ${EXTRA_DTC_OVERLAY_FILE_AS_LIST}) + build_info(devicetree extra-user-files PATH ${EXTRA_DTC_OVERLAY_FILE_AS_LIST}) list(APPEND dts_files ${EXTRA_DTC_OVERLAY_FILE_AS_LIST} @@ -399,6 +399,6 @@ elseif(stderr) endif() endif(DTC) -build_info(devicetree files VALUE ${dts_files}) -build_info(devicetree include-dirs VALUE ${DTS_ROOT_SYSTEM_INCLUDE_DIRS}) -build_info(devicetree bindings-dirs VALUE ${DTS_ROOT_BINDINGS}) +build_info(devicetree files PATH ${dts_files}) +build_info(devicetree include-dirs PATH ${DTS_ROOT_SYSTEM_INCLUDE_DIRS}) +build_info(devicetree bindings-dirs PATH ${DTS_ROOT_BINDINGS}) diff --git a/cmake/modules/kconfig.cmake b/cmake/modules/kconfig.cmake index 43e73511768..97086cdf19d 100644 --- a/cmake/modules/kconfig.cmake +++ b/cmake/modules/kconfig.cmake @@ -98,13 +98,13 @@ set(PARSED_KCONFIG_SOURCES_TXT ${PROJECT_BINARY_DIR}/kconfig/sources.txt) if(CONF_FILE) string(CONFIGURE "${CONF_FILE}" CONF_FILE_EXPANDED) string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE_EXPANDED}") - build_info(kconfig user-files VALUE ${CONF_FILE_AS_LIST}) + build_info(kconfig user-files PATH ${CONF_FILE_AS_LIST}) endif() if(EXTRA_CONF_FILE) string(CONFIGURE "${EXTRA_CONF_FILE}" EXTRA_CONF_FILE_EXPANDED) string(REPLACE " " ";" EXTRA_CONF_FILE_AS_LIST "${EXTRA_CONF_FILE_EXPANDED}") - build_info(kconfig extra-user-files VALUE ${EXTRA_CONF_FILE_AS_LIST}) + build_info(kconfig extra-user-files PATH ${EXTRA_CONF_FILE_AS_LIST}) endif() zephyr_file(CONF_FILES ${BOARD_EXTENSION_DIRS} KCONF board_extension_conf_files SUFFIX ${FILE_SUFFIX}) @@ -360,7 +360,7 @@ endif() if(CREATE_NEW_DOTCONFIG) set(input_configs_flags --handwritten-input-configs) set(input_configs ${merge_config_files} ${FORCED_CONF_FILE}) - build_info(kconfig files VALUE ${input_configs}) + build_info(kconfig files PATH ${input_configs}) else() set(input_configs ${DOTCONFIG} ${FORCED_CONF_FILE}) endif() From c126c8ee7b0d9f4860f2426898125e2e03e00898 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 28 Nov 2024 09:07:00 +0100 Subject: [PATCH 15/16] [nrf fromtree] cmake: deprecate BUILD_NO_GAP_FILL and introduce type specific gap fill Deprecate BUILD_NO_GAP_FILL as it gives a false impression that gap filling can be disabled in binary files. Binary files are always gap filled due to the fact they contain no address information. Only option for binary files is to control the gap fill pattern. When no gap fill is enabled in binary files, then a default pattern is used by the tool, which usually is 0x00. Generally the pattern 0x00 leads to unnecessary flash writes, as a flash generally contains 0xFF after an erase. Therefore provide a gap fill pattern Kconfig setting instead, with default value of 0xFF. For hex-files, intel hex and s19, then gap filling is generally not needed but in order to still support cases where gap filling is required then this commit introduces BUILD_OUTPUT_HEX_GAP_FILL and BUILD_OUTPUT_S19_GAP_FILL. Both settings are disabled per default. Signed-off-by: Torsten Rasmussen (cherry picked from commit 2e8868c16e5ecfab8e823096892e02dac037c222) --- CMakeLists.txt | 13 ++++++------- Kconfig.zephyr | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 410aff11ab4..64c0f1f8ef5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1617,10 +1617,9 @@ list(APPEND ) list(APPEND post_build_byproducts ${KERNEL_MAP_NAME}) -if(NOT CONFIG_BUILD_NO_GAP_FILL) - # Use ';' as separator to get proper space in resulting command. - set(GAP_FILL "$0xff") -endif() +# Use ';' as separator to get proper space in resulting command. +set(gap_fill_prop "$") +set(gap_fill "$<$:${gap_fill_prop}${CONFIG_BUILD_GAP_FILL_PATTERN}>") if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE) target_link_libraries(${logical_target_for_zephyr_elf} $) @@ -1681,7 +1680,7 @@ if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd) post_build_commands COMMAND $ $ - ${GAP_FILL} + $<$:${gap_fill}> $ihex ${remove_sections_argument_list} $${KERNEL_ELF_NAME} @@ -1703,7 +1702,7 @@ if(CONFIG_BUILD_OUTPUT_BIN) post_build_commands COMMAND $ $ - ${GAP_FILL} + ${gap_fill} $binary ${remove_sections_argument_list} $${KERNEL_ELF_NAME} @@ -1790,7 +1789,7 @@ if(CONFIG_BUILD_OUTPUT_S19) post_build_commands COMMAND $ $ - ${GAP_FILL} + $<$:${gap_fill}> $srec $1 $${KERNEL_ELF_NAME} diff --git a/Kconfig.zephyr b/Kconfig.zephyr index 68abf89b42f..510769d8aa7 100644 --- a/Kconfig.zephyr +++ b/Kconfig.zephyr @@ -706,8 +706,21 @@ config CLEANUP_INTERMEDIATE_FILES from the build process. Note this breaks incremental builds, west spdx (Software Bill of Material generation), and maybe others. +config BUILD_GAP_FILL_PATTERN + hex "Gap fill pattern" + default 0xFF + help + Pattern used for gap filling of output files. + This value should be set to the value of a clean flash as this can + significantly reduce flash write times. + This setting only defines the gap fill pattern and doesn't enable gap + filling. + Note: binary files are always gap filled as they contain no address + information. + config BUILD_NO_GAP_FILL - bool "Don't fill gaps in generated hex/bin/s19 files." + bool "Don't fill gaps in generated hex/s19 files [DEPRECATED]." + select DEPRECATED config BUILD_OUTPUT_HEX bool "Build a binary in HEX format" @@ -715,6 +728,12 @@ config BUILD_OUTPUT_HEX Build an Intel HEX binary zephyr/zephyr.hex in the build directory. The name of this file can be customized with CONFIG_KERNEL_BIN_NAME. +config BUILD_OUTPUT_HEX_GAP_FILL + bool "Fill gaps in hex files" + depends on !BUILD_NO_GAP_FILL + help + Fill gaps in hex based files. + config BUILD_OUTPUT_BIN bool "Build a binary in BIN format" default y @@ -749,6 +768,12 @@ config BUILD_OUTPUT_S19 Build an S19 binary zephyr/zephyr.s19 in the build directory. The name of this file can be customized with CONFIG_KERNEL_BIN_NAME. +config BUILD_OUTPUT_S19_GAP_FILL + bool "Fill gaps in s19 files" + depends on !BUILD_NO_GAP_FILL + help + Fill gaps in s19 based files. + config BUILD_OUTPUT_UF2 bool "Build a binary in UF2 format" depends on BUILD_OUTPUT_BIN From 55f9255312b9e52caf5218f2328395735f6b6345 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 28 Nov 2024 09:16:38 +0100 Subject: [PATCH 16/16] [nrf fromtree] cmake: clear llvm bintool gapfill property The llvm-objcopy doesn't support a gap-fill argument, therefore clear the property to remove the error: > llvm-objcopy: error: unknown argument '--gap-fill' Signed-off-by: Torsten Rasmussen (cherry picked from commit 71fd4d9c98c7fe0845b70693e8daea46c6646b80) --- cmake/bintools/llvm/target_bintools.cmake | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cmake/bintools/llvm/target_bintools.cmake b/cmake/bintools/llvm/target_bintools.cmake index cd3d55342b5..f310d57bf2b 100644 --- a/cmake/bintools/llvm/target_bintools.cmake +++ b/cmake/bintools/llvm/target_bintools.cmake @@ -44,11 +44,8 @@ set_property(TARGET bintools PROPERTY elfconvert_flag_section_remove "--remove-s set_property(TARGET bintools PROPERTY elfconvert_flag_section_only "--only-section=") set_property(TARGET bintools PROPERTY elfconvert_flag_section_rename "--rename-section;") -# Note, placing a ';' at the end results in the following param to be a list, -# and hence space separated. -# Thus the command line argument becomes: -# `--gap-file ` instead of `--gap-fill` (The latter would result in an error) -set_property(TARGET bintools PROPERTY elfconvert_flag_gapfill "--gap-fill;") +# llvm-objcopy doesn't support gap fill argument. +set_property(TARGET bintools PROPERTY elfconvert_flag_gapfill "") set_property(TARGET bintools PROPERTY elfconvert_flag_srec_len "--srec-len=") set_property(TARGET bintools PROPERTY elfconvert_flag_infile "")