update_roots.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. """Update the roots of the Carbon build used for dependency checking.
  3. The dependency checking cannot use wildcard queries, so we use them here and
  4. then create lists of relevant roots in the build file.
  5. """
  6. __copyright__ = """
  7. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  8. Exceptions. See /LICENSE for license information.
  9. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. """
  11. import os
  12. import subprocess
  13. from pathlib import Path
  14. # Change the working directory to the repository root so that the remaining
  15. # operations reliably operate relative to that root.
  16. os.chdir(Path(__file__).parents[2])
  17. print("Compute non-test C++ root targets...")
  18. query_arg = (
  19. "let non_tests = attr("
  20. " testonly, 0, //..."
  21. # Exclude tree_sitter because its @platforms dependency errors in
  22. # query. Note if it ends up in releases, we might want to do more,
  23. # but that should also be caught by check_deps.py.
  24. " except //utils/tree_sitter/..."
  25. # Exclude tcmalloc as an optional external library.
  26. " except //bazel/malloc:tcmalloc_if_linux_opt"
  27. ") in kind('(cc|pkg)_.* rule', deps($non_tests))"
  28. )
  29. non_test_cc_roots_query = subprocess.check_output(
  30. [
  31. "./scripts/run_bazel.py",
  32. "query",
  33. "--noshow_progress",
  34. "--noimplicit_deps",
  35. "--notool_deps",
  36. "--output=minrank",
  37. query_arg,
  38. ],
  39. universal_newlines=True,
  40. )
  41. ranked_targets = [line.split() for line in non_test_cc_roots_query.splitlines()]
  42. roots = [target for rank, target in ranked_targets if int(rank) == 0]
  43. print("Found roots:\n%s" % "\n".join(roots))
  44. print("Replace non-test C++ roots in the BUILD file...")
  45. buildozer_run = subprocess.run(
  46. [
  47. "./scripts/run_buildozer.py",
  48. "remove data",
  49. ]
  50. + ["add data '%s'" % root for root in roots]
  51. + ["//bazel/check_deps:non_test_cc_rules"],
  52. )
  53. if buildozer_run.returncode == 3:
  54. print("No changes needed!")
  55. else:
  56. buildozer_run.check_returncode()
  57. print("Successfully updated roots in the BUILD file!")