2020-11-20 16:57:46 -09:00
|
|
|
# Macros and functions related to detecting details of the Python environment.
|
|
|
|
|
|
2026-01-04 16:43:56 -09:00
|
|
|
set(MLIR_MINIMUM_PYTHON_VERSION 3.10)
|
2021-10-12 18:32:48 -08:00
|
|
|
# Finds and configures python packages needed to build MLIR Python bindings.
|
|
|
|
|
macro(mlir_configure_python_dev_packages)
|
2024-11-27 16:11:32 -09:00
|
|
|
if(NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES)
|
|
|
|
|
if(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH)
|
|
|
|
|
# Prime the search for python to see if there is a full development
|
|
|
|
|
# package. This seems to work around cmake bugs searching only for
|
|
|
|
|
# Development.Module in some environments. However, in other environments
|
|
|
|
|
# it may interfere with the subsequent search for Development.Module.
|
2026-01-04 16:43:56 -09:00
|
|
|
find_package(Python3 ${MLIR_MINIMUM_PYTHON_VERSION}
|
2024-11-27 16:11:32 -09:00
|
|
|
COMPONENTS Interpreter Development)
|
|
|
|
|
endif()
|
2022-07-20 06:21:36 -08:00
|
|
|
|
2024-11-27 16:11:32 -09:00
|
|
|
# After CMake 3.18, we are able to limit the scope of the search to just
|
|
|
|
|
# Development.Module. Searching for Development will fail in situations where
|
|
|
|
|
# the Python libraries are not available. When possible, limit to just
|
|
|
|
|
# Development.Module.
|
|
|
|
|
# See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
|
|
|
|
|
set(_python_development_component Development.Module)
|
2026-03-01 04:45:52 -09:00
|
|
|
if(MLIR_ENABLE_PYTHON_STABLE_ABI)
|
|
|
|
|
list(APPEND _python_development_component Development.SABIModule)
|
|
|
|
|
endif()
|
2022-07-20 06:21:36 -08:00
|
|
|
|
2026-01-04 16:43:56 -09:00
|
|
|
find_package(Python3 ${MLIR_MINIMUM_PYTHON_VERSION}
|
2024-11-27 16:11:32 -09:00
|
|
|
COMPONENTS Interpreter ${_python_development_component} REQUIRED)
|
2024-12-09 12:37:43 -09:00
|
|
|
|
2025-02-18 10:32:26 -09:00
|
|
|
# We look for both Python3 and Python, the search algorithm should be
|
|
|
|
|
# consistent, otherwise disastrous result is almost guaranteed.
|
|
|
|
|
# Warn if the policies for treating virtual environment are not defined
|
|
|
|
|
# consistently.
|
|
|
|
|
# For more details check issue #126162.
|
|
|
|
|
if(((DEFINED Python_FIND_VIRTUALENV) AND (NOT DEFINED Python3_FIND_VIRTUALENV)) OR
|
|
|
|
|
((NOT DEFINED Python_FIND_VIRTUALENV) AND (DEFINED Python3_FIND_VIRTUALENV)))
|
|
|
|
|
message(WARNING "Only one of Python3_FIND_VIRTUALENV and Python_FIND_VIRTUALENV variables is defined. "
|
|
|
|
|
"Make sure that both variables are defined and have the same value.")
|
|
|
|
|
elseif((DEFINED Python_FIND_VIRTUALENV) AND (DEFINED Python3_FIND_VIRTUALENV) AND
|
|
|
|
|
(NOT Python_FIND_VIRTUALENV STREQUAL Python3_FIND_VIRTUALENV))
|
|
|
|
|
message(WARNING "Python3_FIND_VIRTUALENV and Python_FIND_VIRTUALENV are defined differently. "
|
|
|
|
|
"Make sure that the variables have the same values.")
|
|
|
|
|
endif()
|
|
|
|
|
|
2024-12-09 12:37:43 -09:00
|
|
|
# It's a little silly to detect Python a second time, but nanobind's cmake
|
|
|
|
|
# code looks for Python_ not Python3_.
|
2026-01-04 16:43:56 -09:00
|
|
|
find_package(Python ${MLIR_MINIMUM_PYTHON_VERSION}
|
2024-12-09 12:37:43 -09:00
|
|
|
COMPONENTS Interpreter ${_python_development_component} REQUIRED)
|
|
|
|
|
|
2024-11-27 16:11:32 -09:00
|
|
|
unset(_python_development_component)
|
2026-03-01 04:45:52 -09:00
|
|
|
if(MLIR_ENABLE_PYTHON_STABLE_ABI)
|
|
|
|
|
message(STATUS "MLIR Python stable ABI (abi3) enabled")
|
|
|
|
|
endif()
|
2024-11-27 16:11:32 -09:00
|
|
|
message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
|
|
|
|
|
message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
|
2025-12-19 08:51:22 -09:00
|
|
|
message(STATUS "Python extension suffix for modules: '${Python3_SOABI}'")
|
|
|
|
|
if(nanobind_DIR)
|
|
|
|
|
message(STATUS "Using explicit nanobind cmake directory: ${nanobind_DIR} (-Dnanobind_DIR to change)")
|
|
|
|
|
else()
|
|
|
|
|
message(STATUS "Checking for nanobind in python path...")
|
|
|
|
|
execute_process(
|
|
|
|
|
COMMAND "${Python3_EXECUTABLE}"
|
|
|
|
|
-c "import nanobind;print(nanobind.cmake_dir(), end='')"
|
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
|
|
|
RESULT_VARIABLE STATUS
|
|
|
|
|
OUTPUT_VARIABLE PACKAGE_DIR
|
|
|
|
|
ERROR_QUIET)
|
|
|
|
|
if(NOT STATUS EQUAL "0")
|
2026-01-08 08:34:33 -09:00
|
|
|
message(FATAL_ERROR "not found (install via 'pip install nanobind' or set nanobind_DIR)")
|
2025-12-19 08:51:22 -09:00
|
|
|
endif()
|
|
|
|
|
message(STATUS "found (${PACKAGE_DIR})")
|
|
|
|
|
set(nanobind_DIR "${PACKAGE_DIR}")
|
|
|
|
|
execute_process(
|
|
|
|
|
COMMAND "${Python3_EXECUTABLE}"
|
|
|
|
|
-c "import nanobind;print(nanobind.include_dir(), end='')"
|
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
|
|
|
RESULT_VARIABLE STATUS
|
|
|
|
|
OUTPUT_VARIABLE PACKAGE_DIR
|
|
|
|
|
ERROR_QUIET)
|
|
|
|
|
if(NOT STATUS EQUAL "0")
|
2026-01-08 08:34:33 -09:00
|
|
|
message(FATAL_ERROR "not found (install via 'pip install nanobind' or set nanobind_DIR)")
|
2025-12-19 08:51:22 -09:00
|
|
|
endif()
|
|
|
|
|
set(nanobind_INCLUDE_DIR "${PACKAGE_DIR}")
|
|
|
|
|
endif()
|
2025-10-07 08:30:10 -08:00
|
|
|
find_package(nanobind 2.9 CONFIG REQUIRED)
|
2026-07-22 12:22:03 -08:00
|
|
|
message(STATUS "Found nanobind v${nanobind_VERSION}")
|
2024-12-09 12:37:43 -09:00
|
|
|
endif()
|
2025-10-06 09:17:04 -08:00
|
|
|
endmacro()
|