bkryza / clang-uml

Customizable automatic UML diagram generator for C++ based on Clang.
Apache License 2.0
592 stars 40 forks source link

troubles using bazel #236

Closed tsr-boxbot closed 6 months ago

tsr-boxbot commented 7 months ago

I'm having some trouble using this tool with bazel 6.3.2.

To show the issue I used a small test project found here: https://github.com/tsr-boxbot/bazel_code_coverage_problems

The following was done on hash 91932a84deaf74662657ebb89af0710789a5bc34 of that project.

First I generated the compilation database by using https://github.com/grailbio/bazel-compilation-database using this command

bazel-compdb -q //... -- --config=clang_config

which gives me compilation database:

[
  {
    "command": "/usr/bin/clang-13 -std=c++20 -D\"BAZEL_CURRENT_REPOSITORY=\"\"\" -iquote . -iquote bazel-out/k8-fastbuild/bin -x c++ -c add.cpp",
    "directory": "/home/tyler/.cache/bazel/_bazel_tyler/a2dbe2b273e2ac218eb21f623e5b82b5/execroot/test",
    "file": "add.cpp"
  },
  {
    "command": "/usr/bin/clang-13 -std=c++20 -D\"BAZEL_CURRENT_REPOSITORY=\"\"\" -iquote . -iquote bazel-out/k8-fastbuild/bin -x c++ -c add.h",
    "directory": "/home/tyler/.cache/bazel/_bazel_tyler/a2dbe2b273e2ac218eb21f623e5b82b5/execroot/test",
    "file": "add.h"
  },
  {
    "command": "/usr/bin/clang-13 -std=c++20 -D\"BAZEL_CURRENT_REPOSITORY=\"\"\" -isystem external/com_google_googletest/googlemock -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googlemock -isystem external/com_google_googletest/googlemock/include -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googlemock/include -isystem external/com_google_googletest/googletest -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googletest -isystem external/com_google_googletest/googletest/include -isystem bazel-out/k8-fastbuild/bin/external/com_google_googletest/googletest/include -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/com_google_googletest -iquote bazel-out/k8-fastbuild/bin/external/com_google_googletest -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -x c++ -c add.test.cpp",
    "directory": "/home/tyler/.cache/bazel/_bazel_tyler/a2dbe2b273e2ac218eb21f623e5b82b5/execroot/test",
    "file": "add.test.cpp"
  },
 *snipped*
]

When I use this .clang-uml file:

# Change to directory where compile_commands.json is
compilation_database_dir: .
# Change to directory where diagram should be written
output_directory: docs/diagrams
diagrams:
  example_class_diagram:
    type: class
    glob:
      - add.cpp
      - add.test.cpp
      - add.h

I see this:

$ clang-uml
[info] [tid 453985] [cli_handler.cc:303] Loaded clang-uml config from .clang-uml
[info] [tid 453985] [cli_handler.cc:326] Loading compilation database from /home/tyler/git/clang-uml/bazel_code_coverage_problems/ directory
[error] [tid 453985] [generators.cc:234] Diagram example_class_diagram generation failed: no translation units found. Please make sure that your 'glob' patterns match at least 1 file in 'compile_commands.json

and there's clearly an add.cpp in the compile commands file. Note all the commands I ran were done in the base of the repository.

I got clang-uml from the ppa, here's the version info:

$ clang-uml --version
clang-uml 0.5.0
Copyright (C) 2021-2024 Bartek Kryza <bkryza@gmail.com>
Linux x86_64 6.5.0-14-generic
Built against LLVM/Clang libraries version: 15.0.7
Using LLVM/Clang libraries version: Ubuntu clang version 15.0.7

I'd really like to use this tool, please lmk what I can do to make this work! <3

bkryza commented 7 months ago

@tsr-boxbot I can't reproduce as I get some error from Bazel when trying to generate the compile_commands.json using this plugin:

❯ ../bazel-compdb/bazel-compilation-database-0.5.2/generate.py -q //... -- --config=clang_config
ERROR: Unable to find package for @@[unknown repo 'bazel_compdb' requested from @@]//:aspects.bzl: The repository '@@[unknown repo 'bazel_compdb' requested from @@]' could not be resolved: No repository visible as '@bazel_compdb' from main repository.
...

However, the reason for your error is that by default this plugin generates "directory" entries in compile_commands.json pointing to some temporary Bazel build directory.

It seems this plugin has an option -s, which generates the paths pointing to the original source directory, try to regenerate your compile commands with this command:

bazel-compdb -s -q //... -- --config=clang_config

Also, glob pattern only applies to translation units, so you shouldn't put any header files there:

compilation_database_dir: .
output_directory: docs/diagrams
diagrams:
  example_class_diagram:
    type: class
    glob:
      - add.cpp
      - add.test.cpp
tsr-boxbot commented 6 months ago

@bkryza sorry for the delay in responding to you, your find on the -s flag fixed this issue for me and made your work here usable for me! Thank you and much appreciated