@@ -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
@@ -157,8 +156,8 @@ function(generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTP
157
156
"${MLIR_DEPENDS_TARGET} .sources.MLIRPythonSources.Core.Python"
158
157
"${DEPENDS_TARGET} "
159
158
)
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} )
162
161
set (NB_STUBGEN_CUSTOM_TARGET "${_name} " PARENT_SCOPE)
163
162
endfunction ()
164
163
@@ -271,10 +270,14 @@ endfunction()
271
270
# DAG of source modules is included.
272
271
# COMMON_CAPI_LINK_LIBS: List of dylibs (typically one) to make every
273
272
# 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.
274
277
function (add_mlir_python_modules name )
275
278
cmake_parse_arguments (ARG
276
- ""
277
- "ROOT_PREFIX;INSTALL_PREFIX"
279
+ "GENERATE_TYPE_STUBS "
280
+ "ROOT_PREFIX;INSTALL_PREFIX;PACKAGE_PREFIX "
278
281
"COMMON_CAPI_LINK_LIBS;DECLARED_SOURCES"
279
282
${ARGN} )
280
283
# Helper to process an individual target.
@@ -309,23 +312,27 @@ function(add_mlir_python_modules name)
309
312
add_dependencies (${modules_target} ${_extension_target} )
310
313
mlir_python_setup_extension_rpath(${_extension_target} )
311
314
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} " )
313
320
generate_type_stubs(
314
- ${_module_name }
321
+ ${_fully_qualified_module_name }
315
322
${_extension_target}
316
323
${name}
317
324
"${CMAKE_CURRENT_BINARY_DIR} /_mlir_libs"
318
325
OUTPUTS "${_generate_type_stubs} "
319
326
)
320
327
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 " )
322
329
declare_mlir_python_sources(
323
330
${_stubgen_target}
324
331
ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR} /_mlir_libs"
325
332
ADD_TO_PARENT "${sources_target} "
326
333
SOURCES "${_generate_type_stubs} "
327
334
)
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 " )
329
336
add_mlir_python_sources_target(${_pure_sources_target}
330
337
INSTALL_COMPONENT ${modules_target}
331
338
INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
0 commit comments