Skip to content

Commit

Permalink
fix: autogenerate version string correctly in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyscot committed Nov 2, 2024
1 parent 3692293 commit 922b166
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 135 deletions.
121 changes: 0 additions & 121 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 @@ -21,7 +21,7 @@ anstyle = "1.0.8"
anyhow = "1.0.89"
capnp = "0.20.1"
capnp-futures = "0.20.0"
clap = { version = "4.5.19", features = ["wrap_help", "derive", "cargo", "help"] }
clap = { version = "4.5.19", features = ["wrap_help", "derive", "cargo", "help", "string"] }
console = "0.15.8"
dns-lookup = "2.0.4"
futures-util = { version = "0.3.31", default-features = false }
Expand All @@ -44,7 +44,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
nix = { version = "0.29.0", features = ["socket"] }

[build-dependencies]
built = { version = "0.7.4", features = ["git2"] }
capnpc = "0.20.0"

[dev-dependencies]
Expand Down
40 changes: 39 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(missing_docs)]

fn main() {
built::write_built_file().expect("Failed to acquire build-time information");
process_version_string();

capnpc::CompilerCommand::new()
.src_prefix("schema")
Expand All @@ -17,3 +17,41 @@ fn main() {
.run()
.expect("control protocol compiler command");
}

fn process_version_string() {
let hash = git_short_hash().unwrap();
println!("cargo:rustc-env=QCP_BUILD_GIT_HASH={hash}");
let cargo_version = env!("CARGO_PKG_VERSION");

let version_string = if let Some(tag) = github_tag() {
// This is a tagged build running in CI
println!("cargo:rustc-env=QCP_CI_TAG_VERSION={tag}");
// Sanity check. We tag releases as "v1.2.3", so strip off the leading v before matching.
let short_tag = tag.strip_prefix("v").unwrap_or(&tag);
assert_eq!(
cargo_version, short_tag,
"mismatched cargo and CI version tags"
);
tag
} else {
format!("{cargo_version}+g{hash}")
};
println!("cargo:rustc-env=QCP_VERSION_STRING={version_string}");
}

fn github_tag() -> Option<String> {
std::env::var("GITHUB_REF_TYPE")
.is_ok_and(|v| v == "tag")
.then(|| std::env::var("GITHUB_REF_NAME").unwrap())
}

fn git_short_hash() -> Option<String> {
use std::process::Command;
let args = &["rev-parse", "--short=8", "HEAD"];
let output = Command::new("git").args(args).output().unwrap();
let rev = String::from_utf8_lossy(&output.stdout).trim().to_string();
if rev.is_empty() {
return None;
}
Some(rev)
}
15 changes: 11 additions & 4 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// QCP top-level command-line arguments
// (c) 2024 Ross Younger

use crate::build_info;
use clap::Parser;
use clap::{Args as _, FromArgMatches as _, Parser};

/// Options that switch us into another mode i.e. which don't require source/destination arguments
pub(crate) const MODE_OPTIONS: &[&str] = &["server", "help_buffers"];

#[derive(Debug, Parser, Clone)]
#[command(
author,
version,
version(build_info::GIT_VERSION),
// we set short/long version strings explicitly, see custom_parse()
about,
before_help = "e.g. qcp some/file my-server:some-directory/",
infer_long_args(true)
Expand Down Expand Up @@ -70,3 +68,12 @@ pub(crate) struct CliArgs {
//
// N.B. ClientOptions has positional arguments!
}

impl CliArgs {
/// Sets up and executes ou
pub(crate) fn custom_parse() -> Self {
let cli = clap::Command::new(clap::crate_name!());
let cli = CliArgs::augment_args(cli).version(crate::version::short());
CliArgs::from_arg_matches(&cli.get_matches_from(std::env::args_os())).unwrap()
}
}
3 changes: 1 addition & 2 deletions src/cli/cli_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
server::server_main,
util::setup_tracing,
};
use clap::Parser;
use indicatif::{MultiProgress, ProgressDrawTarget};
use tracing::error_span;

Expand All @@ -23,7 +22,7 @@ use tracing::error_span;
#[tokio::main(flavor = "current_thread")]
#[allow(clippy::missing_panics_doc)]
pub async fn cli() -> anyhow::Result<ExitCode> {
let args = CliArgs::parse();
let args = CliArgs::custom_parse();
if args.help_buffers {
os::print_udp_buffer_size_help_message(
args.bandwidth.recv_buffer(),
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,4 @@ pub mod doc;

pub mod os;

/// Build-time info (autogenerated by `built`)
#[allow(unreachable_pub)]
mod build_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
mod version;
12 changes: 12 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Autogenerated build-time information
// (c) 2024 Ross Younger

/// Short version string
pub(crate) fn short() -> String {
// this _should_ be provided by our build script; if not, something went wrong
if let Some(v) = option_env!("QCP_VERSION_STRING") {
return v.to_string();
}
let hash = option_env!("QCP_BUILD_GIT_HASH").unwrap_or("???");
format!("{}+g{hash}", env!("CARGO_PKG_VERSION"))
}

0 comments on commit 922b166

Please sign in to comment.