forbid_llvm_googletest.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. """Detects and prevents dependencies on LLVM's googletest.
  3. Carbon uses googletest directly, and it's a significantly more recent version
  4. than is provided by LLVM. Using both versions in the same binary leads to
  5. problems, so this detects dependencies.
  6. We also have some dependency checking at //bazel/check_deps. This is a separate
  7. script because check_deps relies on being able to validate specific binaries
  8. which change infrequently, whereas this effectively monitors all cc_test rules,
  9. the set of which is expected to be altered more often.
  10. """
  11. __copyright__ = """
  12. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  13. Exceptions. See /LICENSE for license information.
  14. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  15. """
  16. import subprocess
  17. import scripts_utils
  18. _MESSAGE = """\
  19. Dependencies on @llvm-project//llvm:gtest are forbidden, but a dependency path
  20. was detected:
  21. %s
  22. Carbon uses GoogleTest through @com_google_googletest, which is a different
  23. version than LLVM uses at @llvm-project//llvm:gtest. As a consequence,
  24. dependencies on @llvm-project//llvm:gtest must be avoided.
  25. """
  26. def main() -> None:
  27. scripts_utils.chdir_repo_root()
  28. args = [
  29. scripts_utils.locate_bazel(),
  30. "query",
  31. "somepath(//..., @llvm-project//third-party/unittest:gtest)",
  32. ]
  33. p = subprocess.run(
  34. args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8"
  35. )
  36. if p.returncode != 0:
  37. print(p.stderr)
  38. exit(f"bazel query returned {p.returncode}")
  39. if p.stdout:
  40. exit(_MESSAGE % p.stdout)
  41. print("Done!")
  42. if __name__ == "__main__":
  43. main()