diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake --- a/mlir/cmake/modules/AddMLIRPython.cmake +++ b/mlir/cmake/modules/AddMLIRPython.cmake @@ -547,6 +547,19 @@ $<$:LINKER:--exclude-libs,ALL> ) + if(WIN32) + # On Windows, pyconfig.h (and by extension python.h) hardcode the version of the + # python library which will be used for linkage depending on the flavor of the build. + # pybind11 has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG + # is not passed in as a compile definition, pybind11 undefs _DEBUG when including + # python.h, so that the release python library would be used). + # Since mlir uses pybind11, we can leverage their workaround by never directly + # pyconfig.h or python.h and instead relying on the pybind11 headers to include the + # necessary python headers. This results in mlir always linking against the + # release python library via the (undocumented) cmake property Python3_LIBRARY_RELEASE. + target_link_libraries(${libname} PRIVATE ${Python3_LIBRARY_RELEASE}) + endif() + ################################################################################ # Install ################################################################################ diff --git a/mlir/include/mlir-c/Bindings/Python/Interop.h b/mlir/include/mlir-c/Bindings/Python/Interop.h --- a/mlir/include/mlir-c/Bindings/Python/Interop.h +++ b/mlir/include/mlir-c/Bindings/Python/Interop.h @@ -21,7 +21,14 @@ #ifndef MLIR_C_BINDINGS_PYTHON_INTEROP_H #define MLIR_C_BINDINGS_PYTHON_INTEROP_H -#include +// We *should*, in theory, include Python.h here in order to import the correct +// definitions for what we need below, however, importing Python.h directly on +// Windows results in the enforcement of either pythonX.lib or pythonX_d.lib +// depending on the build flavor. Instead, we rely on the fact that this file +// (Interop.h) is always included AFTER pybind11 and will therefore have access +// to the definitions from Python.h in addition to having a workaround applied +// through the pybind11 headers that allows us to control which python library +// is used. #include "mlir-c/AffineExpr.h" #include "mlir-c/AffineMap.h" diff --git a/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp b/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp --- a/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp +++ b/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp @@ -6,7 +6,6 @@ // //===----------------------------------------------------------------------===// -#include "mlir-c/Bindings/Python/Interop.h" #include "mlir-c/ExecutionEngine.h" #include "mlir/Bindings/Python/PybindAdaptors.h" diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -19,7 +19,6 @@ #include "mlir-c/Registration.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" -#include #include