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
use std::path::Path;

/// The resources of one program: RustCord's icon, the manifest, and the
/// details Windows shows for the file (Properties, Task Manager).
fn resources(out: &Path, manifest_dir: &Path, bin: &str, description: &str) {
    // (Forward slashes: a backslash is an escape in a .rc string.)
    let path = |p: &Path| p.display().to_string().replace('\\', "/");
    let version = std::env::var("CARGO_PKG_VERSION").unwrap_or_default();
    let part = |name: &str| std::env::var(name).ok().and_then(|v| v.parse::<u16>().ok()).unwrap_or(0);
    let numbers = format!("{},{},{},0", part("CARGO_PKG_VERSION_MAJOR"), part("CARGO_PKG_VERSION_MINOR"), part("CARGO_PKG_VERSION_PATCH"));
    let rc = format!(
        r#"1 ICON "{icon}"
1 24 "{manifest}"
1 VERSIONINFO
FILEVERSION {numbers}
PRODUCTVERSION {numbers}
FILEOS 0x40004L
FILETYPE 0x1L
BEGIN
  BLOCK "StringFileInfo"
  BEGIN
    BLOCK "040904b0"
    BEGIN
      VALUE "CompanyName", "RustCord"
      VALUE "FileDescription", "{description}"
      VALUE "FileVersion", "{version}"
      VALUE "ProductName", "RustCord"
      VALUE "ProductVersion", "{version}"
    END
  END
  BLOCK "VarFileInfo"
  BEGIN
    VALUE "Translation", 0x409, 1200
  END
END
"#,
        icon = path(&manifest_dir.join("../rustcord/assets/rustcord.ico")),
        manifest = path(&manifest_dir.join("setup.manifest")),
    );
    let file = out.join(format!("{bin}.rc"));
    std::fs::write(&file, rc).expect("writing the resource script");
    // The manifest matters: without it Windows asks for administrator rights
    // (it does that for any .exe named "setup" or "uninstall"), and the
    // programs' windows don't exist in the old common controls it would load.
    embed_resource::compile_for(&file, [bin], embed_resource::NONE);
}

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=setup.manifest");
    println!("cargo:rerun-if-changed=../rustcord/assets/rustcord.ico");
    let manifest_dir = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default());
    if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
        let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap_or_default());
        resources(&out, &manifest_dir, "rustcord-setup", "RustCord Setup");
        resources(&out, &manifest_dir, "rustcord-uninstall", "RustCord Uninstaller");
        // Windows looks for a program's DLLs in the program's own folder first. An
        // installer runs from Downloads, where anything may have been saved next
        // to it under a system DLL's name: have the DLLs this program links to
        // loaded from System32 only (the loader reads this flag from the .exe).
        if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") {
            println!("cargo:rustc-link-arg-bins=/DEPENDENTLOADFLAG:0x800");
        }
    }

    // The installer carries the release rustcord.exe and the uninstaller,
    // compressed by rustcord-pack. They are built first (see
    // build-installer.cmd); without them, or with ones older than the programs
    // they were made from, the workspace still builds and the setup program
    // just says what is missing.
    println!("cargo::rustc-check-cfg=cfg(has_payload)");
    let release = manifest_dir.join("..").join("..").join("target").join("release");
    let modified = |p: &Path| std::fs::metadata(p).and_then(|m| m.modified()).ok();
    let mut complete = true;
    for name in ["rustcord.exe", "rustcord-uninstall.exe"] {
        let (program, packed) = (release.join(name), release.join(format!("{name}.gz")));
        println!("cargo:rerun-if-changed={}", program.display());
        println!("cargo:rerun-if-changed={}", packed.display());
        complete &= matches!((modified(&program), modified(&packed)), (Some(program), Some(packed)) if packed >= program);
    }
    if complete {
        println!("cargo:rustc-cfg=has_payload");
    }
}