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

C++ put test types out of the SDK and into the test executable #3007

Merged
merged 5 commits into from
Aug 18, 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
2 changes: 1 addition & 1 deletion crates/re_types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const SOURCE_HASH_PATH: &str = "./source_hash.txt";
const DEFINITIONS_DIR_PATH: &str = "./definitions";
const ENTRYPOINT_PATH: &str = "./definitions/rerun/archetypes.fbs";
const DOC_EXAMPLES_DIR_PATH: &str = "../../docs/code-examples";
const CPP_OUTPUT_DIR_PATH: &str = "../../rerun_cpp/src/rerun";
const CPP_OUTPUT_DIR_PATH: &str = "../../rerun_cpp";
const RUST_OUTPUT_DIR_PATH: &str = ".";
const PYTHON_OUTPUT_DIR_PATH: &str = "../../rerun_py/rerun_sdk/rerun/_rerun2";

Expand Down
2 changes: 1 addition & 1 deletion crates/re_types/source_hash.txt

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

2 changes: 1 addition & 1 deletion crates/re_types_builder/src/bin/build_re_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use camino::Utf8Path;

const DEFINITIONS_DIR_PATH: &str = "crates/re_types/definitions";
const ENTRYPOINT_PATH: &str = "crates/re_types/definitions/rerun/archetypes.fbs";
const CPP_OUTPUT_DIR_PATH: &str = "rerun_cpp/src/rerun";
const CPP_OUTPUT_DIR_PATH: &str = "rerun_cpp";
const RUST_OUTPUT_DIR_PATH: &str = "crates/re_types/.";
const PYTHON_OUTPUT_DIR_PATH: &str = "rerun_py/rerun_sdk/rerun/_rerun2";

Expand Down
80 changes: 75 additions & 5 deletions crates/re_types_builder/src/codegen/cpp/includes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,91 @@ use std::collections::BTreeSet;
use proc_macro2::TokenStream;
use quote::quote;

use crate::objects::is_testing_fqname;

use super::{NEWLINE_TOKEN, SYS_INCLUDE_PATH_PREFIX_TOKEN, SYS_INCLUDE_PATH_SUFFIX_TOKEN};

/// Keeps track of necessary includes for a file.
#[derive(Default)]
pub struct Includes {
system: BTreeSet<String>,
local: BTreeSet<String>,
fqname: String,
}

impl Includes {
pub fn new(fqname: String) -> Self {
Self {
fqname,
system: BTreeSet::new(),
local: BTreeSet::new(),
}
}

/// `#include <vector>` etc
pub system: BTreeSet<String>,
pub fn insert_system(&mut self, name: &str) {
self.system.insert(name.to_owned());
}

/// Insert a relative include path.
pub fn insert_relative(&mut self, name: &str) {
self.local.insert(name.to_owned());
}

/// `#include datatypes.hpp"` etc
pub local: BTreeSet<String>,
/// Insert an include path that is in the `rerun` folder of the sdk.
pub fn insert_rerun(&mut self, name: &str) {
if is_testing_fqname(&self.fqname) {
self.insert_system(&format!("rerun/{name}"));
} else {
self.local.insert(format!("../{name}"));
}
}

/// Insert an include path to another generated type.
pub fn insert_rerun_type(&mut self, included_fqname: &str) {
let included_fqname_without_testing = included_fqname.replace(".testing", "");

let components = included_fqname_without_testing
.split('.')
.collect::<Vec<_>>();

if let ["rerun", obj_kind, typname] = components[..] {
let typname = crate::to_snake_case(typname);

if is_testing_fqname(&self.fqname) == is_testing_fqname(included_fqname) {
// If the type is in the same library, we use a relative path.
if self
.fqname
.starts_with(&included_fqname[..included_fqname.len() - typname.len()])
{
// Types are next to each other, can skip going into the obj_kind folder.
self.local.insert(format!("{typname}.hpp"));
} else {
self.local.insert(format!("../{obj_kind}/{typname}.hpp"));
}
} else {
// Types are not in the same library, need to treat this like a rerun sdk header.
assert!(
is_testing_fqname(&self.fqname) || !is_testing_fqname(included_fqname),
"A non-testing type can't include a testing type."
);
self.insert_rerun(&format!("{obj_kind}/{typname}.hpp"));
}
} else {
panic!(
"Can't figure out include for {included_fqname:?} when adding includes for {:?}",
self.fqname
);
}
}
}

impl quote::ToTokens for Includes {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self { system, local } = self;
let Self {
system,
local,
fqname: _,
} = self;

let hash = quote! { # };
let system = system.iter().map(|name| {
Expand Down
Loading