check_non_test_cc_deps.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. """Check that non-test C++ rules only depend on Carbon and LLVM.
  3. Carbon works to ensure its user-visible libraries and binaries only depend on
  4. their code and LLVM. Among other benefits, this provides a single, simple
  5. license used for the whole project.
  6. However, we frequently use third-party projects and libraries where useful in
  7. our test code. Here, we verify that the dependencies of non-test C++ rules only
  8. include Carbon and LLVM code.
  9. """
  10. __copyright__ = """
  11. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  12. Exceptions. See /LICENSE for license information.
  13. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  14. """
  15. import os
  16. import sys
  17. from pathlib import Path
  18. runfiles = Path(os.environ["TEST_SRCDIR"])
  19. deps_path = (
  20. runfiles / "carbon" / "bazel" / "check_deps" / "non_test_cc_deps.txt"
  21. )
  22. try:
  23. with deps_path.open() as deps_file:
  24. deps = deps_file.read().splitlines()
  25. except FileNotFoundError:
  26. sys.exit("ERROR: unable to find deps file: %s" % deps_path)
  27. for dep in deps:
  28. print("Checking dependency: " + dep)
  29. repo, _, rule = dep.partition("//")
  30. if repo == "" and not rule.startswith("third_party"):
  31. # Carbon code is always allowed.
  32. continue
  33. if repo == "@llvm-project":
  34. package, _, rule = rule.partition(":")
  35. # Other packages in the LLVM project shouldn't be accidentally used
  36. # in Carbon. We can expand the above list if use cases emerge.
  37. if package not in ("llvm", "lld", "clang", "clang-tools-extra/clangd"):
  38. sys.exit(
  39. "ERROR: unexpected dependency into the LLVM project: %s" % dep
  40. )
  41. # Check for accidentally using the copy of GoogleTest in LLVM.
  42. if rule in ("gmock", "gtest", "gtest_main"):
  43. sys.exit(
  44. "ERROR: dependency on LLVM's GoogleTest from non-test code: %s"
  45. % dep
  46. )
  47. # The rest of LLVM, LLD, and Clang themselves are safe to depend on.
  48. continue
  49. if repo in ("@llvm_terminfo", "@llvm_zlib", "@llvm_zstd"):
  50. # These are stubs wrapping system libraries for LLVM. They aren't
  51. # distributed and so should be fine.
  52. continue
  53. if repo in (
  54. "@com_github_google_benchmark",
  55. "@com_github_protocolbuffers_protobuf",
  56. "@com_google_absl",
  57. "@com_google_googletest",
  58. ):
  59. # This should never be reached from non-test code, but these targets do
  60. # exist. Specially diagnose them to try to provide a more helpful
  61. # message.
  62. sys.exit("ERROR: dependency only allowed in test code: %s" % dep)
  63. # Conservatively fail if a dependency isn't explicitly allowed above.
  64. sys.exit("ERROR: unknown dependency: %s" % dep)