Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

3.9 KB
Raw Blame
fn main() {
    // Give the Windows .exe RustCord's icon.
    println!("cargo:rerun-if-changed=rustcord.rc");
    println!("cargo:rerun-if-changed=assets/rustcord.ico");
    if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
        // The version in the exe's file properties is the workspace's: the
        // .rc is a template, filled in here and compiled from OUT_DIR.
        let env = |k: &str| std::env::var(k).unwrap_or_default();
        let part = |k: &str| env(k).parse::<u16>().unwrap_or(0);
        let commas = format!("{},{},{},0", part("CARGO_PKG_VERSION_MAJOR"), part("CARGO_PKG_VERSION_MINOR"), part("CARGO_PKG_VERSION_PATCH"));
        let icon = std::path::Path::new(&env("CARGO_MANIFEST_DIR")).join("assets").join("rustcord.ico").display().to_string().replace('\\', "/");
        if let Ok(template) = std::fs::read_to_string("rustcord.rc") {
            let rc = template.replace("@ICON@", &icon).replace("@VERSION_COMMAS@", &commas).replace("@VERSION@", &env("CARGO_PKG_VERSION"));
            let path = std::path::Path::new(&env("OUT_DIR")).join("rustcord.rc");
            if std::fs::write(&path, rc).is_ok() {
                let _ = embed_resource::compile(&path, embed_resource::NONE);
            }
        }
    }

    // Windows looks for a program's DLLs in the program's own folder first:
    // have the DLLs RustCord links to loaded from System32 only, so a file
    // dropped next to the .exe under a system DLL's name is never run (the
    // loader reads this flag from the .exe).
    if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") && std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") {
        println!("cargo:rustc-link-arg-bins=/DEPENDENTLOADFLAG:0x800");
    }

    // rrsans (gg sans, Discord's font) is compiled into the exe from fonts/rrsans.
    // Without those files RustCord still builds, with Segoe UI.
    println!("cargo::rustc-check-cfg=cfg(rustcord_gg_sans)");
    println!("cargo:rerun-if-changed=fonts/rrsans");
    let dir = std::path::Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default()).join("fonts").join("rrsans");
    let needed = ["rrsans.ttf", "rrsanssemibold.ttf", "rrsansmono.ttf", "rrsansmedium.ttf", "rrsansbold.ttf", "rrsansitalic.ttf", "rrsansbolditalic.ttf"];
    for f in needed {
        println!("cargo:rerun-if-changed=fonts/rrsans/{f}");
    }
    if needed.iter().all(|f| dir.join(f).is_file()) {
        println!("cargo:rustc-cfg=rustcord_gg_sans");
    }

    // Discord's display-name fonts (Nitro "Display Name Styles"), from
    // fonts/dns/dns<font_id>-<Name>.ttf|otf: a table of (font id, bytes).
    println!("cargo:rerun-if-changed=fonts/dns");
    let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
    let dns = std::path::Path::new(&manifest).join("fonts").join("dns");
    let mut rows = Vec::new();
    if let Ok(entries) = std::fs::read_dir(&dns) {
        let mut files: Vec<_> = entries.flatten().map(|e| e.path()).collect();
        files.sort();
        for path in files {
            let Some(stem) = path.file_stem().and_then(|s| s.to_str()) else { continue };
            let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("");
            if !matches!(ext, "ttf" | "otf") {
                continue;
            }
            let Some(id) = stem.strip_prefix("dns").and_then(|r| r.split('-').next()).and_then(|n| n.parse::<u32>().ok()) else { continue };
            println!("cargo:rerun-if-changed={}", path.display());
            rows.push(format!("    ({id}, include_bytes!({:?})),", path.display().to_string()));
        }
    }
    let out = std::path::Path::new(&std::env::var("OUT_DIR").unwrap_or_default()).join("dns_fonts.rs");
    let code = format!("/// Display-name fonts by Discord font id (generated by build.rs).\npub const DNS_FONTS: &[(u32, &[u8])] = &[\n{}\n];\n", rows.join("\n"));
    let _ = std::fs::write(out, code);
}