Skip to content

Commit 1846523

Browse files
authored
[mlir] Make MLIR Python sources idempotent (#97167)
Make MLIR Python sources idempotent by moving the custom commands off the custom target for each source. Previously, a custom target was created for each MLIR Python source file and a custom command added to symlink it to a destination path. However this causes the build to not be idempotent because custom targets always run. Instead, this PR separates the symlink/copy to destination paths into custom commands and makes the custom target depend on the output. That prevents re-running them and restores idempotency. Testing: - Build with `-D MLIR_ENABLE_BINDINGS_PYTHON=ON`. Prior to this change, the build is not idempotent (building twice re-runs the symlink/copy command for Python source targets). After this change it is idempotent.
1 parent 3b639d7 commit 1846523

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,6 @@ function(add_mlir_python_sources_target name)
565565
message(FATAL_ERROR "Unhandled arguments to add_mlir_python_sources_target(${name}, ... : ${ARG_UNPARSED_ARGUMENTS}")
566566
endif()
567567

568-
add_custom_target(${name})
569-
570568
# On Windows create_symlink requires special permissions. Use copy_if_different instead.
571569
if(CMAKE_HOST_WIN32)
572570
set(_link_or_copy copy_if_different)
@@ -575,8 +573,6 @@ function(add_mlir_python_sources_target name)
575573
endif()
576574

577575
foreach(_sources_target ${ARG_SOURCES_TARGETS})
578-
add_dependencies(${name} ${_sources_target})
579-
580576
get_target_property(_src_paths ${_sources_target} SOURCES)
581577
if(NOT _src_paths)
582578
get_target_property(_src_paths ${_sources_target} INTERFACE_SOURCES)
@@ -590,6 +586,8 @@ function(add_mlir_python_sources_target name)
590586
get_target_property(_root_dir ${_sources_target} INTERFACE_INCLUDE_DIRECTORIES)
591587
endif()
592588

589+
# Initialize an empty list of all Python source destination paths.
590+
set(all_dest_paths "")
593591
foreach(_src_path ${_src_paths})
594592
file(RELATIVE_PATH _source_relative_path "${_root_dir}" "${_src_path}")
595593
set(_dest_path "${ARG_OUTPUT_DIRECTORY}/${_source_relative_path}")
@@ -598,13 +596,17 @@ function(add_mlir_python_sources_target name)
598596
file(MAKE_DIRECTORY "${_dest_dir}")
599597

600598
add_custom_command(
601-
TARGET ${name} PRE_BUILD
599+
OUTPUT "${_dest_path}"
600+
PRE_BUILD
602601
COMMENT "Copying python source ${_src_path} -> ${_dest_path}"
603602
DEPENDS "${_src_path}"
604-
BYPRODUCTS "${_dest_path}"
605603
COMMAND "${CMAKE_COMMAND}" -E ${_link_or_copy}
606604
"${_src_path}" "${_dest_path}"
607605
)
606+
607+
# Track the symlink or copy command output.
608+
list(APPEND all_dest_paths "${_dest_path}")
609+
608610
if(ARG_INSTALL_DIR)
609611
# We have to install each file individually because we need to preserve
610612
# the relative directory structure in the install destination.
@@ -621,6 +623,9 @@ function(add_mlir_python_sources_target name)
621623
endif()
622624
endforeach()
623625
endforeach()
626+
627+
# Create a new custom target that depends on all symlinked or copied sources.
628+
add_custom_target("${name}" DEPENDS ${all_dest_paths})
624629
endfunction()
625630

626631
################################################################################

0 commit comments

Comments
 (0)