From b5d14f0ba23498c9a96153197df7a01bd2f6cdc3 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 15 Oct 2024 09:51:07 -0700 Subject: [PATCH 1/3] cmake: Force the CMake build flags to match our desired value In addition to warning about a mismatch between Zephyr's optimization setting and the CMAKE_BUILD_TYPE, apply Zephyr's optimization flags to the relevant CMAKE_C_FLAGS_ value so that when those are used, the right value is applied. When the warning is disabled, leave things alone. Signed-off-by: Keith Packard --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ce4b5ff1e9e91..8715acd873b9c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2167,6 +2167,7 @@ if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING)) The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'. This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'" ) + set(CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase} ${OPTIMIZATION_FLAG}) endif() endif() From daee96c57818a7855e295ab89081e2bd3f6e7abd Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 15 Oct 2024 12:06:45 -0700 Subject: [PATCH 2/3] tests/kernel: Add common test using newlib Cover the other common C library Signed-off-by: Keith Packard --- tests/kernel/common/testcase.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/kernel/common/testcase.yaml b/tests/kernel/common/testcase.yaml index c4c17e2eb51b26..c4d875f3939a61 100644 --- a/tests/kernel/common/testcase.yaml +++ b/tests/kernel/common/testcase.yaml @@ -47,6 +47,12 @@ tests: tags: picolibc extra_configs: - CONFIG_PICOLIBC=y + kernel.common.newlib: + filter: CONFIG_NEWLIB_LIBC_SUPPORTED + min_ram: 32 + tags: newlib + extra_configs: + - CONFIG_NEWLIB_LIBC=y kernel.common.lto: # CONFIG_CODE_DATA_RELOCATION causes a build error (issue #69730) filter: CONFIG_ISR_TABLES_LOCAL_DECLARATION_SUPPORTED and not CONFIG_CODE_DATA_RELOCATION From 56f66913d49b1da80d8c9fbfd8714279dd15a1b5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 15 Oct 2024 12:08:17 -0700 Subject: [PATCH 3/3] cmake/compiler: Postpone toolchain filename computation until needed Instead of pre-computing the toolchain library path using options available early in the configuration process, delay until the related file names are actually needed, by which time the remaining compiler options have been computed (most notably ${OPTIMIZATION_FLAG}). Remove the computatlib of LIBGCC_DIR entirely; that will be done by the compiler driver at link time without needed any help. Signed-off-by: Keith Packard --- cmake/compiler/gcc/target.cmake | 32 ++++++++++---------------------- cmake/linker/ld/target.cmake | 14 ++++++++++---- cmake/linker/xt-ld/target.cmake | 9 ++++++++- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/cmake/compiler/gcc/target.cmake b/cmake/compiler/gcc/target.cmake index 7e8ffc48173373..814dc9a08644fb 100644 --- a/cmake/compiler/gcc/target.cmake +++ b/cmake/compiler/gcc/target.cmake @@ -85,31 +85,19 @@ if(SYSROOT_DIR) list(APPEND TOOLCHAIN_C_FLAGS --sysroot=${SYSROOT_DIR} ) +endif() - # Use sysroot dir to set the libc path's - execute_process( - COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-multi-directory - OUTPUT_VARIABLE NEWLIB_DIR +# Use the compiler to find a file within the toolchain +# This can be used to find files like crtbegin.o and crtend.o +function(compiler_file_name file output) + execute_process(COMMAND ${CMAKE_C_COMPILER} + ${TOOLCHAIN_C_FLAGS} ${OPTIMIZATION_FLAG} + --print-file-name=${file} + OUTPUT_VARIABLE file_name OUTPUT_STRIP_TRAILING_WHITESPACE ) - - set(LIBC_LIBRARY_DIR "\"${SYSROOT_DIR}\"/lib/${NEWLIB_DIR}") -endif() - -# This libgcc code is partially duplicated in compiler/*/target.cmake -execute_process( - COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name - OUTPUT_VARIABLE LIBGCC_FILE_NAME - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - -assert_exists(LIBGCC_FILE_NAME) - -get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY) - -assert_exists(LIBGCC_DIR) - -set_linker_property(PROPERTY lib_include_dir "-L\"${LIBGCC_DIR}\"") + set(${output} ${file_name} PARENT_SCOPE) +endfunction() # 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/ld/target.cmake b/cmake/linker/ld/target.cmake index 5e0a117c014360..14fa3cd73a3b65 100644 --- a/cmake/linker/ld/target.cmake +++ b/cmake/linker/ld/target.cmake @@ -155,10 +155,16 @@ macro(toolchain_linker_finalize) 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") + if(CONFIG_CPP_EXCEPTIONS) + if(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") + elseif(COMMAND compiler_file_name) + compiler_file_name("crtbegin.o" CRTBEGIN) + compiler_file_name("crtend.o" CRTEND) + set(cpp_link " ${CRTBEGIN} ${link_libraries} ${CRTEND}") + endif() endif() endif() set(CMAKE_CXX_LINK_EXECUTABLE " ${cpp_link}") diff --git a/cmake/linker/xt-ld/target.cmake b/cmake/linker/xt-ld/target.cmake index 3546881cc718e7..ac1b3acba5acec 100644 --- a/cmake/linker/xt-ld/target.cmake +++ b/cmake/linker/xt-ld/target.cmake @@ -17,8 +17,15 @@ if(CONFIG_CPP_EXCEPTIONS) # 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 + if (LIBGCC_DIR) + set(CMAKE_CXX_LINK_EXECUTABLE " ${LIBGCC_DIR}/crtbegin.o -o ${LIBGCC_DIR}/crtend.o") + elseif(COMMAND compiler_file_name) + compiler_file_name("crtbegin.o" CRTBEGIN) + compiler_file_name("crtend.o" CRTEND) + set(CMAKE_CXX_LINK_EXECUTABLE + " ${CRTBEGIN} -o ${CRTEND}") + endif() endif() # Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen}