create_compdb.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. """Create a compilation database for Clang tools like `clangd`.
  3. If you want `clangd` to be able to index this project, run this script from
  4. the workspace root to generate a rich compilation database. After the first
  5. run, you should only need to run it if you encounter `clangd` problems, or if
  6. you want `clangd` to build an up-to-date index of the entire project. Note
  7. that in the latter case you may need to manually clear and rebuild clangd's
  8. index after running this script.
  9. Note that this script will build generated files in the Carbon project and
  10. otherwise touch the Bazel build. It works to do the minimum amount necessary.
  11. Once setup, generally subsequent builds, even of small parts of the project,
  12. different configurations, or that hit errors won't disrupt things. But, if
  13. you do hit errors, you can get things back to a good state by fixing the
  14. build of generated files and re-running this script.
  15. """
  16. __copyright__ = """
  17. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  18. Exceptions. See /LICENSE for license information.
  19. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  20. """
  21. import subprocess
  22. import scripts_utils
  23. def _build_generated_files(bazel: str) -> None:
  24. print("Building the generated files so that tools can find them...")
  25. # Collect the generated file labels. Include some rules which generate
  26. # files but aren't classified as "generated file".
  27. kinds_query = (
  28. "filter("
  29. ' ".*\\.(h|cpp|cc|c|cxx|def|inc)$",'
  30. ' kind("generated file", deps(//...))'
  31. ")"
  32. " union "
  33. 'kind("cc_proto_library", deps(//...))'
  34. )
  35. generated_file_labels = subprocess.check_output(
  36. [bazel, "query", "--keep_going", "--output=label", kinds_query],
  37. stderr=subprocess.DEVNULL,
  38. encoding="utf-8",
  39. ).splitlines()
  40. print(f"Found {len(generated_file_labels)} generated files...")
  41. # Directly build these labels so that indexing can find them. Allow this to
  42. # fail in case there are build errors in the client, and just warn the user
  43. # that they may be missing generated files.
  44. subprocess.check_call(
  45. [bazel, "build", "--keep_going"] + generated_file_labels
  46. )
  47. def main() -> None:
  48. scripts_utils.chdir_repo_root()
  49. bazel = scripts_utils.locate_bazel()
  50. _build_generated_files(bazel)
  51. print("Generating compile_commands.json (may take a few minutes)...")
  52. subprocess.run([bazel, "run", "@hedron_compile_commands//:refresh_all"])
  53. if __name__ == "__main__":
  54. main()