baoyachi / shadow-rs

A build-time information stored in your rust project.(binary,lib,cdylib,dylib)
https://docs.rs/shadow-rs
MIT License
447 stars 41 forks source link

Consider generating a struct, or otherwise simplify usage #80

Open jaskij opened 2 years ago

jaskij commented 2 years ago

TL;DR: make it easier to write shared code interfacing shadow's output.

Currently, shadow only generates pub const VAR: &str variables - it's great as is, but is also quite a bit unwieldy, leading to a lot of boilerplate in multi-binary projects.

There are two main benefits to this approach:

This also ties into what @epage wrote in https://github.com/baoyachi/shadow-rs/issues/75#issuecomment-1048035937 - such a struct would work as an accessor. Or rather, structs, as it would be then split per-source (git, rust, etc).

In my project I have a struct (not using all variables), which could easily be made as a part of code generation, like this:

pub struct BuildInfo {
    pub version_major: &'static str,
    pub version_minor: &'static str,
    pub version_patch: &'static str,
    pub version_pre: &'static str,
    pub git_commit_hash: &'static str,
    pub git_short_hash: &'static str,
    pub git_clean: bool,
    pub build_type: &'static str,
    pub cargo_version: &'static str,
    pub rust_version: &'static str,
    pub rust_channel: &'static str,
    pub target: &'static str,
}

which is then initialized with:

pub(crate) const BUILD_INFO: BuildInfo = BuildInfo {
    version_major: PKG_VERSION_MAJOR,
    version_minor: PKG_VERSION_MINOR,
    version_patch: PKG_VERSION_PATCH,
    version_pre: PKG_VERSION_PRE,
    git_commit_hash: COMMIT_HASH,
    git_short_hash: SHORT_COMMIT,
    git_clean: GIT_CLEAN,
    build_type: BUILD_RUST_CHANNEL,
    cargo_version: CARGO_VERSION,
    rust_version: RUST_VERSION,
    rust_channel: RUST_CHANNEL,
    target: BUILD_TARGET,
};
baoyachi commented 2 years ago

That's a good suggestion. I'll think about it. @jaskij THX.

baoyachi commented 2 years ago

solution ref : https://github.com/aptos-labs/aptos-core/blob/c7978de51c52a5f6517c88f3ae24d9c241d4beb7/crates/aptos-telemetry/src/build_information.rs