Make miss dependencies when add_custom_command depends on add_custom_target or add_custom_command
Hi,
When in a subdirectory's CMakeLists.txt
, I have:
add_custom_command(
OUTPUT ${TEST_OUTPUT}
COMMAND ${CMAKE_COMMAND} -E copy ${TEST_INPUT} ${TEST_OUTPUT}
DEPENDS ${TEST_INPUT}
)
add_custom_target(gen_test_file DEPENDS ${TEST_OUTPUT})
And in parent CMakeLists.txt
:
add_custom_command(
OUTPUT test_file2.txt
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_PROPERTY:gen_test_file,GENERATED_FILE> test_file2.txt
DEPENDS gen_test_file
)
The gen_test_file dependency is not honored by make and NMake when ${TEST_OUTPUT}
is changed.
If I touch the ${TEST_INPUT}
file, gen_test_file
is rebuilt but not test_file2.txt
:
[ 25%] Generating test_file.txt
[ 25%] Built target gen_test_file
[ 50%] Built target gen_test_file2
[100%] Built target exe_file
I'm expecting that output instead:
[ 25%] Generating test_file.txt
[ 25%] Built target gen_test_file
[ 50%] Generating test_file2.txt
[ 50%] Built target gen_test_file2
[100%] Built target exe_file
I'm attaching add_custom_target_dependencies.zip which contains an example. I'm executing these commands in the unzip directory containing the parent CMakeLists.txt:
mkdir build
cd build
cmake ..
# For the first build, all files are generated, that's OK everything works as expeced
cmake --build .
# Here I will touch the ${TEST_INPUT} file
cmake --build . --target touch_test_input
# Here, only test_file.txt is regenerated, I expect that test_file2.txt to be regenerated too
cmake --build .
If I try to change the dependency of the add_custom_command
generating test_file2.txt
to $<TARGET_PROPERTY:gen_test_file,GENERATED_FILE>
and recreate the build dir from scratch, then make fail to find how to build 'subdir\test_file.txt'
.
If I change it to gen_test_file $<TARGET_PROPERTY:gen_test_file,GENERATED_FILE>
, the build works fine and test_file2.txt
is regenerated when test_file.txt
changes as expected. But these dependencies seems duplicated to me.
In fact, I'm trying to understand the intended behavior around add_custom_target. Maybe I'm not using it in the right way, but I expect its behavior to be the same as other target types. I'm trying to wrap custom commands with dependencies between each other.
The behavior is the same between cmake 3.16.3 and 3.17.0-rc3 regarding this.
If needed, I can work on patches, but I need to ensure what's expected before.