From 5945a98ced2e1f4794f26f39256c95d875220408 Mon Sep 17 00:00:00 2001
From: Marc Chevrier <marc.chevrier@gmail.com>
Date: Fri, 23 Jun 2023 14:36:55 +0200
Subject: link dependencies: deactivate the feature

Unfortunately it breaks in combination with LTO due to a bug in the GNU
linker.

Fixes: #25014
---
 Help/variable/CMAKE_LINK_DEPENDS_USE_LINKER.rst    |  8 ++++++++
 Modules/Compiler/GNU.cmake                         | 13 ++++++++++---
 Tests/RunCMake/BuildDepends/LinkDependsCheck.cmake |  3 ++-
 Tests/RunCMake/BuildDepends/RunCMakeTest.cmake     |  4 ++--
 4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/Help/variable/CMAKE_LINK_DEPENDS_USE_LINKER.rst b/Help/variable/CMAKE_LINK_DEPENDS_USE_LINKER.rst
index e1b37a5..1867ad8 100644
--- a/Help/variable/CMAKE_LINK_DEPENDS_USE_LINKER.rst
+++ b/Help/variable/CMAKE_LINK_DEPENDS_USE_LINKER.rst
@@ -10,3 +10,11 @@ variable with value ``FALSE``, you can deactivate this feature.
 
 This feature is also deactivated if the :prop_tgt:`LINK_DEPENDS_NO_SHARED`
 target property is true.
+
+.. note::
+
+  CMake version |release| defaults this variable to ``FALSE`` because
+  GNU binutils linkers (``ld``, ``ld.bfd``, ``ld.gold``) generate spurious
+  dependencies on temporary files when LTO is enabled.  See `GNU bug 30568`_.
+
+.. _`GNU bug 30568`: https://sourceware.org/bugzilla/show_bug.cgi?id=30568
diff --git a/Modules/Compiler/GNU.cmake b/Modules/Compiler/GNU.cmake
index 251e05a..d01054b 100644
--- a/Modules/Compiler/GNU.cmake
+++ b/Modules/Compiler/GNU.cmake
@@ -53,6 +53,9 @@ macro(__compiler_gnu lang)
   endif()
 
   # define flags for linker depfile generation
+  set(CMAKE_${lang}_LINKER_DEPFILE_FLAGS "LINKER:--dependency-file,<DEP_FILE>")
+  set(CMAKE_${lang}_LINKER_DEPFILE_FORMAT gcc)
+
   if(NOT DEFINED CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
     ## Ensure ninja tool is recent enough...
     if(CMAKE_GENERATOR MATCHES "^Ninja")
@@ -82,15 +85,19 @@ macro(__compiler_gnu lang)
       unset(_linker_capabilities)
     endif()
   endif()
-
   if (CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
-    set(CMAKE_${lang}_LINKER_DEPFILE_FLAGS "LINKER:--dependency-file,<DEP_FILE>")
-    set(CMAKE_${lang}_LINKER_DEPFILE_FORMAT gcc)
     set(CMAKE_${lang}_LINK_DEPENDS_USE_LINKER TRUE)
   else()
     unset(CMAKE_${lang}_LINK_DEPENDS_USE_LINKER)
   endif()
 
+  # For now, due to GNU binutils ld bug when LTO is enabled (see GNU bug
+    # `30568 <https://sourceware.org/bugzilla/show_bug.cgi?id=30568>`_),
+  # deactivate this feature.
+  if (NOT DEFINED CMAKE_LINK_DEPENDS_USE_LINKER)
+    set(CMAKE_LINK_DEPENDS_USE_LINKER FALSE)
+  endif()
+
   # Initial configuration flags.
   string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
   string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g")
diff --git a/Tests/RunCMake/BuildDepends/LinkDependsCheck.cmake b/Tests/RunCMake/BuildDepends/LinkDependsCheck.cmake
index a21096b..596e1eb 100644
--- a/Tests/RunCMake/BuildDepends/LinkDependsCheck.cmake
+++ b/Tests/RunCMake/BuildDepends/LinkDependsCheck.cmake
@@ -2,7 +2,8 @@
 enable_language(C)
 
 file(WRITE "${CMAKE_BINARY_DIR}/LinkDependsUseLinker.cmake"
-     "set(CMAKE_C_LINK_DEPENDS_USE_LINKER \"${CMAKE_C_LINK_DEPENDS_USE_LINKER}\")\n")
+     "set(CMAKE_LINK_DEPENDS_USE_LINKER ${CMAKE_LINK_DEPENDS_USE_LINKER})
+set(CMAKE_C_LINK_DEPENDS_USE_LINKER ${CMAKE_C_LINK_DEPENDS_USE_LINKER})\n")
 
 
 file(GENERATE  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/check-$<LOWER_CASE:$<CONFIG>>.cmake"
diff --git a/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake b/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake
index dfa4f49..fada37a 100644
--- a/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake
+++ b/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake
@@ -199,12 +199,12 @@ endif()
 run_BuildDepends(CustomCommandUnityBuild)
 unset(run_BuildDepends_skip_step_2)
 
-#if (RunCMake_GENERATOR MATCHES "Make|Ninja" AND CMAKE_C_LINK_DEPENDS_USE_LINKER)
 if (RunCMake_GENERATOR MATCHES "Make|Ninja")
   set(run_BuildDepends_skip_step_2 1)
   run_BuildDepends(LinkDependsCheck)
   include("${RunCMake_BINARY_DIR}/LinkDependsCheck-build/LinkDependsUseLinker.cmake")
-  if (CMAKE_C_LINK_DEPENDS_USE_LINKER)
+  if ((NOT DEFINED CMAKE_LINK_DEPENDS_USE_LINKER OR CMAKE_LINK_DEPENDS_USE_LINKER)
+      AND CMAKE_C_LINK_DEPENDS_USE_LINKER)
     run_BuildDepends(LinkDependsExternalLibrary)
     unset(run_BuildDepends_skip_step_2)
     run_BuildDepends(LinkDepends)
-- 
cgit v0.12