Expand description
Build-time fingerprint generator for use in a downstream build.rs.
Produces fingerprint.json in $OUT_DIR: a compact, normalised,
deterministically sorted JSON document capturing a stable snapshot of the
build environment.
§Functions
| Function | Description |
|---|---|
generate_fingerprint | Always call this. Writes compact fingerprint.json to $OUT_DIR and emits cargo:rerun-if-changed directives. Pass true to also export a pretty-printed copy alongside Cargo.toml. |
export | Optional standalone export. Writes a pretty-printed fingerprint.json alongside Cargo.toml. Note: incurs a second cargo metadata call if used after generate_fingerprint(false). |
§Sections captured
| Section | Contents |
|---|---|
package | Crate name and version |
build | Profile, opt-level, target triple, rustc version, and active feature flags |
deps | Full normalised cargo metadata dependency graph (sorted, no absolute paths) |
cargo_lock_sha256 | SHA-256 of Cargo.lock (comment lines stripped) |
source | SHA-256 of every .rs file under src/ |
§Usage
In the downstream crate’s build.rs:
ⓘ
fn main() {
// Pass `true` to also write a pretty-printed copy alongside Cargo.toml.
toolkit_zero::dependency_graph::build::generate_fingerprint(cfg!(debug_assertions))
.expect("fingerprint generation failed");
}Embed the fingerprint in the binary:
ⓘ
const BUILD_TIME_FINGERPRINT: &str = include_str!(concat!(env!("OUT_DIR"), "/fingerprint.json"));§Concerns
- Not tamper-proof — the fingerprint resides as plain text in the binary’s read-only data section. It is informational in nature; it does not constitute a security boundary.
- Export file —
generate_fingerprint(true)(orexport(true)) writesfingerprint.jsonto the crate root. Add it to.gitignoreto prevent unintentional commits. - Build-time overhead —
cargo metadatais executed on every rebuild. Thecargo:rerun-if-changeddirectives restrict this to changes insrc/,Cargo.toml, orCargo.lock. - Path stripping — absolute paths (
workspace_root,manifest_path,src_path,path, and others) are removed fromcargo metadataoutput to ensure the fingerprint is stable across machines and checkout locations. - Feature scope —
build.featurescaptures the active features of the crate being built, not toolkit-zero’s own features. - Compile-time only — the snapshot does not update at runtime.
- Atomic writes — both fingerprint files are written via a
.tmprename so a partially-written file is never observed by a parallel reader.
Enums§
- Build
Time Fingerprint Error - Errors that can occur while generating
fingerprint.json.
Functions§
- export
- Write a pretty-printed
fingerprint.jsonalongside the crate’sCargo.tomlwhenenabledistrue. - generate_
fingerprint - Generate
fingerprint.jsonin$OUT_DIR.