From f4f8a67aaf13bc66a2b7d55561b14a3724a5e0de Mon Sep 17 00:00:00 2001 From: Stella Laurenzo Date: Fri, 20 Nov 2020 17:57:46 -0800 Subject: [PATCH] [mlir][Python] Support finding pybind11 from the python environment. * Makes `pip install pybind11` do the right thing with no further config. * Since we now require a version of pybind11 greater than many LTS OS installs (>=2.6), a more convenient way to get a recent version is preferable. * Also adds the version spec to find_package so it will skip older versions that may be lying around. * Tested the full matrix of old system install, no system install, pip install and no pip install. Differential Revision: https://reviews.llvm.org/D91903 --- mlir/CMakeLists.txt | 8 +++----- mlir/cmake/modules/MLIRDetectPythonEnv.cmake | 26 ++++++++++++++++++++++++++ mlir/docs/Bindings/Python.md | 5 +++-- 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 mlir/cmake/modules/MLIRDetectPythonEnv.cmake diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt index 09ab67f..c10a169 100644 --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -78,14 +78,12 @@ set(MLIR_PYTHON_BINDINGS_VERSION_LOCKED 1 CACHE BOOL "Links to specific python libraries, resolving all symbols.") if(MLIR_BINDINGS_PYTHON_ENABLED) + include(MLIRDetectPythonEnv) find_package(Python3 COMPONENTS Interpreter Development REQUIRED) message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}") message(STATUS "Found python libraries: ${Python3_LIBRARIES}") - find_package(pybind11 CONFIG REQUIRED) - # TODO: pybind11 v2.6 switched from pybind11_INCLUDE_DIRS (plural) to - # pybind11_INCLUDE_DIR (singular). A lot has changed in this area since this - # was written and overall python config and pybind11 should be modernized. - set(pybind11_INCLUDE_DIR ${pybind11_INCLUDE_DIR} ${pybind11_INCLUDE_DIRS}) + mlir_detect_pybind11_install() + find_package(pybind11 2.6 CONFIG REQUIRED) message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}") message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', " "suffix = '${PYTHON_MODULE_SUFFIX}', " diff --git a/mlir/cmake/modules/MLIRDetectPythonEnv.cmake b/mlir/cmake/modules/MLIRDetectPythonEnv.cmake new file mode 100644 index 0000000..e3572c3 --- /dev/null +++ b/mlir/cmake/modules/MLIRDetectPythonEnv.cmake @@ -0,0 +1,26 @@ +# Macros and functions related to detecting details of the Python environment. + +# Detects a pybind11 package installed in the current python environment +# and sets variables to allow it to be found. This allows pybind11 to be +# installed via pip, which typically yields a much more recent version than +# the OS install, which will be available otherwise. +function(mlir_detect_pybind11_install) + if(pybind11_DIR) + message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)") + else() + message(CHECK_START "Checking for pybind11 in python path...") + execute_process( + COMMAND "${PYTHON_EXECUTABLE}" + -c "import pybind11;print(pybind11.get_cmake_dir(), end='')" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + RESULT_VARIABLE STATUS + OUTPUT_VARIABLE PACKAGE_DIR + ERROR_QUIET) + if(NOT STATUS EQUAL "0") + message(CHECK_FAIL "not found (install via 'pip install pybind11' or set pybind11_DIR)") + return() + endif() + message(CHECK_PASS "found (${PACKAGE_DIR})") + set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE) + endif() +endfunction() diff --git a/mlir/docs/Bindings/Python.md b/mlir/docs/Bindings/Python.md index a1626ea..89f2742 100644 --- a/mlir/docs/Bindings/Python.md +++ b/mlir/docs/Bindings/Python.md @@ -6,9 +6,10 @@ Current status: Under development and not enabled by default ### Pre-requisites -* [`pybind11`](https://github.com/pybind/pybind11) must be installed and able to - be located by CMake. Note: minimum version required: :2.6.0 * A relatively recent Python3 installation +* [`pybind11`](https://github.com/pybind/pybind11) must be installed and able to + be located by CMake (auto-detected if installed via + `python -m pip install pybind11`). Note: minimum version required: :2.6.0. ### CMake variables -- 2.7.4