[binutils-gdb] gdb: fail configure if Python version is too old for limited API

Matthieu Longo via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b1cc575c428b342446143512e8b3d438b09e7c22

commit b1cc575c428b342446143512e8b3d438b09e7c22
Author: Matthieu Longo <[email protected]>
Date:   Fri Mar 6 17:50:45 2026 +0000

    gdb: fail configure if Python version is too old for limited API
    
    GDB can be built against the Python limited API using the configure
    flag '--enable-py-limited-api=yes'. This flag is currently experimental,
    and the build is not yet fully successful. Today, the minimum required
    Python version for this option is 3.11. This requirement is not final
    and will be raised to a later version as the migration progresses.
    
    However, the configure script does not currently report an error if an
    older version of Python is used. Instead, the build fails later with
    numerous errors that are difficult to relate to Python limited API
    compatiblity.
    
    This patch adds a version check when '--enable-py-limited-api=yes' is
    specified, ensuring that the provided Python version meets the minimum
    requirements for the limited API support. If it does not, configure will
    now fail with a clear error message.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
    
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/configure    | 41 +++++++++++++++++++++++++++++++++++++++--
 gdb/configure.ac | 37 +++++++++++++++++++++++++++++++++----
 2 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/gdb/configure b/gdb/configure
index 2ff36178a7e..14d0848a227 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -28509,6 +28509,7 @@ fi
     # do except assume that the compiler will be able to find those files.
     python_includes=
     python_libs=
+    python_prefix=
     have_python_config=no
   fi
 
@@ -28745,6 +28746,7 @@ else
 fi
 
 
+
 # Check whether to build GDB against Python limited C API.
 # Check whether --enable-py-limited-api was given.
 if test "${enable_py_limited_api+set}" = set; then :
@@ -28765,11 +28767,46 @@ fi
 if test "$enable_py_limited_api" = yes; then
   # The minimal Python limited API version is currently set to 3.11 for the
   # support of PyBuffer_FillInfo and PyBuffer_Release.
-  # The choice of the minimal version for the Python limited API won't be frozen
-  # until the end of the migration.
+  # The choice of the minimal version for the Python limited API won't be
+  # frozen until the end of the migration.
+  old_CFLAGS="$CFLAGS"
+  CFLAGS="$CFLAGS $PYTHON_CFLAGS"
+  old_CPPFLAGS="$CPPFLAGS"
+  CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
+  old_LIBS="$LIBS"
+  LIBS="$LIBS $PYTHON_LIBS"
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <Python.h>
+#ifndef PY_VERSION_HEX
+#error "PY_VERSION_HEX is not defined"
+#endif
+#if PY_VERSION_HEX < 0x030b0000
+#error "Python limited API support requires at least Python version 3.11"
+#endif
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
 
 $as_echo "#define Py_LIMITED_API 0x030b0000" >>confdefs.h
 
+
+else
+
+      as_fn_error $? "Python limited API support requires at least Python version 3.11" "$LINENO" 5
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+  CFLAGS="$old_CFLAGS"
+  CPPFLAGS="$old_CPPFLAGS"
+  LIBS="$old_LIBS"
 fi
 
 # -------------------- #
diff --git a/gdb/configure.ac b/gdb/configure.ac
index dc918ae28c7..a6da4ac7b24 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -933,6 +933,7 @@ else
     # do except assume that the compiler will be able to find those files.
     python_includes=
     python_libs=
+    python_prefix=
     have_python_config=no
   fi
 
@@ -1062,6 +1063,13 @@ AC_SUBST(PYTHON_CPPFLAGS)
 AC_SUBST(PYTHON_LIBS)
 AM_CONDITIONAL(HAVE_PYTHON, test "${have_libpython}" != no)
 
+dnl Use --enable-py-limited-api to enable the build of GDB against the Python
+dnl limited API.
+dnl
+dnl no -   Disable the Python limited API.
+dnl yes -  Use the Python limited API to build GDB, error if the selected
+dnl        version of Python is not compatible with the Python limited API.
+
 # Check whether to build GDB against Python limited C API.
 AC_ARG_ENABLE([py-limited-api],
 	      [AS_HELP_STRING([--enable-py-limited-api],
@@ -1072,10 +1080,31 @@ AC_ARG_ENABLE([py-limited-api],
 if test "$enable_py_limited_api" = yes; then
   # The minimal Python limited API version is currently set to 3.11 for the
   # support of PyBuffer_FillInfo and PyBuffer_Release.
-  # The choice of the minimal version for the Python limited API won't be frozen
-  # until the end of the migration.
-  AC_DEFINE(Py_LIMITED_API, 0x030b0000,
-	    [Define if GDB should be built against the Python limited C API.])
+  # The choice of the minimal version for the Python limited API won't be
+  # frozen until the end of the migration.
+  old_CFLAGS="$CFLAGS"
+  CFLAGS="$CFLAGS $PYTHON_CFLAGS"
+  old_CPPFLAGS="$CPPFLAGS"
+  CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
+  old_LIBS="$LIBS"
+  LIBS="$LIBS $PYTHON_LIBS"
+  AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
+    [[#include <Python.h>
+#ifndef PY_VERSION_HEX
+#error "PY_VERSION_HEX is not defined"
+#endif
+#if PY_VERSION_HEX < 0x030b0000
+#error "Python limited API support requires at least Python version 3.11"
+#endif
+    ]],[[]])],
+    [AC_DEFINE(Py_LIMITED_API, 0x030b0000,
+      [Define if GDB should be built against the Python limited C API.])
+    ],[
+      AC_MSG_ERROR([Python limited API support requires at least Python version 3.11])
+    ])
+  CFLAGS="$old_CFLAGS"
+  CPPFLAGS="$old_CPPFLAGS"
+  LIBS="$old_LIBS"
 fi
 
 # -------------------- #
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.