1pub mod bazel;
2use anyhow::anyhow;
5use anyhow::Context;
6use anyhow::Result;
7use bazel::Bazel;
8use std::env;
9use std::fs;
10use std::path::{Path, PathBuf};
11use std::process::Command;
12
13const BAZEL_MINIMAL_VERSION: &str = "8.0.0";
14const BAZEL_MAXIMAL_VERSION: &str = "8.99.99";
15const BAZEL_DOWNLOAD_VERSION: Option<&str> = Some("8.3.1");
16
17const SUPPORTED_TARGETS: &[&str] = &[
19 "x86_64-unknown-linux-gnu",
21 "aarch64-unknown-linux-gnu",
22 "armv7-unknown-linux-gnueabi",
23 "i686-unknown-linux-gnu",
24 "aarch64-linux-android",
30 "armv7-linux-androideabi",
31 "x86_64-linux-android",
32 "i686-linux-android",
33 "x86_64-apple-darwin",
35 "aarch64-apple-darwin",
36 "arm64e-apple-darwin",
37 "aarch64-apple-ios",
39 "aarch64-apple-ios-sim",
40 "x86_64-apple-ios",
41 "arm64e-apple-ios",
42 "aarch64-apple-tvos",
44 "aarch64-apple-tvos-sim",
45 "x86_64-apple-tvos",
46 "aarch64-apple-watchos",
48 "aarch64-apple-watchos-sim",
49 "x86_64-apple-watchos-sim",
50 "arm64_32-apple-watchos",
51 "armv7k-apple-watchos",
52 "aarch64-apple-visionos",
54 "aarch64-apple-visionos-sim",
55 "x86_64-pc-windows-msvc",
57 "wasm32-unknown-emscripten",
59];
60
61fn is_supported_target(target: &str) -> bool {
63 SUPPORTED_TARGETS.contains(&target)
64}
65
66fn is_cross_rs() -> bool {
67 env::var("CROSS_SYSROOT").is_ok() && env::var("CROSS_TOOLCHAIN_PREFIX").is_ok()
68}
69
70fn is_windows(target: &str) -> bool {
71 target.contains("windows")
72}
73
74fn target_config(target: &str) -> Option<&'static str> {
75 if target.contains("apple") {
76 if target.contains("darwin") {
77 return Some("macos");
78 } else if target.contains("ios") {
79 return Some("ios");
80 }
81 }
82 if target.contains("windows") {
83 return Some("msvc");
84 }
85 None
86}
87
88fn work_dir(target: &str) -> Result<PathBuf> {
89 if !is_supported_target(target) {
90 return Err(anyhow::anyhow!(
91 "Unsupported target for cel-build-utils: {}. See SUPPORTED_TARGETS in cel-build-utils/src/lib.rs",
92 target
93 ));
94 }
95
96 let dir = if is_windows(target) {
97 "cel-windows"
98 } else {
99 "cel"
100 };
101
102 Ok(Path::new(env!("CARGO_MANIFEST_DIR")).join(dir))
103}
104
105pub fn version() -> &'static str {
106 env!("CARGO_PKG_VERSION")
107}
108
109pub struct Build {
110 out_dir: Option<PathBuf>,
111 target: Option<String>,
112}
113
114impl Default for Build {
115 fn default() -> Self {
116 Self::new()
117 }
118}
119
120pub struct Artifacts {
121 include_dir: PathBuf,
122 lib_dir: PathBuf,
123 libs: Vec<String>,
124 #[allow(dead_code)]
125 target: String,
126}
127
128impl Build {
129 pub fn new() -> Build {
130 Build {
131 out_dir: env::var_os("OUT_DIR").map(|s| PathBuf::from(s).join("cel")),
132 target: env::var("TARGET").ok(),
133 }
134 }
135
136 pub fn out_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Build {
137 self.out_dir = Some(path.as_ref().to_path_buf());
138 self
139 }
140
141 pub fn target(&mut self, target: &str) -> &mut Build {
142 self.target = Some(target.to_string());
143 self
144 }
145
146 pub fn build(&mut self) -> Artifacts {
148 match self.try_build() {
149 Ok(a) => a,
150 Err(e) => {
151 println!("cargo:warning=libcel: failed to build cel-cpp from source\n{e}");
152 std::process::exit(1)
153 }
154 }
155 }
156
157 pub fn try_build(&mut self) -> Result<Artifacts> {
158 let target = self.target.as_ref().context("TARGET dir not set")?;
159 let out_dir = self.out_dir.as_ref().context("OUT_DIR not set")?;
160 if !out_dir.exists() {
161 fs::create_dir_all(out_dir)
162 .context(format!("failed_to create out_dir: {}", out_dir.display()))?;
163 }
164 let work_dir = work_dir(target)?;
165 let install_dir = out_dir.join("install");
166
167 let install_library_dir = install_dir.join("lib");
168 let install_include_dir = install_dir.join("include");
169 let libs = vec!["cel".to_owned()];
170
171 let mut bazel = Bazel::new(
172 target.clone(),
173 BAZEL_MINIMAL_VERSION,
174 BAZEL_MAXIMAL_VERSION,
175 out_dir,
176 BAZEL_DOWNLOAD_VERSION,
177 )?
178 .with_work_dir(&work_dir);
179
180 if is_cross_rs() {
181 bazel = bazel.with_option("--output_user_root=/tmp/bazel");
182 }
183
184 let mut build_command = bazel.build(["//:cel"]);
185
186 build_command.arg(format!("--platforms=//:{target}"));
187
188 if let Some(config) = target_config(target) {
189 build_command.arg(format!("--config={config}"));
190 }
191
192 self.run_command(build_command, "building cel")
193 .map_err(|e| anyhow!(e))?;
194
195 if install_dir.exists() {
196 fs::remove_dir_all(&install_dir).context(format!(
197 "failed to remove install_dir: {}",
198 install_dir.display()
199 ))?;
200 }
201
202 fs::create_dir_all(&install_dir).context(format!(
204 "failed to create install_dir: {}",
205 install_dir.display()
206 ))?;
207
208 fs::create_dir(&install_include_dir).context(format!(
210 "failed to create install_include_dir: {}",
211 install_include_dir.display()
212 ))?;
213 fs::create_dir(&install_library_dir).context(format!(
215 "failed to create install_library_dir: {}",
216 install_library_dir.display()
217 ))?;
218
219 let include_mapping = vec![
221 ("bazel-cel/external/cel-cpp+", "."),
222 ("bazel-cel/external/abseil-cpp+/absl", "absl"),
223 ("bazel-cel/external/protobuf+/src/google", "google"),
224 (
225 "bazel-bin/external/cel-spec+/proto/cel/expr/_virtual_includes/checked_proto/cel",
226 "cel",
227 ),
228 (
229 "bazel-bin/external/cel-spec+/proto/cel/expr/_virtual_includes/value_proto/cel",
230 "cel",
231 ),
232 (
233 "bazel-bin/external/cel-spec+/proto/cel/expr/_virtual_includes/syntax_proto/cel",
234 "cel",
235 ),
236 ];
237
238 for (f, t) in include_mapping {
239 #[cfg(windows)]
240 let f = f.replace("bazel-cel", "bazel-cel-windows");
241 #[cfg(windows)]
242 let f = f.replace("/", "\\");
243 #[cfg(windows)]
244 let t = t.replace("/", "\\");
245
246 let f = work_dir.join(f);
247 let t = install_include_dir.join(t);
248 cp_r(&f, &t)?;
249 }
250
251 let libcel_name = if is_windows(target) {
252 "cel.lib"
253 } else {
254 "libcel.a"
255 };
256 std::fs::copy(
257 work_dir.join("bazel-bin").join(libcel_name),
258 install_library_dir.join(libcel_name),
259 )
260 .context(format!("failed to copy {libcel_name}"))?;
261
262 Ok(Artifacts {
263 lib_dir: install_library_dir,
264 include_dir: install_include_dir,
265 libs,
266 target: target.to_owned(),
267 })
268 }
269
270 #[track_caller]
271 fn run_command(&self, mut command: Command, desc: &str) -> Result<Vec<u8>, String> {
272 let output = command.output();
274
275 let verbose_error = match output {
276 Ok(output) => {
277 let status = output.status;
278 if status.success() {
279 return Ok(output.stdout);
280 }
281 let stdout = String::from_utf8_lossy(&output.stdout);
282 let stderr = String::from_utf8_lossy(&output.stderr);
283 format!(
284 "'{exe}' reported failure with {status}\nstdout: {stdout}\nstderr: {stderr}",
285 exe = command.get_program().to_string_lossy()
286 )
287 }
288 Err(failed) => match failed.kind() {
289 std::io::ErrorKind::NotFound => format!(
290 "Command '{exe}' not found. Is {exe} installed?",
291 exe = command.get_program().to_string_lossy()
292 ),
293 _ => format!(
294 "Could not run '{exe}', because {failed}",
295 exe = command.get_program().to_string_lossy()
296 ),
297 },
298 };
299
300 println!("cargo:warning={desc}: {verbose_error}");
301 Err(format!(
302 "Error {desc}:
303 {verbose_error}
304 Command failed: {command:?}"
305 ))
306 }
307}
308
309fn cp_r(src: &Path, dst: &Path) -> Result<()> {
310 for f in fs::read_dir(src).map_err(|e| anyhow!("{}: {e}", src.display()))? {
312 let f = match f {
313 Ok(f) => f,
314 _ => continue,
315 };
316 fs::create_dir_all(dst).map_err(|e| anyhow!("failed to create dir {dst:?}: {e}"))?;
317
318 let file_name = f.file_name();
319 let mut path = f.path();
320
321 if file_name.to_str() == Some(".git") {
324 continue;
325 }
326
327 let dst = dst.join(file_name);
328 let mut ty = f.file_type().map_err(|e| anyhow!("failed to read file type {f:?}: {e}"))?;
329 while ty.is_symlink() {
330 let link_path = fs::read_link(f.path()).map_err(|e| anyhow!("failed to read link {f:?}: {e}"))?;
331 if link_path.is_relative() {
332 path = f.path().parent().unwrap().join(link_path);
333 } else {
334 path = link_path;
335 }
336 ty = fs::metadata(&path).map_err(|e| anyhow!("failed to read metadata {path:?}: {e}"))?.file_type();
337 }
338
339 if ty.is_dir() {
340 cp_r(&f.path(), &dst)?;
342 } else {
343 let _ = fs::remove_file(&dst);
345 if let Err(e) = fs::copy(&path, &dst) {
346 return Err(anyhow!(
348 "failed to copy '{}' to '{}': {e}",
349 path.display(),
350 dst.display()
351 ));
352 }
353 }
354 }
355 Ok(())
356}
357
358#[allow(dead_code)]
359fn sanitize_sh(path: &Path) -> String {
360 if !cfg!(windows) {
361 return path.to_string_lossy().into_owned();
362 }
363 let path = path.to_string_lossy().replace("\\", "/");
364 return change_drive(&path).unwrap_or(path);
365
366 fn change_drive(s: &str) -> Option<String> {
367 let mut ch = s.chars();
368 let drive = ch.next().unwrap_or('C');
369 if ch.next() != Some(':') {
370 return None;
371 }
372 if ch.next() != Some('/') {
373 return None;
374 }
375 Some(format!("/{}/{}", drive, &s[drive.len_utf8() + 2..]))
376 }
377}
378
379impl Artifacts {
380 pub fn include_dir(&self) -> &Path {
381 &self.include_dir
382 }
383
384 pub fn lib_dir(&self) -> &Path {
385 &self.lib_dir
386 }
387
388 pub fn libs(&self) -> &[String] {
389 &self.libs
390 }
391
392 pub fn print_cargo_metadata(&self) {
393 println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
394 for lib in self.libs.iter() {
395 println!("cargo:rustc-link-lib=static={lib}");
396 }
397 println!("cargo:include={}", self.include_dir.display());
398 println!("cargo:lib={}", self.lib_dir.display());
399 }
400}