migrate_cpp.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Migrates C++ code to Carbon."""
  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 glob
  9. import os
  10. import subprocess
  11. import sys
  12. _CPP_REFACTORING = "./cpp_refactoring/cpp_refactoring"
  13. _H_EXTS = {".h", ".hpp"}
  14. _CPP_EXTS = {".c", ".cc", ".cpp", ".cxx"}
  15. class _Workflow(object):
  16. def __init__(self):
  17. """Parses command-line arguments and flags."""
  18. parser = argparse.ArgumentParser(description=__doc__)
  19. parser.add_argument(
  20. "dir",
  21. type=str,
  22. help="A directory containing C++ files to migrate to Carbon.",
  23. )
  24. parsed_args = parser.parse_args()
  25. self._parsed_args = parsed_args
  26. self._data_dir = os.path.dirname(sys.argv[0])
  27. # Validate arguments.
  28. if not os.path.isdir(parsed_args.dir):
  29. sys.exit("%r must point to a directory." % parsed_args.dir)
  30. def run(self):
  31. """Runs the migration workflow."""
  32. try:
  33. self._gather_files()
  34. self._clang_tidy()
  35. self._cpp_refactoring()
  36. self._rename_files()
  37. self._print_header("Done!")
  38. except subprocess.CalledProcessError as e:
  39. # Discard the stack for subprocess errors.
  40. sys.exit(e)
  41. def _data_file(self, relative_path):
  42. """Returns the path to a data file."""
  43. return os.path.join(self._data_dir, relative_path)
  44. @staticmethod
  45. def _print_header(header):
  46. print("*" * 79)
  47. print("* %-75s *" % header)
  48. print("*" * 79)
  49. def _gather_files(self):
  50. """Returns the list of C++ files to convert."""
  51. self._print_header("Gathering C++ files...")
  52. all_files = glob.glob(
  53. os.path.join(self._parsed_args.dir, "**/*.*"), recursive=True
  54. )
  55. exts = _CPP_EXTS.union(_H_EXTS)
  56. cpp_files = [f for f in all_files if os.path.splitext(f)[1] in exts]
  57. if not cpp_files:
  58. sys.exit(
  59. "%r doesn't contain any C++ files to convert."
  60. % self._parsed_args.dir
  61. )
  62. self._cpp_files = sorted(cpp_files)
  63. print("%d files found." % len(self._cpp_files))
  64. def _clang_tidy(self):
  65. """Runs clang-tidy to fix C++ files in a directory."""
  66. self._print_header("Running clang-tidy...")
  67. with open(self._data_file("clang_tidy.yaml")) as f:
  68. config = f.read()
  69. subprocess.run(
  70. ["clang-tidy", "--fix", "--config", config] + self._cpp_files,
  71. check=True,
  72. )
  73. def _cpp_refactoring(self):
  74. """Runs cpp_refactoring to migrate C++ files towards Carbon syntax."""
  75. self._print_header("Running cpp_refactoring...")
  76. cpp_refactoring = self._data_file(_CPP_REFACTORING)
  77. subprocess.run([cpp_refactoring] + self._cpp_files, check=True)
  78. def _rename_files(self):
  79. """Renames C++ files to the destination Carbon filenames."""
  80. api_renames = 0
  81. impl_renames = 0
  82. for f in self._cpp_files:
  83. parts = os.path.splitext(f)
  84. if parts[1] in _H_EXTS:
  85. os.rename(f, parts[0] + ".carbon")
  86. api_renames += 1
  87. else:
  88. os.rename(f, parts[0] + ".impl.carbon")
  89. impl_renames += 1
  90. print(
  91. "Renaming resulted in %d API files and %d impl files."
  92. % (api_renames, impl_renames)
  93. )
  94. if __name__ == "__main__":
  95. _Workflow().run()