forked from zed-industries/zed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
200 lines (174 loc) · 8.55 KB
/
Copy pathbuild.rs
File metadata and controls
200 lines (174 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#![allow(clippy::disallowed_methods, reason = "build scripts are exempt")]
use std::process::Command;
fn main() {
if cfg!(target_os = "macos") {
println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.15.7");
// Weakly link ReplayKit to ensure Zed can be used on macOS 10.15+.
println!("cargo:rustc-link-arg=-Wl,-weak_framework,ReplayKit");
// Seems to be required to enable Swift concurrency
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/swift");
// Register exported Objective-C selectors, protocols, etc
println!("cargo:rustc-link-arg=-Wl,-ObjC");
// weak link to support Catalina
println!("cargo:rustc-link-arg=-Wl,-weak_framework,ScreenCaptureKit");
}
// Populate git sha environment variable if git is available
println!("cargo:rerun-if-changed=../../.git/logs/HEAD");
println!(
"cargo:rustc-env=TARGET={}",
std::env::var("TARGET").unwrap()
);
if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output()
&& output.status.success()
{
let git_sha = String::from_utf8_lossy(&output.stdout);
let git_sha = git_sha.trim();
println!("cargo:rustc-env=ZED_COMMIT_SHA={git_sha}");
if let Some(build_identifier) = option_env!("GITHUB_RUN_NUMBER") {
println!("cargo:rustc-env=ZED_BUILD_ID={build_identifier}");
}
if let Ok(build_profile) = std::env::var("PROFILE")
&& build_profile == "release"
{
// This is currently the best way to make `cargo build ...`'s build script
// to print something to stdout without extra verbosity.
println!("cargo::warning=Info: using '{git_sha}' hash for ZED_COMMIT_SHA env var");
}
}
#[cfg(target_os = "windows")]
{
#[cfg(target_env = "msvc")]
{
// todo(windows): This is to avoid stack overflow. Remove it when solved.
println!("cargo:rustc-link-arg=/stack:{}", 8 * 1024 * 1024);
}
if cfg!(target_arch = "x86_64") || cfg!(target_arch = "aarch64") {
let out_dir = std::env::var("OUT_DIR").unwrap();
let out_dir: &std::path::Path = out_dir.as_ref();
let target_dir = std::path::Path::new(&out_dir)
.parent()
.and_then(|p| p.parent())
.and_then(|p| p.parent())
.expect("Failed to find target directory");
let conpty_dll_target = target_dir.join("conpty.dll");
let open_console_target = target_dir.join("OpenConsole.exe");
let conpty_url = "https://github.com/microsoft/terminal/releases/download/v1.23.13503.0/Microsoft.Windows.Console.ConPTY.1.23.251216003.nupkg";
let nupkg_path = out_dir.join("conpty.nupkg.zip");
let extract_dir = out_dir.join("conpty");
let download_script = format!(
"$ProgressPreference = 'SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '{}' -OutFile '{}'",
conpty_url,
nupkg_path.display()
);
let download_result = Command::new("powershell")
.args([
"-NoProfile",
"-NonInteractive",
"-Command",
&download_script,
])
.output();
match download_result {
Ok(output) if output.status.success() => {
println!("Downloaded conpty nupkg successfully");
let extract_script = format!(
"$ProgressPreference = 'SilentlyContinue'; Expand-Archive -Path '{}' -DestinationPath '{}' -Force",
nupkg_path.display(),
extract_dir.display()
);
let extract_result = Command::new("powershell")
.args(["-NoProfile", "-NonInteractive", "-Command", &extract_script])
.output();
match extract_result {
Ok(output) if output.status.success() => {
let (conpty_dll_source, open_console_source) =
if cfg!(target_arch = "x86_64") {
(
extract_dir.join("runtimes/win-x64/native/conpty.dll"),
extract_dir
.join("build/native/runtimes/x64/OpenConsole.exe"),
)
} else {
(
extract_dir.join("runtimes/win-arm64/native/conpty.dll"),
extract_dir
.join("build/native/runtimes/arm64/OpenConsole.exe"),
)
};
match std::fs::copy(&conpty_dll_source, &conpty_dll_target) {
Ok(_) => {
println!("Copied conpty.dll to {}", conpty_dll_target.display())
}
Err(e) => println!(
"cargo::warning=Failed to copy conpty.dll from {}: {}",
conpty_dll_source.display(),
e
),
}
match std::fs::copy(&open_console_source, &open_console_target) {
Ok(_) => println!(
"Copied OpenConsole.exe to {}",
open_console_target.display()
),
Err(e) => println!(
"cargo::warning=Failed to copy OpenConsole.exe from {}: {}",
open_console_source.display(),
e
),
}
}
Ok(output) => {
println!(
"cargo::warning=Failed to extract conpty nupkg: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Err(e) => {
println!(
"cargo::warning=Failed to run PowerShell for extraction: {}",
e
);
}
}
}
Ok(output) => {
println!(
"cargo::warning=Failed to download conpty nupkg: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Err(e) => {
println!(
"cargo::warning=Failed to run PowerShell for download: {}",
e
);
}
}
}
let release_channel = option_env!("RELEASE_CHANNEL").unwrap_or("dev");
let icon = match release_channel {
"stable" => "resources/windows/app-icon.ico",
"preview" => "resources/windows/app-icon-preview.ico",
"nightly" => "resources/windows/app-icon-nightly.ico",
"dev" => "resources/windows/app-icon-dev.ico",
_ => "resources/windows/app-icon-dev.ico",
};
let icon = std::path::Path::new(icon);
println!("cargo:rerun-if-env-changed=RELEASE_CHANNEL");
println!("cargo:rerun-if-changed={}", icon.display());
let mut res = winresource::WindowsResource::new();
// Depending on the security applied to the computer, winresource might fail
// fetching the RC path. Therefore, we add a way to explicitly specify the
// toolkit path, allowing winresource to use a valid RC path.
if let Some(explicit_rc_toolkit_path) = std::env::var("ZED_RC_TOOLKIT_PATH").ok() {
res.set_toolkit_path(explicit_rc_toolkit_path.as_str());
}
res.set_icon(icon.to_str().unwrap());
res.set("FileDescription", "Zed");
res.set("ProductName", "Zed");
if let Err(e) = res.compile() {
eprintln!("{}", e);
std::process::exit(1);
}
}
}