Skip to content

Commit

Permalink
Write module files and include it in the top-level <rerun.hpp>
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jul 12, 2023
1 parent cf37540 commit af2dba2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/re_types/source_hash.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This is a sha256 hash for all direct and indirect dependencies of this crate's build script.
# It can be safely removed at anytime to force the build script to run again.
# Check out build.rs to see how it's computed.
3034f4c917b0b412824518949d63813d53031084e7a974d9b778c656157a65ed
6e109c1fdf4ece25d6dc0e12b446ea8463f4a42c370708aeb2c2494bdcfbc07a
52 changes: 41 additions & 11 deletions crates/re_types_builder/src/codegen/cpp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::BTreeSet;

use anyhow::Context as _;
use camino::Utf8PathBuf;
use camino::{Utf8Path, Utf8PathBuf};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::{codegen::AUTOGEN_WARNING, ArrowRegistry, Object, ObjectKind, Objects};
use crate::{codegen::AUTOGEN_WARNING, ArrowRegistry, ObjectKind, Objects};

const NEWLINE_TOKEN: &str = "RE_TOKEN_NEWLINE";

Expand All @@ -27,23 +27,46 @@ impl CppCodeGenerator {
object_kind: ObjectKind,
folder_name: &str,
) -> BTreeSet<Utf8PathBuf> {
let mut filepaths = BTreeSet::default();

let folder_path = self.output_path.join(folder_name);
std::fs::create_dir_all(&folder_path)
.with_context(|| format!("{folder_path:?}"))
.unwrap();
for obj in objects.ordered_objects(object_kind.into()) {

let mut filepaths = BTreeSet::default();

// Generate folder contents:
let ordered_objects = objects.ordered_objects(object_kind.into());
for &obj in &ordered_objects {
let filename = obj.snake_case_name();
let (hpp, cpp) = generate_hpp_cpp(objects, arrow_registry, obj);
for (extension, tokens) in [("hpp", hpp), ("cpp", cpp)] {
let string = string_from_token_stream(obj, &tokens);
let string = string_from_token_stream(&tokens, obj.relative_filepath());
let filepath = folder_path.join(format!("{filename}.{extension}"));
write_file(&filepath, string);
filepaths.insert(filepath);
}
}

{
// Generate module file that includes all the headers:
let hash = quote! { # };
let pragma_once = pragma_once();
let header_file_names = ordered_objects
.iter()
.map(|obj| format!("{folder_name}/{}.hpp", obj.snake_case_name()));
let tokens = quote! {
#pragma_once
#(#hash include #header_file_names "RE_TOKEN_NEWLINE")*
};
let filepath = folder_path
.parent()
.unwrap()
.join(format!("{folder_name}.hpp"));
let string = string_from_token_stream(&tokens, None);
write_file(&filepath, string);
filepaths.insert(filepath);
}

// Clean up old files:
for entry in std::fs::read_dir(folder_path).unwrap().flatten() {
let filepath = Utf8PathBuf::try_from(entry.path()).unwrap();
Expand Down Expand Up @@ -78,11 +101,11 @@ impl crate::CodeGenerator for CppCodeGenerator {
}
}

fn string_from_token_stream(obj: &Object, token_stream: &TokenStream) -> String {
fn string_from_token_stream(token_stream: &TokenStream, source_path: Option<&Utf8Path>) -> String {
let mut code = String::new();
code.push_str(&format!("// {AUTOGEN_WARNING}\n"));
if let Some(relative_path) = obj.relative_filepath() {
code.push_str(&format!("// Based on {relative_path:?}"));
if let Some(source_path) = source_path {
code.push_str(&format!("// Based on {source_path:?}"));
}

code.push('\n');
Expand Down Expand Up @@ -127,11 +150,11 @@ fn generate_hpp_cpp(
let snake_case_name = obj.snake_case_name();

let hash = quote! { # };
let pragma_once = pragma_once();
let header_file_name = format!("{snake_case_name}.hpp");

let hpp = quote! {
#hash pragma once #NEWLINE_TOKEN #NEWLINE_TOKEN

#pragma_once
namespace rr {
namespace #obj_kind_ident {
struct #pascal_case_ident { };
Expand All @@ -142,3 +165,10 @@ fn generate_hpp_cpp(

(hpp, cpp)
}

fn pragma_once() -> TokenStream {
let hash = quote! { # };
quote! {
#hash pragma once #NEWLINE_TOKEN #NEWLINE_TOKEN
}
}
7 changes: 7 additions & 0 deletions rerun_cpp/src/archetypes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// NOTE: This file was autogenerated by re_types_builder; DO NOT EDIT.

#pragma once

#include "archetypes/fuzzy.hpp"
#include "archetypes/points2d.hpp"
#include "archetypes/transform3d.hpp"
14 changes: 14 additions & 0 deletions rerun_cpp/src/components.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// NOTE: This file was autogenerated by re_types_builder; DO NOT EDIT.

#pragma once

#include "components/class_id.hpp"
#include "components/color.hpp"
#include "components/draw_order.hpp"
#include "components/fuzzy.hpp"
#include "components/instance_key.hpp"
#include "components/keypoint_id.hpp"
#include "components/label.hpp"
#include "components/point2d.hpp"
#include "components/radius.hpp"
#include "components/transform3d.hpp"
19 changes: 19 additions & 0 deletions rerun_cpp/src/datatypes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// NOTE: This file was autogenerated by re_types_builder; DO NOT EDIT.

#pragma once

#include "datatypes/angle.hpp"
#include "datatypes/fuzzy.hpp"
#include "datatypes/mat3x3.hpp"
#include "datatypes/mat4x4.hpp"
#include "datatypes/point2d.hpp"
#include "datatypes/quaternion.hpp"
#include "datatypes/rotation3d.hpp"
#include "datatypes/rotation_axis_angle.hpp"
#include "datatypes/scale3d.hpp"
#include "datatypes/transform3d.hpp"
#include "datatypes/translation_and_mat3x3.hpp"
#include "datatypes/translation_rotation_scale3d.hpp"
#include "datatypes/vec2d.hpp"
#include "datatypes/vec3d.hpp"
#include "datatypes/vec4d.hpp"
6 changes: 6 additions & 0 deletions rerun_cpp/src/rerun.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// The Rerun C++ SDK.
#pragma once

// Auto-generated:
#include "archetypes.hpp"
#include "components.hpp"
#include "datatypes.hpp"

// Manually written:
#include "recording_stream.hpp"

namespace rr {
Expand Down

0 comments on commit af2dba2

Please sign in to comment.