build-data 0.1.1

Include build data in your program: date, rustc version, git commit & branch, etc.
Documentation
build-data-0.1.1 has been yanked.

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

build-data

Include build data in your program.

Features

  • Saves build-time data:
    • Git commit, branch, and dirtiness
    • Date & time
    • Epoch time
    • Hostname
    • Rustc version
  • Does all of its work in your build.rs.
  • No macros
  • Depends only on core::alloc at runtime.
  • Light build dependencies
  • forbid(unsafe_code)
  • 100% test coverage

Alternatives

  • 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-data = "0"

[build-dependencies]
build-data-writer = "0"

Add a build.rs file next to your Cargo.toml. Call build_data_writer::write to collect data and write it to the file.

// build.rs
use std::env;
use std::path::Path;

fn main() {
    build_data_writer::write(
        &Path::new(&env::var_os("OUT_DIR").unwrap())
        .join("build-data.txt")
    ).unwrap();
    build_data_writer::no_debug_rebuilds();
}

When you run cargo build, Cargo compiles and runs your build.rs which writes the file:

// target/build-data.txt
GIT_BRANCH:release
GIT_COMMIT:a5547bfb1edb9712588f0f85d3e2c8ba618ac51f
GIT_DIRTY:false
HOSTNAME:builder2
RUSTC_VERSION:rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)
TIME:2021-04-14T06:25:59+00:00
TIME_SECONDS:1618381559

Include and parse the file in your program. See include_str!, concat!, env!, and build_data::BuildData::new.

// src/bin/main.rs
fn main() {
    let bd = build_data::BuildData::new(include_str!(
        concat!(env!("OUT_DIR"), "/build-data.txt")
    )).unwrap();
    // Built 2021-04-14T06:25:59+00:00 branch=release
    // commit=a5547bfb1edb9712588f0f85d3e2c8ba618ac51f
    // host=builder2
    // rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)
    log!("{}", bd);
}

Cargo Geiger Safety Report


Metric output format: x/y
    x = unsafe code used by the build
    y = total unsafe code found in the crate

Symbols: 
    🔒  = No `unsafe` usage found, declares #![forbid(unsafe_code)]
    ❓  = No `unsafe` usage found, missing #![forbid(unsafe_code)]
    ☢️  = `unsafe` usage found

Functions  Expressions  Impls  Traits  Methods  Dependency

0/0        0/0          0/0    0/0     0/0      🔒  build-data 0.1.1

0/0        0/0          0/0    0/0     0/0    

Changelog

  • v0.1.1 - Update docs.
  • v0.1.0 - Initial version

To Do

  • Accept only &'static str and remove dependencyon alloc.
  • See if we can make new into a const fn.

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

License: Apache-2.0