@@ -105,16 +105,16 @@ endfunction()
105105# 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)
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
109109# (e.g., something like StandalonePythonModules.extension._mlir.dso)
110110# OUTPUT_DIR: The root output directory to emit the type stubs into.
111111# OUTPUTS: List of expected outputs.
112112# Outputs:
113113# 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 )
115115 cmake_parse_arguments (ARG
116116 ""
117- ""
117+ "FQ_MODULE_NAME;DEPENDS_TARGET;CORE_MLIR_DEPENDS_TARGET;OUTPUT_DIR "
118118 "OUTPUTS"
119119 ${ARGN} )
120120 # for people doing find_package(nanobind)
@@ -134,30 +134,30 @@ function(generate_type_stubs FQ_MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET O
134134
135135 file (REAL_PATH "${MLIR_BINARY_DIR} /${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX} /.." _import_path)
136136
137- set (NB_STUBGEN_CMD
137+ set (_nb_stubgen_cmd
138138 "${Python_EXECUTABLE} "
139139 "${NB_STUBGEN} "
140140 --module
141- "${FQ_MODULE_NAME } "
141+ "${ARG_FQ_MODULE_NAME } "
142142 -i
143143 "${_import_path} "
144144 --recursive
145145 --include -private
146146 --output -dir
147- "${OUTPUT_DIR } "
147+ "${ARG_OUTPUT_DIR } "
148148 --quiet )
149149
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)
151151 add_custom_command (
152152 OUTPUT ${_generated_type_stubs}
153- COMMAND ${NB_STUBGEN_CMD }
153+ COMMAND ${_nb_stubgen_cmd }
154154 WORKING_DIRECTORY "${CMAKE_CURRENT_FUNCTION_LIST_DIR} "
155155 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 } "
159159 )
160- set (_name "${FQ_MODULE_NAME } .type_stubs" )
160+ set (_name "${ARG_FQ_MODULE_NAME } .type_stubs" )
161161 add_custom_target ("${_name} " DEPENDS ${_generated_type_stubs} )
162162 set (NB_STUBGEN_CUSTOM_TARGET "${_name} " PARENT_SCOPE)
163163endfunction ()
@@ -178,7 +178,10 @@ endfunction()
178178# on. These will be collected for all extensions and put into an
179179# aggregate dylib that is linked against.
180180# 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.
182185function (declare_mlir_python_extension name )
183186 cmake_parse_arguments (ARG
184187 ""
@@ -197,7 +200,14 @@ function(declare_mlir_python_extension name)
197200
198201 add_library (${name} INTERFACE )
199202
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 ()
200209 if (ARG_GENERATE_TYPE_STUBS)
210+ list (TRANSFORM ARG_GENERATE_TYPE_STUBS PREPEND "_mlir_libs/" )
201211 declare_mlir_python_sources(
202212 "${name} .type_stub_gen"
203213 ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs"
@@ -283,8 +293,8 @@ endfunction()
283293# extension depend on (see mlir_python_add_common_capi_library).
284294# GENERATE_TYPE_STUBS: Enable type stub generation for all modules
285295# 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.
296+ # PACKAGE_PREFIX: Same as MLIR_PYTHON_PACKAGE_PREFIX not including trailing `.`.
297+ # This is used to determine type stub generation python module names.
288298function (add_mlir_python_modules name )
289299 cmake_parse_arguments (ARG
290300 "GENERATE_TYPE_STUBS"
@@ -325,15 +335,22 @@ function(add_mlir_python_modules name)
325335 get_target_property (_generate_type_stubs ${sources_target} mlir_python_GENERATE_TYPE_STUBS)
326336 if (ARG_GENERATE_TYPE_STUBS AND _generate_type_stubs)
327337 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} " )
329339 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".
331348 list (TRANSFORM _generate_type_stubs REPLACE "_mlir_libs/" "" )
332349 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"
337354 OUTPUTS "${_generate_type_stubs} "
338355 )
339356 add_dependencies ("${modules_target} " "${NB_STUBGEN_CUSTOM_TARGET} " )
0 commit comments