Skip to content

Commit b394941

Browse files
luca-fancellustephanosio
authored andcommitted
cmake: gcc: Use --sysroot with gcc when SYSROOT_DIR is provided
When SYSROOT_DIR is provided, gcc should use it through the --sysroot= option otherwise some commands won't work as expected. For example, in the Yocto environment when cross compiling, --print-libgcc-file-name prints only "libgcc.a" instead of the full path to it and the subsequent assert_exists(LIBGCC_FILE_NAME) will fail. Fixes zephyrproject-rtos#45578 Signed-off-by: Luca Fancellu <[email protected]>
1 parent 4cc5c56 commit b394941

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

cmake/compiler/gcc/target.cmake

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ elseif("${ARCH}" STREQUAL "mips")
7171
include(${CMAKE_CURRENT_LIST_DIR}/target_mips.cmake)
7272
endif()
7373

74+
if(SYSROOT_DIR)
75+
# The toolchain has specified a sysroot dir, pass it to the compiler
76+
list(APPEND TOOLCHAIN_C_FLAGS
77+
--sysroot=${SYSROOT_DIR}
78+
)
79+
80+
# Use sysroot dir to set the libc path's
81+
execute_process(
82+
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-multi-directory
83+
OUTPUT_VARIABLE NEWLIB_DIR
84+
OUTPUT_STRIP_TRAILING_WHITESPACE
85+
)
86+
87+
set(LIBC_LIBRARY_DIR "\"${SYSROOT_DIR}\"/lib/${NEWLIB_DIR}")
88+
endif()
89+
7490
# This libgcc code is partially duplicated in compiler/*/target.cmake
7591
execute_process(
7692
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
@@ -87,18 +103,6 @@ assert_exists(LIBGCC_DIR)
87103
LIST(APPEND LIB_INCLUDE_DIR "-L\"${LIBGCC_DIR}\"")
88104
LIST(APPEND TOOLCHAIN_LIBS gcc)
89105

90-
if(SYSROOT_DIR)
91-
# The toolchain has specified a sysroot dir that we can use to set
92-
# the libc path's
93-
execute_process(
94-
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-multi-directory
95-
OUTPUT_VARIABLE NEWLIB_DIR
96-
OUTPUT_STRIP_TRAILING_WHITESPACE
97-
)
98-
99-
set(LIBC_LIBRARY_DIR "\"${SYSROOT_DIR}\"/lib/${NEWLIB_DIR}")
100-
endif()
101-
102106
# For CMake to be able to test if a compiler flag is supported by the
103107
# toolchain we need to give CMake the necessary flags to compile and
104108
# link a dummy C file.

0 commit comments

Comments
 (0)