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

Analytics: fix workspace detection #1437

Merged
merged 9 commits into from
Mar 2, 2023
6 changes: 1 addition & 5 deletions crates/re_analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ impl Event {
.with_prop("git_branch", build_info.git_branch)
.with_prop("build_date", build_info.datetime)
.with_prop("debug", cfg!(debug_assertions)) // debug-build?
.with_prop(
// proxy for "user checked out the project and built it from source":
"rerun_workspace",
std::env::var("IS_IN_RERUN_WORKSPACE").is_ok(),
)
.with_prop("rerun_workspace", build_info.is_in_rerun_workspace)
}
}

Expand Down
31 changes: 21 additions & 10 deletions crates/re_build_build_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,30 @@ use std::process::Command;
/// 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()
);
set_env("RE_BUILD_TARGET_TRIPLE", &std::env::var("TARGET").unwrap());
set_env("RE_BUILD_GIT_HASH", &git_hash().unwrap_or_default());
set_env("RE_BUILD_GIT_BRANCH", &git_branch().unwrap_or_default());

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}");
// We need to check `IS_IN_RERUN_WORKSPACE` in the build-script (here),
// because otherwise it won't show up when compiling through maturin.
// We must also make an exception for when we build actual wheels (on CI) for release.
if std::env::var("CI").is_ok() {
// Probably building wheels on CI.
// `CI` is an env-var set by GitHub actions.
set_env("RE_BUILD_IS_IN_RERUN_WORKSPACE", "no");
} else {
set_env(
"RE_BUILD_IS_IN_RERUN_WORKSPACE",
&std::env::var("IS_IN_RERUN_WORKSPACE").unwrap_or_default(),
);
}

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}");
set_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") {
Expand All @@ -63,6 +70,10 @@ pub fn export_env_vars() {
}
}

fn set_env(name: &str, value: &str) {
println!("cargo:rustc-env={name}={value}");
}

fn run_command(cmd: &'static str, args: &[&str]) -> anyhow::Result<String> {
let output = Command::new(cmd)
.args(args)
Expand Down
6 changes: 6 additions & 0 deletions crates/re_build_info/src/buid_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ pub struct BuildInfo {
/// Current git branch, or empty string.
pub git_branch: &'static str,

/// True if we are building within the rerun repository workspace.
///
/// This is a good proxy for "user checked out the project and built it from source".
pub is_in_rerun_workspace: bool,

/// Target architecture and OS
///
/// Example: `xaarch64-apple-darwin`
Expand Down Expand Up @@ -52,6 +57,7 @@ impl std::fmt::Display for BuildInfo {
version,
git_hash,
git_branch,
is_in_rerun_workspace: _,
target_triple,
datetime,
} = self;
Expand Down
1 change: 1 addition & 0 deletions crates/re_build_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ macro_rules! build_info {
version: $crate::RustVersion::parse(env!("CARGO_PKG_VERSION")),
git_hash: env!("RE_BUILD_GIT_HASH"),
git_branch: env!("RE_BUILD_GIT_BRANCH"),
is_in_rerun_workspace: env!("RE_BUILD_IS_IN_RERUN_WORKSPACE") == "yes",
target_triple: env!("RE_BUILD_TARGET_TRIPLE"),
datetime: env!("RE_BUILD_DATETIME"),
}
Expand Down
1 change: 1 addition & 0 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ fn about_rerun_ui(ui: &mut egui::Ui, build_info: &re_build_info::BuildInfo) {
version,
git_hash: _,
git_branch: _,
is_in_rerun_workspace: _,
target_triple,
datetime,
} = *build_info;
Expand Down