build.rs

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