Skip to content

Commit

Permalink
introduce re_types_builder
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jun 11, 2023
1 parent e5fe858 commit e4cea00
Show file tree
Hide file tree
Showing 13 changed files with 3,569 additions and 3 deletions.
60 changes: 57 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ re_tensor_ops = { path = "crates/re_tensor_ops", version = "0.7.0-alpha.0", defa
re_time_panel = { path = "crates/re_time_panel", version = "=0.7.0-alpha.0", default-features = false }
re_tracing = { path = "crates/re_tracing", version = "0.7.0-alpha.0", default-features = false }
re_tuid = { path = "crates/re_tuid", version = "0.7.0-alpha.0", default-features = false }
re_types_builder = { path = "crates/re_types_builder", version = "=0.7.0-alpha.0", default-features = false }
re_ui = { path = "crates/re_ui", version = "0.7.0-alpha.0", default-features = false }
re_viewer = { path = "crates/re_viewer", version = "0.7.0-alpha.0", default-features = false }
re_viewer_context = { path = "crates/re_viewer_context", version = "0.7.0-alpha.0", default-features = false }
Expand Down
38 changes: 38 additions & 0 deletions crates/re_types_builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "re_types_builder"
authors.workspace = true
description = "Generates code for Rerun's SDKs from flatbuffers definitions."
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]

# External
anyhow.workspace = true
arrow2.workspace = true
convert_case = "0.6"
flatbuffers = "23.0"
indent = "0.1"
unindent = "0.2"
xshell = "0.2"


[build-dependencies]

# Rerun
re_build_tools.workspace = true

# External
xshell = "0.2"
13 changes: 13 additions & 0 deletions crates/re_types_builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# re_types_builder

Part of the [`rerun`](https://github.com/rerun-io/rerun) family of crates.

[![Latest version](https://img.shields.io/crates/v/re_types_builder.svg)](https://crates.io/crates/re_types_builder)
[![Documentation](https://docs.rs/re_types_builder/badge.svg)](https://docs.rs/re_types_builder)
![MIT](https://img.shields.io/badge/license-MIT-blue.svg)
![Apache](https://img.shields.io/badge/license-Apache-blue.svg)

This crate implements Rerun's code generation tools.

These tools translate language-agnostic IDL definitions (flatbuffers) into code.
They are invoked from `re_types`' build script (`build.rs`).
70 changes: 70 additions & 0 deletions crates/re_types_builder/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Generates flatbuffers reflection code from `reflection.fbs`.
use xshell::{cmd, Shell};

use re_build_tools::{
compute_file_hash, is_tracked_env_var_set, read_versioning_hash, rerun_if_changed,
rerun_if_changed_or_doesnt_exist, write_versioning_hash,
};

// ---

// NOTE: Don't need to add extra context to xshell invocations, it does so on its own.

const SOURCE_HASH_PATH: &str = "./source_hash.txt";
const FBS_REFLECTION_DEFINITION_PATH: &str = "./definitions/reflection.fbs";

fn main() {
if std::env::var("CI").is_ok() {
// Don't run on CI!
//
// The code we're generating here is actual source code that gets committed into the
// repository.
return;
}

if !is_tracked_env_var_set("IS_IN_RERUN_WORKSPACE") {
// Only run if we are in the rerun workspace, not on users machines.
return;
}
if is_tracked_env_var_set("RERUN_IS_PUBLISHING") {
// We don't need to rebuild - we should have done so beforehand!
// See `RELEASES.md`
return;
}

rerun_if_changed_or_doesnt_exist(SOURCE_HASH_PATH);
rerun_if_changed(FBS_REFLECTION_DEFINITION_PATH);

let cur_hash = read_versioning_hash(SOURCE_HASH_PATH);
let new_hash = compute_file_hash(FBS_REFLECTION_DEFINITION_PATH);

// Leave these be please, very useful when debugging.
eprintln!("cur_hash: {cur_hash:?}");
eprintln!("new_hash: {new_hash:?}");

if let Some(cur_hash) = cur_hash {
if cur_hash == new_hash {
// Source definition hasn't changed, no need to do anything.
return;
}
}

let sh = Shell::new().unwrap();
cmd!(
sh,
"flatc -o src/ --rust --gen-onefile --filename-suffix '' {FBS_REFLECTION_DEFINITION_PATH}"
)
.run()
.unwrap();

// NOTE: We're purposefully ignoring the error here.
//
// In the very unlikely chance that the user doesn't have `rustfmt` in their $PATH, there's
// still no good reason to fail the build.
//
// The CI will catch the unformatted file at PR time and complain appropriately anyhow.
cmd!(sh, "cargo fmt").run().ok();

write_versioning_hash(SOURCE_HASH_PATH, new_hash);
}
Loading

0 comments on commit e4cea00

Please sign in to comment.