mumu-string 0.1.0

String functions and tools plugin for the Lava language
Documentation
// FILE: string/build.rs

use std::env;
use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;

fn main() {
    // 1) Figure out where the compiled cdylib ends up.
    //    Typically in target/{profile}/ with name like libmumustring.so.
    let out_dir = env::var("OUT_DIR").unwrap();
    let _profile = env::var("PROFILE").unwrap_or_else(|_| "release".to_string());

    // Usually, OUT_DIR = ".../target/release/build/string-<hash>/out".
    // We’ll go up 2 levels to get ".../target/release".
    let mut artifacts_dir = PathBuf::from(&out_dir);
    artifacts_dir.pop(); // now build/
    artifacts_dir.pop(); // now release/

    // The actual plugin library file:
    // On Linux => "libmumustring.so"
    // On macOS => "libmumustring.dylib"
    // On Windows => "mumustring.dll"
    #[cfg(target_os = "windows")]
    let lib_filename = "mumustring.dll";

    #[cfg(target_os = "macos")]
    let lib_filename = "libmumustring.dylib";

    #[cfg(all(not(windows), not(target_os = "macos")))]
    let lib_filename = "libmumustring.so";

    let lib_path = artifacts_dir.join(&lib_filename);

    // 2) We want a symlink/copy named "libstring.so" (or "libstring.dylib"/"string.dll").
    // So we just replace "mumustring" -> "string" in the filename:
    let link_filename = lib_filename.replace("mumustring", "string");
    let link_path = artifacts_dir.join(&link_filename);

    // 3) Remove any leftover link from a previous build
    let _ = fs::remove_file(&link_path);

    // 4) Attempt to create a symlink (Unix) or copy (elsewhere).
    #[cfg(unix)]
    {
        match std::os::unix::fs::symlink(&lib_path, &link_path) {
            Ok(_) => {
                println!("cargo:warning=Created symlink {:?} -> {:?}", link_filename, lib_filename);
            }
            Err(e) => {
                if e.kind() == ErrorKind::Unsupported {
                    println!("cargo:warning=Symlink not supported => copying instead.");
                    fs::copy(&lib_path, &link_path)
                        .expect("Failed to copy plugin library for string => rename");
                } else {
                    panic!("Failed to symlink for string plugin: {:?}", e);
                }
            }
        }
    }

    #[cfg(not(unix))]
    {
        println!("cargo:warning=On this platform, copying instead of symlink.");
        fs::copy(&lib_path, &link_path)
            .expect("Failed to copy plugin library for string => rename");
    }
}