https://github.com/python/cpython/commit/eb7cd6e636d7a01891b11b1e3b22e9fb4ea45412
commit: eb7cd6e636d7a01891b11b1e3b22e9fb4ea45412
branch: main
author: Neil Schemenauer <[email protected]>
committer: nascheme <[email protected]>
date: 2026-11-12T12:23:43-07:00
summary:

gh-161821: Fall back to lld for Clang LTO builds (gh-161822)

Clang LTO with a GNU linker needs the LLVMgold.so plugin, which some
packages (e.g. LLVM for Debian 13) don't ship.  When using Clang with
--with-lto on non-macOS platforms, configure now test-links an LTO
program and, if that fails, retries with -fuse-ld=lld.

files:
A Misc/NEWS.d/next/Build/2026-11-11-11-36-01.gh-issue-161821.9wjElMQP.rst
M Doc/using/configure.rst
M configure
M configure.ac

diff --git a/Doc/using/configure.rst b/Doc/using/configure.rst
index ea608394b8eb101..4f2e4dfc88d3863 100644
--- a/Doc/using/configure.rst
+++ b/Doc/using/configure.rst
@@ -739,7 +739,14 @@ also be used to improve performance.
    Enable Link Time Optimization (LTO) in any build (disabled by default).
 
    The C compiler Clang requires ``llvm-ar`` for LTO (``ar`` on macOS), as well
-   as an LTO-aware linker (``ld.gold`` or ``lld``).
+   as an LTO-aware linker (``ld.gold`` or ``lld``). GNU linkers require the
+   ``LLVMgold.so`` plugin for Clang LTO; ``lld`` does not. On platforms other
+   than macOS, configure tries ``-fuse-ld=lld`` if the configured linker cannot
+   link a Clang LTO program.
+
+   .. versionchanged:: next
+      Fall back to ``lld`` when the configured linker cannot link Clang LTO
+      programs.
 
    .. versionadded:: 3.6
 
diff --git 
a/Misc/NEWS.d/next/Build/2026-11-11-11-36-01.gh-issue-161821.9wjElMQP.rst 
b/Misc/NEWS.d/next/Build/2026-11-11-11-36-01.gh-issue-161821.9wjElMQP.rst
new file mode 100644
index 000000000000000..2cfece1209c6e94
--- /dev/null
+++ b/Misc/NEWS.d/next/Build/2026-11-11-11-36-01.gh-issue-161821.9wjElMQP.rst
@@ -0,0 +1,4 @@
+When building with ``--with-lto`` and Clang, :program:`configure` now checks
+that the linker can link LTO objects and falls back to ``lld``
+(``-fuse-ld=lld``) if it cannot.  This fixes LTO builds on systems where the
+default GNU linker lacks the ``LLVMgold.so`` plugin, such as Debian 13.
diff --git a/configure b/configure
index 65d58fd1e810e23..e033b0217aefad5 100755
--- a/configure
+++ b/configure
@@ -9314,6 +9314,92 @@ fi
           else
             LTOFLAGS="-flto=${Py_LTO_POLICY}"
           fi
+          LTOCFLAGS="$LTOFLAGS"
+          # GNU ld needs LLVMgold.so for Clang LTO.  If the configured linker
+          # cannot link LTO objects, try lld, which does not need that plugin.
+          save_CFLAGS=$CFLAGS
+save_CPPFLAGS=$CPPFLAGS
+save_LDFLAGS=$LDFLAGS
+save_LIBS=$LIBS
+
+
+            LDFLAGS="$LDFLAGS $LDFLAGS_NODIST $LTOFLAGS"
+            { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether 
Clang LTO works with the configured linker" >&5
+printf %s "checking whether Clang LTO works with the configured linker... " 
>&6; }
+            cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main (void)
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  clang_lto_link=yes
+else case e in #(
+  e) clang_lto_link=no ;;
+esac
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+            { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: 
$clang_lto_link" >&5
+printf "%s\n" "$clang_lto_link" >&6; }
+
+CFLAGS=$save_CFLAGS
+CPPFLAGS=$save_CPPFLAGS
+LDFLAGS=$save_LDFLAGS
+LIBS=$save_LIBS
+
+
+          if test "$clang_lto_link" = no; then
+            save_CFLAGS=$CFLAGS
+save_CPPFLAGS=$CPPFLAGS
+save_LDFLAGS=$LDFLAGS
+save_LIBS=$LIBS
+
+
+              LDFLAGS="$LDFLAGS -fuse-ld=lld"
+              { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether 
Clang LTO works with lld" >&5
+printf %s "checking whether Clang LTO works with lld... " >&6; }
+              cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main (void)
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  clang_lto_link=yes
+else case e in #(
+  e) clang_lto_link=no ;;
+esac
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+              { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: 
$clang_lto_link" >&5
+printf "%s\n" "$clang_lto_link" >&6; }
+
+CFLAGS=$save_CFLAGS
+CPPFLAGS=$save_CPPFLAGS
+LDFLAGS=$save_LDFLAGS
+LIBS=$save_LIBS
+
+
+            if test "$clang_lto_link" = no; then
+              as_fn_error $? "Clang LTO requires a working LTO-aware linker. 
Install lld or the LLVMgold.so plugin, select a linker with LDFLAGS, or disable 
LTO with --without-lto." "$LINENO" 5
+            fi
+            LTOFLAGS="$LTOFLAGS -fuse-ld=lld"
+            LDFLAGS_NOLTO="$LDFLAGS_NOLTO -fuse-ld=lld"
+          fi
           ;;
       esac
       ;;
diff --git a/configure.ac b/configure.ac
index a041f0115bd7ab3..d654f680643041c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2220,6 +2220,32 @@ if test "$Py_LTO" = 'true' ; then
           else
             LTOFLAGS="-flto=${Py_LTO_POLICY}"
           fi
+          LTOCFLAGS="$LTOFLAGS"
+          # GNU ld needs LLVMgold.so for Clang LTO.  If the configured linker
+          # cannot link LTO objects, try lld, which does not need that plugin.
+          WITH_SAVE_ENV([
+            LDFLAGS="$LDFLAGS $LDFLAGS_NODIST $LTOFLAGS"
+            AC_MSG_CHECKING([whether Clang LTO works with the configured 
linker])
+            AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [clang_lto_link=yes], 
[clang_lto_link=no])
+            AC_MSG_RESULT([$clang_lto_link])
+          ])
+          if test "$clang_lto_link" = no; then
+            WITH_SAVE_ENV([
+              LDFLAGS="$LDFLAGS -fuse-ld=lld"
+              AC_MSG_CHECKING([whether Clang LTO works with lld])
+              AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [clang_lto_link=yes], 
[clang_lto_link=no])
+              AC_MSG_RESULT([$clang_lto_link])
+            ])
+            if test "$clang_lto_link" = no; then
+              AC_MSG_ERROR([m4_normalize([
+                Clang LTO requires a working LTO-aware linker.
+                Install lld or the LLVMgold.so plugin, select a linker
+                with LDFLAGS, or disable LTO with --without-lto.
+              ])])
+            fi
+            LTOFLAGS="$LTOFLAGS -fuse-ld=lld"
+            LDFLAGS_NOLTO="$LDFLAGS_NOLTO -fuse-ld=lld"
+          fi
           ;;
       esac
       ;;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to