Skip to content

Commit

Permalink
Drop vergen dependency
Browse files Browse the repository at this point in the history
We only need the version, build time, rust target triple, and git commit
for the version command. These values can easily be generated with little
code so we do not need an extra dependency for this.

The main reason for this change is that vergen will fail if no git
commit is found. On distro build systems this is often the case since
they build from a source tar without the git repo. Instead of erroring
we should just show an empty commit in the version output.

Fixes #247

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
  • Loading branch information
Luap99 committed Mar 8, 2022
1 parent 2e95ae3 commit bdaef37
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 143 deletions.
132 changes: 0 additions & 132 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ sha2 = "0.10.1"
netlink-packet-route = "0.11"

[build-dependencies]
vergen = { version = "6.0.2", default-features = false, features = ["build", "rustc", "git"] }
anyhow = "1.0"
chrono = "*"
27 changes: 23 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
use anyhow::Result;
use vergen::{vergen, Config};
use chrono::Utc;
use std::process::Command;

fn main() -> Result<()> {
fn main() {
// Generate the default 'cargo:' instruction output
vergen(Config::default())
println!("cargo:rerun-if-changed=build.rs");

// get timestamp
let now = Utc::now();
println!("cargo:rustc-env=BUILD_TIMESTAMP={}", now.to_rfc3339());

// get rust target triple from TARGET env
println!(
"cargo:rustc-env=BUILD_TARGET={}",
std::env::var("TARGET").unwrap()
);

// get git commit
let command = Command::new("git").args(&["rev-parse", "HEAD"]).output();
let commit = match command {
Ok(output) => String::from_utf8(output.stdout).unwrap(),
// if error, e.g. build from source with git repo, just show empty string
Err(_) => "".to_string(),
};
println!("cargo:rustc-env=GIT_COMMIT={}", commit);
}
8 changes: 4 additions & 4 deletions src/commands/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ struct Info {
impl Version {
pub fn exec(&self) -> Result<(), Box<dyn Error>> {
let info = Info {
version: env!("VERGEN_BUILD_SEMVER"),
commit: env!("VERGEN_GIT_SHA"),
build_time: env!("VERGEN_BUILD_TIMESTAMP"),
target: env!("VERGEN_RUSTC_HOST_TRIPLE"),
version: env!("CARGO_PKG_VERSION"),
commit: env!("GIT_COMMIT"),
build_time: env!("BUILD_TIMESTAMP"),
target: env!("BUILD_TARGET"),
};

let out = serde_json::to_string_pretty(&info)?;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use netavark::commands::teardown;
use netavark::commands::version;

#[derive(Parser, Debug)]
#[clap(version = env!("VERGEN_BUILD_SEMVER"))]
#[clap(version = env!("CARGO_PKG_VERSION"))]
struct Opts {
/// Instead of reading from STDIN, read the configuration to be applied from the given file.
#[clap(short, long)]
Expand Down

0 comments on commit bdaef37

Please sign in to comment.