# CMake Graphviz, a way to show which source files correspond to targets?

**URL:** https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119
**Category:** Development
**Created:** [May 19, 2025, 6:36pm UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119 "2025-05-19T18:36:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![a\_toldaiev](https://discourse.cmake.org/user_avatar/discourse.cmake.org/a_toldaiev/32/5496_2.png) [@a\_toldaiev](https://discourse.cmake.org/u/a_toldaiev)
#### Post date: [May 19, 2025, 6:36pm UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/1 "2025-05-19T18:36:25Z")

</div>

I would like to add more information about targets in the graph that is built with the graphviz option of CMake:

```auto
cmake -S . --graphviz=targets.dot

```

The `targets.dot` graph shows only target names and their types, and how they depend on each other. I would like to add more information about the targets, hidden in Graphviz [tooltip](https://graphviz.org/docs/attrs/tooltip/) or maybe with some embedded URLs. For example, the information could include the source files of the targets, which `CMakeLists.txt` declared a target, what are the `INSTALL` parameters, `target_compile_options` and `features`, etc.

As far as I understand, currently there is no option in CMake [CMakeGraphVizOptions](https://cmake.org/cmake/help/latest/module/CMakeGraphVizOptions.html) to add such information to the dot graph.

I was wondering whether there is already a way to combine such information in a graph of target dependencies? Or are there existing developments towards that?

For example, CMake could dump the information about the project targets in a serialized format (a json like compile\_commands.json), so that a dot graph could be built out of it with a third party tool. Or maybe there could be a tool that generates such a graph file from the build directory and CMakeFiles etc. (I am not sure if there is a way to get all the information from parsing CMakeFiles. There are tools that turn Makefiles to graphs. That’s probably not the right way to go either.)

If there is currently no clear way to do it, I would like to propose such a feature for development. I could try to put together a draft merge request with the idea.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 19, 2025, 9:53pm UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/2 "2025-05-19T21:53:33Z")

</div>

You might want to check out [CMake’s file API](https://cmake.org/cmake/help/latest/manual/cmake-file-api.7.html). Quite a lot of information about the code model is available through that mechanism, and that should be the canonical place to look for such detailed information, not the graphviz output.

---

<div class="post-metadata">

### Author: ![a\_toldaiev](https://discourse.cmake.org/user_avatar/discourse.cmake.org/a_toldaiev/32/5496_2.png) [@a\_toldaiev](https://discourse.cmake.org/u/a_toldaiev)
#### Post date: [May 20, 2025, 8:23am UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/3 "2025-05-20T08:23:22Z")

</div>

Indeed, it looks like everything is there. I only tried

```auto
touch build/.cmake/api/v1/query/codemodel-v2

```

And the reply contains all the information in jsons:

```auto
build/.cmake/api/v1/reply/*json

```

I will just make the graphs from there. Thanks!

Are there JSON schema files for codemodel-v2 and other object kinds?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 21, 2025, 7:45am UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/4 "2025-05-21T07:45:13Z")

</div>

I don’t know if we have JSON schema files for the file API. @brad.king ?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 21, 2025, 9:23am UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/5 "2025-05-21T09:23:54Z")

</div>

We do not (or at least nothing that I’ve needed to update when adding new data to the files).

---

<div class="post-metadata">

### Author: ![a\_toldaiev](https://discourse.cmake.org/user_avatar/discourse.cmake.org/a_toldaiev/32/5496_2.png) [@a\_toldaiev](https://discourse.cmake.org/u/a_toldaiev)
#### Post date: [May 23, 2025, 4:27pm UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/6 "2025-05-23T16:27:18Z")

</div>

So, I started a [Python project](https://github.com/xealits/cmake_graph_tests) to explore how things work and what graphs could be useful. It looks like file-api contains everything 👍 Thanks!

Running it on CMake itself, I get the following graph (the SVG does not get uploaded correctly):  
 ![targetgraph-](https://discourse.cmake.org/uploads/default/original/2X/1/1e5bb017d191bed5cb5c2e0d032fc0ac1a1fe113.svg)

It just reproduces `cmake --graphviz` and adds the source paths in the tooltips.

When testing it on [cmake\_template](https://github.com/cpp-best-practices/cmake_template), I did not see some expected INTERFACE targets in the reply to the codemodel-v2 query. For example, the INTERFACE targets myproject\_options and myproject\_warnings (declared in ProjectOptions.cmake) that contain the project-wide settings for the C++ compiler, which are set with target\_compile\_features etc commands. Probably, I am missing something. But I thought these two INTERFACE targets are always enabled and always linked to all other targets in the project.

They do show up in the codemodel reply jsons. In the directory-.-Release-…json, under “installers”, it has one with “type”: “export” and a list of “exportTargets” with ids “intro…”, “myproject\_options…” and “myproject\_warnings”. But that’s the only place that mentions these targets.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [June 8, 2025, 7:57am UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/7 "2025-06-08T07:57:47Z")

</div>

I created [this issue](https://gitlab.kitware.com/cmake/cmake/-/issues/26987) to track the request for providing a formal JSON schema for the CMake file API.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [August 9, 2025, 11:17pm UTC](https://discourse.cmake.org/t/cmake-graphviz-a-way-to-show-which-source-files-correspond-to-targets/14119/8 "2025-08-09T23:17:25Z")

</div>

Just noting that CMake master now has a JSON schema for the CMake file API. The change was merged in [MR 10914](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10914) and should be part of the 4.2.0 release in a few months.
