Skip to content

Commit 89450e4

Browse files
committed
cmake: Delay computation of crtbegin/crtend paths until needed
With inclusion of the optimization flag into the multilib selection process, we cannot compute the compiler library path when the compiler's target.cmake is processed as OPTIMIZATION_FLAG is not computed until much later. Instead, have that file expose a function (library_file_path) which can be used to locate the appropriate crtbegin.o and crtend.o files. Stop adding the (possibly incorrect) libgcc directory to LIB_INCLUDE_DIR as the compiler will include the correct directory at link time anyways. For icx, leave code in which discovers whether libgcc.a is available with the assumption that if a libgcc.a is available with one set of compiler options the correct libgcc.a will be available with the final set of compiler options. Signed-off-by: Keith Packard <[email protected]>
1 parent 6913010 commit 89450e4

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

cmake/compiler/gcc/target.cmake

+12-13
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,19 @@ if(SYSROOT_DIR)
9696
set(LIBC_LIBRARY_DIR "\"${SYSROOT_DIR}\"/lib/${NEWLIB_DIR}")
9797
endif()
9898

99-
# This libgcc code is partially duplicated in compiler/*/target.cmake
100-
execute_process(
101-
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
102-
OUTPUT_VARIABLE LIBGCC_FILE_NAME
103-
OUTPUT_STRIP_TRAILING_WHITESPACE
104-
)
105-
106-
assert_exists(LIBGCC_FILE_NAME)
107-
108-
get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY)
109-
110-
assert_exists(LIBGCC_DIR)
99+
# Search for filename in default compiler library path
111100

112-
set_linker_property(PROPERTY lib_include_dir "-L\"${LIBGCC_DIR}\"")
101+
function(library_file_path filename i)
102+
execute_process(
103+
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${OPTIMIZATION_FLAG} --print-file-name ${filename}
104+
OUTPUT_VARIABLE filepath
105+
OUTPUT_STRIP_TRAILING_WHITESPACE
106+
)
107+
if(${filepath} STREQUAL ${filename})
108+
set(filepath "")
109+
endif()
110+
set(${i} "${filepath}" PARENT_SCOPE)
111+
endfunction()
113112

114113
# For CMake to be able to test if a compiler flag is supported by the
115114
# toolchain we need to give CMake the necessary flags to compile and

cmake/compiler/icx/target.cmake

+15-4
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,29 @@ else()
4545
list(APPEND TOOLCHAIN_C_FLAGS "-m32")
4646
endif()
4747

48+
# Search for filename in default compiler library path
49+
50+
function(library_file_path filename i)
51+
execute_process(
52+
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${OPTIMIZATION_FLAG} --print-file-name ${filename}
53+
OUTPUT_VARIABLE filepath
54+
OUTPUT_STRIP_TRAILING_WHITESPACE
55+
)
56+
set(${i} ${filepath} PARENT_SCOPE)
57+
endfunction()
58+
59+
# Add -lgcc if available
4860

4961
# This libgcc code is partially duplicated in compiler/*/target.cmake
5062
execute_process(
5163
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
52-
OUTPUT_VARIABLE LIBGCC_FILE_NAME
64+
OUTPUT_VARIABLE libgcc_file_name
5365
OUTPUT_STRIP_TRAILING_WHITESPACE
5466
)
5567

56-
get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY)
68+
get_filename_component(libgcc_dir ${libgcc_file_name} DIRECTORY)
5769

58-
list(APPEND LIB_INCLUDE_DIR "-L\"${LIBGCC_DIR}\"")
59-
if(LIBGCC_DIR)
70+
if(libgcc_dir)
6071
list(APPEND TOOLCHAIN_LIBS gcc)
6172
endif()
6273

cmake/compiler/xcc/target.cmake

+9-9
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ foreach(file_name include/stddef.h include-fixed/limits.h)
4040
list(APPEND NOSTDINC ${_OUTPUT})
4141
endforeach()
4242

43-
# This libgcc code is partially duplicated in compiler/*/target.cmake
44-
execute_process(
45-
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
46-
OUTPUT_VARIABLE LIBGCC_FILE_NAME
47-
OUTPUT_STRIP_TRAILING_WHITESPACE
48-
)
43+
# Search for filename in default compiler library path
4944

50-
get_filename_component(LIBGCC_DIR ${LIBGCC_FILE_NAME} DIRECTORY)
51-
52-
list(APPEND LIB_INCLUDE_DIR "-L\"${LIBGCC_DIR}\"")
45+
function(library_file_path filename i)
46+
execute_process(
47+
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${OPTIMIZATION_FLAG} --print-file-name ${filename}
48+
OUTPUT_VARIABLE filepath
49+
OUTPUT_STRIP_TRAILING_WHITESPACE
50+
)
51+
set(${i} ${filepath} PARENT_SCOPE)
52+
endfunction()
5353

5454
# For CMake to be able to test if a compiler flag is supported by the
5555
# toolchain we need to give CMake the necessary flags to compile and

cmake/linker/ld/target.cmake

+4-2
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@ macro(toolchain_linker_finalize)
164164

165165
set(cpp_link "${common_link}")
166166
if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "host")
167-
if(CONFIG_CPP_EXCEPTIONS AND LIBGCC_DIR)
167+
library_file_path(crtbegin.o CRTBEGIN_PATH)
168+
library_file_path(crtend.o CRTEND_PATH)
169+
if(CONFIG_CPP_EXCEPTIONS AND CRTBEGIN_PATH AND CRTEND_PATH)
168170
# When building with C++ Exceptions, it is important that crtbegin and crtend
169171
# are linked at specific locations.
170-
set(cpp_link "<LINK_FLAGS> ${LIBGCC_DIR}/crtbegin.o ${link_libraries} ${LIBGCC_DIR}/crtend.o")
172+
set(cpp_link "<LINK_FLAGS> ${CRTBEGIN_PATH} ${link_libraries} ${CRTEND_PATH}")
171173
endif()
172174
endif()
173175
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> ${cpp_link}")

cmake/linker/xt-ld/target.cmake

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ find_program(CMAKE_LINKER xt-ld ${LD_SEARCH_PATH})
1111

1212
set_ifndef(LINKERFLAGPREFIX -Wl)
1313

14-
if(CONFIG_CPP_EXCEPTIONS)
14+
library_file_path(crtbegin.o CRTBEGIN_PATH)
15+
library_file_path(crtend.o CRTEND_PATH)
16+
if(CONFIG_CPP_EXCEPTIONS AND CRTBEGIN_PATH AND CRTEND_PATH)
1517
# When building with C++ Exceptions, it is important that crtbegin and crtend
1618
# are linked at specific locations.
1719
# The location is so important that we cannot let this be controlled by normal
1820
# link libraries, instead we must control the link command specifically as
1921
# part of toolchain.
2022
set(CMAKE_CXX_LINK_EXECUTABLE
21-
"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> ${LIBGCC_DIR}/crtbegin.o <OBJECTS> -o <TARGET> <LINK_LIBRARIES> ${LIBGCC_DIR}/crtend.o")
23+
"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> ${CRTBEGIN_PATH} <OBJECTS> -o <TARGET> <LINK_LIBRARIES> ${CRTEND_PATH}")
2224
endif()
2325

2426
# Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen}

0 commit comments

Comments
 (0)