xref: /aosp_15_r20/external/pytorch/tools/linter/clang_tidy/generate_build_files.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1from __future__ import annotations
2
3import os
4import subprocess
5import sys
6
7
8def run_cmd(cmd: list[str]) -> None:
9    print(f"Running: {cmd}")
10    result = subprocess.run(
11        cmd,
12        capture_output=True,
13    )
14    stdout, stderr = (
15        result.stdout.decode("utf-8").strip(),
16        result.stderr.decode("utf-8").strip(),
17    )
18    print(stdout)
19    print(stderr)
20    if result.returncode != 0:
21        print(f"Failed to run {cmd}")
22        sys.exit(1)
23
24
25def update_submodules() -> None:
26    run_cmd(["git", "submodule", "update", "--init", "--recursive"])
27
28
29def gen_compile_commands() -> None:
30    os.environ["USE_NCCL"] = "0"
31    os.environ["USE_PRECOMPILED_HEADERS"] = "1"
32    os.environ["CC"] = "clang"
33    os.environ["CXX"] = "clang++"
34    run_cmd([sys.executable, "setup.py", "--cmake-only", "build"])
35
36
37def run_autogen() -> None:
38    run_cmd(
39        [
40            sys.executable,
41            "-m",
42            "torchgen.gen",
43            "-s",
44            "aten/src/ATen",
45            "-d",
46            "build/aten/src/ATen",
47            "--per-operator-headers",
48        ]
49    )
50
51    run_cmd(
52        [
53            sys.executable,
54            "tools/setup_helpers/generate_code.py",
55            "--native-functions-path",
56            "aten/src/ATen/native/native_functions.yaml",
57            "--tags-path",
58            "aten/src/ATen/native/tags.yaml",
59            "--gen-lazy-ts-backend",
60        ]
61    )
62
63
64def generate_build_files() -> None:
65    update_submodules()
66    gen_compile_commands()
67    run_autogen()
68
69
70if __name__ == "__main__":
71    generate_build_files()
72