Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/build.rs

1.8 KiB, 1 run

created by r1870400018:59837, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Captures the git commit the engine is built from, for `compile::engine_git_hash` and the wasm
2//! `engineInfo()`. No git, or no repository, yields `unknown` rather than failing the build.
3
4use std::path::Path;
5use std::process::Command;
6
7fn main() {
8 let hash = match git(&["rev-parse", "--short=12", "HEAD"]) {
9 Some(h) if !h.is_empty() => {
10 // Dirty only for this crate's own tree, so an unrelated edit elsewhere in the workspace does
11 // not mark the engine.
12 match git(&["status", "--porcelain", "--", "."]) {
13 Some(s) if !s.is_empty() => format!("{}-dirty", h),
14 _ => h,
15 }
16 },
17 _ => "unknown".to_string(),
18 };
19 println!("cargo:rustc-env=AUSTENITE_GIT_HASH={}", hash);
20
21 // Naming any rerun trigger turns off Cargo's default of rerunning on every package file, so the
22 // crate's sources are named alongside the git state a commit or checkout moves.
23 println!("cargo:rerun-if-changed=build.rs");
24 println!("cargo:rerun-if-changed=Cargo.toml");
25 println!("cargo:rerun-if-changed=src");
26 if let Some(dir) = git(&["rev-parse", "--git-dir"]) {
27 println!("cargo:rerun-if-changed={}", Path::new(&dir).join("HEAD").display());
28 println!("cargo:rerun-if-changed={}", Path::new(&dir).join("index").display());
29 }
30 if let Some(dir) = git(&["rev-parse", "--git-common-dir"]) {
31 println!("cargo:rerun-if-changed={}", Path::new(&dir).join("packed-refs").display());
32 if let Some(head_ref) = git(&["symbolic-ref", "-q", "HEAD"]) {
33 println!("cargo:rerun-if-changed={}", Path::new(&dir).join(head_ref).display());
34 }
35 }
36}
37
38/// The trimmed standard output of a successful git command, or `None`.
39fn git(args: &[&str]) -> Option<String> {
40 match Command::new("git").args(args).output() {
41 Ok(out) if out.status.success() => match String::from_utf8(out.stdout) {
42 Ok(s) => Some(s.trim().to_string()),
43 Err(_) => None,
44 },
45 _ => None,
46 }
47}