Skip to content
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

More information in --version #1388

Merged
merged 16 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -69,6 +71,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 Down Expand Up @@ -45,5 +46,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}");
Wumpf marked this conversation as resolved.
Show resolved Hide resolved

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