|
| 1 | +#!/usr/bin/env python |
| 2 | +r"""Builds production UCCL wheel based on the rocm wheels. |
| 3 | +
|
| 4 | +The UCCL project already has a TheRock build target that is currently |
| 5 | +based on the nightly wheels. |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +from datetime import date |
| 10 | +import json |
| 11 | +import os |
| 12 | +from pathlib import Path |
| 13 | +import platform |
| 14 | +import re |
| 15 | +import shutil |
| 16 | +import shlex |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | +import tempfile |
| 20 | +import textwrap |
| 21 | + |
| 22 | +script_dir = Path(__file__).resolve().parent |
| 23 | + |
| 24 | +is_windows = platform.system() == "Windows" |
| 25 | + |
| 26 | +def exec(args: list[str | Path], cwd: Path, env: dict[str, str] | None = None): |
| 27 | + args = [str(arg) for arg in args] |
| 28 | + full_env = dict(os.environ) |
| 29 | + print(f"++ Exec [{cwd}]$ {shlex.join(args)}") |
| 30 | + if env: |
| 31 | + print(f":: Env:") |
| 32 | + for k, v in env.items(): |
| 33 | + print(f" {k}={v}") |
| 34 | + full_env.update(env) |
| 35 | + subprocess.check_call(args, cwd=str(cwd), env=full_env) |
| 36 | + |
| 37 | + |
| 38 | +def copy_to_output(args: argparse.Namespace, src_file: Path): |
| 39 | + output_dir: Path = args.output_dir |
| 40 | + print(f"++ Copy {src_file} -> {output_dir}") |
| 41 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 42 | + shutil.copy2(src_file, output_dir) |
| 43 | + |
| 44 | + |
| 45 | +def find_built_wheel(dist_dir: Path, dist_package: str) -> Path: |
| 46 | + dist_package = dist_package.replace("-", "_") |
| 47 | + glob = f"{dist_package}-*.whl" |
| 48 | + all_wheels = list(dist_dir.glob(glob)) |
| 49 | + if not all_wheels: |
| 50 | + raise RuntimeError(f"No wheels matching '{glob}' found in {dist_dir}") |
| 51 | + if len(all_wheels) != 1: |
| 52 | + raise RuntimeError(f"Found multiple wheels matching '{glob}' in {dist_dir}") |
| 53 | + return all_wheels[0] |
| 54 | + |
| 55 | + |
| 56 | +def do_build(args: argparse.Namespace): |
| 57 | + uccl_dir: Path = args.uccl_dir |
| 58 | + |
| 59 | + if is_windows: |
| 60 | + print("WARNING: Unsure if UCCL builds on Windows.", file=sys.stderr) |
| 61 | + |
| 62 | + if not uccl_dir.exists(): |
| 63 | + exec(["git", "clone", "--recursive", "https://github.com/uccl-project/uccl.git", str(uccl_dir)], |
| 64 | + cwd=script_dir) |
| 65 | + |
| 66 | + exec(["./build.sh", "therock", "all", args.python_version, args.index_url], |
| 67 | + cwd=uccl_dir) |
| 68 | + |
| 69 | + built_wheel = find_built_wheel(uccl_dir / "wheelhouse-therock", "uccl") |
| 70 | + print(f"Found built wheel: {built_wheel}") |
| 71 | + copy_to_output(args, built_wheel) |
| 72 | + |
| 73 | +def main(argv: list[str]): |
| 74 | + p = argparse.ArgumentParser(prog="build_prod_wheels.py") |
| 75 | + |
| 76 | + p.add_argument( |
| 77 | + "--output-dir", |
| 78 | + type=Path, |
| 79 | + required=True, |
| 80 | + help="Directory to copy built wheels to", |
| 81 | + ) |
| 82 | + p.add_argument( |
| 83 | + "--uccl-dir", |
| 84 | + default=Path(script_dir / "uccl"), |
| 85 | + type=Path, |
| 86 | + help="UCCL source directory", |
| 87 | + ) |
| 88 | + p.add_argument( |
| 89 | + "--python-version", |
| 90 | + default=".".join(platform.python_version_tuple()[:2]), |
| 91 | + type=str, |
| 92 | + help="Python version to use for the build", |
| 93 | + ) |
| 94 | + p.add_argument( |
| 95 | + "--index-url", |
| 96 | + required=True, |
| 97 | + help="Base URL of the Python Package Index." |
| 98 | + ) |
| 99 | + |
| 100 | + args = p.parse_args(argv) |
| 101 | + do_build(args) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + main(sys.argv[1:]) |
0 commit comments