@@ -412,6 +412,33 @@ if(tmva-pymva OR tmva-sofie)
412412endif ()
413413find_package (Python3 3.10 COMPONENTS ${python_components} )
414414
415+ # Detect whether the found Python interpreter is a free-threaded build
416+ # (Py_GIL_DISABLED is defined in pyconfig.h). The limited C API is not
417+ # supported in free-threaded builds; including Python.h with Py_LIMITED_API
418+ # defined produces a hard error there
419+ # (https://docs.python.org/3/howto/free-threading-extensions.html).
420+ # Checking the preprocessor symbol directly is more reliable than asking the
421+ # interpreter (e.g. sysconfig.get_config_var may misreport, and
422+ # sys._is_gil_enabled() can be overridden at runtime via PYTHON_GIL=1).
423+ set (Python3_GIL_DISABLED FALSE )
424+ if (Python3_Development_FOUND OR Python3_Development.Module_FOUND)
425+ include (CheckCXXSourceCompiles )
426+ set (_old_required_includes ${CMAKE_REQUIRED_INCLUDES} )
427+ set (CMAKE_REQUIRED_INCLUDES ${Python3_INCLUDE_DIRS} )
428+ check_cxx_source_compiles ("
429+ #include <Python.h>
430+ #ifndef Py_GIL_DISABLED
431+ #error \" GIL is not disabled\"
432+ #endif
433+ int main() { return 0; }
434+ " ROOT_PYTHON_GIL_DISABLED )
435+ set (CMAKE_REQUIRED_INCLUDES ${_old_required_includes} )
436+ if (ROOT_PYTHON_GIL_DISABLED)
437+ set (Python3_GIL_DISABLED TRUE )
438+ message (STATUS "Python ${Python3_VERSION} is a free-threaded build (Py_GIL_DISABLED defined); the limited C API will not be used" )
439+ endif ()
440+ endif ()
441+
415442#---Check for OpenGL installation-------------------------------------------------------
416443# OpenGL is required by various graf3d features that are enabled with opengl=ON,
417444# or by the Cocoa-related code that always requires it.
0 commit comments