Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support toolchain with optimization levels included in multilib configurations #79887

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
32 changes: 10 additions & 22 deletions cmake/compiler/gcc/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions cmake/linker/ld/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<LINK_FLAGS> ${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 "<LINK_FLAGS> ${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 "<LINK_FLAGS> ${CRTBEGIN} ${link_libraries} ${CRTEND}")
endif()
endif()
endif()
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> ${cpp_link}")
Expand Down
9 changes: 8 additions & 1 deletion cmake/linker/xt-ld/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> ${LIBGCC_DIR}/crtbegin.o <OBJECTS> -o <TARGET> <LINK_LIBRARIES> ${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
"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> ${CRTBEGIN} <OBJECTS> -o <TARGET> <LINK_LIBRARIES> ${CRTEND}")
endif()
endif()

# Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen}
Expand Down
6 changes: 6 additions & 0 deletions tests/kernel/common/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading