mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 21:51:12 +01:00
Initial migration framework (#525)
What this does: - Sets up a `migrate_cpp` tool which currently only runs `clang-tidy`. - This is intended to have more transformations in the future. - Sets up a `migrate_cpp.sh` script. - This copies the original woff2 code into a `carbon` directory and runs the `migrate_cpp` tool on it there. - Adds the initial `carbon` directory of woff2 - To be clear, this is currently only updated via `clang-tidy`. - More transformations should be expected in the future. - Minor related edits. For example: - Adjust pre-commit to skip the `carbon` directory, because it's third-party code and shouldn't be edited in the same way. - Adds `@brotli_carbon` as a local repository so that we can "build" outputs. - Makes clang-tidy from the bootstrap toolchain accessible for BUILD dependencies, as it's then used for `migrate_cpp`. What this does not do: - Any actual transformation of C++ code to Carbon Co-authored-by: Matthew Riley <mdriley@gmail.com>
This commit is contained in:
committed by
GitHub
co-authored by
Matthew Riley
parent
e6d5efe7d9
commit
88a9d244b7
@@ -0,0 +1,15 @@
|
||||
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
# Exceptions. See /LICENSE for license information.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
load("@rules_python//python:defs.bzl", "py_binary")
|
||||
|
||||
py_binary(
|
||||
name = "migrate_cpp",
|
||||
srcs = ["migrate_cpp.py"],
|
||||
data = [
|
||||
":clang_tidy.yaml",
|
||||
"@bootstrap_clang_toolchain//:bin/clang-tidy",
|
||||
],
|
||||
python_version = "PY3",
|
||||
)
|
||||
@@ -0,0 +1,20 @@
|
||||
# C++ migration tooling
|
||||
|
||||
<!--
|
||||
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
Exceptions. See /LICENSE for license information.
|
||||
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
-->
|
||||
|
||||
<!-- toc -->
|
||||
|
||||
## Table of contents
|
||||
|
||||
- [Overview](#overview)
|
||||
|
||||
<!-- tocstop -->
|
||||
|
||||
## Overview
|
||||
|
||||
`migrate_cpp` assists in migration of C++ code to Carbon. It's currently being
|
||||
assembled; more documentation will be added later.
|
||||
@@ -0,0 +1,16 @@
|
||||
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
# Exceptions. See /LICENSE for license information.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
# Use clang-tidy to standardize syntax. This configuration focuses on
|
||||
# modernizations, not casing-related choices, in order to avoid changes which
|
||||
# would affect non-migrated callers.
|
||||
|
||||
---
|
||||
Checks:
|
||||
-*, bugprone-*, -bugprone-reserved-identifier, google-*,
|
||||
-google-readability-todo, misc-definitions-in-headers, misc-misplaced-const,
|
||||
misc-redundant-expression, misc-static-assert,
|
||||
misc-unconventional-assign-operator, misc-uniqueptr-reset-release,
|
||||
misc-unused-*, modernize-*, -modernize-avoid-c-arrays, performance-*,
|
||||
readability-braces-around-statements
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Migrates C++ code to Carbon."""
|
||||
|
||||
__copyright__ = """
|
||||
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
Exceptions. See /LICENSE for license information.
|
||||
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import glob
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
_CLANG_TIDY = "../external/bootstrap_clang_toolchain/bin/clang-tidy"
|
||||
_CPP_EXTS = {".h", ".c", ".cc", ".cpp", ".cxx"}
|
||||
|
||||
|
||||
def _data_file(relative_path):
|
||||
"""Returns the path to a data file."""
|
||||
return os.path.join(os.path.dirname(sys.argv[0]), relative_path)
|
||||
|
||||
|
||||
def _parse_args(args=None):
|
||||
"""Parses command-line arguments and flags."""
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument(
|
||||
"dir",
|
||||
type=str,
|
||||
help="A directory containing C++ files to migrate to Carbon.",
|
||||
)
|
||||
parsed_args = parser.parse_args(args=args)
|
||||
return parsed_args
|
||||
|
||||
|
||||
def _gather_files(parsed_args):
|
||||
"""Returns the list of C++ files to convert."""
|
||||
all_files = glob.glob(
|
||||
os.path.join(parsed_args.dir, "**/*.*"), recursive=True
|
||||
)
|
||||
cpp_files = [f for f in all_files if os.path.splitext(f)[1] in _CPP_EXTS]
|
||||
if not cpp_files:
|
||||
sys.exit(
|
||||
"%r doesn't contain any C++ files to convert." % parsed_args.dir
|
||||
)
|
||||
return sorted(cpp_files)
|
||||
|
||||
|
||||
def _clang_tidy(parsed_args, cpp_files):
|
||||
"""Runs clang-tidy to fix C++ files in a directory."""
|
||||
print("Running clang-tidy...")
|
||||
clang_tidy = _data_file(_CLANG_TIDY)
|
||||
with open(_data_file("clang_tidy.yaml")) as f:
|
||||
config = f.read()
|
||||
subprocess.run([clang_tidy, "--fix", "--config", config] + cpp_files)
|
||||
|
||||
|
||||
def _main():
|
||||
"""Main program execution."""
|
||||
parsed_args = _parse_args()
|
||||
|
||||
# Validate arguments.
|
||||
if not os.path.isdir(parsed_args.dir):
|
||||
sys.exit("%r must point to a directory." % parsed_args.dir)
|
||||
|
||||
cpp_files = _gather_files(parsed_args)
|
||||
_clang_tidy(parsed_args, cpp_files)
|
||||
print("Done!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_main()
|
||||
Reference in New Issue
Block a user