diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ce4b5ff1e9e9..8715acd873b9c 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() diff --git a/cmake/compiler/gcc/target.cmake b/cmake/compiler/gcc/target.cmake index 7e8ffc4817337..814dc9a08644f 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 5e0a117c01436..14fa3cd73a3b6 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 3546881cc718e..ac1b3acba5ace 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} diff --git a/tests/kernel/common/testcase.yaml b/tests/kernel/common/testcase.yaml index c4c17e2eb51b2..c4d875f3939a6 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