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

Add a new build Environment option for CondaBuild to improve conda-built artifacts #4015

Merged
merged 5 commits into from
Oct 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
26 changes: 24 additions & 2 deletions crates/re_build_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,23 @@ pub(crate) fn should_output_cargo_build_instructions() -> bool {
// ------------------

/// Where is this `build.rs` build script running?
#[derive(Clone, Copy)]
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,

/// We are running in the conda build environment.
///
/// This is a particularly special build environment because the branch checked out is
/// from the conda feed-stock and the build happens via source downloaded from the
/// github-hosted tgz.
///
/// See <https://github.com/conda-forge/rerun-sdk-feedstock>.
CondaBuild,

/// Are we a developer running inside the workspace of <https://github.com/rerun-io/rerun> ?
DeveloperInWorkspace,

Expand All @@ -89,6 +99,10 @@ impl Environment {
// `CI` is an env-var set by GitHub actions.
eprintln!("Environment: env-var CI is set");
Self::CI
} else if is_on_conda() {
// `CONDA_BUILD` is an env-var set by conda build
eprintln!("Environment: env-var CONDA_BUILD is set");
Self::CondaBuild
} else if is_tracked_env_var_set("IS_IN_RERUN_WORKSPACE") {
// IS_IN_RERUN_WORKSPACE is set by `.cargo/config.toml` and also in the Rust-analyzer settings in `.vscode/settings.json`
eprintln!("Environment: env-var IS_IN_RERUN_WORKSPACE is set");
Expand All @@ -106,14 +120,20 @@ pub fn is_on_ci() -> bool {
std::env::var("CI").is_ok()
}

/// Are we running in the Conda build environment?
pub fn is_on_conda() -> bool {
// `CONDA_BUILD` is an env-var set by conda build
std::env::var("CONDA_BUILD").is_ok()
}

/// Call from the `build.rs` file of any crate you want to generate build info for.
///
/// Use this crate together with the `re_build_info` crate.
pub fn export_build_info_vars_for_crate(crate_name: &str) {
let environment = Environment::detect();

let export_datetime = match environment {
Environment::PublishingCrates | Environment::CI => true,
Environment::PublishingCrates | Environment::CI | Environment::CondaBuild => true,

Environment::DeveloperInWorkspace => EXPORT_BUILD_TIME_FOR_DEVELOPERS,

Expand All @@ -128,7 +148,9 @@ pub fn export_build_info_vars_for_crate(crate_name: &str) {
Environment::DeveloperInWorkspace => EXPORT_GIT_FOR_DEVELOPERS,

// We shouldn't show the users git hash/branch in the rerun viewer.
Environment::UsedAsDependency => false,
// TODO(jleibs): Conda builds run off a downloaded source tar-ball
// the git environment is from conda itself.
Environment::UsedAsDependency | Environment::CondaBuild => false,
};

if export_datetime {
Expand Down
2 changes: 1 addition & 1 deletion crates/re_build_tools/src/rebuild_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn should_run() -> bool {
// We cannot run this during publishing,
// we don't need to,
// and it can also can cause a Cargo.lock file to be generated.
Environment::PublishingCrates => false,
Environment::PublishingCrates | Environment::CondaBuild => false,

// Dependencies shouldn't change on CI, but who knows 🤷‍♂️
Environment::CI => true,
Expand Down
2 changes: 1 addition & 1 deletion crates/re_renderer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn should_run() -> bool {
Environment::PublishingCrates => false,

// The code we're generating here is actual source code that gets committed into the repository.
Environment::CI => false,
Environment::CI | Environment::CondaBuild => false,

Environment::DeveloperInWorkspace => true,

Expand Down
2 changes: 1 addition & 1 deletion crates/re_types_builder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn should_run() -> bool {
Environment::PublishingCrates => false,

// The code we're generating here is actual source code that gets committed into the repository.
Environment::CI => false,
Environment::CI | Environment::CondaBuild => false,

Environment::DeveloperInWorkspace => {
// This `build.rs` depends on having `flatc` installed,
Expand Down
39 changes: 24 additions & 15 deletions crates/re_viewer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use std::path::Path;

use re_build_tools::Environment;

fn parse_release_version(branch: &str) -> Option<&str> {
// release-\d+.\d+.\d+(-alpha.\d+)?

Expand Down Expand Up @@ -151,21 +153,27 @@ fn parse_frontmatter<P: AsRef<Path>>(path: P) -> anyhow::Result<Option<Frontmatt
)?))
}

fn get_base_url() -> anyhow::Result<String> {
fn get_base_url(build_env: Environment) -> anyhow::Result<String> {
if let Ok(base_url) = re_build_tools::get_and_track_env_var("EXAMPLES_MANIFEST_BASE_URL") {
// override via env var
return Ok(base_url);
}

let branch = re_build_tools::git_branch()?;
if branch == "main" || !re_build_tools::is_on_ci() {
// on `main` and local builds, use `version/nightly`
// this will point to data uploaded by `.github/workflows/reusable_upload_web_demo.yml`
// on every commit to the `main` branch
return Ok("https://demo.rerun.io/version/nightly".into());
}
// In the CondaBuild environment we can't trust the git_branch name -- if it exists
// at all it's going to be the feedstock branch-name, not our Rerun branch. However
// conda should ONLY be building released versions, so we want to version the manifest.
let versioned_manifest = matches!(build_env, Environment::CondaBuild) || {
let branch = re_build_tools::git_branch()?;
if branch == "main" || !re_build_tools::is_on_ci() {
// on `main` and local builds, use `version/nightly`
// this will point to data uploaded by `.github/workflows/reusable_upload_web_demo.yml`
// on every commit to the `main` branch
return Ok("https://demo.rerun.io/version/nightly".into());
}
parse_release_version(&branch).is_some()
};

if parse_release_version(&branch).is_some() {
if versioned_manifest {
let metadata = re_build_tools::cargo_metadata()?;
let workspace_root = metadata
.root_package()
Expand All @@ -187,8 +195,8 @@ fn get_base_url() -> anyhow::Result<String> {

const MANIFEST_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/data/examples_manifest.json");

fn write_examples_manifest() -> anyhow::Result<()> {
let base_url = get_base_url()?;
fn write_examples_manifest(build_env: Environment) -> anyhow::Result<()> {
let base_url = get_base_url(build_env)?;

let mut manifest = vec![];
for example in examples()? {
Expand All @@ -203,18 +211,19 @@ fn write_examples_manifest() -> anyhow::Result<()> {
}

fn write_examples_manifest_if_necessary() {
use re_build_tools::Environment;
let should_run = match Environment::detect() {
let build_env = Environment::detect();

let should_run = match build_env {
// Can't run in thsese situations, because we can't find `examples/python`.
Environment::PublishingCrates | Environment::UsedAsDependency => false,

// Make sure the manifest reflects what is in `examples/python`.
Environment::CI | Environment::DeveloperInWorkspace => true,
Environment::CI | Environment::CondaBuild | Environment::DeveloperInWorkspace => true,
};

if should_run {
re_build_tools::rerun_if_changed_or_doesnt_exist(MANIFEST_PATH);
if let Err(err) = write_examples_manifest() {
if let Err(err) = write_examples_manifest(build_env) {
panic!("{err}");
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/re_web_viewer_server/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn should_run() -> bool {
Environment::PublishingCrates => false,

// TODO(emilk): only build the web viewer explicitly on CI
Environment::CI => true,
Environment::CI | Environment::CondaBuild => true,

Environment::DeveloperInWorkspace => true,

Expand Down
2 changes: 1 addition & 1 deletion examples/rust/objectron/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn should_run() -> bool {

// No need to run this on CI (which means setting up `protoc` etc)
// since the code is committed anyway.
Environment::CI => false,
Environment::CI | Environment::CondaBuild => false,

// Sure - let's keep it up-to-date.
Environment::DeveloperInWorkspace => true,
Expand Down
Loading