@@ -21,14 +21,9 @@ include(FetchContent)
2121set (PYBIND11_FINDPYTHON "ON" )
2222
2323# 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
24+ # trailing "t"); the regular pythonXY.lib is absent. scikit-build-core seeds
2825# 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.
26+ # up front and handle the two ways a stale "pythonXY.lib" reference creeps in.
3227if (DEFINED Python_EXECUTABLE)
3328 execute_process (
3429 COMMAND "${Python_EXECUTABLE} " -c
@@ -37,9 +32,25 @@ if(DEFINED Python_EXECUTABLE)
3732 OUTPUT_STRIP_TRAILING_WHITESPACE
3833 )
3934endif ()
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" )
35+ if (OSQP_PY_GIL_DISABLED STREQUAL "1" )
36+ message (STATUS "Free-threaded Python detected; defining Py_GIL_DISABLED for all targets" )
37+
38+ # (1) Define Py_GIL_DISABLED for EVERY translation unit that includes
39+ # <Python.h>, not just the pybind11 module. OSQP's C sources pull in
40+ # cmake/memory.h and cmake/printing.h (which include <Python.h>) and are
41+ # compiled into osqpstatic/OSQPLIB. Without the macro, the MSVC auto-link
42+ # pragma in pyconfig.h bakes a "pythonXY.lib" (no "t") dependency into those
43+ # objects, and the final link can't satisfy it -> LNK1104. A directory-scope
44+ # definition set before the OSQP subproject is added reaches all targets.
45+ add_compile_definitions (Py_GIL_DISABLED=1 )
46+
47+ # (2) Steer the FindPython invocation that pybind11 performs below to the
48+ # gil_disabled ABI so the module's own import library resolves to
49+ # pythonXYt.lib. The Python_FIND_ABI 4-tuple's last element selects
50+ # gil_disabled (requires CMake >= 3.30). Windows-only ABI naming concern.
51+ if (WIN32 )
52+ set (Python_FIND_ABI "OFF" "ANY" "ANY" "ON" )
53+ endif ()
4354endif ()
4455
4556find_package (pybind11 CONFIG REQUIRED )
@@ -63,15 +74,6 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/bindings.cpp.in
6374pybind11_add_module (${OSQP_EXT_MODULE_NAME} ${CMAKE_CURRENT_BINARY_DIR } /src/bindings.cpp )
6475install (TARGETS ${OSQP_EXT_MODULE_NAME} DESTINATION . COMPONENT python)
6576
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.
71- if (OSQP_PY_GIL_DISABLED STREQUAL "1" )
72- target_compile_definitions (${OSQP_EXT_MODULE_NAME} PRIVATE Py_GIL_DISABLED=1 )
73- endif ()
74-
7577# TODO: We shouldn't have to do this once the interfaces are set up correctly
7678if (${OSQP_ALGEBRA_BACKEND} STREQUAL "builtin" )
7779 target_link_libraries (ext_builtin PUBLIC pybind11::module osqpstatic )
0 commit comments