add_custom_command(DEPFILE): inconsistent dependencies for Ninja generator
When a custom command output is used as input source to add_library()
or add_executable()
, there is potentially some inconsistency on how dependencies are defined.
In the depfile associated to the custom command, output file can be specified as an absolute path. But, in the ninja file rules.ninja
, paths are expressed as relative to the build directory when possible.
As a result, successive launch of ninja
relaunch the execution of the custom command and all dependent actions.
Here a snippet showing the problem:
cmake_minimum_required(VERSION 3.1...3.20)
project(NinjaDepfile C)
file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/source.c" "#include \"source.h\"\n void f() {}\n")
file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/source.h" "\n")
file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/source.c.dep" "${CMAKE_CURRENT_BINARY_DIR}/source.c : ${CMAKE_CURRENT_BINARY_DIR}/source.h")
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/source.c"
COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_BINARY_DIR}/source.c"
DEPFILE "${CMAKE_BINARY_DIR}/source.c.dep")
add_library (ouput SHARED "${CMAKE_CURRENT_BINARY_DIR}/source.c")