custom_command ignore output with same name but no subfolder if set after.
I reach to a strange behavior of cmake when using multiple custom command with same file name and multiple subfolder. Depending on the order cmake miss the generation of the target in the build.make. I use cmake 3.16.3 but the issue is also there in 3.23.2 and 3.24.0-rc4.
add_custom_command(OUTPUT d/file.txt COMMAND mkdir -p d && echo "a" > d/file.txt)
add_custom_command(OUTPUT file.txt COMMAND echo "b" > file.txt)
add_custom_target(tgt DEPENDS d/file.txt file.txt)
give the following result
$ make tgt
Scanning dependencies of target tgt
[100%] Generating d/file.txt
make[3]: *** No rule to make target 'file.txt', needed by 'CMakeFiles/tgt'. Stop.
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/tgt.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/tgt.dir/rule] Error 2
make: *** [Makefile:118: tgt] Error 2
$ grep file.txt CMakeFiles/tgt.dir/build.make # missing target file.txt
CMakeFiles/tgt: d/file.txt
CMakeFiles/tgt: file.txt
d/file.txt:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold --progress-dir=/home/aadolphe/git/test/tmp/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Generating d/file.txt"
mkdir -p d && echo a > d/file.txt
tgt: d/file.txt
But the strange part is that it is working if the 'root' command is set before the subfolder one :
# inverted custom_command order
add_custom_command(OUTPUT file.txt COMMAND echo "b" > file.txt)
add_custom_command(OUTPUT d/file.txt COMMAND mkdir -p d && echo "a" > d/file.txt)
add_custom_target(tgt DEPENDS d/file.txt file.txt)
$ make tgt
Scanning dependencies of target tgt
[ 50%] Generating d/file.txt
[100%] Generating file.txt
[100%] Built target tgt
And for the completion of the test, it also works fine if both custom_command have different subfolder.