|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.9" |
| 4 | +# /// |
| 5 | +"""Build raymake wheels for all supported platforms. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + # Build all platforms (default) |
| 9 | + uv run pypi/raymake/build_wheels.py |
| 10 | +
|
| 11 | + # Build specific platform |
| 12 | + uv run pypi/raymake/build_wheels.py --platform darwin-arm64 |
| 13 | +""" |
| 14 | + |
| 15 | +import argparse |
| 16 | +import os |
| 17 | +import shutil |
| 18 | +import subprocess |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | + |
| 22 | +def get_version_from_git() -> str: |
| 23 | + """Extract version from git tag (e.g., v0.27.0 -> 0.27.0).""" |
| 24 | + try: |
| 25 | + result = subprocess.run( |
| 26 | + ["git", "describe", "--tags", "--abbrev=0"], |
| 27 | + capture_output=True, |
| 28 | + text=True, |
| 29 | + check=True, |
| 30 | + ) |
| 31 | + version = result.stdout.strip().lstrip("v") |
| 32 | + return version |
| 33 | + except subprocess.CalledProcessError: |
| 34 | + return "0.0.0" |
| 35 | + |
| 36 | + |
| 37 | +def write_version_file(script_dir: Path) -> str: |
| 38 | + """Generate VERSION file from git tag.""" |
| 39 | + version = get_version_from_git() |
| 40 | + version_file = script_dir / "VERSION" |
| 41 | + version_file.write_text(f'__version__ = "{version}"\n') |
| 42 | + print(f"Generated VERSION file: {version}") |
| 43 | + return version |
| 44 | + |
| 45 | + |
| 46 | +PLATFORM_MAP = { |
| 47 | + "darwin-arm64": { |
| 48 | + "goos": "darwin", |
| 49 | + "goarch": "arm64", |
| 50 | + "platform": "macosx_12_0_arm64", |
| 51 | + }, |
| 52 | + "linux-amd64": { |
| 53 | + "goos": "linux", |
| 54 | + "goarch": "amd64", |
| 55 | + "platform": "manylinux_2_17_x86_64", |
| 56 | + }, |
| 57 | + "linux-arm64": { |
| 58 | + "goos": "linux", |
| 59 | + "goarch": "arm64", |
| 60 | + "platform": "manylinux_2_17_aarch64", |
| 61 | + }, |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +def build_wheel(platform_key: str, output_dir: Path) -> Path: |
| 66 | + """Build a wheel for the specified platform.""" |
| 67 | + if platform_key not in PLATFORM_MAP: |
| 68 | + raise ValueError( |
| 69 | + f"Unknown platform: {platform_key}. Valid: {list(PLATFORM_MAP.keys())}" |
| 70 | + ) |
| 71 | + |
| 72 | + config = PLATFORM_MAP[platform_key] |
| 73 | + goos = config["goos"] |
| 74 | + goarch = config["goarch"] |
| 75 | + platform_tag = config["platform"] |
| 76 | + |
| 77 | + print(f"\n{'=' * 60}") |
| 78 | + print(f"Building wheel for {platform_key}") |
| 79 | + print(f" GOOS={goos} GOARCH={goarch}") |
| 80 | + print(f" Platform tag: {platform_tag}") |
| 81 | + print(f"{'=' * 60}\n") |
| 82 | + |
| 83 | + # Set environment for cross-compilation |
| 84 | + env = os.environ.copy() |
| 85 | + env["GOOS"] = goos |
| 86 | + env["GOARCH"] = goarch |
| 87 | + env["CGO_ENABLED"] = "0" |
| 88 | + |
| 89 | + # Get the pypi/raymake directory |
| 90 | + script_dir = Path(__file__).parent |
| 91 | + dist_dir = script_dir / "dist" |
| 92 | + |
| 93 | + # Clean dist directory for this build |
| 94 | + if dist_dir.exists(): |
| 95 | + shutil.rmtree(dist_dir) |
| 96 | + |
| 97 | + # Build the wheel |
| 98 | + args = ["uv", "build", "--wheel", f"--config-setting=--plat-name={platform_tag}"] |
| 99 | + subprocess.run(args, check=True, cwd=script_dir, env=env) |
| 100 | + |
| 101 | + # Find the built wheel |
| 102 | + wheels = list(dist_dir.glob("*.whl")) |
| 103 | + if len(wheels) != 1: |
| 104 | + raise RuntimeError(f"Expected 1 wheel in {dist_dir}, but found {len(wheels)}") |
| 105 | + wheel_path = wheels[0] |
| 106 | + |
| 107 | + # Copy to output directory (skip if same location) |
| 108 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 109 | + final_path = output_dir / wheel_path.name |
| 110 | + if wheel_path.resolve() != final_path.resolve(): |
| 111 | + shutil.copy2(wheel_path, final_path) |
| 112 | + print(f"Created: {final_path}") |
| 113 | + |
| 114 | + return final_path |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + parser = argparse.ArgumentParser(description="Build raymake wheels") |
| 119 | + parser.add_argument( |
| 120 | + "--platform", |
| 121 | + choices=list(PLATFORM_MAP.keys()) + ["all"], |
| 122 | + default="all", |
| 123 | + help="Platform to build (default: all)", |
| 124 | + ) |
| 125 | + parser.add_argument( |
| 126 | + "--output-dir", |
| 127 | + type=Path, |
| 128 | + default=Path(__file__).parent.parent.parent / "_release", |
| 129 | + help="Output directory for wheels (default: _release/)", |
| 130 | + ) |
| 131 | + args = parser.parse_args() |
| 132 | + |
| 133 | + if args.platform == "all": |
| 134 | + platforms = list(PLATFORM_MAP.keys()) |
| 135 | + else: |
| 136 | + platforms = [args.platform] |
| 137 | + |
| 138 | + # Generate VERSION file from git tag |
| 139 | + script_dir = Path(__file__).parent |
| 140 | + write_version_file(script_dir) |
| 141 | + |
| 142 | + print(f"Building wheels for: {', '.join(platforms)}") |
| 143 | + print(f"Output directory: {args.output_dir}") |
| 144 | + |
| 145 | + built_wheels = [] |
| 146 | + for platform in platforms: |
| 147 | + wheel_path = build_wheel(platform, args.output_dir) |
| 148 | + built_wheels.append(wheel_path) |
| 149 | + |
| 150 | + print(f"\n{'=' * 60}") |
| 151 | + print("Build complete! Created wheels:") |
| 152 | + for wheel in built_wheels: |
| 153 | + print(f" {wheel}") |
| 154 | + print(f"{'=' * 60}\n") |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + main() |
0 commit comments