[Mlir-commits] [mlir] build multi python bindings for mlir (PR #177143)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sat Jan 24 22:58:03 PST 2026
================
@@ -1046,4 +1254,141 @@ function(add_mlir_python_extension libname extname nb_library_target_name)
RUNTIME DESTINATION ${ARG_INSTALL_DIR}
)
endif()
+
+ ################################################################################
+ # Multi-Python version support: build additional versions
+ ################################################################################
+ if(_MLIR_MULTI_PYTHON_CONFIGS)
+ # Get the primary Python version (the one configured via Python3_EXECUTABLE)
+ execute_process(
+ COMMAND ${Python3_EXECUTABLE} -c "import sys;print(f'{sys.version_info.major}.{sys.version_info.minor}',end='')"
+ OUTPUT_VARIABLE _primary_pyver
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ ERROR_QUIET
+ )
+
+ # Determine which binding library to use
+ set(_use_nanobind FALSE)
+ if(DEFINED ARG_PYTHON_BINDINGS_LIBRARY AND ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "nanobind")
+ set(_use_nanobind TRUE)
+ endif()
+
+ foreach(_pyver IN LISTS _MLIR_MULTI_PYTHON_CONFIGS)
+ # Skip the primary version (already built above)
+ if(_pyver STREQUAL _primary_pyver)
+ continue()
+ endif()
+
+ # Check if this version was found
+ if(NOT MLIR_PYTHON_${_pyver}_EXECUTABLE)
+ continue()
+ endif()
+
+ # Check if the required binding library is available for this version
+ if(_use_nanobind)
+ set(_pyver_binding_dir "${MLIR_PYTHON_${_pyver}_NANOBIND_DIR}")
+ set(_binding_name "nanobind")
+ # nanobind uses lowercase with hyphen: nanobind-config.cmake
+ set(_binding_config "nanobind-config.cmake")
+ else()
+ set(_pyver_binding_dir "${MLIR_PYTHON_${_pyver}_PYBIND11_DIR}")
+ set(_binding_name "pybind11")
+ set(_binding_config "pybind11Config.cmake")
+ endif()
+
+ if(NOT _pyver_binding_dir OR NOT EXISTS "${_pyver_binding_dir}/${_binding_config}")
+ message(WARNING "${_binding_name} not found for Python ${_pyver}, skipping ${extname}. "
+ "Install ${_binding_name} for Python ${_pyver}: python${_pyver} -m pip install ${_binding_name}")
+ continue()
+ endif()
+
+ # Create version-specific target name
+ string(REPLACE "." "" _ver_nodot ${_pyver})
+ set(_versioned_libname "${libname}_py${_ver_nodot}")
+
+ message(STATUS "Building ${extname} for Python ${_pyver} as ${_versioned_libname}")
+
+ # For nanobind, build version-specific static library if not already done
+ if(_use_nanobind)
+ _mlir_build_nanobind_static_for_version(${_pyver})
+ endif()
+
+ # Create a MODULE library for this Python version
+ add_library(${_versioned_libname} MODULE ${ARG_SOURCES})
+
+ # Set include paths for the specific Python version
+ if(_use_nanobind)
+ target_include_directories(${_versioned_libname} PRIVATE
+ ${MLIR_PYTHON_${_pyver}_INCLUDE}
+ ${MLIR_PYTHON_${_pyver}_NANOBIND_INCLUDE}
+ )
+ else()
+ # pybind11 include is typically at: pybind11_DIR/../../../include
+ get_filename_component(_pybind11_include "${_pyver_binding_dir}/../../../include" ABSOLUTE)
+ target_include_directories(${_versioned_libname} PRIVATE
+ ${MLIR_PYTHON_${_pyver}_INCLUDE}
+ ${_pybind11_include}
+ )
+ target_compile_definitions(${_versioned_libname} PRIVATE
+ -DPYBIND11_COMPILER_TYPE=""
+ -DPYBIND11_STDLIB=""
+ -DPYBIND11_BUILD_ABI=""
+ )
+ endif()
----------------
PragmaTwice wrote:
pybind11 support is already removed now: https://discourse.llvm.org/t/psa-remove-pybind11-support/89150
https://github.com/llvm/llvm-project/pull/177143
More information about the Mlir-commits
mailing list