add --gitrev argument to xtask

this allows build systems that lack git state to build an image.
The effective git revision must be correctly computed by the build
system or substituted for everything to work correctly.

Note that completely faking the entry will lead to subtle problems
down the road; for example, a purpose of the revision is to check
which versions are compatible for upgrades against each other
(some upgrade paths require a hop through an older version). Faking
this can bypass the mechanism and then lead to instability down
the road.

However, some build systems (such as guix) strip out the git state,
but the revision can be safely generated in the calling environment
and passed as a variable.
This commit is contained in:
bunnie 2026-02-09 15:08:55 +08:00
parent 5c1be83a5f
commit 32f9ae1631
2 changed files with 28 additions and 7 deletions

View file

@ -58,7 +58,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut builder = Builder::new();
// encodes a timestamp into the build, unless '--no-timestamp' is passed
let do_version = env::args().filter(|x| x == "--no-timestamp").count() == 0;
generate_version(do_version);
let manual_version = get_flag("--gitrev")?;
if manual_version.is_empty() {
generate_version(do_version, None);
} else {
generate_version(do_version, Some(manual_version[0].to_owned()));
}
if do_version {
builder.add_feature("timestamp");
};
@ -927,6 +932,7 @@ fn print_help() {
[--debug-loader]
[--offline]
[--change-target]
[--gitrev revision]
[cratespecs] is a list of 0 or more items of the following syntax:
[name] crate 'name' to be built from local source
@ -953,6 +959,8 @@ be merged in with explicit app/service treatment with the following flags:
[--swap offset:size] Specify a region for swap memory. The behavior of this depends on the target.
[--change-target] Used to clean the cached target/*/*/build/SVD_PATH when changing build targets.
This will also force a full rebuild every time the flag is specified.
[--gitrev revision] Force a git revision and do not execute `git describe --long` as part of the build process. For build systems that lack git state.
Note: there is no sanity checking on the passed revision. If it's specified incorrectly, subtle, weird things could happen.
- An 'app' must be enumerated in apps/manifest.json.
A pre-processor configures the launch menu based on the list of specified apps.

View file

@ -8,13 +8,26 @@ use std::process::Command;
use chrono::Local;
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")
pub(crate) fn generate_version(add_timestamp: bool, forced_version: Option<String>) {
let gitver = {
if let Some(ver) = forced_version {
ver.as_bytes().to_vec()
} else {
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")
};
output.stdout
}
};
let gitver = output.stdout;
let semver = String::from_utf8_lossy(&gitver);
let version_file = "services/xous-ticktimer/src/version.rs";