lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

build.rs (2383B)


      1 use std::env;
      2 use std::path::PathBuf;
      3 use std::process::Command;
      4 use std::time::{SystemTime, UNIX_EPOCH};
      5 
      6 fn main() {
      7     println!("cargo:rerun-if-changed=build.rs");
      8     println!("cargo:rerun-if-env-changed=CARGO_MANIFEST_DIR");
      9     println!("cargo:rerun-if-env-changed=RUSTC");
     10     println!("cargo:rerun-if-env-changed=PROFILE");
     11 
     12     let manifest_dir =
     13         env::var("CARGO_MANIFEST_DIR").expect("missing required env var CARGO_MANIFEST_DIR");
     14 
     15     let mut dir = PathBuf::from(&manifest_dir);
     16     let git_dir = loop {
     17         if dir.join(".git").exists() {
     18             break dir.join(".git");
     19         }
     20         if !dir.pop() {
     21             break PathBuf::from(".git");
     22         }
     23     };
     24 
     25     if git_dir.exists() {
     26         println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
     27         println!(
     28             "cargo:rerun-if-changed={}",
     29             git_dir.join("refs/heads").display()
     30         );
     31         println!("cargo:rerun-if-changed={}", git_dir.join("index").display());
     32     }
     33 
     34     let build_time_unix = SystemTime::now()
     35         .duration_since(UNIX_EPOCH)
     36         .map(|d| d.as_secs())
     37         .expect("system time before unix epoch");
     38     println!("cargo:rustc-env=BUILD_TIME_UNIX={}", build_time_unix);
     39 
     40     let rustc_bin = env::var("RUSTC").expect("missing required env var RUSTC");
     41     if let Ok(out) = Command::new(rustc_bin).arg("--version").output()
     42         && out.status.success()
     43         && let Ok(ver) = String::from_utf8(out.stdout)
     44     {
     45         println!("cargo:rustc-env=RUSTC_VERSION={}", ver.trim());
     46     }
     47 
     48     let git_hash = Command::new("git")
     49         .args(["rev-parse", "--short=12", "HEAD"])
     50         .output()
     51         .ok()
     52         .and_then(|o| {
     53             if o.status.success() {
     54                 String::from_utf8(o.stdout).ok()
     55             } else {
     56                 None
     57             }
     58         })
     59         .map(|s| s.trim().to_string());
     60 
     61     let dirty = Command::new("git")
     62         .args(["status", "--porcelain"])
     63         .output()
     64         .ok()
     65         .map(|o| o.status.success() && !o.stdout.is_empty())
     66         .unwrap_or(false);
     67 
     68     if let Some(mut h) = git_hash {
     69         if dirty {
     70             h.push_str("-dirty");
     71         }
     72         println!("cargo:rustc-env=GIT_HASH={}", h);
     73     }
     74 
     75     let profile = env::var("PROFILE").expect("missing required env var PROFILE");
     76     println!("cargo:rustc-env=PROFILE={}", profile);
     77 }