@@ -109,13 +109,14 @@ endfunction()
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
+ # DEPENDS_TARGET_SRC_DEPS: List of cpp sources for extension library (for generating a DEPFILE).
112
113
# Outputs:
113
114
# NB_STUBGEN_CUSTOM_TARGET: The target corresponding to generation which other targets can depend on.
114
115
function (generate_type_stubs )
115
116
cmake_parse_arguments (ARG
116
117
""
117
118
"FQ_MODULE_NAME;DEPENDS_TARGET;CORE_MLIR_DEPENDS_TARGET;OUTPUT_DIR"
118
- "OUTPUTS"
119
+ "OUTPUTS;DEPENDS_TARGET_SRC_DEPS "
119
120
${ARGN} )
120
121
# for people doing find_package(nanobind)
121
122
if (EXISTS ${nanobind_DIR} /../src/stubgen.py)
@@ -148,6 +149,13 @@ function(generate_type_stubs )
148
149
--quiet )
149
150
150
151
list (TRANSFORM ARG_OUTPUTS PREPEND "${ARG_OUTPUT_DIR} /" OUTPUT_VARIABLE _generated_type_stubs)
152
+ set (_depfile "${ARG_OUTPUT_DIR} /${ARG_FQ_MODULE_NAME} .d" )
153
+ if ((NOT EXISTS ${_depfile} ) AND ARG_DEPENDS_TARGET_SRC_DEPS)
154
+ list (JOIN ARG_DEPENDS_TARGET_SRC_DEPS " " _depfiles)
155
+ list (TRANSFORM _generated_type_stubs APPEND ": ${_depfiles} " OUTPUT_VARIABLE _depfiles)
156
+ list (JOIN _depfiles "\n " _depfiles)
157
+ file (GENERATE OUTPUT "${_depfile} " CONTENT "${_depfiles} " )
158
+ endif ()
151
159
add_custom_command (
152
160
OUTPUT ${_generated_type_stubs}
153
161
COMMAND ${_nb_stubgen_cmd}
@@ -156,6 +164,7 @@ function(generate_type_stubs )
156
164
"${ARG_CORE_MLIR_DEPENDS_TARGET} .extension._mlir.dso"
157
165
"${ARG_CORE_MLIR_DEPENDS_TARGET} .sources.MLIRPythonSources.Core.Python"
158
166
"${ARG_DEPENDS_TARGET} "
167
+ DEPFILE "${_depfile} "
159
168
)
160
169
set (_name "${ARG_FQ_MODULE_NAME} .type_stubs" )
161
170
add_custom_target ("${_name} " DEPENDS ${_generated_type_stubs} )
@@ -332,26 +341,38 @@ function(add_mlir_python_modules name)
332
341
)
333
342
add_dependencies (${modules_target} ${_extension_target} )
334
343
mlir_python_setup_extension_rpath(${_extension_target} )
344
+ # NOTE: `sources_target` (naturally) lists all the sources (it's the INTERFACE
345
+ # target defined above in declare_mlir_python_extension). It's also the name of the
346
+ # target that gets exported (i.e., is populated as an INTERFACE IMPORTED library in MLIRTargets.cmake).
347
+ # This is why all metadata is queried from `sources_target`. On the other hand
348
+ # `_extension_target` is the actual dylib target that's built just above with `add_mlir_python_extension`.
349
+ # That's why dependencies are in terms of `_extension_target`.
335
350
get_target_property (_generate_type_stubs ${sources_target} mlir_python_GENERATE_TYPE_STUBS)
336
351
if (ARG_GENERATE_TYPE_STUBS AND _generate_type_stubs)
337
352
if ((NOT ARG_PACKAGE_PREFIX) OR ("${ARG_PACKAGE_PREFIX} " STREQUAL "" ))
338
353
message (FATAL_ERROR "GENERATE_TYPE_STUBS requires PACKAGE_PREFIX for ${name} " )
339
354
endif ()
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.
355
+ # TL;DR: all paths here are load bearing and annoyingly coupled. Changing paths here
356
+ # (or related code in declare_mlir_python_extension) will break either in-tree or out-of-tree generation.
342
357
#
343
358
# We remove _mlir_libs here because OUTPUT_DIR already includes it.
344
359
# Specifically OUTPUT_DIR already includes it because that's the actual directory
345
360
# where we want stubgen to dump the emitted sources. This is load bearing because up above
346
361
# (in declare_mlir_python_extension) we prefixed all the paths with _mlir_libs and
347
362
# we specified declare_mlir_python_sources with ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}/type_stubs".
363
+ #
364
+ # NOTE: INTERFACE_SOURCES is a genex in the build dir ($<BUILD_INTERFACE> and $<INSTALL_INTERFACE>)
365
+ # which will be evaluated by file(GENERATE ...). In the install dir it's a conventional path
366
+ # (see install/lib/cmake/mlir/MLIRTargets.cmake).
367
+ get_target_property (_extension_srcs ${sources_target} INTERFACE_SOURCES)
348
368
list (TRANSFORM _generate_type_stubs REPLACE "_mlir_libs/" "" )
349
369
generate_type_stubs(
350
370
FQ_MODULE_NAME "${ARG_PACKAGE_PREFIX} ._mlir_libs.${_module_name} "
351
371
DEPENDS_TARGET ${_extension_target}
352
372
CORE_MLIR_DEPENDS_TARGET ${name}
353
373
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR} /type_stubs/_mlir_libs"
354
374
OUTPUTS "${_generate_type_stubs} "
375
+ DEPENDS_TARGET_SRC_DEPS "${_extension_srcs} "
355
376
)
356
377
add_dependencies ("${modules_target} " "${NB_STUBGEN_CUSTOM_TARGET} " )
357
378
endif ()
0 commit comments