@@ -20,6 +20,28 @@ include(FetchContent)
2020# 03/05/24 - Use modern python discovery
2121set (PYBIND11_FINDPYTHON "ON" )
2222
23+ # Free-threaded (no-GIL) CPython on Windows ships only pythonXYt.lib (note the
24+ # trailing "t"); the regular pythonXY.lib is absent. Unless FindPython is asked
25+ # to search the gil_disabled ABI, it resolves the extension module's import
26+ # library to the missing pythonXY.lib and the link fails with
27+ # "LNK1104: cannot open file 'pythonXY.lib'". scikit-build-core seeds
28+ # Python_EXECUTABLE in the initial cache, so detect a free-threaded interpreter
29+ # here and steer the FindPython invocation that pybind11 performs below to the
30+ # correct ABI. The Python_FIND_ABI 4-tuple's last element selects gil_disabled
31+ # (requires CMake >= 3.30). No-op on regular interpreters.
32+ if (DEFINED Python_EXECUTABLE)
33+ execute_process (
34+ COMMAND "${Python_EXECUTABLE} " -c
35+ "import sysconfig;print(1 if sysconfig.get_config_var('Py_GIL_DISABLED') else 0)"
36+ OUTPUT_VARIABLE OSQP_PY_GIL_DISABLED
37+ OUTPUT_STRIP_TRAILING_WHITESPACE
38+ )
39+ endif ()
40+ if (WIN32 AND OSQP_PY_GIL_DISABLED STREQUAL "1" )
41+ message (STATUS "Free-threaded Python detected; searching gil_disabled ABI (pythonXYt.lib)" )
42+ set (Python_FIND_ABI "OFF" "ANY" "ANY" "ON" )
43+ endif ()
44+
2345find_package (pybind11 CONFIG REQUIRED )
2446
2547# 03/05/24 - Workaround because OSQP CMakeLists.txt is using old variable names
@@ -41,20 +63,12 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/bindings.cpp.in
4163pybind11_add_module (${OSQP_EXT_MODULE_NAME} ${CMAKE_CURRENT_BINARY_DIR } /src/bindings.cpp )
4264install (TARGETS ${OSQP_EXT_MODULE_NAME} DESTINATION . COMPONENT python)
4365
44- # Free-threaded (no-GIL) Python on Windows ships only pythonXYt.lib. The MSVC
45- # auto-link pragma in pyconfig.h selects pythonXY.lib (no "t") unless
46- # Py_GIL_DISABLED is defined at compile time, producing a spurious LNK1104 for
47- # the non-existent pythonXY.lib when building cp3XXt wheels. Mirror what
48- # setuptools does: ask the interpreter and define the macro ourselves so the
49- # correct import library is linked. No-op on non-free-threaded interpreters.
50- execute_process (
51- COMMAND "${Python_EXECUTABLE} " -c
52- "import sysconfig;print(1 if sysconfig.get_config_var('Py_GIL_DISABLED') else 0)"
53- OUTPUT_VARIABLE OSQP_PY_GIL_DISABLED
54- OUTPUT_STRIP_TRAILING_WHITESPACE
55- )
66+ # Also define Py_GIL_DISABLED at compile time on free-threaded interpreters.
67+ # The MSVC auto-link pragma in pyconfig.h selects pythonXY.lib (no "t") unless
68+ # this macro is set, and the extension must be built with the same flag the
69+ # interpreter uses to keep the C ABI in sync. Reuses OSQP_PY_GIL_DISABLED
70+ # computed above. No-op on non-free-threaded interpreters.
5671if (OSQP_PY_GIL_DISABLED STREQUAL "1" )
57- message (STATUS "Free-threaded Python detected; defining Py_GIL_DISABLED" )
5872 target_compile_definitions (${OSQP_EXT_MODULE_NAME} PRIVATE Py_GIL_DISABLED=1 )
5973endif ()
6074
0 commit comments