1f4f8a67aSStella Laurenzo# Macros and functions related to detecting details of the Python environment. 2f4f8a67aSStella Laurenzo 33d92722fSStella Laurenzo# Finds and configures python packages needed to build MLIR Python bindings. 43d92722fSStella Laurenzomacro(mlir_configure_python_dev_packages) 5*e0af5032SMike Urbach if(CMAKE_VERSION VERSION_LESS "3.19.0") 63d92722fSStella Laurenzo message(SEND_ERROR 73d92722fSStella Laurenzo "Building MLIR Python bindings is known to rely on CMake features " 8*e0af5032SMike Urbach "that require at least version 3.19. Recommend upgrading to 3.19+ " 93d92722fSStella Laurenzo "for full support. Detected current version: ${CMAKE_VERSION}") 103d92722fSStella Laurenzo endif() 113d92722fSStella Laurenzo 12d75ac581Srkayaith if(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH) 13d75ac581Srkayaith # Prime the search for python to see if there is a full development 14d75ac581Srkayaith # package. This seems to work around cmake bugs searching only for 15d75ac581Srkayaith # Development.Module in some environments. However, in other environments 16d75ac581Srkayaith # it may interfere with the subsequent search for Development.Module. 1784fe34a0SStephen Neuendorffer find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} 18cb318526SJohn Demme COMPONENTS Interpreter Development) 19d75ac581Srkayaith endif() 20*e0af5032SMike Urbach 21*e0af5032SMike Urbach # After CMake 3.18, we are able to limit the scope of the search to just 22*e0af5032SMike Urbach # Development.Module. Searching for Development will fail in situations where 23*e0af5032SMike Urbach # the Python libraries are not available. When possible, limit to just 24*e0af5032SMike Urbach # Development.Module. 25*e0af5032SMike Urbach # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode 263d92722fSStella Laurenzo set(_python_development_component Development.Module) 27*e0af5032SMike Urbach 283d92722fSStella Laurenzo find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} 293d92722fSStella Laurenzo COMPONENTS Interpreter ${_python_development_component} NumPy REQUIRED) 303d92722fSStella Laurenzo unset(_python_development_component) 313d92722fSStella Laurenzo message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}") 323d92722fSStella Laurenzo message(STATUS "Found python libraries: ${Python3_LIBRARIES}") 333d92722fSStella Laurenzo message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}") 343d92722fSStella Laurenzo mlir_detect_pybind11_install() 3589a92fb3SAlex Zinenko find_package(pybind11 2.8 CONFIG REQUIRED) 363d92722fSStella Laurenzo message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}") 373d92722fSStella Laurenzo message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', " 383d92722fSStella Laurenzo "suffix = '${PYTHON_MODULE_SUFFIX}', " 393d92722fSStella Laurenzo "extension = '${PYTHON_MODULE_EXTENSION}") 403d92722fSStella Laurenzoendmacro() 413d92722fSStella Laurenzo 42f4f8a67aSStella Laurenzo# Detects a pybind11 package installed in the current python environment 43f4f8a67aSStella Laurenzo# and sets variables to allow it to be found. This allows pybind11 to be 44f4f8a67aSStella Laurenzo# installed via pip, which typically yields a much more recent version than 45f4f8a67aSStella Laurenzo# the OS install, which will be available otherwise. 46f4f8a67aSStella Laurenzofunction(mlir_detect_pybind11_install) 47f4f8a67aSStella Laurenzo if(pybind11_DIR) 48f4f8a67aSStella Laurenzo message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)") 49f4f8a67aSStella Laurenzo else() 5021b346bdSAlex Zinenko message(STATUS "Checking for pybind11 in python path...") 51f4f8a67aSStella Laurenzo execute_process( 5215481bbaSStella Laurenzo COMMAND "${Python3_EXECUTABLE}" 53f4f8a67aSStella Laurenzo -c "import pybind11;print(pybind11.get_cmake_dir(), end='')" 54f4f8a67aSStella Laurenzo WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 55f4f8a67aSStella Laurenzo RESULT_VARIABLE STATUS 56f4f8a67aSStella Laurenzo OUTPUT_VARIABLE PACKAGE_DIR 57f4f8a67aSStella Laurenzo ERROR_QUIET) 58f4f8a67aSStella Laurenzo if(NOT STATUS EQUAL "0") 5921b346bdSAlex Zinenko message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)") 60f4f8a67aSStella Laurenzo return() 61f4f8a67aSStella Laurenzo endif() 6221b346bdSAlex Zinenko message(STATUS "found (${PACKAGE_DIR})") 63f4f8a67aSStella Laurenzo set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE) 64f4f8a67aSStella Laurenzo endif() 65f4f8a67aSStella Laurenzoendfunction() 66