[][src]Crate built

Provides a crate with information from the time it was built.

built is used as a build-time dependency to collect various information about the build environment, serialize it into Rust-code and compile it into the final crate. The information collected by built include:

  • Various metadata like version, authors, homepage etc. as set by Cargo.toml
  • The tag or commit id if the crate was being compiled from within a git repo.
  • The values of various cfg!, like target_os and target_arch.
  • The features the crate was compiled with.
  • The various dependencies, dependencies of dependencies and their versions cargo ultimately chose to compile.
  • The presence of a CI-platform like Travis CI and AppVeyor.
  • The used compiler and it's version; the used documentation generator and it's version.

See the Options-type regarding what built can serialize.

built does not add any further dependencies to a crate; all information is serialized as types from stdlib. One can include built as a runtime-dependency and use it's convenience functions.

To add built to a crate, add it as a build-time dependency, use a build script that collects and serializes the build-time information and include! that as code.

Add this to Cargo.toml:

[package]
build = "build.rs"

[build-dependencies]
built = "0.4"

Add or modify a build script. In build.rs:

fn main() {
    built::write_built_file().expect("Failed to acquire build-time information");
}

The build-script will by default write a file named built.rs into Cargo's output directory. It can be picked up in main.rs (or anywhere else) like this:

This example is not tested
// Use of a mod or pub mod is not actually necessary.
pub mod built_info {
   // The file has been placed there by the build script.
   include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

And then used somewhere in the crate's code:

/// Determine if current version is a pre-release or was built from a git-repo
fn release_is_unstable() -> bool {
    return !built_info::PKG_VERSION_PRE.is_empty() || built_info::GIT_VERSION.is_some()
}

/// Default log-level, enhanced on CI
fn default_log_level() -> LogLevel {
    if built_info::CI_PLATFORM.is_some() {
        LogLevel::TRACE
    } else {
        LogLevel::ERROR
    }
}

/// If another crate pulls in a dependency we don't like, print a warning
#[cfg(feature = "semver")]
fn check_sane_dependencies() {
    if built::util::parse_versions(&built_info::DEPENDENCIES)
                    .any(|(name, ver)| name == "DeleteAllMyFiles"
                                       && ver < semver::Version::parse("1.1.4").unwrap()) {
        eprintln!("DeleteAllMyFiles < 1.1.4 may not delete all your files. Beware!");
    }
}

Modules

util

Various convenience functions for built at runtime.

Structs

Options

Selects which information built should retrieve and write as Rust code. Used in conjunction with write_built_file_with_opts.

Enums

CIPlatform

Various Continuous Integration platforms whose presence can be detected.

Functions

write_built_file

A shorthand for calling write_built_file() with CARGO_MANIFEST_DIR and [OUT_DIR]/built.rs.

write_built_file_with_opts

Writes rust-code describing the crate at manifest_location to a new file named dst.