Skip to content

Commit 8da6462

Browse files
committed
add DEPFILEs
1 parent 2483348 commit 8da6462

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@ endfunction()
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.
112+
# DEPENDS_TARGET_SRC_DEPS: List of cpp sources for extension library (for generating a DEPFILE).
112113
# Outputs:
113114
# NB_STUBGEN_CUSTOM_TARGET: The target corresponding to generation which other targets can depend on.
114115
function(generate_type_stubs )
115116
cmake_parse_arguments(ARG
116117
""
117118
"FQ_MODULE_NAME;DEPENDS_TARGET;CORE_MLIR_DEPENDS_TARGET;OUTPUT_DIR"
118-
"OUTPUTS"
119+
"OUTPUTS;DEPENDS_TARGET_SRC_DEPS"
119120
${ARGN})
120121
# for people doing find_package(nanobind)
121122
if(EXISTS ${nanobind_DIR}/../src/stubgen.py)
@@ -148,6 +149,13 @@ function(generate_type_stubs )
148149
--quiet)
149150

150151
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()
151159
add_custom_command(
152160
OUTPUT ${_generated_type_stubs}
153161
COMMAND ${_nb_stubgen_cmd}
@@ -156,6 +164,7 @@ function(generate_type_stubs )
156164
"${ARG_CORE_MLIR_DEPENDS_TARGET}.extension._mlir.dso"
157165
"${ARG_CORE_MLIR_DEPENDS_TARGET}.sources.MLIRPythonSources.Core.Python"
158166
"${ARG_DEPENDS_TARGET}"
167+
DEPFILE "${_depfile}"
159168
)
160169
set(_name "${ARG_FQ_MODULE_NAME}.type_stubs")
161170
add_custom_target("${_name}" DEPENDS ${_generated_type_stubs})
@@ -332,26 +341,38 @@ function(add_mlir_python_modules name)
332341
)
333342
add_dependencies(${modules_target} ${_extension_target})
334343
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`.
335350
get_target_property(_generate_type_stubs ${sources_target} mlir_python_GENERATE_TYPE_STUBS)
336351
if(ARG_GENERATE_TYPE_STUBS AND _generate_type_stubs)
337352
if ((NOT ARG_PACKAGE_PREFIX) OR ("${ARG_PACKAGE_PREFIX}" STREQUAL ""))
338353
message(FATAL_ERROR "GENERATE_TYPE_STUBS requires PACKAGE_PREFIX for ${name}")
339354
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.
342357
#
343358
# We remove _mlir_libs here because OUTPUT_DIR already includes it.
344359
# Specifically OUTPUT_DIR already includes it because that's the actual directory
345360
# where we want stubgen to dump the emitted sources. This is load bearing because up above
346361
# (in declare_mlir_python_extension) we prefixed all the paths with _mlir_libs and
347362
# 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)
348368
list(TRANSFORM _generate_type_stubs REPLACE "_mlir_libs/" "")
349369
generate_type_stubs(
350370
FQ_MODULE_NAME "${ARG_PACKAGE_PREFIX}._mlir_libs.${_module_name}"
351371
DEPENDS_TARGET ${_extension_target}
352372
CORE_MLIR_DEPENDS_TARGET ${name}
353373
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/type_stubs/_mlir_libs"
354374
OUTPUTS "${_generate_type_stubs}"
375+
DEPENDS_TARGET_SRC_DEPS "${_extension_srcs}"
355376
)
356377
add_dependencies("${modules_target}" "${NB_STUBGEN_CUSTOM_TARGET}")
357378
endif()

mlir/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ if(MLIR_ENABLE_EXECUTION_ENGINE)
672672
MODULE_NAME _mlirExecutionEngine
673673
ADD_TO_PARENT MLIRPythonSources.ExecutionEngine
674674
ROOT_DIR "${PYTHON_SOURCE_DIR}"
675-
PYTHON_BINDINGS_LIBRARY nanobind
675+
PYTHON_BINDINGS_LIBRARY nanobind
676676
SOURCES
677677
ExecutionEngineModule.cpp
678678
PRIVATE_LINK_LIBS

0 commit comments

Comments
 (0)