build.rs

  1#![allow(clippy::disallowed_methods, reason = "build scripts are exempt")]
  2use std::process::Command;
  3
  4fn main() {
  5    if cfg!(target_os = "macos") {
  6        println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.15.7");
  7
  8        // Weakly link ReplayKit to ensure Zed can be used on macOS 10.15+.
  9        println!("cargo:rustc-link-arg=-Wl,-weak_framework,ReplayKit");
 10
 11        // Seems to be required to enable Swift concurrency
 12        println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/swift");
 13
 14        // Register exported Objective-C selectors, protocols, etc
 15        println!("cargo:rustc-link-arg=-Wl,-ObjC");
 16
 17        // weak link to support Catalina
 18        println!("cargo:rustc-link-arg=-Wl,-weak_framework,ScreenCaptureKit");
 19    }
 20
 21    // Populate git sha environment variable if git is available
 22    println!("cargo:rerun-if-changed=../../.git/logs/HEAD");
 23    println!(
 24        "cargo:rustc-env=TARGET={}",
 25        std::env::var("TARGET").unwrap()
 26    );
 27    if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output()
 28        && output.status.success()
 29    {
 30        let git_sha = String::from_utf8_lossy(&output.stdout);
 31        let git_sha = git_sha.trim();
 32
 33        println!("cargo:rustc-env=ZED_COMMIT_SHA={git_sha}");
 34
 35        if let Some(build_identifier) = option_env!("GITHUB_RUN_NUMBER") {
 36            println!("cargo:rustc-env=ZED_BUILD_ID={build_identifier}");
 37        }
 38
 39        if let Ok(build_profile) = std::env::var("PROFILE")
 40            && build_profile == "release"
 41        {
 42            // This is currently the best way to make `cargo build ...`'s build script
 43            // to print something to stdout without extra verbosity.
 44            println!("cargo::warning=Info: using '{git_sha}' hash for ZED_COMMIT_SHA env var");
 45        }
 46    }
 47
 48    #[cfg(target_os = "windows")]
 49    {
 50        #[cfg(target_env = "msvc")]
 51        {
 52            // todo(windows): This is to avoid stack overflow. Remove it when solved.
 53            println!("cargo:rustc-link-arg=/stack:{}", 8 * 1024 * 1024);
 54        }
 55
 56        if cfg!(target_arch = "x86_64") {
 57            println!("cargo::rerun-if-changed=\\..\\..\\..\\conpty.dll");
 58            println!("cargo::rerun-if-changed=\\..\\..\\..\\OpenConsole.exe");
 59            let conpty_target = std::env::var("OUT_DIR").unwrap() + "\\..\\..\\..\\conpty.dll";
 60            match std::fs::copy("resources/windows/bin/x64/conpty.dll", &conpty_target) {
 61                Ok(_) => println!("Copied conpty.dll to {conpty_target}"),
 62                Err(e) => println!("cargo::warning=Failed to copy conpty.dll: {}", e),
 63            }
 64            let open_console_target =
 65                std::env::var("OUT_DIR").unwrap() + "\\..\\..\\..\\OpenConsole.exe";
 66            match std::fs::copy(
 67                "resources/windows/bin/x64/OpenConsole.exe",
 68                &open_console_target,
 69            ) {
 70                Ok(_) => println!("Copied OpenConsole.exe to {open_console_target}"),
 71                Err(e) => println!("cargo::warning=Failed to copy OpenConsole.exe: {}", e),
 72            }
 73        }
 74
 75        let release_channel = option_env!("RELEASE_CHANNEL").unwrap_or("dev");
 76        let icon = match release_channel {
 77            "stable" => "resources/windows/app-icon.ico",
 78            "preview" => "resources/windows/app-icon-preview.ico",
 79            "nightly" => "resources/windows/app-icon-nightly.ico",
 80            "dev" => "resources/windows/app-icon-dev.ico",
 81            _ => "resources/windows/app-icon-dev.ico",
 82        };
 83        let icon = std::path::Path::new(icon);
 84
 85        println!("cargo:rerun-if-env-changed=RELEASE_CHANNEL");
 86        println!("cargo:rerun-if-changed={}", icon.display());
 87
 88        let mut res = winresource::WindowsResource::new();
 89
 90        // Depending on the security applied to the computer, winresource might fail
 91        // fetching the RC path. Therefore, we add a way to explicitly specify the
 92        // toolkit path, allowing winresource to use a valid RC path.
 93        if let Some(explicit_rc_toolkit_path) = std::env::var("ZED_RC_TOOLKIT_PATH").ok() {
 94            res.set_toolkit_path(explicit_rc_toolkit_path.as_str());
 95        }
 96        res.set_icon(icon.to_str().unwrap());
 97        res.set("FileDescription", "Zed");
 98        res.set("ProductName", "Zed");
 99
100        if let Err(e) = res.compile() {
101            eprintln!("{}", e);
102            std::process::exit(1);
103        }
104    }
105}