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(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH)
6 # Prime the search for python to see if there is a full development
7 # package. This seems to work around cmake bugs searching only for
8 # Development.Module in some environments. However, in other environments
9 # it may interfere with the subsequent search for Development.Module.
10 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
11 COMPONENTS Interpreter Development)
14 # After CMake 3.18, we are able to limit the scope of the search to just
15 # Development.Module. Searching for Development will fail in situations where
16 # the Python libraries are not available. When possible, limit to just
18 # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
19 set(_python_development_component Development.Module)
21 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
22 COMPONENTS Interpreter ${_python_development_component} REQUIRED)
23 unset(_python_development_component)
24 message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
25 message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
26 message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
27 mlir_detect_pybind11_install()
28 find_package(pybind11 2.10 CONFIG REQUIRED)
29 message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
30 message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
31 "suffix = '${PYTHON_MODULE_SUFFIX}', "
32 "extension = '${PYTHON_MODULE_EXTENSION}")
35 # Detects a pybind11 package installed in the current python environment
36 # and sets variables to allow it to be found. This allows pybind11 to be
37 # installed via pip, which typically yields a much more recent version than
38 # the OS install, which will be available otherwise.
39 function(mlir_detect_pybind11_install)
41 message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)")
43 message(STATUS "Checking for pybind11 in python path...")
45 COMMAND "${Python3_EXECUTABLE}"
46 -c "import pybind11;print(pybind11.get_cmake_dir(), end='')"
47 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
48 RESULT_VARIABLE STATUS
49 OUTPUT_VARIABLE PACKAGE_DIR
51 if(NOT STATUS EQUAL "0")
52 message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)")
55 message(STATUS "found (${PACKAGE_DIR})")
56 set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)