Skip to content

Commit

Permalink
More information in --version (#1388)
Browse files Browse the repository at this point in the history
* Add the re_build_info and re_build_build_info crates

* Add datetime

* --version prints the full build info

* Add git branch and clean/dirty flag

* fix extra --dirty

* Fix analytics link

* Add note about not moving the viewer_analytics.rs file

* Add About-menu with version number, target triplet, and build date

also add link to rerun.io in it

* Add help-button to rerun menu

* Fix web build

* Add crate-level docs

* Rerun re_build_build_info when branch or commit hash changes

* Fix merge-snafu

* Ignore if the repo is dirty or clean
  • Loading branch information
emilk committed Feb 26, 2023
1 parent 642b8d3 commit 4f8468a
Show file tree
Hide file tree
Showing 27 changed files with 409 additions and 110 deletions.
24 changes: 23 additions & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ version = "0.2.0"
[workspace.dependencies]
re_analytics = { path = "crates/re_analytics", version = "0.2.0" }
re_arrow_store = { path = "crates/re_arrow_store", version = "0.2.0" }
re_build_build_info = { path = "crates/re_build_build_info", version = "0.2.0" }
re_build_info = { path = "crates/re_build_info", version = "0.2.0" }
re_data_store = { path = "crates/re_data_store", version = "0.2.0" }
re_error = { path = "crates/re_error", version = "0.2.0" }
re_format = { path = "crates/re_format", version = "0.2.0" }
Expand Down Expand Up @@ -68,6 +70,7 @@ polars-ops = "0.27.1"
puffin = "0.14"
reqwest = { version = "0.11", default-features = false }
thiserror = "1.0"
time = "0.3"
tokio = "1.24"
wgpu = { version = "0.15", default-features = false }
wgpu-core = { version = "0.15", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions crates/re_analytics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ all-features = true

[dependencies]
# Internal dependencies:
re_build_info.workspace = true
re_log.workspace = true

# External dependencies:
Expand All @@ -44,5 +45,4 @@ tracing-subscriber = "0.3"


[build-dependencies]
anyhow.workspace = true
glob = "0.3"
re_build_build_info.workspace = true
73 changes: 1 addition & 72 deletions crates/re_analytics/build.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,3 @@
use std::process::Command;

// Situations to consider
// ----------------------
//
// # Using the published crate
//
// The published crate carries its version around, which in turns gives us the git tag, which makes
// the commit hash irrelevant.
// We still need to compute _something_ so that we can actually build, but that value will be
// ignored when the crate is built by the end user anyhow.
//
// # Working directly within the workspace
//
// When working within the workspace, we can simply try and call `git` and we're done.
//
// # Using an unpublished crate (e.g. `path = "..."` or `git = "..."` or `[patch.crates-io]`)
//
// In these cases we may or may not have access to the workspace (e.g. a `path = ...` import likely
// will, while a crate patch won't).
//
// This is not an issue however, as we can simply try and see what we get.
// If we manage to compute a commit hash, great, otherwise we still have the crate version to
// fallback on.

fn main() {
// target triple
println!(
"cargo:rustc-env=__RERUN_TARGET_TRIPLE={}",
std::env::var("TARGET").unwrap()
);

match git_hash() {
Ok(git_hash) => {
println!("cargo:rustc-env=__RERUN_GIT_HASH={git_hash}");
for path in glob::glob("../../.git/refs/heads/**").unwrap() {
println!("cargo:rerun-if-changed={}", path.unwrap().to_string_lossy());
}
}
// NOTE: In 99% of cases, if `git_hash` failed it's because we're not in a git repository
// to begin with, which happens because we've imported the published crate from crates.io.
//
// When that happens, we want the commit hash to be the git tag that corresponds to the
// published version, so that one can always easily checkout the `git_hash` field in the
// analytics.
//
// Example of unlikely cases where the above does not hold:
// - `git` is not installed
// - the user downloaded rerun as a tarball and then imported via a `path = ...` import
// - others?
Err(_) => println!(
"cargo:rustc-env=__RERUN_GIT_HASH=v{}",
env!("CARGO_PKG_VERSION")
),
}
}

fn git_hash() -> anyhow::Result<String> {
let output = Command::new("git").args(["rev-parse", "HEAD"]).output()?;

let git_hash = String::from_utf8(output.stdout)?;
let git_hash = git_hash.trim();
if git_hash.is_empty() {
anyhow::bail!("empty commit hash");
}

let clean = Command::new("git")
.args(["diff-files", "--quiet"])
.output()?
.status
.success();

Ok(format!("{}{}", git_hash, if clean { "" } else { "-dirty" }))
re_build_build_info::export_env_vars();
}
7 changes: 5 additions & 2 deletions crates/re_analytics/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ const DETAILS: &str = "
";

pub fn print_details() {
let git_hash = env!("__RERUN_GIT_HASH").replace("-dirty", "");
eprintln!("{}", DETAILS.replace("GIT_HASH", &git_hash));
let build_info = re_build_info::build_info!();
eprintln!(
"{}",
DETAILS.replace("GIT_HASH", &build_info.git_hash_or_tag())
);
}
11 changes: 0 additions & 11 deletions crates/re_analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ use time::OffsetDateTime;

// ----------------------------------------------------------------------------

/// What target triplet (os, cpu) `re_analytics` was compiled for.
pub const TARGET_TRIPLET: &str = env!("__RERUN_TARGET_TRIPLE");

/// What git hash of the Rerun repository we were compiled in.
///
/// If we are not in the Rerun repository, this will be set
/// to the `re_analytics` crate version (`CARGO_PKG_VERSION`) instead.
pub const GIT_HASH: &str = env!("__RERUN_GIT_HASH");

// ----------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum EventKind {
/// Append a new event to the time series associated with this analytics ID.
Expand Down
22 changes: 22 additions & 0 deletions crates/re_build_build_info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "re_build_build_info"
authors.workspace = true
description = "build.rs helpers for generating build info"
edition.workspace = true
homepage.workspace = true
include.workspace = true
license.workspace = true
publish = true
readme = "README.md"
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[package.metadata.docs.rs]
all-features = true


[dependencies]
anyhow.workspace = true
glob = "0.3"
time = { workspace = true, features = ["formatting"] }
92 changes: 92 additions & 0 deletions crates/re_build_build_info/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! This crate is to be used from `build.rs` build scripts.
//!
//! Use this crate together with the `re_build_info` crate.

use anyhow::Context as _;

use std::process::Command;

// Situations to consider
// ----------------------
//
// # Using the published crate
//
// The published crate carries its version around, which in turns gives us the git tag, which makes
// the commit hash irrelevant.
// We still need to compute _something_ so that we can actually build, but that value will be
// ignored when the crate is built by the end user anyhow.
//
// # Working directly within the workspace
//
// When working within the workspace, we can simply try and call `git` and we're done.
//
// # Using an unpublished crate (e.g. `path = "..."` or `git = "..."` or `[patch.crates-io]`)
//
// In these cases we may or may not have access to the workspace (e.g. a `path = ...` import likely
// will, while a crate patch won't).
//
// This is not an issue however, as we can simply try and see what we get.
// If we manage to compute a commit hash, great, otherwise we still have the crate version to
// fallback on.

/// Call from the `build.rs` file of any crate you want to generate build info for.
pub fn export_env_vars() {
// target triple
println!(
"cargo:rustc-env=RE_BUILD_TARGET_TRIPLE={}",
std::env::var("TARGET").unwrap()
);

let git_hash = git_hash().unwrap_or_default();
println!("cargo:rustc-env=RE_BUILD_GIT_HASH={git_hash}");

let git_branch = git_branch().unwrap_or_default();
println!("cargo:rustc-env=RE_BUILD_GIT_BRANCH={git_branch}");

let time_format =
time::format_description::parse("[year]-[month]-[day]T[hour]:[minute]:[second]Z").unwrap();
let date_time = time::OffsetDateTime::now_utc()
.format(&time_format)
.unwrap();
println!("cargo:rustc-env=RE_BUILD_DATETIME={date_time}");

// Make sure we re-run the build script if the branch or commit changes:
if let Ok(head_path) = git_path("HEAD") {
eprintln!("cargo:rerun-if-changed={head_path}"); // Track changes to branch
if let Ok(head) = std::fs::read_to_string(&head_path) {
if let Some(git_file) = head.strip_prefix("ref: ") {
if let Ok(path) = git_path(git_file) {
eprintln!("cargo:rerun-if-changed={path}"); // Track changes to commit hash
}
}
}
}
}

fn run_command(cmd: &'static str, args: &[&str]) -> anyhow::Result<String> {
let output = Command::new(cmd)
.args(args)
.output()
.with_context(|| format!("running '{cmd}'"))?;
Ok(String::from_utf8(output.stdout)?.trim().to_owned())
}

fn git_hash() -> anyhow::Result<String> {
let git_hash = run_command("git", &["rev-parse", "HEAD"])?;
if git_hash.is_empty() {
anyhow::bail!("empty commit hash");
}
Ok(git_hash)
}

fn git_branch() -> anyhow::Result<String> {
run_command("git", &["symbolic-ref", "--short", "HEAD"])
}

/// From <https://git-scm.com/docs/git-rev-parse>:
///
/// Resolve `$GIT_DIR/<path>` and takes other path relocation variables such as `$GIT_OBJECT_DIRECTORY`, `$GIT_INDEX_FILE…​` into account.
/// For example, if `$GIT_OBJECT_DIRECTORY` is set to /foo/bar then `git rev-parse --git-path objects/abc` returns `/foo/bar/abc`.
fn git_path(path: &str) -> anyhow::Result<String> {
run_command("git", &["rev-parse", "--git-path", path])
}
16 changes: 16 additions & 0 deletions crates/re_build_info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "re_build_info"
authors.workspace = true
description = "Information about the build. Use together with re_build_build_info"
edition.workspace = true
homepage.workspace = true
include.workspace = true
license.workspace = true
publish = true
readme = "README.md"
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[package.metadata.docs.rs]
all-features = true
Loading

1 comment on commit 4f8468a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: 4f8468a Previous: 642b8d3 Ratio
datastore/insert/batch/rects/insert 549792 ns/iter (± 3167) 553015 ns/iter (± 6215) 0.99
datastore/latest_at/batch/rects/query 1842 ns/iter (± 12) 1859 ns/iter (± 1) 0.99
datastore/latest_at/missing_components/primary 356 ns/iter (± 2) 355 ns/iter (± 3) 1.00
datastore/latest_at/missing_components/secondaries 419 ns/iter (± 5) 423 ns/iter (± 0) 0.99
datastore/range/batch/rects/query 151052 ns/iter (± 1292) 155102 ns/iter (± 140) 0.97
mono_points_arrow/generate_message_bundles 48445375 ns/iter (± 1774428) 47120530 ns/iter (± 669492) 1.03
mono_points_arrow/generate_messages 136702497 ns/iter (± 1659147) 126295565 ns/iter (± 1184888) 1.08
mono_points_arrow/encode_log_msg 164855431 ns/iter (± 1057345) 154542958 ns/iter (± 1639842) 1.07
mono_points_arrow/encode_total 351685210 ns/iter (± 3606074) 327157402 ns/iter (± 2075839) 1.07
mono_points_arrow/decode_log_msg 186173612 ns/iter (± 1905498) 181480995 ns/iter (± 1039788) 1.03
mono_points_arrow/decode_message_bundles 73076209 ns/iter (± 1177197) 63653200 ns/iter (± 860200) 1.15
mono_points_arrow/decode_total 256329369 ns/iter (± 2419251) 240086886 ns/iter (± 1479949) 1.07
batch_points_arrow/generate_message_bundles 322821 ns/iter (± 2754) 326585 ns/iter (± 554) 0.99
batch_points_arrow/generate_messages 6190 ns/iter (± 71) 6246 ns/iter (± 80) 0.99
batch_points_arrow/encode_log_msg 365590 ns/iter (± 3324) 373484 ns/iter (± 2663) 0.98
batch_points_arrow/encode_total 712129 ns/iter (± 6658) 717265 ns/iter (± 3685) 0.99
batch_points_arrow/decode_log_msg 346470 ns/iter (± 3586) 352908 ns/iter (± 1515) 0.98
batch_points_arrow/decode_message_bundles 1992 ns/iter (± 23) 2031 ns/iter (± 14) 0.98
batch_points_arrow/decode_total 355692 ns/iter (± 2599) 354202 ns/iter (± 1953) 1.00
arrow_mono_points/insert 6985053068 ns/iter (± 176020015) 6175273502 ns/iter (± 26181726) 1.13
arrow_mono_points/query 1717200 ns/iter (± 14682) 1742076 ns/iter (± 16239) 0.99
arrow_batch_points/insert 2603860 ns/iter (± 19505) 2705043 ns/iter (± 31465) 0.96
arrow_batch_points/query 17440 ns/iter (± 101) 17139 ns/iter (± 145) 1.02
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.