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

Refactor: re_sdk no longer depends on re_viewer #1507

Merged
merged 30 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
da739d5
Refactor: move the session log sink to its own file
emilk Mar 4, 2023
0fc0583
Turn LogSink into a trait
emilk Mar 4, 2023
5913ce8
Move spawning of native viewer to `rerun` crate
emilk Mar 4, 2023
74a2cee
Move the `show()` method to `rerun` crate
emilk Mar 4, 2023
1a3fdc7
re_sdk no longer depend directly on re_viewer
emilk Mar 4, 2023
de0370d
Prevent re_viewer from accidentally depending on re_web_viewer_server
emilk Mar 4, 2023
ab2847e
Improve doc-strings
emilk Mar 4, 2023
593d02b
Remove the native_viewer feature from re_sdk
emilk Mar 4, 2023
bfabb19
Remove direct dependency of rerun_py on re_sdk_comms
emilk Mar 4, 2023
bd2cdc8
Small doc-fixes
emilk Mar 4, 2023
20967f6
Move `clap` hookup from `re_sdk` to `rerun`
emilk Mar 4, 2023
6f0c874
Move the `session.serve()` method to trait `rerun::WebViewerSessionExt`
emilk Mar 4, 2023
ead647d
cleanup
emilk Mar 4, 2023
bde8830
Remove cyclic-dependency-protection because it caused weird warning:
emilk Mar 4, 2023
9cd90c9
Fix web-viewer
emilk Mar 4, 2023
dc0a971
Fix python build
emilk Mar 4, 2023
c04001e
Fix remove_dir_all RUSTSEC
emilk Mar 4, 2023
9f50b60
Merge branch 'emilk/remove-remove_dir_all' into emilk/rust-sdk-refactor
emilk Mar 4, 2023
cc63258
Trim re_sdk dependency list
emilk Mar 4, 2023
6f01eec
Fix doc-tests
emilk Mar 4, 2023
d8043dd
Fix doclinks
emilk Mar 4, 2023
0f1f436
Document rust_version
emilk Mar 4, 2023
98705aa
Better docs for set_sink
emilk Mar 4, 2023
e087a74
Hide `global_session()` behind the `global` feature flag
emilk Mar 5, 2023
62023b5
Fix typos
emilk Mar 5, 2023
501fe30
Merge branch 'main' into emilk/rust-sdk-refactor
teh-cmc Mar 6, 2023
454f78f
Proper rustc & LLVM versions (#1512)
teh-cmc Mar 6, 2023
c9de301
Rename "global" feature to "global_session"
emilk Mar 6, 2023
1dd0333
Replace extension trait with `serve_web_viewer` method
emilk Mar 6, 2023
f5d960a
Merge branch 'main' into emilk/rust-sdk-refactor
emilk Mar 6, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/typos.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://github.com/crate-ci/typos
# Add exceptions to _typos.toml
# install and run locally: cargo install typos-cli && typos

name: Spell Check
on: [pull_request]
Expand Down
10 changes: 3 additions & 7 deletions Cargo.lock

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

24 changes: 19 additions & 5 deletions crates/re_analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,27 @@ impl Event {

/// Adds Rerun version, git hash, build date and similar as properties to the event.
pub fn with_build_info(self, build_info: &re_build_info::BuildInfo) -> Event {
self.with_prop("rerun_version", build_info.version.to_string())
.with_prop("target", build_info.target_triple)
let re_build_info::BuildInfo {
crate_name: _,
version,
rustc_version,
llvm_version,
git_hash: _,
git_branch,
is_in_rerun_workspace,
target_triple,
datetime,
} = build_info;

self.with_prop("rerun_version", version.to_string())
.with_prop("rust_version", (*rustc_version).to_owned())
.with_prop("llvm_version", (*llvm_version).to_owned())
.with_prop("target", *target_triple)
.with_prop("git_hash", build_info.git_hash_or_tag())
.with_prop("git_branch", build_info.git_branch)
.with_prop("build_date", build_info.datetime)
.with_prop("git_branch", *git_branch)
.with_prop("build_date", *datetime)
.with_prop("debug", cfg!(debug_assertions)) // debug-build?
.with_prop("rerun_workspace", build_info.is_in_rerun_workspace)
.with_prop("rerun_workspace", *is_in_rerun_workspace)
}
}

Expand Down
46 changes: 45 additions & 1 deletion crates/re_build_build_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub fn export_env_vars() {
set_env("RE_BUILD_GIT_HASH", &git_hash().unwrap_or_default());
set_env("RE_BUILD_GIT_BRANCH", &git_branch().unwrap_or_default());

// rust version
let (rustc, llvm) = rust_version().unwrap_or_default();
set_env("RE_BUILD_RUSTC_VERSION", &rustc);
set_env("RE_BUILD_LLVM_VERSION", &llvm);

// 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.
Expand Down Expand Up @@ -74,7 +79,7 @@ fn set_env(name: &str, value: &str) {
println!("cargo:rustc-env={name}={value}");
}

fn run_command(cmd: &'static str, args: &[&str]) -> anyhow::Result<String> {
fn run_command(cmd: &str, args: &[&str]) -> anyhow::Result<String> {
let output = Command::new(cmd)
.args(args)
.output()
Expand All @@ -101,3 +106,42 @@ fn git_branch() -> anyhow::Result<String> {
fn git_path(path: &str) -> anyhow::Result<String> {
run_command("git", &["rev-parse", "--git-path", path])
}

/// Returns `(rustc, LLVM)` versions.
///
/// Defaults to `"unknown"` if, for whatever reason, the output from `rustc -vV` did not contain
/// version information and/or the output format underwent breaking changes.
fn rust_version() -> anyhow::Result<(String, String)> {
let cmd = std::env::var("RUSTC").unwrap_or("rustc".into());
let args = &["-vV"];

// $ rustc -vV
// rustc 1.67.0 (fc594f156 2023-01-24)
// binary: rustc
// commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
// commit-date: 2023-01-24
// host: x86_64-unknown-linux-gnu
// release: 1.67.0
// LLVM version: 15.0.6

let res = run_command(&cmd, args)?;

let mut rustc_version = None;
let mut llvm_version = None;

for line in res.lines() {
if let Some(version) = line.strip_prefix("rustc ") {
rustc_version = Some(version.to_owned());
} else if let Some(version) = line.strip_prefix("LLVM version: ") {
llvm_version = Some(version.to_owned());
}
}

// NOTE: This should never happen, but if it does, we want to make sure we can differentiate
// between "failed to invoke rustc" vs. "rustc's output did not contain any version (??)
// and/or the output format has changed".
Ok((
rustc_version.unwrap_or_else(|| "unknown".to_owned()),
llvm_version.unwrap_or_else(|| "unknown".to_owned()),
))
}
19 changes: 19 additions & 0 deletions crates/re_build_info/src/build_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub struct BuildInfo {
/// Crate version, parsed from `CARGO_PKG_VERSION`, ignoring any `+metadata` suffix.
pub version: super::CrateVersion,

/// The raw version string of the Rust compiler used, or an empty string.
pub rustc_version: &'static str,

/// The raw version string of the LLVM toolchain used, or an empty string.
pub llvm_version: &'static str,

/// Git commit hash, or empty string.
pub git_hash: &'static str,

Expand Down Expand Up @@ -55,15 +61,28 @@ impl std::fmt::Display for BuildInfo {
let Self {
crate_name,
version,
rustc_version,
llvm_version,
git_hash,
git_branch,
is_in_rerun_workspace: _,
target_triple,
datetime,
} = self;

let rustc_version = (!rustc_version.is_empty()).then(|| format!("rustc {rustc_version}"));
let llvm_version = (!llvm_version.is_empty()).then(|| format!("LLVM {llvm_version}"));

write!(f, "{crate_name} {version}")?;

if let Some(rustc_version) = rustc_version {
write!(f, " [{rustc_version}")?;
if let Some(llvm_version) = llvm_version {
write!(f, ", {llvm_version}")?;
}
write!(f, "]")?;
}

write!(f, " {target_triple}")?;

if !git_branch.is_empty() {
Expand Down
4 changes: 4 additions & 0 deletions crates/re_build_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ macro_rules! build_info {
$crate::BuildInfo {
crate_name: env!("CARGO_PKG_NAME"),
version: $crate::CrateVersion::parse(env!("CARGO_PKG_VERSION")),
rustc_version: env!("RE_BUILD_RUSTC_VERSION"),
llvm_version: env!("RE_BUILD_LLVM_VERSION"),
git_hash: env!("RE_BUILD_GIT_HASH"),
git_branch: env!("RE_BUILD_GIT_BRANCH"),
// TODO(cmc): `PartialEq` is not available in const contexts, so this won't actually
// build if you try to instantiate a BuildInfo in a constant.
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
3 changes: 2 additions & 1 deletion crates/re_log_types/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ fn test_encode_decode() {
is_official_example: true,
started: Time::now(),
recording_source: crate::RecordingSource::RustSdk {
rust_version: env!("CARGO_PKG_RUST_VERSION").into(),
rustc_version: String::new(),
llvm_version: String::new(),
},
},
})];
Expand Down
8 changes: 6 additions & 2 deletions crates/re_log_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ pub enum RecordingSource {

/// The official Rerun Rust Logging SDK
RustSdk {
rust_version: String,
rustc_version: String,
llvm_version: String,
},

/// Perhaps from some manual data ingestion?
Expand All @@ -273,7 +274,10 @@ impl std::fmt::Display for RecordingSource {
match self {
Self::Unknown => "Unknown".fmt(f),
Self::PythonSdk(version) => write!(f, "Python {version} SDK"),
Self::RustSdk { rust_version } => write!(f, "Rust {rust_version} SDK"),
Self::RustSdk {
rustc_version: rust_version,
llvm_version: _,
} => write!(f, "Rust {rust_version} SDK"),
Self::Other(string) => format!("{string:?}").fmt(f), // put it in quotes
}
}
Expand Down
39 changes: 13 additions & 26 deletions crates/re_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,51 @@ all-features = true


[features]
default = ["demo", "glam", "image", "native_viewer"]

## Enable telemetry using our analytics SDK.
analytics = ["re_web_viewer_server?/analytics", "re_viewer?/analytics"]
default = ["demo", "glam", "image"]

## Enable the `demo` module (helpers for Rerun examples).
demo = []

## Add support for some math operations using [`glam`](https://crates.io/crates/glam/).
glam = ["re_log_types/glam"]

## Add the `global_session` method.
global_session = ["dep:once_cell", "dep:parking_lot"]

## Integration with the [`image`](https://crates.io/crates/image/) crate.
image = ["re_log_types/image"]

## Support for the native viewer.
native_viewer = ["image", "dep:re_viewer"]

## Support serving a web viewer over HTTP.
##
## Enabling this adds quite a bit to the compile time and binary size,
## since it requires compiling and bundling the viewer as wasm.
##
## You also need to install some additional tools, which you can do by running
## [`scripts/setup_web.sh`](https://github.com/rerun-io/rerun/blob/main/scripts/setup_web.sh).
web_viewer = ["dep:re_ws_comms", "dep:re_web_viewer_server", "dep:webbrowser"]
# Add a tokio runtime to the `Session` type.
tokio_runtime = ["dep:tokio"]


[dependencies]
re_build_info.workspace = true
re_error.workspace = true
re_log.workspace = true
re_log_types.workspace = true
re_log.workspace = true
re_memory.workspace = true
re_sdk_comms = { workspace = true, features = ["client"] }
re_smart_channel.workspace = true
re_string_interner.workspace = true

anyhow.workspace = true
arrow2.workspace = true
crossbeam = "0.8"
document-features = "0.2"
lazy_static.workspace = true
nohash-hasher = "0.2"
once_cell = "1.12"
parking_lot = "0.12"
thiserror.workspace = true

# Optional dependencies:
re_ws_comms = { workspace = true, optional = true, features = ["server"] }
re_viewer = { workspace = true, default-features = false, optional = true }
re_web_viewer_server = { workspace = true, optional = true }

webbrowser = { version = "0.8", optional = true }
once_cell = { version = "1.12", optional = true }
parking_lot = { version = "0.12", optional = true }

# Native dependencies:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
clap = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tokio = { workspace = true, optional = true, features = [
"macros",
"rt-multi-thread",
] }


[dev-dependencies]
Expand Down
32 changes: 28 additions & 4 deletions crates/re_sdk/src/demo_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn grid(from: glam::Vec3, to: glam::Vec3, n: usize) -> impl Iterator<Item =
/// * `angular_step`: The factor applied between each step along the trigonometric circle.
/// * `angular_offset`: Offsets the starting position on the trigonometric circle.
/// * `z_step`: The factor applied between between each step along the Z axis.
#[cfg(all(feature = "glam", feature = "native_viewer"))]
#[cfg(feature = "glam")]
pub fn color_spiral(
num_points: usize,
radius: f32,
Expand All @@ -91,10 +91,34 @@ pub fn color_spiral(
.collect();

let colors = (0..num_points)
.map(move |i| {
re_viewer::external::re_renderer::colormap_turbo_srgb(i as f32 / num_points as f32)
})
.map(move |i| colormap_turbo_srgb(i as f32 / num_points as f32))
.collect();

(points, colors)
}

/// Returns sRGB polynomial approximation from Turbo color map, assuming `t` is normalized.
fn colormap_turbo_srgb(t: f32) -> [u8; 4] {
#![allow(clippy::excessive_precision)]
use glam::{Vec2, Vec4, Vec4Swizzles};

const R4: Vec4 = Vec4::new(0.13572138, 4.61539260, -42.66032258, 132.13108234);
const G4: Vec4 = Vec4::new(0.09140261, 2.19418839, 4.84296658, -14.18503333);
const B4: Vec4 = Vec4::new(0.10667330, 12.64194608, -60.58204836, 110.36276771);

const R2: Vec2 = Vec2::new(-152.94239396, 59.28637943);
const G2: Vec2 = Vec2::new(4.27729857, 2.82956604);
const B2: Vec2 = Vec2::new(-89.90310912, 27.34824973);

debug_assert!((0.0..=1.0).contains(&t));

let v4 = glam::vec4(1.0, t, t * t, t * t * t);
let v2 = v4.zw() * v4.z;

[
((v4.dot(R4) + v2.dot(R2)) * 255.0) as u8,
((v4.dot(G4) + v2.dot(G2)) * 255.0) as u8,
((v4.dot(B4) + v2.dot(B2)) * 255.0) as u8,
255,
]
}
5 changes: 4 additions & 1 deletion crates/re_sdk/src/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Context as _;

use re_log_types::LogMsg;

/// Stream log messages to a file.
pub struct FileWriter {
// None = quit
tx: std::sync::mpsc::Sender<Option<LogMsg>>,
Expand Down Expand Up @@ -50,8 +51,10 @@ impl FileWriter {
join_handle: Some(join_handle),
})
}
}

pub fn write(&self, msg: LogMsg) {
impl crate::LogSink for FileWriter {
fn send(&mut self, msg: LogMsg) {
self.tx.send(Some(msg)).ok();
}
}
Loading