|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::path::Path; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +const CLIENT_NAME: &str = "Lighthouse"; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + println!("cargo:rerun-if-changed=build.rs"); |
| 10 | + |
| 11 | + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 12 | + let manifest_path = Path::new(&manifest_dir); |
| 13 | + |
| 14 | + // The crate version is inherited from the workspace. |
| 15 | + let semantic_version = env::var("CARGO_PKG_VERSION").unwrap(); |
| 16 | + |
| 17 | + // Hardcode the .git/ path. |
| 18 | + // This assumes the `lighthouse_version` crate will never move. |
| 19 | + let git_dir = manifest_path.join("../../.git"); |
| 20 | + |
| 21 | + if git_dir.exists() { |
| 22 | + // HEAD either contains a commit hash directly (detached HEAD), or a reference to a branch. |
| 23 | + let head_path = git_dir.join("HEAD"); |
| 24 | + if head_path.exists() { |
| 25 | + println!("cargo:rerun-if-changed={}", head_path.display()); |
| 26 | + |
| 27 | + if let Ok(head_content) = fs::read_to_string(&head_path) { |
| 28 | + let head_content = head_content.trim(); |
| 29 | + |
| 30 | + // If HEAD is a reference, also check that file. |
| 31 | + if let Some(ref_path) = head_content.strip_prefix("ref: ") { |
| 32 | + let full_ref_path = git_dir.join(ref_path); |
| 33 | + if full_ref_path.exists() { |
| 34 | + println!("cargo:rerun-if-changed={}", full_ref_path.display()); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Construct Lighthouse version string without commit hash. |
| 42 | + let base_version = format!("{}/v{}", CLIENT_NAME, semantic_version); |
| 43 | + |
| 44 | + let commit_hash = get_git_hash(7); |
| 45 | + let commit_prefix = get_git_hash(8); |
| 46 | + |
| 47 | + // If commit hash is valid, construct the full version string. |
| 48 | + let version = if !commit_hash.is_empty() && commit_hash.len() >= 7 { |
| 49 | + format!("{}-{}", base_version, commit_hash) |
| 50 | + } else { |
| 51 | + base_version |
| 52 | + }; |
| 53 | + |
| 54 | + println!("cargo:rustc-env=GIT_VERSION={}", version); |
| 55 | + println!("cargo:rustc-env=GIT_COMMIT_PREFIX={}", commit_prefix); |
| 56 | + println!("cargo:rustc-env=CLIENT_NAME={}", CLIENT_NAME); |
| 57 | + println!("cargo:rustc-env=SEMANTIC_VERSION={}", semantic_version); |
| 58 | +} |
| 59 | + |
| 60 | +fn get_git_hash(len: usize) -> String { |
| 61 | + Command::new("git") |
| 62 | + .args(["rev-parse", &format!("--short={}", len), "HEAD"]) |
| 63 | + .output() |
| 64 | + .ok() |
| 65 | + .and_then(|output| { |
| 66 | + if output.status.success() { |
| 67 | + String::from_utf8(output.stdout).ok() |
| 68 | + } else { |
| 69 | + None |
| 70 | + } |
| 71 | + }) |
| 72 | + .map(|s| s.trim().to_string()) |
| 73 | + .unwrap_or_else(|| { |
| 74 | + // Fallback commit prefix for execution engine reporting. |
| 75 | + if len == 8 { |
| 76 | + "00000000".to_string() |
| 77 | + } else { |
| 78 | + String::new() |
| 79 | + } |
| 80 | + }) |
| 81 | +} |
0 commit comments