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 Ok(build_profile) = std::env::var("PROFILE")
36            && build_profile == "release"
37        {
38            // This is currently the best way to make `cargo build ...`'s build script
39            // to print something to stdout without extra verbosity.
40            println!("cargo:warning=Info: using '{git_sha}' hash for ZED_COMMIT_SHA env var");
41        }
42    }
43
44    #[cfg(target_os = "windows")]
45    {
46        #[cfg(target_env = "msvc")]
47        {
48            // todo(windows): This is to avoid stack overflow. Remove it when solved.
49            println!("cargo:rustc-link-arg=/stack:{}", 8 * 1024 * 1024);
50        }
51
52        let release_channel = option_env!("RELEASE_CHANNEL").unwrap_or("dev");
53        let icon = match release_channel {
54            "stable" => "resources/windows/app-icon.ico",
55            "preview" => "resources/windows/app-icon-preview.ico",
56            "nightly" => "resources/windows/app-icon-nightly.ico",
57            "dev" => "resources/windows/app-icon-dev.ico",
58            _ => "resources/windows/app-icon-dev.ico",
59        };
60        let icon = std::path::Path::new(icon);
61
62        println!("cargo:rerun-if-env-changed=RELEASE_CHANNEL");
63        println!("cargo:rerun-if-changed={}", icon.display());
64
65        let mut res = winresource::WindowsResource::new();
66
67        // Depending on the security applied to the computer, winresource might fail
68        // fetching the RC path. Therefore, we add a way to explicitly specify the
69        // toolkit path, allowing winresource to use a valid RC path.
70        if let Some(explicit_rc_toolkit_path) = std::env::var("ZED_RC_TOOLKIT_PATH").ok() {
71            res.set_toolkit_path(explicit_rc_toolkit_path.as_str());
72        }
73        res.set_icon(icon.to_str().unwrap());
74        res.set("FileDescription", "Zed");
75        res.set("ProductName", "Zed");
76
77        if let Err(e) = res.compile() {
78            eprintln!("{}", e);
79            std::process::exit(1);
80        }
81    }
82}