annotate mlir/cmake/modules/MLIRDetectPythonEnv.cmake @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 79ff65ed7e25
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 # Macros and functions related to detecting details of the Python environment.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
3 # Finds and configures python packages needed to build MLIR Python bindings.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
4 macro(mlir_configure_python_dev_packages)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
5 if(CMAKE_VERSION VERSION_LESS "3.19.0")
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
6 message(SEND_ERROR
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
7 "Building MLIR Python bindings is known to rely on CMake features "
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
8 "that require at least version 3.19. Recommend upgrading to 3.19+ "
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
9 "for full support. Detected current version: ${CMAKE_VERSION}")
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
10 endif()
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
11
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
12 if(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
13 # Prime the search for python to see if there is a full development
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
14 # package. This seems to work around cmake bugs searching only for
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
15 # Development.Module in some environments. However, in other environments
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
16 # it may interfere with the subsequent search for Development.Module.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
17 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
18 COMPONENTS Interpreter Development)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
19 endif()
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
20
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
21 # After CMake 3.18, we are able to limit the scope of the search to just
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
22 # Development.Module. Searching for Development will fail in situations where
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
23 # the Python libraries are not available. When possible, limit to just
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
24 # Development.Module.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
25 # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
26 set(_python_development_component Development.Module)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
27
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
28 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
29 COMPONENTS Interpreter ${_python_development_component} NumPy REQUIRED)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
30 unset(_python_development_component)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
31 message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
32 message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
33 message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
34 mlir_detect_pybind11_install()
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
35 find_package(pybind11 2.8 CONFIG REQUIRED)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
36 message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
37 message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
38 "suffix = '${PYTHON_MODULE_SUFFIX}', "
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
39 "extension = '${PYTHON_MODULE_EXTENSION}")
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
40 endmacro()
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
41
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 # Detects a pybind11 package installed in the current python environment
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 # and sets variables to allow it to be found. This allows pybind11 to be
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 # installed via pip, which typically yields a much more recent version than
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 # the OS install, which will be available otherwise.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 function(mlir_detect_pybind11_install)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 if(pybind11_DIR)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)")
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 else()
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 message(STATUS "Checking for pybind11 in python path...")
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 execute_process(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 COMMAND "${Python3_EXECUTABLE}"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 -c "import pybind11;print(pybind11.get_cmake_dir(), end='')"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 RESULT_VARIABLE STATUS
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 OUTPUT_VARIABLE PACKAGE_DIR
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 ERROR_QUIET)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 if(NOT STATUS EQUAL "0")
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)")
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 return()
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 endif()
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 message(STATUS "found (${PACKAGE_DIR})")
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 endif()
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 endfunction()