New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate build info in cargo's build scripts #4051

Open
overvenus opened this Issue Jan 10, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@overvenus
Copy link
Member

overvenus commented Jan 10, 2019

Feature Request

Is your feature request related to a problem? Please describe:
Currently, building a tikv-server needs a Makefile to collect git hash and others, otherwise tikv-server --version shows unknown. This means a proper build needs helps from the outside of cargo.

Describe the feature you'd like:

Instead of Makefile, we can collect these info in cargo's build script.

rustc-env=VAR=VALUE indicates that the specified environment variable will be added to the environment which the compiler is run within. The value can be then retrieved by the env! macro in the compiled crate. This is useful for embedding additional metadata in crate's code, such as the hash of Git HEAD or the unique identifier of a continuous integration server.

https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script

EDIT: we should use rustc-env instead of rustc-cfg.

@overvenus overvenus added the C: Build label Jan 10, 2019

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Feb 1, 2019

Here are at least some of the symptoms:

ubuntu@ip-172-30-0-238:~/tikv2$ cargo run --bin tikv-server --
   Compiling tikv v3.0.0-beta (/home/ubuntu/tikv2)
    Finished dev [unoptimized + debuginfo] target(s) in 46.53s
     Running `target/debug/tikv-server`
[2019/02/01 20:55:29.730 +00:00] [INFO] [mod.rs:25] ["Welcome to TiKV. \nRelease Version:   3.0.0-beta\nGit Commit Hash:   Unknown (env var does not exist when building)\nGit Commit Branch: Unknown (env var does not exist when building)\nUTC Build Time:    Unknown (env var does not exist when building)\nRust Version:      Unknown (env var does not exist when building)"]

Many 'Unknowns" in there.

And here's a list of all the stuff tikv is trying to pull out of the environment at build time:

ubuntu@ip-172-30-0-238:~/tikv2$ rg "env.*!"
src/server/node.rs
108:        store.set_version(env!("CARGO_PKG_VERSION").to_string());

components/test_util/src/security.rs
19:    let p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

tests/integrations/config/mod.rs
45:    let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

src/bin/util/mod.rs
14:        env!("CARGO_PKG_VERSION"),
15:        option_env!("TIKV_BUILD_GIT_HASH").unwrap_or(fallback),
16:        option_env!("TIKV_BUILD_GIT_BRANCH").unwrap_or(fallback),
17:        option_env!("TIKV_BUILD_TIME").unwrap_or(fallback),
18:        option_env!("TIKV_BUILD_RUSTC_VERSION").unwrap_or(fallback),

All of the CARGO_ vars are set by cargo; all the TIKV_ vars are set by the makefile and are need to be set in the build script instead.

Build scripts can set environment variables for their packages by doing thing like

println!("cargo:rustc-env=TIKV_BUILD_TIME=whenever");

So the task is to add a build script to tikv, figure out the correct value of these TIKV_ env vars, tell them to cargo, then remove them from the makefile.

The rustc-version can be retrieved from the rustc_version crate. I don't see another crate that will give you the correct values for the other three, but one can crib off of clippy's similar code.

@overvenus or I can mentor.

@mathstuf

This comment has been minimized.

Copy link

mathstuf commented Feb 6, 2019

We use this approach in ghostflow-director. Extracting this out into a crate for use in build scripts would also be great (embed-vcs-info?).

@mathstuf

This comment has been minimized.

Copy link

mathstuf commented Feb 6, 2019

It'd be nice to also handle build-from-tarball cases where a vcs repository is not available. Git has export-subst and $Format:…$ substitution for this, but I don't know about other version control tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment