VS 2019 16.1 rebuilds add_custom_command files every time
EDIT: After discussion below, this issue is caused by the VS 2019 16.1 update and is a regression in MSBuild's evaluation of custom command dependencies. VS 2019 16.1 contains a fix for rebuilding when the list of dependencies changes. However, its check does not work correctly when a dependency is listed more than once and causes it to re-build the custom command every time. CMake 3.14.4 and below list the source file to which a custom command is attached as an explicit dependency, but MSBuild considers it to be an implicit dependency too and triggers its duplicate dependency bug. CMake 3.14.5 contains a fix to the .vcxproj
generator to avoid such duplicate dependencies and work around the MSBuild bug.
Microsoft is aware of the problem and plans to fix MSBuild in a future VS 2019 16.* update.
The original description follows.
The following example will print "RUNNING COMMAND" every time the GenerateSomeFile target is built when a Visual Studio solution is generated with the VS 2019 generator. It is only printed the first time when the VS 2017 generator is used.
cmake version 3.14.4
project(CUSTOM_COMMAND_TEST)
add_custom_command(OUTPUT "some_file.g"
COMMAND ${CMAKE_COMMAND} -E touch "some_file.g"
COMMAND ${CMAKE_COMMAND} -E echo "RUNNING COMMAND")
add_custom_target(GenerateSomeFile
${CMAKE_COMMAND} -E echo "RUNNING TARGET"
DEPENDS "some_file.g")