firmware: add commit hash into firmware

This commit is contained in:
linuswck 2025-01-21 17:04:09 +08:00
parent 227d9d4877
commit 1b7f5bc3e4
2 changed files with 32 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use std::{env, fs::File, io::Write, path::PathBuf};
use std::{env, fs::File, io::Write, path::PathBuf, process::Command};
fn main() {
// Put the linker script somewhere the linker can find it
@ -12,4 +12,34 @@ fn main() {
// Only re-run the build script when memory.x is changed,
// instead of when any part of the source code changes.
println!("cargo:rerun-if-changed=memory.x");
let revision = env::var("BUILD_REVISION");
match revision {
Ok(r) => {
// Skip if the environment variable is already set
println!("cargo:rustc-env=BUILD_REVISION={}", r);
}
Err(_) => {
let output = Command::new("git")
.args(&["rev-parse", "--short=7", "HEAD"])
.output()
.expect("Failed to execute git command");
let dirty = !Command::new("git")
.args(&["status", "-s"])
.output()
.expect("Failed to execute git command").stdout.is_empty();
let build_revision: String;
if dirty {
build_revision = String::from_utf8(output.stdout)
.expect("Invalid UTF-8 sequence") + "-dirty";
} else {
build_revision = String::from_utf8(output.stdout)
.expect("Invalid UTF-8 sequence");
}
println!("cargo:rustc-env=BUILD_REVISION={}", build_revision.trim());
}
};
}

View File

@ -39,6 +39,7 @@
nativeBuildInputs = [ pkgs.llvm ];
buildPhase = ''
export BUILD_REVISION=${toString (self.shortRev or self.dirtyShortRev or "unknown")}
cargo build --release --bin kirdy
'';