cp2k-libtorch-sys 0.3.2

Prebuilt libtorch (CPU) build input for cp2k-rs Skala support
//! Provides the prebuilt libtorch (PyTorch C++ runtime) as a build input for
//! GauXC's ONEDFT / Skala support, exported via `cargo:root`
//! (DEP_CP2K_LIBTORCH_ROOT).
//!
//! libtorch ships only as prebuilt shared libraries (no supported static
//! build). We download the official CPU package — version + cxx11-abi variant
//! kept in sync with CP2K 2026.2's `install_libtorch.sh` (2.7.1) — and export
//! its prefix. GauXC links it dynamically. The `.so` files are **not** shipped
//! with the worker: at runtime the worker resolves libtorch from the user's
//! system / venv torch (see `cp2k_rs._fetch`), which is also what selects the
//! matching worker variant.
//!
//! For the GPU cells (later) the URL/variant switches to a CUDA/ROCm libtorch
//! keyed off the target; the mechanism is identical.

use cp2k_build_utils as bu;
use std::process::Command;

const LIBTORCH_VER: &str = "2.7.1";
// libtorch-cxx11-abi-shared-with-deps-2.7.1+cpu.zip from download.pytorch.org.
const LIBTORCH_SHA256: &str = "63d572598c8d532128a335018913e795c1bbb32602ce378896dc8cfbb5590976";

fn main() {
    println!("cargo:rerun-if-changed=build.rs");

    let stable_dir = bu::stable_dir("libtorch");
    // The zip extracts a single top-level `libtorch/` directory.
    let install_dir = stable_dir.join("libtorch");

    if install_dir.join("lib").join("libtorch.so").exists() {
        println!(
            "cargo:warning=libtorch already present at {}",
            install_dir.display()
        );
        bu::emit_root(&install_dir);
        return;
    }

    println!("cargo:warning=Downloading libtorch {LIBTORCH_VER} (CPU, cxx11-abi)...");
    let zip = stable_dir.join(format!(
        "libtorch-cxx11-abi-shared-with-deps-{LIBTORCH_VER}+cpu.zip"
    ));
    bu::download(
        &format!(
            "https://download.pytorch.org/libtorch/cpu/\
             libtorch-cxx11-abi-shared-with-deps-{LIBTORCH_VER}%2Bcpu.zip"
        ),
        &zip,
    );
    bu::verify_sha256(&zip, LIBTORCH_SHA256);

    println!("cargo:warning=Extracting libtorch...");
    bu::run(
        Command::new("unzip")
            .args(["-q", "-o", zip.to_str().unwrap(), "-d"])
            .arg(stable_dir.to_str().unwrap()),
        "libtorch extract",
    );

    println!("cargo:warning=libtorch ready at {}", install_dir.display());
    bu::emit_root(&install_dir);
}