#!/usr/bin/env python3
"""Print the directory cargo uses for build artefacts."""

import json
import os
import subprocess
import sys

# Resolve the workspace manifest from this script's location so the answer is
# independent of the caller's working directory.
repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
manifest = os.path.join(repo_root, "Cargo.toml")

result = subprocess.run(
    [
        "cargo", "metadata", "--format-version", "1", "--no-deps", "--offline",
        "--manifest-path", manifest,
    ],
    capture_output=True,
    text=True,
)
if result.returncode != 0:
    sys.exit(result.stderr.strip() or "cargo metadata failed")
print(json.loads(result.stdout)["target_directory"])
