Comparing to other answers I thought it is more convenient if graphviz generation runs only when it is required. Also I check if graphiz/dot itself exists:
find_program(GRAPHVIZ dot)
if(GRAPHVIZ)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/_graphviz
COMMAND ${CMAKE_COMMAND} "--graphviz=graphviz/deps.txt" .
COMMAND dot -Tsvg graphviz/deps.txt -o deps.svg
COMMAND dot -Tpng graphviz/deps.txt -o deps.png
COMMENT "Plotting dependencies graph to deps.svg"
DEPENDS <some_target>
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
add_custom_target(graphviz ALL
DEPENDS ${CMAKE_BINARY_DIR}/_graphviz)
endif()
When some_target
changes it also will try to regenerate OUTPUT _graphviz
file, thus plot dependencies.
By default TARGET graphviz
target always builds, but it is empty, so plotting itself is not called, if OUTPUT _graphviz
is not updated
p.s. found this seeing answer of Alternative to CMake POST_BUILD command when target is in subdirectory