59 lines
2.4 KiB
Rust
59 lines
2.4 KiB
Rust
|
|
//! Read-only build metadata generation for HCIE GUI packages.
|
||
|
|
//!
|
||
|
|
//! **Purpose:** Publishes the build number prepared by `scripts/cargo-with-build-id.sh` to every
|
||
|
|
//! frontend without mutating watched Cargo inputs from inside a build script.
|
||
|
|
//! **Logic & Workflow:** Uses the wrapper-provided override when present, otherwise reads the
|
||
|
|
//! canonical repository `build.id`, validates it, and emits synchronized compile-time variables.
|
||
|
|
//! **Side Effects / Dependencies:** Reads one repository file and writes only Cargo directives.
|
||
|
|
|
||
|
|
use std::fs;
|
||
|
|
use std::io;
|
||
|
|
use std::path::PathBuf;
|
||
|
|
|
||
|
|
/// Reads and validates the canonical or wrapper-provided build identifier.
|
||
|
|
///
|
||
|
|
/// **Returns:** The current build ID as `u64`.
|
||
|
|
/// **Side Effects / Dependencies:** Reads `build.id` when no override is provided.
|
||
|
|
fn build_id() -> u64 {
|
||
|
|
if let Ok(value) = std::env::var("HCIE_BUILD_ID_OVERRIDE") {
|
||
|
|
return value
|
||
|
|
.parse::<u64>()
|
||
|
|
.expect("HCIE_BUILD_ID_OVERRIDE must be an unsigned integer");
|
||
|
|
}
|
||
|
|
|
||
|
|
let manifest_dir = PathBuf::from(
|
||
|
|
std::env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is required"),
|
||
|
|
);
|
||
|
|
let canonical = manifest_dir
|
||
|
|
.parent()
|
||
|
|
.expect("hcie-build-info must live directly under the repository root")
|
||
|
|
.join("build.id");
|
||
|
|
match fs::read_to_string(&canonical) {
|
||
|
|
Ok(value) => value.trim().parse::<u64>().unwrap_or_else(|error| {
|
||
|
|
panic!("invalid integer in {}: {error}", canonical.display())
|
||
|
|
}),
|
||
|
|
Err(error) if error.kind() == io::ErrorKind::NotFound => 0,
|
||
|
|
Err(error) => panic!("failed to read {}: {error}", canonical.display()),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Publishes synchronized build metadata to the library crate.
|
||
|
|
fn main() {
|
||
|
|
let manifest_dir = PathBuf::from(
|
||
|
|
std::env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is required"),
|
||
|
|
);
|
||
|
|
let canonical = manifest_dir
|
||
|
|
.parent()
|
||
|
|
.expect("hcie-build-info must live directly under the repository root")
|
||
|
|
.join("build.id");
|
||
|
|
println!("cargo:rerun-if-changed={}", canonical.display());
|
||
|
|
println!("cargo:rerun-if-env-changed=HCIE_BUILD_ID_OVERRIDE");
|
||
|
|
|
||
|
|
let build_id = build_id();
|
||
|
|
let base_version = std::env::var("CARGO_PKG_VERSION").expect("package version is required");
|
||
|
|
println!("cargo:rustc-env=HCIE_BUILD_ID={build_id}");
|
||
|
|
println!(
|
||
|
|
"cargo:rustc-env=HCIE_BUILD_VERSION={base_version}+build.{build_id}"
|
||
|
|
);
|
||
|
|
}
|