create_compdb.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 json
  22. import os
  23. import re
  24. import shutil
  25. import subprocess
  26. import sys
  27. from pathlib import Path
  28. # Change the working directory to the repository root so that the remaining
  29. # operations reliably operate relative to that root.
  30. os.chdir(Path(__file__).parent.parent)
  31. directory = Path.cwd()
  32. # We use the `BAZEL` environment variable if present. If not, then we try to
  33. # use `bazelisk` and then `bazel`.
  34. bazel = os.environ.get("BAZEL")
  35. if not bazel:
  36. bazel = "bazelisk"
  37. if not shutil.which(bazel):
  38. bazel = "bazel"
  39. if not shutil.which(bazel):
  40. sys.exit("Unable to run Bazel")
  41. # Load compiler flags. We do this first in order to fail fast if not run from
  42. # the workspace root.
  43. print("Reading the arguments to use...")
  44. try:
  45. with open("compile_flags.txt") as flag_file:
  46. arguments = [line.strip() for line in flag_file]
  47. except FileNotFoundError:
  48. sys.exit(Path(sys.argv[0]).name + " must be run from the project root")
  49. # Prepend the `clang` executable path to the arguments that looks into our
  50. # downloaded Clang toolchain.
  51. arguments = [str(Path("bazel-clang-toolchain/bin/clang"))] + arguments
  52. print("Building compilation database...")
  53. # Find all of the C++ source files that we expect to compile cleanly as
  54. # stand-alone files. This is a bit simpler than scraping the actual compile
  55. # actions and allows us to directly index header-only libraries easily and
  56. # pro-actively index the specific headers in the project.
  57. source_files_query = subprocess.run(
  58. [
  59. bazel,
  60. "query",
  61. "--keep_going",
  62. "--output=location",
  63. # Workaround for https://github.com/bazelbuild/bazel/issues/8900
  64. "--incompatible_display_source_file_location",
  65. 'filter(".*\\.(h|cpp|cc|c|cxx)$", kind("source file", deps(//...)))',
  66. ],
  67. check=True,
  68. stdout=subprocess.PIPE,
  69. stderr=subprocess.DEVNULL,
  70. universal_newlines=True,
  71. ).stdout
  72. source_files = [
  73. Path(line.split(":")[0]) for line in source_files_query.splitlines()
  74. ]
  75. # Filter into the Carbon source files that we'll find directly in the
  76. # workspace, and LLVM source files that need to be mapped through the merged
  77. # LLVM tree in Bazel's execution root.
  78. carbon_files = [
  79. f.relative_to(directory)
  80. for f in source_files
  81. if f.parts[: len(directory.parts)] == directory.parts
  82. ]
  83. llvm_files = [
  84. Path("bazel-execroot/external").joinpath(
  85. *f.parts[f.parts.index("llvm-project") :]
  86. )
  87. for f in source_files
  88. if "llvm-project" in f.parts
  89. ]
  90. print(
  91. "Found %d Carbon source files and %d LLVM source files..."
  92. % (len(carbon_files), len(llvm_files))
  93. )
  94. # Now collect the generated file labels.
  95. generated_file_labels = subprocess.run(
  96. [
  97. bazel,
  98. "query",
  99. "--keep_going",
  100. "--output=label",
  101. (
  102. 'filter(".*\\.(h|cpp|cc|c|cxx|def|inc)$",'
  103. 'kind("generated file", deps(//...)))'
  104. ),
  105. ],
  106. check=True,
  107. stdout=subprocess.PIPE,
  108. stderr=subprocess.DEVNULL,
  109. universal_newlines=True,
  110. ).stdout.splitlines()
  111. print("Found %d generated files..." % (len(generated_file_labels),))
  112. # Directly build these labels so that indexing can find them. Allow this to
  113. # fail in case there are build errors in the client, and just warn the user
  114. # that they may be missing generated files.
  115. print("Building the generated files so that tools can find them...")
  116. subprocess.run([bazel, "build", "--keep_going"] + generated_file_labels)
  117. # Manually translate the label to a user friendly path into the Bazel output
  118. # symlinks.
  119. def _label_to_path(s):
  120. # Map external repositories to their part of the output tree.
  121. s = re.sub(r"^@([^/]+)//", r"bazel-bin/external/\1/", s)
  122. # Map this repository to the root of the output tree.
  123. s = s if not s.startswith("//") else "bazel-bin/" + s[len("//") :]
  124. # Replace the colon used to mark the package name with a slash.
  125. s = s.replace(":", "/")
  126. # Convert to a native path.
  127. return Path(s)
  128. generated_files = [_label_to_path(label) for label in generated_file_labels]
  129. # Generate compile_commands.json with an entry for each C++ input.
  130. entries = [
  131. {
  132. "directory": str(directory),
  133. "file": str(f),
  134. "arguments": arguments + [str(f)],
  135. }
  136. for f in carbon_files + llvm_files + generated_files
  137. ]
  138. with open("compile_commands.json", "w") as json_file:
  139. json.dump(entries, json_file, indent=2)