custom_command deduplication in VS generator does not work if dependency chain has targets created in different directory
I ran into a problem similar to as described in #16767 (closed). !1889 (merged) should have fixed that issue, but in my case it doesn't work.
It seems that the deduplication doesn't work if the dependency chain contains targets created in an upper level directory. I used the test-case CMake\Tests\RunCMake\add_custom_command\AssigningMultipleTargets.cmake
as a basis:
Given top level CMakeLists.txt:
cmake_minimum_required(VERSION 3.25)
project(test)
enable_language(CXX)
add_custom_target(middle)
add_subdirectory(subdir)
Given subdir/CMakeLists.txt:
add_custom_command(OUTPUT generated.cpp DEPENDS a.c
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/generate-once.cmake ${CMAKE_CURRENT_BINARY_DIR}/generated.cpp VERBATIM
)
add_executable(exe1 ${CMAKE_CURRENT_BINARY_DIR}/generated.cpp)
#add_custom_target(middle)
add_executable(exe2 ${CMAKE_CURRENT_BINARY_DIR}/generated.cpp)
add_dependencies(exe1 middle)
add_dependencies(middle exe2)
If generated, the custom command will show up in both exe1 and exe2! So deduplication doesn't work.
However if you comment out add_custom_target(middle)
in the top level CMakeLists.txt and uncomment it in subdir/CMakeLists.txt, then it works as expected, ie. the custom command will only show up in exe2 (the "least-dependent" target).
The only difference between the two cases is where the "middle" target is created. If created in the upper dir, indirect dependency traversal seems to break and deduplication fails.
Is this a bug or a known limitation?
PS.: Additional files used above are here:
subdir/generate-once.cmake:
if (${CMAKE_ARGC} LESS 4)
message(FATAL_ERROR "Too few arguments")
endif()
set(output "${CMAKE_ARGV3}")
if(EXISTS ${output})
message(FATAL_ERROR "${output} already exists")
endif()
file(WRITE ${output} "int main() { return 0; }\n")
subdir/a.c:
void a() {}