Parity between Ninja and GNU Makefiles regarding POST_BUILD byproducts by adding them as prerequisites for the target
When a target has POST_BUILD actions, any file specified in the BYPRODUCTS only triggers a relink in Ninja but not on GNU Makefiles. Consider this code:
add_executable(foo a.cpp)
add_custom_command(TARGET foo
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} --only-keep-debug foo foo.sym
COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=foo.sym foo
COMMAND ${CMAKE_STRIP} --strip-debug foo
BYPRODUCTS foo.sym
)
If we delete foo.sym
it's not enough with rerunning the custom command, as the executable has been stripped by COMMAND ${CMAKE_STRIP} --strip-debug foo
resulting in an empty foo.sym
. We need to relink the target's executable. Ninja does that by grouping the target and all its POST_BUILD commands in a single line.
In order to implement that for GNU Makefiles, according to http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/ section "Handling Deleted Dependency Files" we can list the byproducts as a prerequisite to the target and create an empty recipe for them with the byproduct: ;
syntax.
In in foo.dir/build.make we change:
foo: CMakeFiles/foo.dir/a.cpp.o
foo: CMakeFiles/foo.dir/build.make
foo: CMakeFiles/foo.dir/link.txt
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/pnavarro/tests/cm2/b/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking CXX executable foo"
$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/foo.dir/link.txt --verbose=$(VERBOSE)
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "Creating symbols"
/usr/bin/objcopy --only-keep-debug foo foo.sym
/usr/bin/objcopy --add-gnu-debuglink=foo.sym foo
/usr/bin/strip --strip-debug foo
to
foo.sym: ;
foo: CMakeFiles/foo.dir/a.cpp.o
foo: CMakeFiles/foo.dir/build.make
foo: foo.sym
foo: CMakeFiles/foo.dir/link.txt
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/pnavarro/tests/cm2/b/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking CXX executable foo"
$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/foo.dir/link.txt --verbose=$(VERBOSE)
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "Creating symbols"
/usr/bin/objcopy --only-keep-debug foo foo.sym
/usr/bin/objcopy --add-gnu-debuglink=foo.sym foo
/usr/bin/strip --strip-debug foo
Deleting or changing foo.sym will correctly relink foo and regenerate the symbols. I think that, at least, POST_BUILD byproducts should be listed in that way (or something similar) so the target is always relinked if any of them has changed.