build.rs (1779B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::process::Command; 18 19 fn run_command(command: &str, args: &[&str]) -> Result<String, String> { 20 let output = Command::new(command) 21 .args(args) 22 .output() 23 .map_err(|e| format!("Failed to execute {}: {}", command, e))?; 24 25 if output.status.success() { 26 Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) 27 } else { 28 Err(format!( 29 "Command failed: {} {:?}\nStderr: {}", 30 command, 31 args, 32 String::from_utf8_lossy(&output.stderr) 33 )) 34 } 35 } 36 37 fn main() -> Result<(), String> { 38 // Tell Cargo to re-run the build script if the commit hash changes 39 // This covers both detached HEAD state and changes to local branch references 40 println!("cargo:rerun-if-changed=.git/HEAD"); 41 println!("cargo:rerun-if-changed=.git/refs/"); 42 43 // Get the short commit hash 44 let commit_hash = run_command("git", &["rev-parse", "--short", "HEAD"])?; 45 46 // Set the environment variable VERSION 47 println!("cargo:rustc-env=GIT_HASH={}", commit_hash); 48 49 Ok(()) 50 }