lit_test.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Runs `lit` for testing."""
  2. __copyright__ = """
  3. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. Exceptions. See /LICENSE for license information.
  5. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. """
  7. import argparse
  8. import os
  9. import subprocess
  10. _PASSTHROUGH_FLAGS = ["filter", "filter-out"]
  11. def _parse_args():
  12. """Parses command line arguments, returning the result."""
  13. arg_parser = argparse.ArgumentParser(description=__doc__)
  14. arg_parser.add_argument(
  15. "test_dir", help="The directory containing tests to run."
  16. )
  17. arg_parser.add_argument(
  18. "lit_args", nargs="*", help="Arguments to pass through to lit."
  19. )
  20. return arg_parser.parse_args()
  21. def _normalize(relative_base, target):
  22. """Given a target, normalizes it to a relative path."""
  23. assert target
  24. if target.startswith(":"):
  25. # Local target; :foo -> my/dir/foo
  26. return os.path.join(relative_base, target[1:])
  27. elif target[0].isalpha():
  28. # Local target; foo -> my/dir/foo
  29. return os.path.join(relative_base, target)
  30. if ":" in target:
  31. # Specified target; //foo:bar -> //foo/bar
  32. target = target.replace(":", "/")
  33. else:
  34. # Default target; //foo -> //foo/foo
  35. target = os.path.join(target, os.path.basename(target))
  36. if target.startswith("@"):
  37. return os.path.join("external/", target[1:])
  38. elif target.startswith("//"):
  39. return target[2:]
  40. else:
  41. raise ValueError("Unhandled target path: %s" % target)
  42. def main():
  43. parsed_args = _parse_args()
  44. bin_dir = os.getcwd()
  45. # Figure out the actual path for the test_dir.
  46. relative_base = os.path.dirname(_normalize("", os.environ["TEST_TARGET"]))
  47. test_dir = os.path.join(
  48. bin_dir, _normalize(relative_base, parsed_args.test_dir)
  49. )
  50. args = [
  51. os.path.join(os.environ["TEST_SRCDIR"], "llvm-project/llvm/lit"),
  52. "--path=%s" % bin_dir,
  53. test_dir,
  54. "-sv",
  55. ]
  56. # Run lit.
  57. p = subprocess.run(args=args + parsed_args.lit_args)
  58. # Do this instead of check_call to hide stack traces.
  59. if p.returncode != 0:
  60. exit("lit failed, exit code %d" % p.returncode)
  61. if __name__ == "__main__":
  62. exit(main())