@@ -105,16 +105,16 @@ endfunction()
105
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
- # MLIR_DEPENDS_TARGET : The dso target corresponding to the main/core extension module
108
+ # CORE_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: List of expected outputs.
112
112
# Outputs:
113
113
# NB_STUBGEN_CUSTOM_TARGET: The target corresponding to generation which other targets can depend on.
114
- function (generate_type_stubs FQ_MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTPUT_DIR )
114
+ function (generate_type_stubs )
115
115
cmake_parse_arguments (ARG
116
116
""
117
- ""
117
+ "FQ_MODULE_NAME;DEPENDS_TARGET;CORE_MLIR_DEPENDS_TARGET;OUTPUT_DIR "
118
118
"OUTPUTS"
119
119
${ARGN} )
120
120
# for people doing find_package(nanobind)
@@ -134,30 +134,30 @@ function(generate_type_stubs FQ_MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET O
134
134
135
135
file (REAL_PATH "${MLIR_BINARY_DIR} /${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX} /.." _import_path)
136
136
137
- set (NB_STUBGEN_CMD
137
+ set (_nb_stubgen_cmd
138
138
"${Python_EXECUTABLE} "
139
139
"${NB_STUBGEN} "
140
140
--module
141
- "${FQ_MODULE_NAME } "
141
+ "${ARG_FQ_MODULE_NAME } "
142
142
-i
143
143
"${_import_path} "
144
144
--recursive
145
145
--include -private
146
146
--output -dir
147
- "${OUTPUT_DIR } "
147
+ "${ARG_OUTPUT_DIR } "
148
148
--quiet )
149
149
150
- list (TRANSFORM ARG_OUTPUTS PREPEND "${OUTPUT_DIR } /" OUTPUT_VARIABLE _generated_type_stubs)
150
+ list (TRANSFORM ARG_OUTPUTS PREPEND "${ARG_OUTPUT_DIR } /" OUTPUT_VARIABLE _generated_type_stubs)
151
151
add_custom_command (
152
152
OUTPUT ${_generated_type_stubs}
153
- COMMAND ${NB_STUBGEN_CMD }
153
+ COMMAND ${_nb_stubgen_cmd }
154
154
WORKING_DIRECTORY "${CMAKE_CURRENT_FUNCTION_LIST_DIR} "
155
155
DEPENDS
156
- "${MLIR_DEPENDS_TARGET } .extension._mlir.dso"
157
- "${MLIR_DEPENDS_TARGET } .sources.MLIRPythonSources.Core.Python"
158
- "${DEPENDS_TARGET } "
156
+ "${ARG_CORE_MLIR_DEPENDS_TARGET } .extension._mlir.dso"
157
+ "${ARG_CORE_MLIR_DEPENDS_TARGET } .sources.MLIRPythonSources.Core.Python"
158
+ "${ARG_DEPENDS_TARGET } "
159
159
)
160
- set (_name "${FQ_MODULE_NAME } .type_stubs" )
160
+ set (_name "${ARG_FQ_MODULE_NAME } .type_stubs" )
161
161
add_custom_target ("${_name} " DEPENDS ${_generated_type_stubs} )
162
162
set (NB_STUBGEN_CUSTOM_TARGET "${_name} " PARENT_SCOPE)
163
163
endfunction ()
@@ -178,7 +178,10 @@ endfunction()
178
178
# on. These will be collected for all extensions and put into an
179
179
# aggregate dylib that is linked against.
180
180
# PYTHON_BINDINGS_LIBRARY: Either pybind11 or nanobind.
181
- # GENERATE_TYPE_STUBS: List of generated type stubs expected from stubgen relative to _mlir_libs.
181
+ # GENERATE_TYPE_STUBS: Either
182
+ # 1. OFF (default)
183
+ # 2. ON if ${MODULE_NAME}.pyi is the only stub
184
+ # 3. A list of generated type stubs expected from stubgen relative to _mlir_libs.
182
185
function (declare_mlir_python_extension name )
183
186
cmake_parse_arguments (ARG
184
187
""
@@ -197,7 +200,14 @@ function(declare_mlir_python_extension name)
197
200
198
201
add_library (${name} INTERFACE )
199
202
203
+ if (NOT ARG_GENERATE_TYPE_STUBS)
204
+ set (ARG_GENERATE_TYPE_STUBS OFF )
205
+ endif ()
206
+ if ("${ARG_GENERATE_TYPE_STUBS} " STREQUAL "ON" )
207
+ set (ARG_GENERATE_TYPE_STUBS "${ARG_MODULE_NAME} .pyi" )
208
+ endif ()
200
209
if (ARG_GENERATE_TYPE_STUBS)
210
+ list (TRANSFORM ARG_GENERATE_TYPE_STUBS PREPEND "_mlir_libs/" )
201
211
declare_mlir_python_sources(
202
212
"${name} .type_stub_gen"
203
213
ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs"
@@ -282,9 +292,9 @@ endfunction()
282
292
# COMMON_CAPI_LINK_LIBS: List of dylibs (typically one) to make every
283
293
# extension depend on (see mlir_python_add_common_capi_library).
284
294
# GENERATE_TYPE_STUBS: Enable type stub generation for all modules
285
- # which have enabled type stub generation.
286
- # PACKAGE_PREFIX: Same as MLIR_PYTHON_PACKAGE_PREFIX. This is used
287
- # to determine type stub generation python module names.
295
+ # which have declared type stub generation with `declare_mlir_python_extension` .
296
+ # PACKAGE_PREFIX: Same as MLIR_PYTHON_PACKAGE_PREFIX not including trailing `.`.
297
+ # This is used to determine type stub generation python module names.
288
298
function (add_mlir_python_modules name )
289
299
cmake_parse_arguments (ARG
290
300
"GENERATE_TYPE_STUBS"
@@ -325,15 +335,22 @@ function(add_mlir_python_modules name)
325
335
get_target_property (_generate_type_stubs ${sources_target} mlir_python_GENERATE_TYPE_STUBS)
326
336
if (ARG_GENERATE_TYPE_STUBS AND _generate_type_stubs)
327
337
if ((NOT ARG_PACKAGE_PREFIX) OR ("${ARG_PACKAGE_PREFIX} " STREQUAL "" ))
328
- message (FATAL_ERROR "GENERATE_TYPE_STUBS requires PACKAGE_PREFIX" )
338
+ message (FATAL_ERROR "GENERATE_TYPE_STUBS requires PACKAGE_PREFIX for ${name} " )
329
339
endif ()
330
- set (_fully_qualified_module_name "${ARG_PACKAGE_PREFIX} ._mlir_libs.${_module_name} " )
340
+ # TL;DR: everything here is load bearing and annoyingly coupled. Changing anything here
341
+ # (or in declare_mlir_python_extension) will break either in-tree or out-of-tree generation.
342
+ #
343
+ # We remove _mlir_libs here because OUTPUT_DIR already includes it.
344
+ # Specifically OUTPUT_DIR already includes it because that's the actual directory
345
+ # where we want stubgen to dump the emitted sources. This is load bearing because up above
346
+ # (in declare_mlir_python_extension) we prefixed all the paths with _mlir_libs and
347
+ # we specified declare_mlir_python_sources with ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}/type_stubs".
331
348
list (TRANSFORM _generate_type_stubs REPLACE "_mlir_libs/" "" )
332
349
generate_type_stubs(
333
- ${_fully_qualified_module_name}
334
- ${_extension_target}
335
- ${name}
336
- "${CMAKE_CURRENT_BINARY_DIR} /type_stubs/_mlir_libs"
350
+ FQ_MODULE_NAME " ${ARG_PACKAGE_PREFIX} ._mlir_libs. ${_module_name} "
351
+ DEPENDS_TARGET ${_extension_target}
352
+ CORE_MLIR_DEPENDS_TARGET ${name}
353
+ OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs/_mlir_libs"
337
354
OUTPUTS "${_generate_type_stubs} "
338
355
)
339
356
add_dependencies ("${modules_target} " "${NB_STUBGEN_CUSTOM_TARGET} " )
0 commit comments