From a0c280eae479be1b0f1cd28288d59a5aba18afe5 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Wed, 5 Jan 2022 20:40:35 +0100 Subject: [PATCH] Export some more version metadata --- build.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- src/main.rs | 4 ++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index a353525..733c61e 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,4 @@ +use std::process::Command; use walkdir::{WalkDir, DirEntry}; fn is_hidden(entry: &DirEntry) -> bool { @@ -8,8 +9,25 @@ fn is_hidden(entry: &DirEntry) -> bool { } fn main() { - // Tell cargo to only run this script if the schema files or this script have changed - println!("cargo:rerun-if-changed=schema"); + // Build version number using the current git commit id + let out = Command::new("git").arg("rev-list") + .args(["HEAD", "-1"]) + .output() + .expect("failed to run `git rev-list HEAD -1`"); + let owned_gitrev = String::from_utf8(out.stdout) + .expect("git rev-list output was not valid UTF8"); + let gitrev = owned_gitrev.trim(); + let abbrev = &gitrev[0..9]; + println!("cargo:rustc-env=CARGO_PKG_VERSION_GITREV={}", gitrev); + + let out = Command::new("git").arg("log") + .args(["-1", "--format=%as"]) + .output() + .expect("failed to run `git log -1 --format=\"format:%as\"`"); + let commit_date = String::from_utf8(out.stdout) + .expect("git log output was not valid UTF8"); + let commit_date = commit_date.trim(); + println!("cargo:rustc-env=BFFH_GIT_COMMIT_DATE={}", commit_date); let mut compile_command = ::capnpc::CompilerCommand::new(); @@ -17,6 +35,8 @@ fn main() { // i.e. a file "user.capnp" will result in module "schema::user" compile_command.default_parent_module(vec!["schema".into()]); + println!(">>> Collecting schemas..."); + for entry in WalkDir::new("schema") .max_depth(2) .into_iter() @@ -30,10 +50,35 @@ fn main() { .unwrap_or(false) ) { - println!("Collecting schema file {}", entry.path().display()); + println!(" Collecting schema file {}", entry.path().display()); compile_command.file(entry.path()); } - println!("Compiling schemas..."); + println!(">>> Compiling schemas..."); compile_command.run().expect("Failed to generate API code"); + + println!(">>> Building version number..."); + + let rustc = std::env::var("RUSTC").unwrap(); + let out = Command::new(rustc).arg("--version") + .output() + .expect("failed to run `rustc --version`"); + let rustc_version = String::from_utf8(out.stdout) + .expect("rustc --version returned invalid UTF-8"); + let rustc_version = rustc_version.trim(); + println!("cargo:rustc-env=CARGO_RUSTC_VERSION={}", rustc_version); + + let tagged_release = option_env!("BFFHD_BUILD_TAGGED_RELEASE") == Some("1"); + let release = if tagged_release { + format!("BFFH {version} [{rustc}]", + version = env!("CARGO_PKG_VERSION"), + rustc = rustc_version) + } else { + format!("BFFH {version} ({gitrev} {date}) [{rustc}]", + version=env!("CARGO_PKG_VERSION"), + gitrev=abbrev, + date=commit_date, + rustc=rustc_version) + }; + println!("cargo:rustc-env=BFFHD_RELEASE_STRING={}", release); } diff --git a/src/main.rs b/src/main.rs index 07afdb7..81b756e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,10 @@ use slog::Logger; use paho_mqtt::AsyncClient; use crate::config::{ActorConn, Config, InitiatorConn}; +const RELEASE: &'static str = env!("BFFHD_RELEASE_STRING"); +const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +const GITREV: &'static str = env!("CARGO_PKG_VERSION_GITREV"); + fn main() { use clap::{crate_version, crate_description, crate_name};