Skip to content

Commit

Permalink
C++ put test types out of the SDK and into the test executable (rerun…
Browse files Browse the repository at this point in the history
…-io#3007)

* Part of rerun-io#2919
* Depends on rerun-io#3001
* Solves C++ part of rerun-io#2537

### What

Moves out all affix-fuzzer and co. testing types of the actual SDK.

While working on that...
* I figured out why warnings from arrow headers showed up only
sometimes: When Arrow isn't added explicitely as a dependency, CMake
doesn't seem to declare it as system header which normally suppresses
warnings (as you can imagine this got really bad now when adding more
cpps that include arrow to the test library). Obvious workaround is to
add arrow explicitly to the text executable.
* internal include paths got shorter/more natural in some cases. I had
to make the `Includes` utility more clever in order to support types
outside of the sdk, so we got this almost for free :)

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/3007) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/3007)
- [Docs
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp%2Fseparate-test-types/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp%2Fseparate-test-types/examples)
  • Loading branch information
Wumpf authored Aug 18, 2023
1 parent 6cf7020 commit fe3fcff
Show file tree
Hide file tree
Showing 98 changed files with 463 additions and 216 deletions.
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

0 comments on commit fe3fcff

Please sign in to comment.