@@ -102,15 +102,15 @@ endfunction()
102
102
# Function: generate_type_stubs
103
103
# Turns on automatic type stub generation (via nanobind's stubgen) for extension modules.
104
104
# 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) .
106
106
# DEPENDS_TARGET: The dso target corresponding to the extension module
107
107
# (e.g., something like StandalonePythonModules.extension._standaloneDialectsNanobind.dso)
108
108
# MLIR_DEPENDS_TARGET: The dso target corresponding to the main/core extension module
109
109
# (e.g., something like StandalonePythonModules.extension._mlir.dso)
110
110
# OUTPUT_DIR: The root output directory to emit the type stubs into.
111
111
# Outputs:
112
112
# 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)
114
114
cmake_parse_arguments (ARG
115
115
""
116
116
""
@@ -131,14 +131,13 @@ function(generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTP
131
131
endif ()
132
132
file (REAL_PATH "${NB_STUBGEN} " NB_STUBGEN)
133
133
134
- set (_module "${MLIR_PYTHON_PACKAGE_PREFIX} ._mlir_libs.${MODULE_NAME} " )
135
134
file (REAL_PATH "${MLIR_BINARY_DIR} /${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX} /.." _import_path)
136
135
137
136
set (NB_STUBGEN_CMD
138
137
"${Python_EXECUTABLE} "
139
138
"${NB_STUBGEN} "
140
139
--module
141
- "${_module } "
140
+ "${FQ_MODULE_NAME } "
142
141
-i
143
142
"${_import_path} "
144
143
--recursive
@@ -156,7 +155,7 @@ function(generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTP
156
155
"${MLIR_DEPENDS_TARGET} .sources.MLIRPythonSources.Core.Python"
157
156
"${DEPENDS_TARGET} "
158
157
)
159
- set (_name "MLIRPythonModuleStubs_ ${_module} " )
158
+ set (_name "${FQ_MODULE_NAME} .type_stubs " )
160
159
add_custom_target ("${_name} " ALL DEPENDS ${_generated_type_stubs} )
161
160
set (NB_STUBGEN_CUSTOM_TARGET "${_name} " PARENT_SCOPE)
162
161
endfunction ()
@@ -270,10 +269,14 @@ endfunction()
270
269
# DAG of source modules is included.
271
270
# COMMON_CAPI_LINK_LIBS: List of dylibs (typically one) to make every
272
271
# 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.
273
276
function (add_mlir_python_modules name )
274
277
cmake_parse_arguments (ARG
275
- ""
276
- "ROOT_PREFIX;INSTALL_PREFIX"
278
+ "GENERATE_TYPE_STUBS "
279
+ "ROOT_PREFIX;INSTALL_PREFIX;PACKAGE_PREFIX "
277
280
"COMMON_CAPI_LINK_LIBS;DECLARED_SOURCES"
278
281
${ARGN} )
279
282
# Helper to process an individual target.
@@ -308,23 +311,27 @@ function(add_mlir_python_modules name)
308
311
add_dependencies (${modules_target} ${_extension_target} )
309
312
mlir_python_setup_extension_rpath(${_extension_target} )
310
313
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} " )
312
319
generate_type_stubs(
313
- ${_module_name }
320
+ ${_fully_qualified_module_name }
314
321
${_extension_target}
315
322
${name}
316
323
"${CMAKE_CURRENT_SOURCE_DIR} /mlir/_mlir_libs"
317
324
OUTPUTS "${_generate_type_stubs} "
318
325
)
319
326
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 " )
321
328
declare_mlir_python_sources(
322
329
${_stubgen_target}
323
330
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR} /mlir/_mlir_libs"
324
331
ADD_TO_PARENT "${sources_target} "
325
332
SOURCES "${_generate_type_stubs} "
326
333
)
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 " )
328
335
add_mlir_python_sources_target(${_pure_sources_target}
329
336
INSTALL_COMPONENT ${modules_target}
330
337
INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
0 commit comments