update_roots.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. non_test_cc_roots_query = subprocess.check_output(
  19. [
  20. "./scripts/run_bazel.py",
  21. "query",
  22. "--noshow_progress",
  23. "--noimplicit_deps",
  24. "--notool_deps",
  25. "--output=minrank",
  26. (
  27. 'let non_tests = kind("cc.* rule", attr(testonly, 0, //...))'
  28. ' in kind("cc.* rule", deps($non_tests))'
  29. ),
  30. ],
  31. universal_newlines=True,
  32. )
  33. ranked_targets = [line.split() for line in non_test_cc_roots_query.splitlines()]
  34. roots = [target for rank, target in ranked_targets if int(rank) == 0]
  35. print("Found roots:\n%s" % "\n".join(roots))
  36. print("Replace non-test C++ roots in the BUILD file...")
  37. buildozer_run = subprocess.run(
  38. [
  39. "./scripts/run_buildozer.py",
  40. "remove data",
  41. ]
  42. + ["add data '%s'" % root for root in roots]
  43. + ["//bazel/check_deps:non_test_cc_rules"],
  44. )
  45. if buildozer_run.returncode == 3:
  46. print("No changes needed!")
  47. else:
  48. buildozer_run.check_returncode()
  49. print("Successfully updated roots in the BUILD file!")