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