-
Notifications
You must be signed in to change notification settings - Fork 373
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
Refactor our build.rs
files
#3789
Changes from 11 commits
f4bb70f
a9b87f9
50cbb8c
4a5f924
f39658d
26eb60f
eb96915
f7fadbd
339f1bf
68a6dac
5f5c9c3
1ba2bc1
e2cd48d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
fn main() { | ||
re_build_tools::rebuild_if_crate_changed("re_analytics"); | ||
re_build_tools::export_env_vars(); | ||
re_build_tools::export_build_info_vars_for_crate("re_analytics"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
#![allow(clippy::unwrap_used)] | ||
|
||
//! 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 _; | ||
|
||
|
@@ -36,31 +34,88 @@ pub(crate) fn should_output_cargo_build_instructions() -> bool { | |
OUTPUT_CARGO_BUILD_INSTRUCTIONS.load(Ordering::Relaxed) | ||
} | ||
|
||
// 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. | ||
pub enum Environment { | ||
/// We are running `cargo publish` (via `scripts/ci/crates.py`); _probably_ on CI. | ||
PublishingCrates, | ||
|
||
/// We are running on CI, but NOT publishing crates | ||
CI, | ||
|
||
/// Are we a developer running inside the workspace of <https://github.com/rerun-io/rerun> ? | ||
DeveloperInWorkspace, | ||
|
||
/// We are likely running on a users machine. | ||
/// | ||
/// Try to do as little work as possible. | ||
ProbablyUserMachine, | ||
} | ||
|
||
impl Environment { | ||
/// Detect what environment we are running in. | ||
pub fn detect() -> Self { | ||
if is_publishing_crates() { | ||
Self::PublishingCrates | ||
} else if is_on_ci() { | ||
Self::CI | ||
} else if is_in_rerun_workspace() { | ||
Self::DeveloperInWorkspace | ||
} else { | ||
Self::ProbablyUserMachine | ||
} | ||
} | ||
} | ||
|
||
/// Are we running inside the workspace of <https://github.com/rerun-io/rerun> ? | ||
/// | ||
/// Otherwise we might be running on users machines. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an oddly formulated comment 🤔 Running inside the workspace and running on users machines are not antithetical There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, good catch - I'll improve the naming and documentation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to communicate "Rerun user" |
||
fn is_in_rerun_workspace() -> bool { | ||
is_tracked_env_var_set("IS_IN_RERUN_WORKSPACE") | ||
} | ||
|
||
/// Are we running on a CI machine? | ||
pub fn is_on_ci() -> bool { | ||
// `CI` is an env-var set by GitHub actions. | ||
std::env::var("CI").is_ok() | ||
} | ||
|
||
/// Are we currently in the process of publishing crates? | ||
/// | ||
/// This is usually done on CI. | ||
fn is_publishing_crates() -> bool { | ||
// "RERUN_IS_PUBLISHING" is set by `scripts/ci/crates.py` | ||
is_tracked_env_var_set("RERUN_IS_PUBLISHING") | ||
} | ||
|
||
/// Call from the `build.rs` file of any crate you want to generate build info for. | ||
pub fn export_env_vars() { | ||
/// | ||
/// Use this crate together with the `re_build_info` crate. | ||
pub fn export_build_info_vars_for_crate(crate_name: &str) { | ||
rebuild_if_crate_changed(crate_name); | ||
export_build_info_env_vars(); | ||
} | ||
|
||
/// # Situations to consider regarding git | ||
/// | ||
/// ## 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 export_build_info_env_vars() { | ||
// target triple | ||
set_env("RE_BUILD_TARGET_TRIPLE", &std::env::var("TARGET").unwrap()); | ||
set_env("RE_BUILD_GIT_HASH", &git_hash().unwrap_or_default()); | ||
|
@@ -74,9 +129,8 @@ pub fn export_env_vars() { | |
// 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. | ||
if is_on_ci() { | ||
// e.g. building wheels on CI. | ||
set_env("RE_BUILD_IS_IN_RERUN_WORKSPACE", "no"); | ||
} else { | ||
set_env( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
fn main() { | ||
re_build_tools::rebuild_if_crate_changed("re_data_source"); | ||
re_build_tools::export_env_vars(); | ||
re_build_tools::export_build_info_vars_for_crate("re_data_source"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
fn main() { | ||
re_build_tools::rebuild_if_crate_changed("re_sdk"); | ||
re_build_tools::export_env_vars(); | ||
re_build_tools::export_build_info_vars_for_crate("re_sdk"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, that whole notion of "user machine" is confusing.
I guess this means "OutOfWorkspace" or smth, which is what I would call it then.