Skip to content

Commit 049e4af

Browse files
sudeep-mohantyespressif-bot
authored andcommitted
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 c143e68 commit 049e4af

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
@@ -213,8 +213,35 @@ endif()
213213
list(APPEND link_options "-fno-lto")
214214

215215
if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
216+
# Not all versions of the MacOS linker support the -warn_commons flag.
217+
# ld version 1053.12 (and above) have been tested to support it.
218+
# Hence, we extract the version string from the linker output
219+
# before including the flag.
220+
221+
# Get the ld version, capturing both stdout and stderr
222+
execute_process(
223+
COMMAND ${CMAKE_LINKER} -v
224+
OUTPUT_VARIABLE LD_VERSION_OUTPUT
225+
ERROR_VARIABLE LD_VERSION_ERROR
226+
OUTPUT_STRIP_TRAILING_WHITESPACE
227+
ERROR_STRIP_TRAILING_WHITESPACE
228+
)
229+
230+
# Combine stdout and stderr
231+
set(LD_VERSION_OUTPUT "${LD_VERSION_OUTPUT}\n${LD_VERSION_ERROR}")
232+
233+
# Extract the version string
234+
string(REGEX MATCH "PROJECT:(ld|dyld)-([0-9]+)\\.([0-9]+)" LD_VERSION_MATCH "${LD_VERSION_OUTPUT}")
235+
set(LD_VERSION_MAJOR_MINOR "${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
236+
237+
message(STATUS "Linker Version: ${LD_VERSION_MAJOR_MINOR}")
238+
239+
# Compare the version with 1053.12
240+
if(LD_VERSION_MAJOR_MINOR VERSION_GREATER_EQUAL "1053.12")
241+
list(APPEND link_options "-Wl,-warn_commons")
242+
endif()
243+
216244
list(APPEND link_options "-Wl,-dead_strip")
217-
list(APPEND link_options "-Wl,-warn_commons")
218245
else()
219246
list(APPEND link_options "-Wl,--gc-sections")
220247
list(APPEND link_options "-Wl,--warn-common")

0 commit comments

Comments
 (0)