fastatomstruct 1.5.1

A Python package for the analysis of atomic structures and dynamics
"""
Build script for fastatomstruct that includes the viewer binary.
This is called by maturin during the build process.
"""

import subprocess
import sys
import os
from pathlib import Path


def build_viewer():
    """Build the frontend WASM and viewer binary if not already built."""
    project_root = Path(__file__).parent

    # Check if we should build the viewer
    # Skip if SKIP_VIEWER_BUILD environment variable is set
    if os.environ.get("SKIP_VIEWER_BUILD"):
        print("Skipping viewer build (SKIP_VIEWER_BUILD is set)")
        return

    print("=" * 80)
    print("Building fastatomstruct-viewer binary...")
    print("=" * 80)

    # Determine the target from environment (set by maturin/cargo)
    target = os.environ.get("CARGO_BUILD_TARGET", "")

    # Build the frontend WASM first
    print("\n[1/2] Building frontend WASM...")
    wasm_cmd = [
        "cargo",
        "build",
        "--release",
        "--target",
        "wasm32-unknown-unknown",
        "--package",
        "fastatomstruct_frontend",
    ]
    try:
        subprocess.run(wasm_cmd, check=True, cwd=project_root)
        print("✓ Frontend WASM built successfully")
    except subprocess.CalledProcessError as e:
        print(f"✗ Failed to build frontend WASM: {e}", file=sys.stderr)
        print(
            "  The Python package will be built without the viewer binary.",
            file=sys.stderr,
        )
        return

    # Build the viewer binary
    print("\n[2/2] Building viewer binary...")
    viewer_cmd = ["cargo", "build", "--release", "--bin", "fastatomstruct-viewer"]

    if target:
        viewer_cmd.extend(["--target", target])
        print(f"Building for target: {target}")

    try:
        subprocess.run(viewer_cmd, check=True, cwd=project_root)
        print("✓ Viewer binary built successfully")
        print("=" * 80)
    except subprocess.CalledProcessError as e:
        print(f"✗ Failed to build viewer binary: {e}", file=sys.stderr)
        print(
            "  The Python package will be built without the viewer binary.",
            file=sys.stderr,
        )


if __name__ == "__main__":
    build_viewer()