Skip to content

Commit 86a82f2

Browse files
authored
[MLIR][Python] make sure stubs get installed with LLVM_DISTRIBUTION_COMPONENTS (#168407)
Fixes #168393. Also adds top-level `MLIR_PYTHON_STUBGEN_ENABLED` CMake option.
1 parent b79a665 commit 86a82f2

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

mlir/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ set(MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES 0 CACHE BOOL
210210
'Development.Module' and ensure that find_package(pybind11) is \
211211
satisfied (and keep up to date as requirements evolve).")
212212

213+
set(_mlir_python_stubgen_enabled ON)
214+
# Stubgen doesn't work when cross-compiling (stubgen will run in the host interpreter and then fail
215+
# to find the extension module for the host arch).
216+
# Note: Stubgen requires some extra handling to work properly when sanitizers are enabled,
217+
# so we skip running it in that case now.
218+
if(CMAKE_CROSSCOMPILING OR (NOT LLVM_USE_SANITIZER STREQUAL ""))
219+
set(_mlir_python_stubgen_enabled OFF)
220+
endif()
221+
222+
option(MLIR_PYTHON_STUBGEN_ENABLED
223+
"Generate Python type stubs for the MLIR Python bindings."
224+
${_mlir_python_stubgen_enabled})
225+
213226
if(MLIR_ENABLE_BINDINGS_PYTHON)
214227
include(MLIRDetectPythonEnv)
215228
# Note that both upstream and downstreams often call this macro. It gates

mlir/examples/standalone/python/CMakeLists.txt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@ add_mlir_python_common_capi_library(StandalonePythonCAPI
7474

7575
set(StandalonePythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
7676

77-
set(_mlir_python_stubgen_enabled ON)
78-
if(CMAKE_CROSSCOMPILING OR (NOT LLVM_USE_SANITIZER STREQUAL ""))
79-
set(_mlir_python_stubgen_enabled OFF)
80-
endif()
81-
82-
if(_mlir_python_stubgen_enabled)
77+
if(MLIR_PYTHON_STUBGEN_ENABLED)
8378
# Everything here is very tightly coupled. See the ample descriptions at the bottom of
8479
# mlir/python/CMakeLists.txt.
8580

@@ -146,7 +141,7 @@ set(_declared_sources
146141
)
147142
# For an external projects build, the MLIRPythonExtension.Core.type_stub_gen
148143
# target already exists and can just be added to DECLARED_SOURCES.
149-
if(EXTERNAL_PROJECT_BUILD AND _mlir_python_stubgen_enabled)
144+
if(EXTERNAL_PROJECT_BUILD AND MLIR_PYTHON_STUBGEN_ENABLED)
150145
list(APPEND _declared_sources MLIRPythonExtension.Core.type_stub_gen)
151146
endif()
152147

@@ -158,7 +153,7 @@ add_mlir_python_modules(StandalonePythonModules
158153
StandalonePythonCAPI
159154
)
160155

161-
if(_mlir_python_stubgen_enabled)
156+
if(MLIR_PYTHON_STUBGEN_ENABLED)
162157
if(NOT EXTERNAL_PROJECT_BUILD)
163158
add_dependencies(StandalonePythonModules "${_mlir_typestub_gen_target}")
164159
endif()

mlir/python/CMakeLists.txt

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -895,24 +895,8 @@ add_mlir_python_common_capi_library(MLIRPythonCAPI
895895
################################################################################
896896

897897
_flatten_mlir_python_targets(mlir_python_sources_deps MLIRPythonSources)
898-
add_custom_target("mlir-python-sources" DEPENDS ${mlir_python_sources_deps})
899-
if(NOT LLVM_ENABLE_IDE)
900-
add_llvm_install_targets(install-mlir-python-sources
901-
DEPENDS mlir-python-sources
902-
COMPONENT mlir-python-sources
903-
)
904-
endif()
905-
906-
set(_mlir_python_stubgen_enabled ON)
907-
# Stubgen doesn't work when cross-compiling (stubgen will run in the host interpreter and then fail
908-
# to find the extension module for the host arch).
909-
# Note: Stubgen requires some extra handling to work properly when sanitizers are enabled,
910-
# so we skip running it in that case now.
911-
if(CMAKE_CROSSCOMPILING OR (NOT LLVM_USE_SANITIZER STREQUAL ""))
912-
set(_mlir_python_stubgen_enabled OFF)
913-
endif()
914898

915-
if(_mlir_python_stubgen_enabled)
899+
if(MLIR_PYTHON_STUBGEN_ENABLED)
916900
# _mlir stubgen
917901
# Note: All this needs to come before add_mlir_python_modules(MLIRPythonModules so that the install targets for the
918902
# generated type stubs get created.
@@ -965,6 +949,7 @@ if(_mlir_python_stubgen_enabled)
965949
ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}/type_stubs"
966950
SOURCES "${_core_type_stub_sources}"
967951
)
952+
list(APPEND mlir_python_sources_deps MLIRPythonExtension.Core.type_stub_gen)
968953

969954
# _mlirPythonTestNanobind stubgen
970955

@@ -995,13 +980,21 @@ if(_mlir_python_stubgen_enabled)
995980
endif()
996981
endif()
997982

983+
add_custom_target("mlir-python-sources" DEPENDS ${mlir_python_sources_deps})
984+
if(NOT LLVM_ENABLE_IDE)
985+
add_llvm_install_targets(install-mlir-python-sources
986+
DEPENDS mlir-python-sources
987+
COMPONENT mlir-python-sources
988+
)
989+
endif()
990+
998991
################################################################################
999992
# The fully assembled package of modules.
1000993
# This must come last.
1001994
################################################################################
1002995

1003996
set(_declared_sources MLIRPythonSources MLIRPythonExtension.RegisterEverything)
1004-
if(_mlir_python_stubgen_enabled)
997+
if(MLIR_PYTHON_STUBGEN_ENABLED)
1005998
list(APPEND _declared_sources MLIRPythonExtension.Core.type_stub_gen)
1006999
endif()
10071000

@@ -1014,7 +1007,7 @@ add_mlir_python_modules(MLIRPythonModules
10141007
COMMON_CAPI_LINK_LIBS
10151008
MLIRPythonCAPI
10161009
)
1017-
if(_mlir_python_stubgen_enabled)
1010+
if(MLIR_PYTHON_STUBGEN_ENABLED)
10181011
add_dependencies(MLIRPythonModules "${_mlir_typestub_gen_target}")
10191012
if(MLIR_INCLUDE_TESTS)
10201013
add_dependencies(MLIRPythonModules "${_mlirPythonTestNanobind_typestub_gen_target}")

0 commit comments

Comments
 (0)