From 2d336d1fedb692350a1e4f131246f60a1195cd93 Mon Sep 17 00:00:00 2001
From: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Date: Sun, 7 Dec 2025 13:57:06 +0100
Subject: [PATCH] configure.ac: detect unusable termio operations

Some termio operations are not actually usable on some
architectures. For example, the TCGETA, TCSETA, TCSETAF and TCSETAW
are defined with a reference to "struct termio" on alpha, hppa and
sparc64, but "struct termio" is no longer defined since glibc 2.42,
causing a build failure.

Instead of using those operations as soon as they are defined, this
commit checks more carefully that they are actually usable. This is
done using a new m4 macro PY_CHECK_IOCTL.

Upstream: https://github.com/python/cpython/pull/142380
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
[ Bernd Kuhls: ported to Python 3.14.2]
Signed-off-by: Bernd Kuhls <bernd@kuhls.net>
---
 Modules/termios.c |  8 ++++----
 configure.ac      | 26 ++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/Modules/termios.c b/Modules/termios.c
index efb5fcc17fa..3177cb0747a 100644
--- a/Modules/termios.c
+++ b/Modules/termios.c
@@ -1115,7 +1115,7 @@ static struct constant {
 #ifdef TCFLSH
     {"TCFLSH", TCFLSH},
 #endif
-#ifdef TCGETA
+#if defined(HAVE_TCGETA)
     {"TCGETA", TCGETA},
 #endif
 #ifdef TCGETS
@@ -1127,13 +1127,13 @@ static struct constant {
 #ifdef TCSBRKP
     {"TCSBRKP", TCSBRKP},
 #endif
-#ifdef TCSETA
+#if defined(HAVE_TCSETA)
     {"TCSETA", TCSETA},
 #endif
-#ifdef TCSETAF
+#if defined(HAVE_TCSETAF)
     {"TCSETAF", TCSETAF},
 #endif
-#ifdef TCSETAW
+#if defined(HAVE_TCSETAW)
     {"TCSETAW", TCSETAW},
 #endif
 #ifdef TCSETS
diff --git a/configure.ac b/configure.ac
index 3980548b253..48604024fcf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -101,6 +101,26 @@ AC_DEFUN([PY_STDLIB_MOD_SET_NA], [
     AS_VAR_SET([py_cv_module_]mod, [n/a])])
 ])
 
+dnl PY_CHECK_IOCTL(IOCTL_SYMBOL)
+AC_DEFUN([PY_CHECK_IOCTL],
+[
+  AC_MSG_CHECKING([for $1])
+  AC_COMPILE_IFELSE(
+    [AC_LANG_PROGRAM(
+       [[#include <sys/ioctl.h>]],
+       [[
+         /* Test whether $1 is declared */
+         long val = $1;
+         return 0;
+       ]]
+     )],
+    [AC_MSG_RESULT([yes])
+     AC_DEFINE([HAVE_$1], [1],
+               [Define this if $1 termio operation is usable])],
+    [AC_MSG_RESULT([no])]
+  )
+])
+
 AC_SUBST([BASECPPFLAGS])
 if test "$srcdir" != . -a "$srcdir" != "$(pwd)"; then
     # If we're building out-of-tree, we need to make sure the following
@@ -8217,6 +8237,12 @@ AC_ARG_ENABLE(idle3,
 	AS_HELP_STRING([--disable-idle3], [disable idle3 IDE]),
 	[ IDLE="${enableval}" ], [ IDLE=yes ])
 
+# ioctls used by Modules/termios.c but not usable on all platforms
+PY_CHECK_IOCTL([TCGETA])
+PY_CHECK_IOCTL([TCSETA])
+PY_CHECK_IOCTL([TCSETAF])
+PY_CHECK_IOCTL([TCSETAW])
+
 # generate output files
 AC_CONFIG_FILES(m4_normalize([
   Makefile.pre
-- 
2.51.1

