llvm_symlinks_test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. """Checks various LLVM tool symlinks behave as expected."""
  3. __copyright__ = """
  4. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  5. Exceptions. See /LICENSE for license information.
  6. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. """
  8. from pathlib import Path
  9. import subprocess
  10. import os
  11. import sys
  12. import unittest
  13. class LLVMSymlinksTest(unittest.TestCase):
  14. def setUp(self) -> None:
  15. # The install root is adjacent to the test script
  16. self.install_root = Path(sys.argv[0]).parent / "prefix_root"
  17. self.tmpdir = Path(os.environ["TEST_TMPDIR"])
  18. self.test_o_file = self.tmpdir / "test.o"
  19. self.test_o_file.touch()
  20. def get_link_cmd(self, clang: Path) -> list[str | Path]:
  21. return [
  22. clang,
  23. # We pick an arbitrary linux target to get stable results.
  24. "--target=aarch64-unknown-linux-gnu",
  25. # Verbose printing to help with debugging.
  26. "-v",
  27. # Print out the link command rather than running it.
  28. "-###",
  29. # Give the link command an output.
  30. "-o",
  31. self.tmpdir / "test",
  32. # A test input file. This won't be read though.
  33. self.test_o_file,
  34. ]
  35. def test_clang(self) -> None:
  36. bin = self.install_root / "lib/carbon/llvm/bin/clang"
  37. run = subprocess.run(
  38. self.get_link_cmd(bin), check=True, capture_output=True, text=True
  39. )
  40. # Check that we do have a plausible link command.
  41. self.assertRegex(run.stderr, r'"-m" "aarch64linux"')
  42. # Ensure it doesn't contain the C++ standard library.
  43. self.assertNotRegex(run.stderr, r'"-lstdc++"')
  44. def test_clangplusplus(self) -> None:
  45. bin = self.install_root / "lib/carbon/llvm/bin/clang++"
  46. run = subprocess.run(
  47. self.get_link_cmd(bin), check=True, capture_output=True, text=True
  48. )
  49. # Check that we do have a plausible link command.
  50. self.assertRegex(run.stderr, r'"-m" "aarch64linux"')
  51. # Ensure it doesn't contain the C++ standard library.
  52. self.assertNotRegex(run.stderr, r'"-lstdc++"')
  53. def test_clang_cl(self) -> None:
  54. bin = self.install_root / "lib/carbon/llvm/bin/clang-cl"
  55. run = subprocess.run(
  56. # Use the `cl.exe`-specific help flag to test the mode.
  57. [bin, "/?"],
  58. check=True,
  59. capture_output=True,
  60. text=True,
  61. )
  62. # This should print the help string, including `cl.exe` specifics.
  63. self.assertRegex(run.stdout, r"CL.EXE COMPATIBILITY OPTIONS:")
  64. def test_clang_cpp(self) -> None:
  65. # Note that this is a test of the C-preprocessor mode, not C++ mode.
  66. # Create a test file that we'll preprocess.
  67. text_file = self.tmpdir / "test.txt"
  68. with open(text_file, "w") as f:
  69. f.write("TEST\n")
  70. # Run the preprocessor using a CPP-specific command line reading from
  71. # the test file and writing to stdout. We define a macro that we'll
  72. # check is expanded.
  73. bin = self.install_root / "lib/carbon/llvm/bin/clang-cpp"
  74. run = subprocess.run(
  75. [bin, "-D", "TEST=SUCCESS", text_file, "-"],
  76. check=True,
  77. capture_output=True,
  78. text=True,
  79. )
  80. self.assertEqual(run.stderr, "")
  81. self.assertRegex(run.stdout, r"(^|\n)SUCCESS\n")
  82. if __name__ == "__main__":
  83. unittest.main()