========================================
shadow-rs
allows you to recall properties of the build process and environment at runtime, including:
Cargo.toml
project versiondebug
or release
You can use this tool to check in production exactly where a binary came from and how it was built.
shadow-rs
might be used to provide build-time information at run-time.shadow-rs
might be used to provide build-time information at run-time,and add custom
hook.Cargo.toml
fieldsModify your Cargo.toml
like so:
```TOML [package] build = "build.rs"
[dependencies] shadow-rs = "0.6"
[build-dependencies] shadow-rs = "0.6" ```
build.rs
fileNow in the root of your project (same directory as Cargo.toml
) add a file build.rs
:
* with add custom const
or fn
see:exampleshadowhook
rust
fn main() -> shadow_rs::SdResult<()> {
shadow_rs::new()
}
In your rust file (e.g. *.rs
):
```rust use shadow_rs::shadow;
shadow!(build); ```
Notice that the shadow!
macro is provided the identifier build
. You can now use this identifier to access
build-time information.
```rust fn main() {
//shadow-rs built in function
println!("{}", shadow_rs::is_debug()); // check if this is a debug build
println!("{}", shadow_rs::branch()); // get current project git branch. e.g.'master'
println!("{}", shadow_rs::tag()); // get current project git head tag. e.g.'v1.5.3'
//shadow-rs built in const
println!("{}", build::version()); // the version (description binary detail information)
println!("{}", build::clap_version()); // usually used by clap crates version() (description binary detail information)
println!("{}", build::PKG_VERSION); // current package version. e.g. '1.3.15-beta2'
println!("{}", build::PKG_VERSION_MAJOR); //current package major version. e.g. '1'
println!("{}", build::PKG_VERSION_MINOR); //current package minor version. e.g. '3'
println!("{}", build::PKG_VERSION_PATCH); //current package minor version. e.g. '15'
println!("{}", build::PKG_VERSION_PRE); //current package minor version. e.g. 'beta2'
println!("{}", build::BRANCH); // the branch, e.g. 'master'
println!("{}", build::TAG); // the tag, e.g. 'v1.0.0'
println!("{}", build::SHORT_COMMIT); // short commit hash, e.g. '8405e28e'
println!("{}", build::COMMIT_HASH); // full commit hash, e.g. '8405e28e64080a09525a6cf1b07c22fcaf71a5c5'
println!("{}", build::COMMIT_DATE); // commit date, e.g. '2020-08-16 11:52:47'
println!("{}", build::COMMIT_DATE_2822); // commit date, e.g. 'Thu, 24 Jun 2021 21:33:59 +0800'
println!("{}", build::COMMIT_DATE_3339); // commit date, e.g. '2021-06-24T21:33:59.972494+08:00'
println!("{}", build::COMMIT_AUTHOR); // commit author, e.g. 'baoyachi'
println!("{}", build::COMMIT_EMAIL); // commit email, e.g. 'example@gmail.com'
println!("{}", build::BUILD_OS); // the OS that built the binary, e.g. 'macos-x86_64'
println!("{}", build::RUST_VERSION); // rustc version e.g. 'rustc 1.45.0 (5c1f21c3b 2020-07-13)'
println!("{}", build::RUST_CHANNEL); // rust toolchain e.g. 'stable-x86_64-apple-darwin (default)'
println!("{}", build::CARGO_VERSION); // cargo version e.g. 'cargo 1.45.0 (744bd1fbb 2020-06-15)'
println!("{}", build::CARGO_TREE); // e.g. the output of '$ cargo tree'
println!("{}", build::PROJECT_NAME); // your project name, e.g. 'shadow-rs'
println!("{}", build::BUILD_TIME); // time when start build occurred, e.g. '2020-08-16 14:50:25'
println!("{}", build::BUILD_TIME_2822); // time when start build occurred by rfc2822, e.g. 'Thu, 24 Jun 2021 21:33:59 +0800'
println!("{}", build::BUILD_TIME_3339); // time when start build occurred by rfc3339, e.g. '2021-06-24T21:33:59.972494+08:00'
println!("{}", build::BUILD_RUST_CHANNEL); // e.g. 'debug'
} ```
And you can also use shadow-rs
with clap
.
| function | desc | | ------ | ------ | | is_debug() | check if this is a debug build.e.g.'true/false' | | branch() | get current project branch.e.g.'master/develop' | | tag() | get current project tag.e.g.'v1.3.5' |
| const/fn | example |
| ------ | ------ |
| version() | support mini version information.It's use easy. |
| clapversion() | support mini version information for clap.It's use easy. |
| BRANCH | master/develop |
| TAG | v1.0.0 |
| SHORTCOMMIT | 8405e28e |
| COMMITHASH | 8405e28e64080a09525a6cf1b07c22fcaf71a5c5 |
| COMMITDATE | 2020-08-16 13:12:52 |
| COMMITDATE2822 | Thu, 24 Jun 2021 21:33:59 +0800 |
| COMMITDATE3339 | 2021-06-24T21:33:59.972494+08:00 |
| COMMITAUTHOR | baoyachi |
| COMMITEMAIL | xxx@gmail.com |
| BUILDOS | macos-x8664 |
| RUSTVERSION | rustc 1.45.0 (5c1f21c3b 2020-07-13) |
| RUSTCHANNEL | stable-x8664-apple-darwin (default) |
| CARGOVERSION | cargo 1.45.0 (744bd1fbb 2020-06-15) |
| PKGVERSION | 0.3.13 |
| CARGOTREE | cargo tree |
| PROJECTNAME | shadow-rs |
| BUILDTIME | 2021-06-24 21:33:59 |
| BUILDTIME2822 | Thu, 24 Jun 2021 21:33:59 +0800 |
| BUILDTIME3339 | 2021-06-24T15:53:55+08:00 |
| BUILDRUSTCHANNEL | debug/release |
If you have any questions, please create an issue so we may improve the documentation where it may be unclear.
If you are using shadow-rs
, please tell me! Or instead, consider making a note
here: Shadow Users Collection.
nushell |
![]() starship |
![]() kime |
alexandrie alexandrie |