Crate build_data[−][src]
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_NAME
CARGO_PKG_VERSION
CARGO_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.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
Happy Contributors 🙂
Fixing bugs and adding features is easy and fast. Send us a pull request and we intend to:
- Always respond within 24 hours
- Provide clear & concrete feedback
- Immediately make a new release for your accepted change
Structs
OnceI64 | Caches an i64. |
Enums
RustChannel |
Functions
escape_ascii | Converts a byte slice into a string using
|
exec | Executes |
format_date | Formats the epoch timestamp as a UTC date like |
format_time | Formats the epoch timestamp as a UTC time like |
format_timestamp | Formats the epoch timestamp as a UTC timestamp like |
get_env | Gets the environment variable named |
get_git_branch | Gets the current branch of the source code directory. |
get_git_commit | Gets the latest git commit of the source code directory. |
get_git_commit_short | Gets the latest git commit of the source code directory. Returns the truncated hash. |
get_git_dirty | Returns |
get_hostname | Gets the name of the current machine. |
get_rustc_version | Gets the version of the Rust compiler used to build the build script. |
get_source_time | Gets the modification time of the source code. |
no_debug_rebuilds | Tells cargo not to rebuild |
now | Gets the current time as an epoch timestamp. |
parse_rustc_channel | Gets the channel from the rustc version string. |
parse_rustc_semver | Gets the dotted-numeric version from the rustc version string. |
parse_rustc_version | Parses the output of |
set_BUILD_DATE | Sets the |
set_BUILD_EPOCH_TIME | Sets the |
set_BUILD_HOSTNAME | Sets the |
set_BUILD_TIME | Sets the |
set_BUILD_TIMESTAMP | Sets the |
set_GIT_BRANCH | Sets the |
set_GIT_COMMIT | Sets the |
set_GIT_COMMIT_SHORT | Sets the |
set_GIT_DIRTY | Sets the |
set_RUSTC_VERSION | Sets the |
set_RUSTC_VERSION_SEMVER | Sets the |
set_RUST_CHANNEL | Sets the |
set_SOURCE_DATE | Sets the |
set_SOURCE_EPOCH_TIME | Sets the |
set_SOURCE_TIME | Sets the |
set_SOURCE_TIMESTAMP | Sets the |