Skip to content

Commit 08bfede

Browse files
[mlir] Add option to disable MLIR Python dev package configuration.
Adds a CMake option MLIR_CONFIGURE_PYTHON_DEV_PACKAGES which gates doing package discovery and configuration for Python dev packages by MLIR. The default Python setup that MLIR does has been a problem for super-projects that include LLVM for a long time because it forces a very specific package discovery mechanism that is not uniform in all uses. When reviewing #117922, I noted that this would effectively be a break the world event for downstreams, forcing them to adapt their nanobind dep to the exact way that MLIR does it. Adding the option to just wholesale skip the built-in configuration heuristics at least gives us a mechanism to tell downstreams to migrate to, giving them complete control and not requiring packaging workarounds. This seemed a better option than (once again) creating a situation where downstreams could not integrate the dep change without doing tricky infra upgrades, and it removes the burden from the author of that patch from needing to think about how this affects super-projects that include MLIR (i.e. they can just be told to do it themselves as needed vs being in a wedged state and unable to upgrade).
1 parent 80afdbe commit 08bfede

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

mlir/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ configure_file(
162162
# Requires:
163163
# The pybind11 library can be found (set with -DPYBIND_DIR=...)
164164
# The python executable is correct (set with -DPython3_EXECUTABLE=...)
165+
# By default, find_package and probing for installed pybind11 is performed.
166+
# Super projects can set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES=ON to
167+
# disable all package setup and control it themselves.
165168
#-------------------------------------------------------------------------------
166169

167170
set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
@@ -170,8 +173,17 @@ set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
170173
"Prime the python detection by searching for a full 'Development' \
171174
component first (temporary while diagnosing environment specific Python \
172175
detection issues)")
176+
set(MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES 0 CACHE BOOL
177+
"Performs python dev package configuration sufficient to use all MLIR \
178+
python features. Super-projects that wish to control their own setup \
179+
must perform an appropriate find_package of Python3 with \
180+
'Development.Module' and ensure that find_package(pybind11) is \
181+
satisfied (and keep up to date as requirements evolve).")
182+
173183
if(MLIR_ENABLE_BINDINGS_PYTHON)
174184
include(MLIRDetectPythonEnv)
185+
# Note that both upstream and downstreams often call this macro. It gates
186+
# internally on the MLIR_CONFIGURE_PYTHON_DEV_PACKAGES option.
175187
mlir_configure_python_dev_packages()
176188
endif()
177189

mlir/cmake/modules/MLIRDetectPythonEnv.cmake

+28-26
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@
22

33
# Finds and configures python packages needed to build MLIR Python bindings.
44
macro(mlir_configure_python_dev_packages)
5-
if(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH)
6-
# Prime the search for python to see if there is a full development
7-
# package. This seems to work around cmake bugs searching only for
8-
# Development.Module in some environments. However, in other environments
9-
# it may interfere with the subsequent search for Development.Module.
10-
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
11-
COMPONENTS Interpreter Development)
12-
endif()
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 development
8+
# package. This seems to work around cmake bugs searching only for
9+
# Development.Module in some environments. However, in other environments
10+
# it may interfere with the subsequent search for Development.Module.
11+
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
12+
COMPONENTS Interpreter Development)
13+
endif()
1314

14-
# After CMake 3.18, we are able to limit the scope of the search to just
15-
# Development.Module. Searching for Development will fail in situations where
16-
# the Python libraries are not available. When possible, limit to just
17-
# Development.Module.
18-
# See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
19-
set(_python_development_component Development.Module)
15+
# After CMake 3.18, we are able to limit the scope of the search to just
16+
# Development.Module. Searching for Development will fail in situations where
17+
# the Python libraries are not available. When possible, limit to just
18+
# Development.Module.
19+
# See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
20+
set(_python_development_component Development.Module)
2021

21-
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
22-
COMPONENTS Interpreter ${_python_development_component} REQUIRED)
23-
unset(_python_development_component)
24-
message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
25-
message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
26-
message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
27-
mlir_detect_pybind11_install()
28-
find_package(pybind11 2.10 CONFIG REQUIRED)
29-
message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
30-
message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
31-
"suffix = '${PYTHON_MODULE_SUFFIX}', "
32-
"extension = '${PYTHON_MODULE_EXTENSION}")
22+
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
23+
COMPONENTS Interpreter ${_python_development_component} REQUIRED)
24+
unset(_python_development_component)
25+
message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
26+
message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
27+
message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
28+
mlir_detect_pybind11_install()
29+
find_package(pybind11 2.10 CONFIG REQUIRED)
30+
message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
31+
message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
32+
"suffix = '${PYTHON_MODULE_SUFFIX}', "
33+
"extension = '${PYTHON_MODULE_EXTENSION}")
34+
endif()
3335
endmacro()
3436

3537
# Detects a pybind11 package installed in the current python environment

0 commit comments

Comments
 (0)