1# Macros and functions related to detecting details of the Python environment.
2
3# Finds and configures python packages needed to build MLIR Python bindings.
4macro(mlir_configure_python_dev_packages)
5  if(CMAKE_VERSION VERSION_LESS "3.15.0")
6  message(SEND_ERROR
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}")
10  endif()
11
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
15  # Development.Module.
16  # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
17  if(CMAKE_VERSION VERSION_LESS "3.18.0")
18    message(WARNING
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}"
23    )
24    set(_python_development_component Development)
25  else()
26    # Prime the search for python to see if there is a full
27    # development package. This seems to work around cmake bugs
28    # searching only for Development.Module in some environments.
29    find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
30      COMPONENTS Development)
31    set(_python_development_component Development.Module)
32  endif()
33  find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
34    COMPONENTS Interpreter ${_python_development_component} NumPy REQUIRED)
35  unset(_python_development_component)
36  message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
37  message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
38  message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
39  mlir_detect_pybind11_install()
40  find_package(pybind11 2.8 CONFIG REQUIRED)
41  message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
42  message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
43                 "suffix = '${PYTHON_MODULE_SUFFIX}', "
44                 "extension = '${PYTHON_MODULE_EXTENSION}")
45endmacro()
46
47# Detects a pybind11 package installed in the current python environment
48# and sets variables to allow it to be found. This allows pybind11 to be
49# installed via pip, which typically yields a much more recent version than
50# the OS install, which will be available otherwise.
51function(mlir_detect_pybind11_install)
52  if(pybind11_DIR)
53    message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)")
54  else()
55    message(STATUS "Checking for pybind11 in python path...")
56    execute_process(
57      COMMAND "${Python3_EXECUTABLE}"
58      -c "import pybind11;print(pybind11.get_cmake_dir(), end='')"
59      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
60      RESULT_VARIABLE STATUS
61      OUTPUT_VARIABLE PACKAGE_DIR
62      ERROR_QUIET)
63    if(NOT STATUS EQUAL "0")
64      message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)")
65      return()
66    endif()
67    message(STATUS "found (${PACKAGE_DIR})")
68    set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
69  endif()
70endfunction()
71