From d87171a2f19ef4ea0b6f75ab02e3896e65e39376 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Wed, 14 Aug 2024 14:56:42 +0200 Subject: [PATCH] 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 https://github.com/espressif/esp-idf/issues/13185 --- CMakeLists.txt | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5869c4c1e4..f85ac1cda5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -198,8 +198,35 @@ endif() list(APPEND link_options "-fno-lto") if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") + # Not all versions of the MacOS linker support the -warn_commons flag. + # ld version 1053.12 (and above) have been tested to support it. + # Hence, we extract the version string from the linker output + # before including the flag. + + # Get the ld version, capturing both stdout and stderr + execute_process( + COMMAND ${CMAKE_LINKER} -v + OUTPUT_VARIABLE LD_VERSION_OUTPUT + ERROR_VARIABLE LD_VERSION_ERROR + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_STRIP_TRAILING_WHITESPACE + ) + + # Combine stdout and stderr + set(LD_VERSION_OUTPUT "${LD_VERSION_OUTPUT}\n${LD_VERSION_ERROR}") + + # Extract the version string + string(REGEX MATCH "PROJECT:(ld|dyld)-([0-9]+)\\.([0-9]+)" LD_VERSION_MATCH "${LD_VERSION_OUTPUT}") + set(LD_VERSION_MAJOR_MINOR "${CMAKE_MATCH_2}.${CMAKE_MATCH_3}") + + message(STATUS "Linker Version: ${LD_VERSION_MAJOR_MINOR}") + + # Compare the version with 1053.12 + if(LD_VERSION_MAJOR_MINOR VERSION_GREATER_EQUAL "1053.12") + list(APPEND link_options "-Wl,-warn_commons") + endif() + list(APPEND link_options "-Wl,-dead_strip") - list(APPEND link_options "-Wl,-warn_commons") else() list(APPEND link_options "-Wl,--gc-sections") list(APPEND link_options "-Wl,--warn-common")