cmakeadd-custom-target

Cmake option GRAPHVIZ_CUSTOM_TARGETS doesn't work as expected


I am following instructions from the cmake documentation for graphviz dependency graph here ( https://cmake.org/cmake/help/latest/module/CMakeGraphVizOptions.html) and for custom targets here ( https://cmake.org/cmake/help/latest/command/add_custom_target.html).

I want to see custom targets in the generated dependency graph. The relevant option is GRAPHVIZ_CUSTOM_TARGETS. Even after setting it to TRUE, custom targets are not shown.

What am I doing wrong?

Minimum working example:

CMakeLists.txt

cmake_minimum_required(VERSION 3.31)
set(GRAPHVIZ_CUSTOM_TARGETS TRUE)
project("Dummy" LANGUAGES C)

add_executable(dummy_bin dummy.c)
add_custom_target(dummy_custom DEPENDS ${CMAKE_BINARY_DIR}/dummy_bin COMMENT "FOO")

dummy.c

#include "stdio.h"

int main(){
    printf("Hello");
    return 0;
}

Command:

mkdir build  
cmake --graphviz=foo.dot ..
dot -Tsvg -o foo.svg foo.dot

Output

(dummy_custom is missing) Dependency graph missing 'dummy_custom' custom target


Solution

  • Variables like GRAPHVIS_* should be defined not in the CMakeLists.txt, but in the separate file named CMakeGraphVizOptions.cmake, located in the top-level source directory of your project. (If you want to generate that file alongside with your project, then place it into the top-level build directory). From the documentation:

    The look and content of the generated graphs can be controlled using the file CMakeGraphVizOptions.cmake. This file is first searched in CMAKE_BINARY_DIR, and then in CMAKE_SOURCE_DIR. If found, the variables set in it are used to adjust options for the generated Graphviz files.

    Correct:

    File CMakeGraphVizOptions.cmake:

    set(GRAPHVIZ_CUSTOM_TARGETS TRUE)
    

    File CMakeLists.txt

    cmake_minimum_required(VERSION 3.31)
    
    project("Dummy" LANGUAGES C)
    
    add_executable(dummy_bin dummy.c)
    add_custom_target(dummy_custom DEPENDS ${CMAKE_BINARY_DIR}/dummy_bin COMMENT "FOO")