xous-core/xtask/src/versioning.rs
Sylvain Bellemare 5c424fa678
xtask: deterministic builds with SOURCE_DATE_EPOCH
When SOURCE_DATE_EPOCH is set, use it instead of Local::now() for the
embedded build timestamp. This enables deterministic builds via Guix,
Nix, and other reproducible build systems.

See: https://reproducible-builds.org/docs/source-date-epoch/

Design notes:

- Panics on invalid SOURCE_DATE_EPOCH rather than warn-and-fallback,
  because silent fallback would produce non-reproducible output while
  the user expects reproducibility. Build systems should fail loudly
  on misconfiguration.

- Preserves Local::now() fallback when SOURCE_DATE_EPOCH is unset,
  keeping local timezone for developer convenience during debugging.
2026-02-10 23:39:35 +09:00

101 lines
4 KiB
Rust

// Versioning infrastructure
// This generates the SEMVER data that is displayed when querying `xous ver`
// It also generates timestamps, if demanded.
use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::process::Command;
use chrono::{Local, TimeZone, Utc};
pub(crate) fn generate_version(add_timestamp: bool) {
let output = if cfg!(target_os = "windows") {
Command::new("cmd").args(["/C", "git describe --long"]).output().expect("failed to execute process")
} else {
Command::new("sh").arg("-c").arg("git describe --long").output().expect("failed to execute process")
};
let gitver = output.stdout;
let semver = String::from_utf8_lossy(&gitver);
let version_file = "services/xous-ticktimer/src/version.rs";
let boot0_version_file = "bao1x-boot/boot0/src/version.rs";
let boot1_version_file = "bao1x-boot/boot1/src/version.rs";
// Read the existing file to see if it needs to be updated.
let mut existing_data = Vec::new();
if let Ok(mut f) = std::fs::File::open(version_file) {
f.read_to_end(&mut existing_data).ok();
}
let mut new_data = Vec::new();
print_header(&mut new_data);
if add_timestamp {
// Use SOURCE_DATE_EPOCH for reproducible builds if available
// See: https://reproducible-builds.org/docs/source-date-epoch/
let timestamp_str = if let Ok(sde) = std::env::var("SOURCE_DATE_EPOCH") {
let secs: i64 = sde.parse().expect("SOURCE_DATE_EPOCH must be a valid Unix timestamp");
Utc.timestamp_opt(secs, 0).single().expect("Invalid SOURCE_DATE_EPOCH timestamp").to_rfc2822()
} else {
Local::now().to_rfc2822()
};
write!(new_data, "#[allow(dead_code)]\npub const TIMESTAMP: &'static str = \"{}\";\n", timestamp_str)
.expect("couldn't add our timestamp");
} else {
write!(new_data, "#[allow(dead_code)]\npub const TIMESTAMP: &'static str = \"omitted\";\n")
.expect("couldn't add our timestamp");
}
let mut semver_code = Vec::new();
writeln!(
semver_code,
"pub const SEMVER: &'static str = \"{}\";",
semver.strip_suffix("\r\n").or(semver.strip_suffix('\n')).unwrap_or(&semver)
)
.expect("couldn't add our semver");
new_data.extend_from_slice(&semver_code);
if existing_data != new_data {
let mut vfile = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(version_file)
.expect("Can't open our version file for writing");
vfile.write_all(&new_data).expect("couldn't write new timestamp to version.rs");
let mut vfile = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(boot0_version_file)
.expect("Can't open our version file for writing");
vfile.write_all(&semver_code).expect("couldn't write semver to version.rs");
let mut vfile = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(boot1_version_file)
.expect("Can't open our version file for writing");
vfile.write_all(&semver_code).expect("couldn't write semver to version.rs");
}
}
fn print_header<U: Write>(out: &mut U) {
let s = r####"#![cfg_attr(rustfmt, rustfmt_skip)]
// Versioning information is kept in a separate file, attached to a small, well-known server in the Xous System
// This is a trade-off between rebuild times and flexibility.
// This was autogenerated by xtask/src/main.rs:print_header(). Do not edit manually.
pub(crate) fn get_version() -> crate::api::VersionString {
let mut v = crate::api::VersionString {
version: String::new()
};
v.version.push_str(SEMVER);
v.version.push_str("\n");
v.version.push_str(TIMESTAMP);
v
}
"####;
out.write_all(s.as_bytes()).expect("couldn't write our version template header");
}