brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.9 KiB · d18f8c0 Raw
127 lines · plain
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(NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES)6    if(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH)7      # Prime the search for python to see if there is a full development8      # package. This seems to work around cmake bugs searching only for9      # Development.Module in some environments. However, in other environments10      # it may interfere with the subsequent search for Development.Module.11      find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}12        COMPONENTS Interpreter Development)13    endif()14 15    # After CMake 3.18, we are able to limit the scope of the search to just16    # Development.Module. Searching for Development will fail in situations where17    # the Python libraries are not available. When possible, limit to just18    # Development.Module.19    # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode20    set(_python_development_component Development.Module)21 22    find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}23      COMPONENTS Interpreter ${_python_development_component} REQUIRED)24 25    # We look for both Python3 and Python, the search algorithm should be26    # consistent, otherwise disastrous result is almost guaranteed.27    # Warn if the policies for treating virtual environment are not defined28    # consistently.29    # For more details check issue #126162.30    if(((DEFINED Python_FIND_VIRTUALENV) AND (NOT DEFINED Python3_FIND_VIRTUALENV)) OR31       ((NOT DEFINED Python_FIND_VIRTUALENV) AND (DEFINED Python3_FIND_VIRTUALENV)))32      message(WARNING "Only one of Python3_FIND_VIRTUALENV and Python_FIND_VIRTUALENV variables is defined. "33                      "Make sure that both variables are defined and have the same value.")34    elseif((DEFINED Python_FIND_VIRTUALENV) AND (DEFINED Python3_FIND_VIRTUALENV) AND35           (NOT Python_FIND_VIRTUALENV STREQUAL Python3_FIND_VIRTUALENV))36      message(WARNING "Python3_FIND_VIRTUALENV and Python_FIND_VIRTUALENV are defined differently. "37                      "Make sure that the variables have the same values.")38    endif()39 40    # It's a little silly to detect Python a second time, but nanobind's cmake41    # code looks for Python_ not Python3_.42    find_package(Python ${LLVM_MINIMUM_PYTHON_VERSION}43      COMPONENTS Interpreter ${_python_development_component} REQUIRED)44 45    unset(_python_development_component)46    message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")47    message(STATUS "Found python libraries: ${Python3_LIBRARIES}")48    message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")49    mlir_detect_pybind11_install()50    find_package(pybind11 2.10 CONFIG REQUIRED)51    message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")52    message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "53                  "suffix = '${PYTHON_MODULE_SUFFIX}', "54                  "extension = '${PYTHON_MODULE_EXTENSION}")55 56    mlir_detect_nanobind_install()57    find_package(nanobind 2.9 CONFIG REQUIRED)58    message(STATUS "Found nanobind v${nanobind_VERSION}: ${nanobind_INCLUDE_DIR}")59    message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "60                  "suffix = '${PYTHON_MODULE_SUFFIX}', "61                  "extension = '${PYTHON_MODULE_EXTENSION}")62  endif()63endmacro()64 65# Detects a pybind11 package installed in the current python environment66# and sets variables to allow it to be found. This allows pybind11 to be67# installed via pip, which typically yields a much more recent version than68# the OS install, which will be available otherwise.69function(mlir_detect_pybind11_install)70  if(pybind11_DIR)71    message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)")72  else()73    message(STATUS "Checking for pybind11 in python path...")74    execute_process(75      COMMAND "${Python3_EXECUTABLE}"76      -c "import pybind11;print(pybind11.get_cmake_dir(), end='')"77      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}78      RESULT_VARIABLE STATUS79      OUTPUT_VARIABLE PACKAGE_DIR80      ERROR_QUIET)81    if(NOT STATUS EQUAL "0")82      message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)")83      return()84    endif()85    message(STATUS "found (${PACKAGE_DIR})")86    set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)87  endif()88endfunction()89 90 91# Detects a nanobind package installed in the current python environment92# and sets variables to allow it to be found. This allows nanobind to be93# installed via pip, which typically yields a much more recent version than94# the OS install, which will be available otherwise.95function(mlir_detect_nanobind_install)96  if(nanobind_DIR)97    message(STATUS "Using explicit nanobind cmake directory: ${nanobind_DIR} (-Dnanobind_DIR to change)")98  else()99    message(STATUS "Checking for nanobind in python path...")100    execute_process(101      COMMAND "${Python3_EXECUTABLE}"102      -c "import nanobind;print(nanobind.cmake_dir(), end='')"103      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}104      RESULT_VARIABLE STATUS105      OUTPUT_VARIABLE PACKAGE_DIR106      ERROR_QUIET)107    if(NOT STATUS EQUAL "0")108      message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")109      return()110    endif()111    message(STATUS "found (${PACKAGE_DIR})")112    set(nanobind_DIR "${PACKAGE_DIR}" PARENT_SCOPE)113    execute_process(114      COMMAND "${Python3_EXECUTABLE}"115      -c "import nanobind;print(nanobind.include_dir(), end='')"116      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}117      RESULT_VARIABLE STATUS118      OUTPUT_VARIABLE PACKAGE_DIR119      ERROR_QUIET)120    if(NOT STATUS EQUAL "0")121      message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")122      return()123    endif()124    set(nanobind_INCLUDE_DIR "${PACKAGE_DIR}" PARENT_SCOPE)125  endif()126endfunction()127