Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
183 lines (165 loc) · 6.07 KB
/
Copy pathbuild.rs
File metadata and controls
183 lines (165 loc) · 6.07 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
use std::{
env, fs,
path::{Path, PathBuf},
};
/// Normalize path only on Windows, else just return unchanged.
fn maybe_normalize_windows_path(path: &Path) -> PathBuf {
#[cfg(windows)]
{
const EXTENDED_PREFIX: &str = r"\\?\";
let s = path.display().to_string();
if s.starts_with(EXTENDED_PREFIX) {
PathBuf::from(&s[EXTENDED_PREFIX.len()..])
} else {
path.to_path_buf()
}
}
#[cfg(not(windows))]
{
path.to_path_buf()
}
}
/// Emit cargo link metadata for a directory of schema files.
/// This sets `cargo:rerun-if-changed` and `cargo:{env_var}=` for downstream crates.
fn emit_link_metadata(
manifest_dir: &Path,
dir: &Path,
env_var: &str,
extension: &str,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
let abs = manifest_dir
.join(dir)
.canonicalize()
.map(|p| maybe_normalize_windows_path(&p))?;
println!("cargo:rerun-if-changed={}", abs.display());
println!("cargo:{}={}", env_var, abs.display());
for entry in fs::read_dir(&abs)? {
let path = entry?.path();
if path.extension().and_then(|e| e.to_str()) == Some(extension) {
println!("cargo:rerun-if-changed={}", path.display());
}
}
Ok(abs)
}
// --- Regeneration support (requires protoc) ---
#[cfg(feature = "regenerate")]
mod regen {
use super::maybe_normalize_windows_path;
use std::{
env, fs,
path::{Path, PathBuf},
};
/// Get the path to protoc, checking multiple sources:
/// 1. PROTOC_EXECUTABLE environment variable
/// 2. PROTOSOL_PROTOC environment variable
/// 3. opt/bin/protoc relative to CARGO_MANIFEST_DIR
fn get_protoc_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
if let Ok(path) = env::var("PROTOC_EXECUTABLE") {
return Ok(PathBuf::from(path));
}
if let Ok(path) = env::var("PROTOSOL_PROTOC") {
return Ok(PathBuf::from(path));
}
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
let local_protoc = manifest_dir.join("opt").join("bin").join("protoc");
if local_protoc.exists() {
return Ok(local_protoc);
}
panic!(
"protoc not found. Install it via ./deps.sh or set PROTOC_EXECUTABLE. \
See .gitmodules for the pinned version."
);
}
fn get_files(
manifest_dir: &Path,
dir: &Path,
extension: &str,
) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
let abs = manifest_dir
.join(dir)
.canonicalize()
.map(|p| maybe_normalize_windows_path(&p))?;
let mut files = vec![];
for entry in fs::read_dir(&abs)? {
let path = entry?.path();
if path.extension().and_then(|e| e.to_str()) == Some(extension) {
files.push(path);
}
}
files.sort();
Ok(files)
}
pub fn compile_protos(
manifest_dir: &Path,
out_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
let proto_dir = PathBuf::from("proto");
let proto_files = get_files(manifest_dir, &proto_dir, "proto")?;
let abs_proto_dir = manifest_dir
.join(&proto_dir)
.canonicalize()
.map(|p| maybe_normalize_windows_path(&p))?;
let protoc_path = get_protoc_path()?;
let mut config = prost_build::Config::new();
config.protoc_executable(protoc_path);
config.out_dir(out_dir);
config.file_descriptor_set_path(out_dir.join("file_descriptor_set.bin"));
config.compile_protos(
&proto_files
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>(),
&[abs_proto_dir],
)?;
Ok(())
}
/// Remove existing generated files from src/generated/ to avoid stale outputs.
pub fn clean_generated(generated_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
fs::create_dir_all(generated_dir)?;
for entry in fs::read_dir(generated_dir)? {
let path = entry?.path();
let is_generated = path
.extension()
.and_then(|e| e.to_str())
.is_some_and(|ext| ext == "rs" || ext == "bin");
if is_generated {
fs::remove_file(&path)?;
}
}
Ok(())
}
/// Parse .gitmodules and print pinned versions as cargo warnings for traceability.
pub fn print_pinned_versions(manifest_dir: &Path) {
let gitmodules_path = manifest_dir.join(".gitmodules");
if let Ok(content) = fs::read_to_string(&gitmodules_path) {
let mut current_name = String::new();
for line in content.lines() {
let trimmed = line.trim();
if trimmed.starts_with("[submodule ") {
current_name = trimmed
.trim_start_matches("[submodule \"")
.trim_end_matches("\"]")
.to_string();
} else if trimmed.starts_with("branch = ") {
let branch = trimmed.trim_start_matches("branch = ");
println!("cargo:warning=protosol: {current_name} branch hint {branch} (from .gitmodules)");
}
}
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
// Always emit link metadata for downstream crates
emit_link_metadata(&manifest_dir, Path::new("proto"), "PROTO_DIR", "proto")?;
// Regeneration: rebuild from proto sources directly into src/generated/
#[cfg(feature = "regenerate")]
{
let generated_dir = manifest_dir.join("src").join("generated");
regen::print_pinned_versions(&manifest_dir);
regen::clean_generated(&generated_dir)?;
regen::compile_protos(&manifest_dir, &generated_dir)?;
println!("cargo:warning=protosol: regenerated src/generated/ from proto sources");
}
Ok(())
}