-
Notifications
You must be signed in to change notification settings - Fork 334
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++ codegen #2678
Merged
Merged
C++ codegen #2678
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1e19b7b
First steps of C++ codegen
emilk 8a60fd1
Move C and C++ examples to examples/ folder
emilk 4826c5e
Write module files and include it in the top-level <rerun.hpp>
emilk c046f6d
One .hpp/.cpp pair per type
emilk 8705307
fixup
emilk 82f515a
Do the formatting of the rust code as a function instead of syscall
emilk 8d79b7c
Add a TODO
emilk 6dccef6
Merge branch 'main' into emilk/cpp-codegen
emilk 72c9a6c
Parallelize C++ codegen
emilk b605088
Parallelize codegen of Rust, Python, C++
emilk a3ee0ad
add cmake and cpp tooling to recommended extensions
Wumpf 64b9867
add missing cmake related gitignore entries
Wumpf a6ea573
fix missing newline
Wumpf 95a52f7
add clang format to needed packages for development (otherwise codege…
Wumpf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
project(rerun_cpp_proj LANGUAGES CXX) | ||
|
||
add_subdirectory(rerun_cpp) # The Rerun C++ SDK library | ||
add_subdirectory(examples/cpp/minimal) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
7b07a9ead58634313a05cbe4384f177af85241e46701dea4e14fa46ac85916a3 | ||
48b1929d5cb17125eaae7733df116017fed8e27f5202d2365146e68e9a1a5b16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
use std::collections::BTreeSet; | ||
|
||
use anyhow::Context as _; | ||
use camino::{Utf8Path, Utf8PathBuf}; | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote}; | ||
use rayon::prelude::*; | ||
|
||
use crate::{codegen::AUTOGEN_WARNING, ArrowRegistry, ObjectKind, Objects}; | ||
|
||
const NEWLINE_TOKEN: &str = "RE_TOKEN_NEWLINE"; | ||
|
||
pub struct CppCodeGenerator { | ||
output_path: Utf8PathBuf, | ||
} | ||
|
||
impl CppCodeGenerator { | ||
pub fn new(output_path: impl Into<Utf8PathBuf>) -> Self { | ||
Self { | ||
output_path: output_path.into(), | ||
} | ||
} | ||
|
||
fn generate_folder( | ||
&self, | ||
objects: &Objects, | ||
arrow_registry: &ArrowRegistry, | ||
object_kind: ObjectKind, | ||
folder_name: &str, | ||
) -> BTreeSet<Utf8PathBuf> { | ||
let folder_path = self.output_path.join(folder_name); | ||
std::fs::create_dir_all(&folder_path) | ||
.with_context(|| format!("{folder_path:?}")) | ||
.unwrap(); | ||
|
||
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(&tokens, obj.relative_filepath()); | ||
let filepath = folder_path.join(format!("{filename}.{extension}")); | ||
write_file(&filepath, string); | ||
let inserted = filepaths.insert(filepath); | ||
assert!( | ||
inserted, | ||
"Multiple objects with the same name: {:?}", | ||
obj.name | ||
); | ||
} | ||
} | ||
|
||
{ | ||
// 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(); | ||
if !filepaths.contains(&filepath) { | ||
std::fs::remove_file(filepath).ok(); | ||
} | ||
} | ||
|
||
filepaths | ||
} | ||
} | ||
|
||
impl crate::CodeGenerator for CppCodeGenerator { | ||
fn generate( | ||
&mut self, | ||
objects: &Objects, | ||
arrow_registry: &ArrowRegistry, | ||
) -> BTreeSet<Utf8PathBuf> { | ||
ObjectKind::ALL | ||
.par_iter() | ||
.map(|object_kind| { | ||
let folder_name = object_kind.plural_snake_case(); | ||
self.generate_folder(objects, arrow_registry, *object_kind, folder_name) | ||
}) | ||
.flatten() | ||
.collect() | ||
} | ||
} | ||
|
||
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(source_path) = source_path { | ||
code.push_str(&format!("// Based on {source_path:?}\n")); | ||
} | ||
|
||
code.push('\n'); | ||
code.push_str( | ||
&token_stream | ||
.to_string() | ||
.replace(&format!("{NEWLINE_TOKEN:?}"), "\n"), | ||
); | ||
code.push('\n'); | ||
|
||
// clang_format has a bit of an ugly API: https://github.com/KDAB/clang-format-rs/issues/3 | ||
clang_format::CLANG_FORMAT_STYLE | ||
.set(clang_format::ClangFormatStyle::File) | ||
.ok(); | ||
code = clang_format::clang_format(&code).expect("Failed to run clang-format"); | ||
|
||
code | ||
} | ||
|
||
fn write_file(filepath: &Utf8PathBuf, code: String) { | ||
if let Ok(existing) = std::fs::read_to_string(filepath) { | ||
if existing == code { | ||
// Don't touch the timestamp unnecessarily | ||
return; | ||
Comment on lines
+131
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason we do this here and not in the other generators? another candidate for becoming a general utility |
||
} | ||
} | ||
|
||
std::fs::write(filepath, code) | ||
.with_context(|| format!("{filepath}")) | ||
.unwrap(); | ||
} | ||
|
||
fn generate_hpp_cpp( | ||
_objects: &Objects, | ||
_arrow_registry: &ArrowRegistry, | ||
obj: &crate::Object, | ||
) -> (TokenStream, TokenStream) { | ||
let obj_kind_ident = format_ident!("{}", obj.kind.plural_snake_case()); | ||
|
||
let pascal_case_name = &obj.name; | ||
let pascal_case_ident = format_ident!("{pascal_case_name}"); | ||
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! { | ||
#pragma_once | ||
namespace rr { | ||
namespace #obj_kind_ident { | ||
struct #pascal_case_ident { }; | ||
} | ||
} | ||
}; | ||
let cpp = quote! { #hash include #header_file_name }; | ||
|
||
(hpp, cpp) | ||
} | ||
|
||
fn pragma_once() -> TokenStream { | ||
let hash = quote! { # }; | ||
quote! { | ||
#hash pragma once #NEWLINE_TOKEN #NEWLINE_TOKEN | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems we don't have this on the other impls. We should generalize that step. Need to take care of custom files since it shouldn't remove any custom implementation files