@@ -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
@@ -157,8 +156,8 @@ function(generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTP
157156 "${MLIR_DEPENDS_TARGET} .sources.MLIRPythonSources.Core.Python"
158157 "${DEPENDS_TARGET} "
159158 )
160- set (_name "MLIRPythonModuleStubs_ ${_module} " )
161- add_custom_target ("${_name} " ALL DEPENDS ${_generated_type_stubs} )
159+ set (_name "${FQ_MODULE_NAME} .type_stubs " )
160+ add_custom_target ("${_name} " DEPENDS ${_generated_type_stubs} )
162161 set (NB_STUBGEN_CUSTOM_TARGET "${_name} " PARENT_SCOPE)
163162endfunction ()
164163
@@ -271,10 +270,14 @@ endfunction()
271270# DAG of source modules is included.
272271# COMMON_CAPI_LINK_LIBS: List of dylibs (typically one) to make every
273272# extension depend on (see mlir_python_add_common_capi_library).
273+ # GENERATE_TYPE_STUBS: Enable type stub generation for all modules
274+ # which have enabled type stub generation.
275+ # PACKAGE_PREFIX: Same as MLIR_PYTHON_PACKAGE_PREFIX. This is used
276+ # to determine type stub generation python module names.
274277function (add_mlir_python_modules name )
275278 cmake_parse_arguments (ARG
276- ""
277- "ROOT_PREFIX;INSTALL_PREFIX"
279+ "GENERATE_TYPE_STUBS "
280+ "ROOT_PREFIX;INSTALL_PREFIX;PACKAGE_PREFIX "
278281 "COMMON_CAPI_LINK_LIBS;DECLARED_SOURCES"
279282 ${ARGN} )
280283 # Helper to process an individual target.
@@ -309,23 +312,27 @@ function(add_mlir_python_modules name)
309312 add_dependencies (${modules_target} ${_extension_target} )
310313 mlir_python_setup_extension_rpath(${_extension_target} )
311314 get_target_property (_generate_type_stubs ${sources_target} mlir_python_GENERATE_TYPE_STUBS)
312- if (_generate_type_stubs)
315+ if (ARG_GENERATE_TYPE_STUBS AND _generate_type_stubs)
316+ if ((NOT ARG_PACKAGE_PREFIX) OR ("${ARG_PACKAGE_PREFIX} " STREQUAL "" ))
317+ message (FATAL_ERROR "GENERATE_TYPE_STUBS requires PACKAGE_PREFIX" )
318+ endif ()
319+ set (_fully_qualified_module_name "${ARG_PACKAGE_PREFIX} ._mlir_libs.${_module_name} " )
313320 generate_type_stubs(
314- ${_module_name }
321+ ${_fully_qualified_module_name }
315322 ${_extension_target}
316323 ${name}
317324 "${CMAKE_CURRENT_BINARY_DIR} /_mlir_libs"
318325 OUTPUTS "${_generate_type_stubs} "
319326 )
320327 add_dependencies ("${modules_target} " "${NB_STUBGEN_CUSTOM_TARGET} " )
321- set (_stubgen_target "${MLIR_PYTHON_PACKAGE_PREFIX} . ${_module_name} _type_stub_gen " )
328+ set (_stubgen_target "${_fully_qualified_module_name} .type_stub_gen " )
322329 declare_mlir_python_sources(
323330 ${_stubgen_target}
324331 ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR} /_mlir_libs"
325332 ADD_TO_PARENT "${sources_target} "
326333 SOURCES "${_generate_type_stubs} "
327334 )
328- set (_pure_sources_target "${modules_target} .sources.${sources_target} _type_stub_gen " )
335+ set (_pure_sources_target "${modules_target} .sources.${sources_target} .type_stub_gen " )
329336 add_mlir_python_sources_target(${_pure_sources_target}
330337 INSTALL_COMPONENT ${modules_target}
331338 INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
0 commit comments