Cargo: Publish build date as environment variable

Created on 13 Mar 2016  路  7Comments  路  Source: rust-lang/cargo

Currently, it is possible to query the build version in a crate via env!("CARGO_PKG_VERSION"), but there does not seem to be any support for getting the build date, which is commonly also bundled with the application. Might I suggest a similar CARGO_BUILD_DATE environment variable, ideally in an easily parsable format for rust-chrono such as ISO 8601?

Most helpful comment

@sunjay the "easiest" way to do that now is to have a build script that extracts the hash, writes it to a file, and then include_str! is in the main program. Not exactly easy per se but gets the job done!

All 7 comments

This might actually be best done as a rustc feature rather than a Cargo feature, in theory all sorts of compiles would want this kind of info!

As a rustc feature, is there any way to execute an arbitrary console command at compile time and put the result in a &'static str, similar to env!? That would also be a reasonable solution, although it would rely on the existence of certain commands, such as date, which might be better left to a build script.

What about the build script leaving a cargo:key=value line and the crate script getting value out with an environment variable CARGO_key?

Not currently, unfortunately, but it seems like something that wouldn't be totally unreasonable for the compiler to define.

The build script can print out:

cargo:rustc-cfg=foo="bar"

but unfortunately you can't get the value at compile-time (you can only test against it).

Embedding the build date would make the build not reproducible: two builds of the same source would produce different binaries. See https://reproducible-builds.org/ and https://reproducible-builds.org/specs/source-date-epoch/ .

I would suggest using the version number, git commit hash, or similar values that won't change unless the source does.

Is there a way to embed the git commit hash?

@sunjay the "easiest" way to do that now is to have a build script that extracts the hash, writes it to a file, and then include_str! is in the main program. Not exactly easy per se but gets the job done!

I'm going to close this since it sounds like this should be in rust-lang/rust instead.

Was this page helpful?
0 / 5 - 0 ratings