Version stamp a rust binary

Is there a way to compile in a constant that only changes if the source or deps change? Basically a way for a binary to know what "version" it is? (does not have to be incrementing, just different if the binary has changed)

I would like to avoid timestamp because it's not constant between rebuilds. I tried hashing the sources in a build script, but the dependencies could also change. And dependencies could be path or git based, so even cargo.toml is not enough. At that point I was looking at hashing all sources, cargo.toml and cargo.lock, and I thought maybe there is an easier way to get the same end result...

Hash your sources, Cargo.toml, Cargo.lock. If Cargo.lock is missing, create a special hash that indicates a fail build.

Perhaps additionally bake in the Git-Commit-Hash and if the workspace is dirty or not and if this dirtyness is affecting files that are considered source as separate fields.

1 Like