@@ -102,15 +102,15 @@ endfunction()
102102# Function: generate_type_stubs
103103# Turns on automatic type stub generation (via nanobind's stubgen) for extension modules.
104104# Arguments:
105- # MODULE_NAME : The name of the extension module as specified in declare_mlir_python_extension .
105+ # FQ_MODULE_NAME : The fully-qualified name of the extension module (used for importing in python) .
106106# DEPENDS_TARGET: The dso target corresponding to the extension module
107107# (e.g., something like StandalonePythonModules.extension._standaloneDialectsNanobind.dso)
108108# MLIR_DEPENDS_TARGET: The dso target corresponding to the main/core extension module
109109# (e.g., something like StandalonePythonModules.extension._mlir.dso)
110110# OUTPUT_DIR: The root output directory to emit the type stubs into.
111111# Outputs:
112112# NB_STUBGEN_CUSTOM_TARGET: The target corresponding to generation which other targets can depend on.
113- function (generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTPUT_DIR)
113+ function (generate_type_stubs FQ_MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTPUT_DIR)
114114 cmake_parse_arguments (ARG
115115 ""
116116 ""
@@ -131,14 +131,13 @@ function(generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTP
131131 endif ()
132132 file (REAL_PATH "${NB_STUBGEN} " NB_STUBGEN)
133133
134- set (_module "${MLIR_PYTHON_PACKAGE_PREFIX} ._mlir_libs.${MODULE_NAME} " )
135134 file (REAL_PATH "${MLIR_BINARY_DIR} /${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX} /.." _import_path)
136135
137136 set (NB_STUBGEN_CMD
138137 "${Python_EXECUTABLE} "
139138 "${NB_STUBGEN} "
140139 --module
141- "${_module } "
140+ "${FQ_MODULE_NAME } "
142141 -i
143142 "${_import_path} "
144143 --recursive
@@ -156,7 +155,7 @@ function(generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTP
156155 "${MLIR_DEPENDS_TARGET} .sources.MLIRPythonSources.Core.Python"
157156 "${DEPENDS_TARGET} "
158157 )
159- set (_name "MLIRPythonModuleStubs_ ${_module} " )
158+ set (_name "${FQ_MODULE_NAME} .type_stubs " )
160159 add_custom_target ("${_name} " ALL DEPENDS ${_generated_type_stubs} )
161160 set (NB_STUBGEN_CUSTOM_TARGET "${_name} " PARENT_SCOPE)
162161endfunction ()
@@ -270,10 +269,14 @@ endfunction()
270269# DAG of source modules is included.
271270# COMMON_CAPI_LINK_LIBS: List of dylibs (typically one) to make every
272271# extension depend on (see mlir_python_add_common_capi_library).
272+ # GENERATE_TYPE_STUBS: Enable type stub generation for all modules
273+ # which have enabled type stub generation.
274+ # PACKAGE_PREFIX: Same as MLIR_PYTHON_PACKAGE_PREFIX. This is used
275+ # to determine type stub generation python module names.
273276function (add_mlir_python_modules name )
274277 cmake_parse_arguments (ARG
275- ""
276- "ROOT_PREFIX;INSTALL_PREFIX"
278+ "GENERATE_TYPE_STUBS "
279+ "ROOT_PREFIX;INSTALL_PREFIX;PACKAGE_PREFIX "
277280 "COMMON_CAPI_LINK_LIBS;DECLARED_SOURCES"
278281 ${ARGN} )
279282 # Helper to process an individual target.
@@ -308,23 +311,27 @@ function(add_mlir_python_modules name)
308311 add_dependencies (${modules_target} ${_extension_target} )
309312 mlir_python_setup_extension_rpath(${_extension_target} )
310313 get_target_property (_generate_type_stubs ${sources_target} mlir_python_GENERATE_TYPE_STUBS)
311- if (_generate_type_stubs)
314+ if (ARG_GENERATE_TYPE_STUBS AND _generate_type_stubs)
315+ if ((NOT ARG_PACKAGE_PREFIX) OR ("${ARG_PACKAGE_PREFIX} " STREQUAL "" ))
316+ message (FATAL_ERROR "GENERATE_TYPE_STUBS requires PACKAGE_PREFIX" )
317+ endif ()
318+ set (_fully_qualified_module_name "${ARG_PACKAGE_PREFIX} ._mlir_libs.${_module_name} " )
312319 generate_type_stubs(
313- ${_module_name }
320+ ${_fully_qualified_module_name }
314321 ${_extension_target}
315322 ${name}
316323 "${CMAKE_CURRENT_SOURCE_DIR} /mlir/_mlir_libs"
317324 OUTPUTS "${_generate_type_stubs} "
318325 )
319326 add_dependencies ("${modules_target} " "${NB_STUBGEN_CUSTOM_TARGET} " )
320- set (_stubgen_target "${MLIR_PYTHON_PACKAGE_PREFIX} . ${_module_name} _type_stub_gen " )
327+ set (_stubgen_target "${_fully_qualified_module_name} .type_stub_gen " )
321328 declare_mlir_python_sources(
322329 ${_stubgen_target}
323330 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR} /mlir/_mlir_libs"
324331 ADD_TO_PARENT "${sources_target} "
325332 SOURCES "${_generate_type_stubs} "
326333 )
327- set (_pure_sources_target "${modules_target} .sources.${sources_target} _type_stub_gen " )
334+ set (_pure_sources_target "${modules_target} .sources.${sources_target} .type_stub_gen " )
328335 add_mlir_python_sources_target(${_pure_sources_target}
329336 INSTALL_COMPONENT ${modules_target}
330337 INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
0 commit comments