Skip to content

Commit 78a42a8

Browse files
fix(cmake): Fixed linker not supporting -warn_commons for linux target on MacOS
This commit updates the ld linker flags to conditionally include the -warn_commons flag when the linux target is built on MacOS. This is because, not all versions of ld support the -warn_commons option. Closes #13185
1 parent e5e1463 commit 78a42a8

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

CMakeLists.txt

+28-1
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,35 @@ endif()
227227
list(APPEND link_options "-fno-lto")
228228

229229
if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
230+
# Not all versions of the MacOS linker support the -warn_commons flag.
231+
# ld version 1053.12 (and above) have been tested to support it.
232+
# Hence, we extract the version string from the linker output
233+
# before including the flag.
234+
235+
# Get the ld version, capturing both stdout and stderr
236+
execute_process(
237+
COMMAND ${CMAKE_LINKER} -v
238+
OUTPUT_VARIABLE LD_VERSION_OUTPUT
239+
ERROR_VARIABLE LD_VERSION_ERROR
240+
OUTPUT_STRIP_TRAILING_WHITESPACE
241+
ERROR_STRIP_TRAILING_WHITESPACE
242+
)
243+
244+
# Combine stdout and stderr
245+
set(LD_VERSION_OUTPUT "${LD_VERSION_OUTPUT}\n${LD_VERSION_ERROR}")
246+
247+
# Extract the version string
248+
string(REGEX MATCH "PROJECT:(ld|dyld)-([0-9]+)\\.([0-9]+)" LD_VERSION_MATCH "${LD_VERSION_OUTPUT}")
249+
set(LD_VERSION_MAJOR_MINOR "${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
250+
251+
message(STATUS "Linker Version: ${LD_VERSION_MAJOR_MINOR}")
252+
253+
# Compare the version with 1053.12
254+
if(LD_VERSION_MAJOR_MINOR VERSION_GREATER_EQUAL "1053.12")
255+
list(APPEND link_options "-Wl,-warn_commons")
256+
endif()
257+
230258
list(APPEND link_options "-Wl,-dead_strip")
231-
list(APPEND link_options "-Wl,-warn_commons")
232259
else()
233260
list(APPEND link_options "-Wl,--gc-sections")
234261
list(APPEND link_options "-Wl,--warn-common")

0 commit comments

Comments
 (0)