#!/usr/bin/env python3 """Measures the weight of candidate dependencies for one platform. Usage: tools/measure_deps.py "serde" "gltf --no-default-features" ... Each argument is passed to `cargo add` in a throwaway project, so the numbers are reproducible. Only crates that build on TARGET are counted. Output: a Markdown table row per dependency. """ import json import pathlib import shlex import subprocess import sys import tempfile TARGET = "x86_64-unknown-linux-gnu" def run(args, cwd): return subprocess.run(args, cwd=cwd, check=True, capture_output=True, text=True).stdout def measure(spec): with tempfile.TemporaryDirectory() as tmp: run(["cargo", "init", "--lib", "--name", "probe", "--quiet"], tmp) run(["cargo", "add", "--quiet", *shlex.split(spec)], tmp) run(["cargo", "fetch", "--quiet"], tmp) meta = json.loads(run(["cargo", "metadata", "--format-version", "1", "--filter-platform", TARGET], tmp)) root = meta["resolve"]["root"] packages = {p["id"]: p for p in meta["packages"]} crates = size = lines = 0 for node in meta["resolve"]["nodes"]: if node["id"] == root: continue crates += 1 folder = pathlib.Path(packages[node["id"]]["manifest_path"]).parent for path in folder.rglob("*"): if path.is_file(): size += path.stat().st_size if path.suffix == ".rs": with open(path, "rb") as handle: lines += sum(1 for _ in handle) return crates, size / 1048576, lines / 1000 def main(): print(f"Platform: {TARGET}\n") print("| Dependency | Crates | Source, MB | Rust lines, k |") print("|---|---|---|---|") for spec in sys.argv[1:]: try: crates, megabytes, kilolines = measure(spec) print(f"| `{spec}` | {crates} | {megabytes:.1f} | {kilolines:.0f} |") except subprocess.CalledProcessError as error: print(f"| `{spec}` | failed: {error.stderr.strip().splitlines()[-1] if error.stderr else error} | | |") if __name__ == "__main__": main()