diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -78,14 +78,12 @@ "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 --- /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 --- a/mlir/docs/Bindings/Python.md +++ b/mlir/docs/Bindings/Python.md @@ -6,9 +6,10 @@ ### 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