11include (AddMLIRPython)
22
3+ # Specifies that all MLIR packages are co-located under the `mlir_standalone`
4+ # top level package (the API has been embedded in a relocatable way).
5+ add_compile_definitions ("MLIR_PYTHON_PACKAGE_PREFIX=${MLIR_PYTHON_PACKAGE_PREFIX} ." )
6+
37################################################################################
48# Structural groupings.
59################################################################################
@@ -23,11 +27,6 @@ declare_mlir_python_sources(MLIRPythonSources.Core.Python
2327 passmanager.py
2428 rewrite.py
2529 dialects/_ods_common.py
26-
27- # The main _mlir module has submodules: include stubs from each.
28- _mlir_libs/_mlir/__init__.pyi
29- _mlir_libs/_mlir/ir.pyi
30- _mlir_libs/_mlir/passmanager.pyi
3130)
3231
3332declare_mlir_python_sources(MLIRPythonSources.Core.Python.Extras
@@ -663,7 +662,7 @@ if(MLIR_ENABLE_EXECUTION_ENGINE)
663662 MODULE_NAME _mlirExecutionEngine
664663 ADD_TO_PARENT MLIRPythonSources.ExecutionEngine
665664 ROOT_DIR "${PYTHON_SOURCE_DIR} "
666- PYTHON_BINDINGS_LIBRARY nanobind
665+ PYTHON_BINDINGS_LIBRARY nanobind
667666 SOURCES
668667 ExecutionEngineModule.cpp
669668 PRIVATE_LINK_LIBS
@@ -814,10 +813,11 @@ endif()
814813# once ready.
815814################################################################################
816815
816+ set (MLIRPythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR} /${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX} " )
817817add_mlir_python_common_capi_library(MLIRPythonCAPI
818818 INSTALL_COMPONENT MLIRPythonModules
819819 INSTALL_DESTINATION "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX} /_mlir_libs"
820- OUTPUT_DIRECTORY "${MLIR_BINARY_DIR} /python_packages/mlir_core/mlir /_mlir_libs"
820+ OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX} /_mlir_libs"
821821 RELATIVE_INSTALL_ROOT "../../../.."
822822 DECLARED_HEADERS
823823 MLIRPythonCAPI.HeaderSources
@@ -846,7 +846,7 @@ endif()
846846################################################################################
847847
848848add_mlir_python_modules(MLIRPythonModules
849- ROOT_PREFIX " ${MLIR_BINARY_DIR} /python_packages/mlir_core/mlir"
849+ ROOT_PREFIX ${MLIRPythonModules_ROOT_PREFIX}
850850 INSTALL_PREFIX "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX} "
851851 DECLARED_SOURCES
852852 MLIRPythonSources
@@ -855,3 +855,78 @@ add_mlir_python_modules(MLIRPythonModules
855855 COMMON_CAPI_LINK_LIBS
856856 MLIRPythonCAPI
857857)
858+
859+ # _mlir stubgen
860+
861+ set (_core_type_stub_sources
862+ _mlir/__init__.pyi
863+ _mlir/ir.pyi
864+ _mlir/passmanager.pyi
865+ _mlir/rewrite.pyi
866+ )
867+
868+ # Note 1: INTERFACE_SOURCES is a genex ($<BUILD_INTERFACE> $<INSTALL_INTERFACE>)
869+ # which will be evaluated by file(GENERATE ...) inside generate_type_stubs. This will evaluate to the correct
870+ # thing in the build dir (i.e., actual source dir paths) and in the install dir (where it's a conventional path; see install/lib/cmake/mlir/MLIRTargets.cmake).
871+ #
872+ # Note 2: MLIRPythonExtension.Core is the target that is defined using target_sources(INTERFACE) **NOT** MLIRPythonModules.extension._mlir.dso.
873+ # So be sure to use the correct target!
874+ get_target_property (_core_extension_srcs MLIRPythonExtension.Core INTERFACE_SOURCES)
875+
876+ # Why is MODULE_NAME _mlir_libs._mlir here but mlir._mlir_libs._mlirPythonTestNanobind below???
877+ # The _mlir extension can be imported independently of any other python code and/or extension modules.
878+ # I.e., you could do `cd $MLIRPythonModules_ROOT_PREFIX && python -c "import _mlir_libs._mlir"` (try it!).
879+ # _mlir is also (currently) the only extension for which this is possible because dialect extensions modules,
880+ # which generally make use of `mlir_value_subclass/mlir_type_subclass/mlir_attribute_subclass`, perform an
881+ # `import mlir` right when they're loaded (see the mlir_*_subclass ctors in NanobindAdaptors.h).
882+ # Note, this also why IMPORT_PATHS "${MLIRPythonModules_ROOT_PREFIX}" here while below
883+ # "${MLIRPythonModules_ROOT_PREFIX}/.." (because MLIR_BINDINGS_PYTHON_INSTALL_PREFIX, by default, ends at mlir).
884+ #
885+ # Further note: this function creates file targets like "${CMAKE_CURRENT_BINARY_DIR}/type_stubs/_mlir_libs/_mlir/__init__.pyi".
886+ # These must match the file targets that declare_mlir_python_sources expects, which are like "${ROOT_DIR}/${WHATEVER_SOURCE}".
887+ # This is why _mlir_libs is prepended below.
888+ generate_type_stubs(
889+ MODULE_NAME _mlir_libs._mlir
890+ DEPENDS_TARGETS MLIRPythonModules.extension._mlir.dso
891+ OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs/_mlir_libs"
892+ OUTPUTS "${_core_type_stub_sources} "
893+ DEPENDS_TARGET_SRC_DEPS "${_core_extension_srcs} "
894+ IMPORT_PATHS "${MLIRPythonModules_ROOT_PREFIX} "
895+ )
896+ add_dependencies (MLIRPythonModules "${NB_STUBGEN_CUSTOM_TARGET} " )
897+
898+ list (TRANSFORM _core_type_stub_sources PREPEND "_mlir_libs/" )
899+ declare_mlir_python_sources(
900+ MLIRPythonExtension.Core.type_stub_gen
901+ ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs"
902+ ADD_TO_PARENT MLIRPythonSources.Core
903+ SOURCES "${_core_type_stub_sources} "
904+ )
905+
906+ # _mlirPythonTestNanobind stubgen
907+
908+ if (MLIR_INCLUDE_TESTS)
909+ get_target_property (_test_extension_srcs MLIRPythonTestSources.PythonTestExtensionNanobind INTERFACE_SOURCES)
910+ generate_type_stubs(
911+ # This is the FQN path because dialect modules import _mlir when loaded. See above.
912+ MODULE_NAME mlir._mlir_libs._mlirPythonTestNanobind
913+ DEPENDS_TARGETS
914+ # You need both _mlir and _mlirPythonTestNanobind because dialect modules import _mlir when loaded
915+ # (so _mlir needs to be built before calling stubgen).
916+ MLIRPythonModules.extension._mlir.dso
917+ MLIRPythonModules.extension._mlirPythonTestNanobind.dso
918+ # You need this one so that ir.py "built" because mlir._mlir_libs.__init__.py import mlir.ir in _site_initialize.
919+ MLIRPythonModules.sources .MLIRPythonSources.Core.Python
920+ OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs/_mlir_libs"
921+ OUTPUTS _mlirPythonTestNanobind.pyi
922+ DEPENDS_TARGET_SRC_DEPS "${_test_extension_srcs} "
923+ IMPORT_PATHS "${MLIRPythonModules_ROOT_PREFIX} /.."
924+ )
925+ add_dependencies (MLIRPythonModules "${NB_STUBGEN_CUSTOM_TARGET} " )
926+ declare_mlir_python_sources(
927+ MLIRPythonTestSources.PythonTestExtensionNanobind.type_stub_gen
928+ ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs"
929+ ADD_TO_PARENT MLIRPythonTestSources.Dialects
930+ SOURCES _mlir_libs/_mlirPythonTestNanobind.pyi
931+ )
932+ endif ()
0 commit comments