-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
203 lines (179 loc) · 6.88 KB
/
Copy pathbuild.rs
File metadata and controls
203 lines (179 loc) · 6.88 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
201
202
203
use std::env;
use std::error::Error;
use std::fs;
use std::path::Path;
use std::process::Command;
use vergen_gitcl::{Build, Cargo, Emitter, Gitcl, Rustc};
fn main() -> Result<(), Box<dyn Error>> {
verify_prebuilt_libghostty()?;
compress_embedded_fonts()?;
extract_ref_script()?;
update_readme_version()?;
compile_freebsd_compat()?;
// Emit build/cargo/git/rustc metadata as VERGEN_* env vars for runtime
// logging and rich --version output.
let build = Build::all_build();
let cargo = Cargo::all_cargo();
let rustc = Rustc::all_rustc();
let mut emitter = Emitter::default();
emitter
.add_instructions(&build)?
.add_instructions(&cargo)?
.add_instructions(&rustc)?;
if in_git_worktree() {
let gitcl = Gitcl::all_git();
emitter.add_instructions(&gitcl)?;
} else {
let sha = env::var("VERGEN_GIT_SHA").unwrap_or_else(|_| "unknown".to_string());
let branch = env::var("VERGEN_GIT_BRANCH").unwrap_or_else(|_| "unknown".to_string());
let date = env::var("VERGEN_GIT_COMMIT_DATE").unwrap_or_else(|_| "unknown".to_string());
let dirty = env::var("VERGEN_GIT_DIRTY").unwrap_or_else(|_| "false".to_string());
println!("cargo:rustc-env=VERGEN_GIT_SHA={sha}");
println!("cargo:rustc-env=VERGEN_GIT_BRANCH={branch}");
println!("cargo:rustc-env=VERGEN_GIT_COMMIT_DATE={date}");
println!("cargo:rustc-env=VERGEN_GIT_DIRTY={dirty}");
}
emitter.emit()?;
Ok(())
}
fn verify_prebuilt_libghostty() -> Result<(), Box<dyn Error>> {
if env::var_os("GHOSTTY_SOURCE_DIR").is_some() {
return Err("GHOSTTY_SOURCE_DIR must be unset for evp builds; evp requires prebuilt pkg-config assets in assets/libghostty. Remove GHOSTTY_SOURCE_DIR and rerun.".into());
}
let required = [
"assets/libghostty/lib/libghostty-vt.a",
"assets/libghostty/include/ghostty/vt.h",
"assets/libghostty/share/pkgconfig/libghostty-vt-static.pc",
];
let missing = required
.iter()
.filter(|p| !Path::new(p).exists())
.copied()
.collect::<Vec<_>>();
if !missing.is_empty() {
let joined = missing.join(", ");
return Err(format!(
"missing prebuilt libghostty assets: {joined}. Run `docker buildx bake extract-libghostty` from the evp repo root before building."
)
.into());
}
Ok(())
}
fn compress_embedded_fonts() -> Result<(), Box<dyn Error>> {
let out_dir = env::var("OUT_DIR")?;
let out_dir = Path::new(&out_dir);
// All fonts are pre-compressed to WOFF2 in assets/fonts/*.woff2
// Just copy them to OUT_DIR for embedding in the binary.
let faces = [
"JetBrainsMonoNerdFontMono-Regular",
"JetBrainsMonoNerdFontMono-Bold",
"JetBrainsMonoNerdFontMono-Italic",
"JetBrainsMonoNerdFontMono-BoldItalic",
"NotoSansMono-Regular",
"NotoEmoji-Regular",
"NotoSansSymbols2-Regular",
"NotoSansMonoCJKjp-Subset",
"unifont_upper-17.0.04",
"unifont_csur-17.0.04",
];
for face in faces {
let src = Path::new("assets/fonts").join(format!("{}.woff2", face));
println!("cargo:rerun-if-changed={}", src.display());
let out = out_dir.join(format!("{}.woff2", face));
fs::copy(&src, &out)?;
}
Ok(())
}
fn extract_ref_script() -> Result<(), Box<dyn Error>> {
let readme_path = Path::new("README.md");
println!("cargo:rerun-if-changed={}", readme_path.display());
let out_dir = env::var("OUT_DIR")?;
let out_path = Path::new(&out_dir).join("ref_script.tape");
if !readme_path.exists() {
fs::write(out_path, "# (Reference tape not available during build)")?;
return Ok(());
}
let readme = fs::read_to_string(readme_path)?;
let start_marker = "<!-- START_REF_SCRIPT -->";
let end_marker = "<!-- END_REF_SCRIPT -->";
if let Some(start_idx) = readme.find(start_marker) {
if let Some(end_idx) = readme[start_idx..].find(end_marker) {
let actual_end_idx = start_idx + end_idx;
let block = &readme[start_idx + start_marker.len()..actual_end_idx];
let mut inside = false;
let mut extracted_lines = Vec::new();
for line in block.lines() {
let trimmed = line.trim();
if trimmed.starts_with("```") {
inside = !inside;
continue;
}
if inside {
extracted_lines.push(line);
}
}
let extracted = extracted_lines.join("\n");
fs::write(&out_path, extracted)?;
return Ok(());
}
}
Err("Could not find START_REF_SCRIPT or END_REF_SCRIPT markers in README.md".into())
}
fn in_git_worktree() -> bool {
Command::new("git")
.args(["rev-parse", "--is-inside-work-tree"])
.output()
.ok()
.filter(|out| out.status.success())
.and_then(|out| String::from_utf8(out.stdout).ok())
.is_some_and(|stdout| stdout.trim() == "true")
}
fn update_readme_version() -> Result<(), Box<dyn Error>> {
let readme_path = Path::new("README.md");
if !readme_path.exists() {
return Ok(());
}
let readme = fs::read_to_string(readme_path)?;
let version = env::var("CARGO_PKG_VERSION")?;
let mut updated = false;
let mut lines = Vec::new();
for line in readme.lines() {
if line.contains("uses: HalFrgrd/evp@v")
&& line.contains(" # Replace with the desired release tag")
{
let prefix = "uses: HalFrgrd/evp@v";
let suffix = " # Replace with the desired release tag";
if let Some(start) = line.find(prefix) {
if let Some(end) = line.find(suffix) {
let old_version_part = &line[start + prefix.len()..end];
if old_version_part != version {
let mut new_line = String::new();
new_line.push_str(&line[..start]);
new_line.push_str(prefix);
new_line.push_str(&version);
new_line.push_str(suffix);
lines.push(new_line);
updated = true;
continue;
}
}
}
}
lines.push(line.to_string());
}
if updated {
let content = lines.join("\n") + "\n";
fs::write(readme_path, content)?;
}
Ok(())
}
fn compile_freebsd_compat() -> Result<(), Box<dyn Error>> {
if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("freebsd") {
println!("cargo:rerun-if-changed=src/freebsd_compat.c");
cc::Build::new()
.file("src/freebsd_compat.c")
.compile("freebsd_compat");
println!("cargo:rustc-link-arg=-Wl,--undefined=copy_file_range");
}
Ok(())
}