pytorch/tools/linter/clang_tidy/generate_build_files.py
Elton Leander Pinto a1ad28da10 Refactor clang_tidy.py (#61119)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/61119

This change spilts the clang-tidy CI job into smaller steps and uses a
refactored version of the clang_tidy.py script.

The new folder structure is as follows:
```
tools/linter/clang_tidy
|_ __main__py
|_ requirements.txt
|_ run.py
|_ setup.sh
```

`__main__.py`

This script will run `tools/linter/clang_tidy/setup.sh` if a `build`
directory doesn't exist, mimicing what used to be done as a separate
step in the CI job.

After that, it will invoke `clang-tidy` with default arguments being
declared in the script itself (as opposed to declaring them in
lint.yml).

The reasoning behind this approach is two-fold:

- Make it easier to run `clang-tidy` locally using this script
- De-duplicate the option passing

`requirements.txt`

Contains a list of additional python dependencies needed by the
`clang-tidy` script.

`setup.sh`

If a build directory doesn't exist, this command will run the necessary
codegen and build commands for running `clang-tidy`

Example usage:
```
python3 tools/linter/clang_tidy --parallel
```
Notice that we don't have to put the `.py` at the end of `clang_tidy`.

Test Plan:
Run the following command:
```
python3 tools/linter/clang_tidy --paths torch/csrc/fx --parallel
```

Reviewed By: walterddr, janeyx99

Differential Revision: D29568582

Pulled By: 1ntEgr8

fbshipit-source-id: cd6d11c5cb8ba9f1344a87c35647a1cd8dd45b04
2021-07-06 16:02:11 -07:00

63 lines
1.5 KiB
Python

import subprocess
import sys
import os
from typing import List
def run_cmd(cmd: List[str]) -> None:
print(f"Running: {cmd}")
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
stdout, stderr = result.stdout.decode("utf-8").strip(), result.stderr.decode("utf-8").strip()
print(stdout)
print(stderr)
if result.returncode != 0:
print(f"Failed to run {cmd}")
exit(1)
def run_timed_cmd(cmd: List[str]) -> None:
run_cmd(["time"] + cmd)
def update_submodules() -> None:
run_cmd(["git", "submodule", "update", "--init", "--recursive"])
def gen_compile_commands() -> None:
os.environ["USE_NCCL"] = "0"
os.environ["USE_DEPLOY"] = "1"
run_timed_cmd([sys.executable, "setup.py", "--cmake-only", "build"])
def run_autogen() -> None:
run_timed_cmd(
[
sys.executable,
"-m",
"tools.codegen.gen",
"-s",
"aten/src/ATen",
"-d",
"build/aten/src/ATen",
]
)
run_timed_cmd(
[
sys.executable,
"tools/setup_helpers/generate_code.py",
"--declarations-path",
"build/aten/src/ATen/Declarations.yaml",
"--native-functions-path",
"aten/src/ATen/native/native_functions.yaml",
"--nn-path",
"aten/src",
]
)
def generate_build_files() -> None:
update_submodules()
gen_compile_commands()
run_autogen()