Crate build_data
source ·Expand description
§build-data
Include build data in your program.
§Features
- Saves build-time data:
- Git commit, branch, and dirtiness
- Source modification date & time
- Rustc version
- Rust channel (stable, nightly, or beta)
- Does all of its work in your
build.rs. - Sets environment variables.
Use
env!to use them in your program. - No macros
- No runtime dependencies
- Light build dependencies
forbid(unsafe_code)- 100% test coverage
§Alternatives
- Environment variables that cargo sets for crates:
CARGO_PKG_NAMECARGO_PKG_VERSIONCARGO_BIN_NAME- others
vergen- Mature & very popular
- Good API, needs only
env!to retrieve values - Excellent test coverage
- Heavy build dependencies
build-info- Mature
- Confusing API
- Uses procedural macros
§Example
// Cargo.toml
[dependencies]
[build-dependencies]
build-data = "0"
Add a build.rs
file next to your Cargo.toml.
Call build_data::set_* functions to
set variables.
// build.rs
fn main() {
build_data::set_GIT_BRANCH();
build_data::set_GIT_COMMIT();
build_data::set_GIT_DIRTY();
build_data::set_SOURCE_TIMESTAMP();
build_data::no_debug_rebuilds();
}Use env! to access the
variables in your program:
// src/bin/main.rs
fn main() {
// Built from branch=release
// commit=a5547bfb1edb9712588f0f85d3e2c8ba618ac51f
// dirty=false
// source_timestamp=2021-04-14T06:25:59+00:00
println!("Built from branch={} commit={} dirty={} source_timestamp={}",
env!("GIT_BRANCH"),
env!("GIT_COMMIT"),
env!("GIT_DIRTY"),
env!("SOURCE_TIMESTAMP"),
);
}§Cargo Geiger Safety Report
§Changelog
- v0.2.0
- Add
set_TARGET. Thanks tison! - Use u64 for timestamps in helper functions.
- Add
- v0.1.5 - Update a dependency. Thanks dignifiedquire!
- v0.1.4 - Update a dependency.
- v0.1.3 - Update docs.
- v0.1.2 - Rewrote based on feedback from r/rust.
- v0.1.1 - Update docs.
- v0.1.0 - Initial version
§To Do
Enums§
Functions§
- Converts a byte slice into a string using
core::ascii::escape_defaultto escape each byte. - Executes
cmdwithargsas parameters, waits for it to exit, and returns its stdout, trimmed, and escaped withescape_ascii. - Formats the epoch timestamp as a UTC date like
"2021-05-04Z". - Formats the epoch timestamp as a UTC time like
"13:02:59Z". - Formats the epoch timestamp as a UTC timestamp like
"20201-05-04T13:02:59Z". - Gets the environment variable named
nameif it is set. - Gets the current branch of the source code directory.
- Gets the latest git commit of the source code directory.
- Gets the latest git commit of the source code directory. Returns the truncated hash.
- Returns
trueif the source directory contains uncommitted changes. - Gets the name of the current machine.
- Gets the version of the Rust compiler used to build the build script.
- Gets the modification time of the source code.
- Errors
- Tells cargo not to rebuild
build.rsduring debug builds when other files change. - Gets the current time as an epoch timestamp, caching it so future calls return the same time.
- Gets the channel from the rustc version string.
- Gets the dotted-numeric version from the rustc version string.
- Parses the output of
rustc --version. - Sets the
BUILD_DATEenv variable with the current date, in UTC. - Sets the
BUILD_EPOCH_TIMEenv variable, with the current time. - Sets the
BUILD_HOSTNAMEenv variable, with the hostname of the machine executing the build. - Sets the
BUILD_TIMEenv variable, with the current time, in UTC. - Sets the
BUILD_TIMESTAMPenv variable, with the current date & time, in UTC. - Sets the
GIT_BRANCHenv variable. - Sets the
GIT_COMMITenv variable. - Sets the
GIT_COMMIT_SHORTenv variable. - Sets the
GIT_DIRTYenv variable. - Sets the
RUSTC_VERSIONenv variable to the output ofrustc --version. - Sets the
RUSTC_VERSION_SEMVERto the dotted version number of therustcused by the current build. - Sets the
RUST_CHANNELenv variable to Rust channel used by the current build. - Sets the
SOURCE_DATEenv variable. - Sets the
SOURCE_EPOCH_TIMEenv variable. - Sets the
SOURCE_TIMEenv variable. - Sets the
SOURCE_TIMESTAMPenv variable. - Panics