1 # Macros and functions related to detecting details of the Python environment.
3 # Finds and configures python packages needed to build MLIR Python bindings.
4 macro(mlir_configure_python_dev_packages)
5 if(CMAKE_VERSION VERSION_LESS "3.15.0")
7 "Building MLIR Python bindings is known to rely on CMake features "
8 "that require at least version 3.15. Recommend upgrading to 3.18+ "
9 "for full support. Detected current version: ${CMAKE_VERSION}")
12 # After CMake 3.18, we are able to limit the scope of the search to just
13 # Development.Module. Searching for Development will fail in situations where
14 # the Python libraries are not available. When possible, limit to just
16 # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
17 if(CMAKE_VERSION VERSION_LESS "3.18.0")
19 "This version of CMake is not compatible with statically built Python "
20 "installations. If Python fails to detect below this may apply to you. "
21 "Recommend upgrading to at least CMake 3.18. "
22 "Detected current version: ${CMAKE_VERSION}"
24 set(_python_development_component Development)
26 if(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH)
27 # Prime the search for python to see if there is a full development
28 # package. This seems to work around cmake bugs searching only for
29 # Development.Module in some environments. However, in other environments
30 # it may interfere with the subsequent search for Development.Module.
31 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
32 COMPONENTS Interpreter Development)
34 set(_python_development_component Development.Module)
36 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
37 COMPONENTS Interpreter ${_python_development_component} NumPy REQUIRED)
38 unset(_python_development_component)
39 message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
40 message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
41 message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
42 mlir_detect_pybind11_install()
43 find_package(pybind11 2.8 CONFIG REQUIRED)
44 message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
45 message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
46 "suffix = '${PYTHON_MODULE_SUFFIX}', "
47 "extension = '${PYTHON_MODULE_EXTENSION}")
50 # Detects a pybind11 package installed in the current python environment
51 # and sets variables to allow it to be found. This allows pybind11 to be
52 # installed via pip, which typically yields a much more recent version than
53 # the OS install, which will be available otherwise.
54 function(mlir_detect_pybind11_install)
56 message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)")
58 message(STATUS "Checking for pybind11 in python path...")
60 COMMAND "${Python3_EXECUTABLE}"
61 -c "import pybind11;print(pybind11.get_cmake_dir(), end='')"
62 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
63 RESULT_VARIABLE STATUS
64 OUTPUT_VARIABLE PACKAGE_DIR
66 if(NOT STATUS EQUAL "0")
67 message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)")
70 message(STATUS "found (${PACKAGE_DIR})")
71 set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)