|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Tool quickly rebuild one or two files with debug info |
| 3 | +# Mimics following behavior: |
| 4 | +# - touch file |
| 5 | +# - ninja -j1 -v -n torch_python | sed -e 's/-O[23]/-g/g' -e 's#\[[0-9]\+\/[0-9]\+\] \+##' |sh |
| 6 | +# - Copy libs from build/lib to torch/lib folder |
| 7 | + |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | +from typing import Any, List, Optional, Tuple |
| 12 | + |
| 13 | +PYTORCH_ROOTDIR = Path(__file__).resolve().parent.parent |
| 14 | +TORCH_DIR = PYTORCH_ROOTDIR / "torch" |
| 15 | +TORCH_LIB_DIR = TORCH_DIR / "lib" |
| 16 | +BUILD_DIR = PYTORCH_ROOTDIR / "build" |
| 17 | +BUILD_LIB_DIR = BUILD_DIR / "lib" |
| 18 | + |
| 19 | + |
| 20 | +def check_output(args: List[str], cwd: Optional[str] = None) -> str: |
| 21 | + return subprocess.check_output(args, cwd=cwd).decode("utf-8") |
| 22 | + |
| 23 | + |
| 24 | +def parse_args() -> Any: |
| 25 | + from argparse import ArgumentParser |
| 26 | + |
| 27 | + parser = ArgumentParser(description="Incremental build PyTorch with debinfo") |
| 28 | + parser.add_argument("--verbose", action="store_true") |
| 29 | + parser.add_argument("files", nargs="?", action="append") |
| 30 | + return parser.parse_args() |
| 31 | + |
| 32 | + |
| 33 | +def get_lib_extension() -> str: |
| 34 | + if sys.platform == "linux": |
| 35 | + return "so" |
| 36 | + if sys.platform == "darwin": |
| 37 | + return "dylib" |
| 38 | + raise RuntimeError(f"Usupported platform {sys.platform}") |
| 39 | + |
| 40 | + |
| 41 | +def create_symlinks() -> None: |
| 42 | + """Creates symlinks from build/lib to torch/lib""" |
| 43 | + if not TORCH_LIB_DIR.exists(): |
| 44 | + raise RuntimeError(f"Can't create symlinks as {TORCH_LIB_DIR} does not exist") |
| 45 | + if not BUILD_LIB_DIR.exists(): |
| 46 | + raise RuntimeError(f"Can't create symlinks as {BUILD_LIB_DIR} does not exist") |
| 47 | + for torch_lib in TORCH_LIB_DIR.glob(f"*.{get_lib_extension()}"): |
| 48 | + if torch_lib.is_symlink(): |
| 49 | + continue |
| 50 | + build_lib = BUILD_LIB_DIR / torch_lib.name |
| 51 | + if not build_lib.exists(): |
| 52 | + raise RuntimeError(f"Can't find {build_lib} corresponding to {torch_lib}") |
| 53 | + torch_lib.unlink() |
| 54 | + torch_lib.symlink_to(build_lib) |
| 55 | + |
| 56 | + |
| 57 | +def has_build_ninja() -> bool: |
| 58 | + return (BUILD_DIR / "build.ninja").exists() |
| 59 | + |
| 60 | + |
| 61 | +def is_devel_setup() -> bool: |
| 62 | + output = check_output([sys.executable, "-c", "import torch;print(torch.__file__)"]) |
| 63 | + return output.strip() == str(TORCH_DIR / "__init__.py") |
| 64 | + |
| 65 | + |
| 66 | +def create_build_plan() -> List[Tuple[str, str]]: |
| 67 | + output = check_output( |
| 68 | + ["ninja", "-j1", "-v", "-n", "torch_python"], cwd=str(BUILD_DIR) |
| 69 | + ) |
| 70 | + rc = [] |
| 71 | + for line in output.split("\n"): |
| 72 | + if not line.startswith("["): |
| 73 | + continue |
| 74 | + line = line.split("]", 1)[1].strip() |
| 75 | + if line.startswith(": &&") and line.endswith("&& :"): |
| 76 | + line = line[4:-4] |
| 77 | + line = line.replace("-O2", "-g").replace("-O3", "-g") |
| 78 | + name = line.split("-o ", 1)[1].split(" ")[0] |
| 79 | + rc.append((name, line)) |
| 80 | + return rc |
| 81 | + |
| 82 | + |
| 83 | +def main() -> None: |
| 84 | + if sys.platform == "win32": |
| 85 | + print("Not supported on Windows yet") |
| 86 | + sys.exit(-95) |
| 87 | + if not is_devel_setup(): |
| 88 | + print( |
| 89 | + "Not a devel setup of PyTorch, please run `python3 setup.py develop --user` first" |
| 90 | + ) |
| 91 | + sys.exit(-1) |
| 92 | + if not has_build_ninja(): |
| 93 | + print("Only ninja build system is supported at the moment") |
| 94 | + sys.exit(-1) |
| 95 | + args = parse_args() |
| 96 | + for file in args.files: |
| 97 | + if file is None: |
| 98 | + continue |
| 99 | + Path(file).touch() |
| 100 | + build_plan = create_build_plan() |
| 101 | + if len(build_plan) == 0: |
| 102 | + return print("Nothing to do") |
| 103 | + if len(build_plan) > 100: |
| 104 | + print("More than 100 items needs to be rebuild, run `ninja torch_python` first") |
| 105 | + sys.exit(-1) |
| 106 | + for idx, (name, cmd) in enumerate(build_plan): |
| 107 | + print(f"[{idx + 1 } / {len(build_plan)}] Building {name}") |
| 108 | + if args.verbose: |
| 109 | + print(cmd) |
| 110 | + subprocess.check_call(["sh", "-c", cmd], cwd=BUILD_DIR) |
| 111 | + create_symlinks() |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments