Sign in Sign up
kretrod/lodestone Public
Branches
master
38 lines (34 loc) · 1.3 KB Raw
//! Embed the client library into the loader so what ships is a single file.
//!
//! The library is a sibling build artefact rather than a source file, so this
//! points `include_bytes!` at it and re-runs when it changes. If it has not
//! been built yet an empty placeholder is emitted instead, which keeps the
//! loader compiling on its own — it simply falls back to a path argument.

use std::path::PathBuf;

fn main() {
    let out = PathBuf::from(std::env::var("OUT_DIR").unwrap());
    let target = std::env::var("TARGET").unwrap_or_default();

    // A cdylib is a .dll on Windows and a lib*.so on Linux.
    let artifact = if target.contains("windows") {
        "lodestone_client.dll"
    } else {
        "liblodestone_client.so"
    };
    let lib = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../target")
        .join(&target)
        .join("release")
        .join(artifact);

    println!("cargo:rerun-if-changed={}", lib.display());
    let embedded = out.join("client.bin");
    match std::fs::read(&lib) {
        Ok(bytes) => {
            std::fs::write(&embedded, bytes).unwrap();
            println!("cargo:rustc-cfg=has_embedded_client");
        }
        Err(_) => {
            std::fs::write(&embedded, []).unwrap();
        }
    }
    println!("cargo:rustc-env=LODESTONE_CLIENT_DLL={}", embedded.display());
}