Files
SpacetimeDB/crates/codegen/examples/regen-cpp-moduledef.rs
Jason Larabie 52b6c66fa1 Add C++ Bindings (#3544)
# Description of Changes

This adds C++ server bindings (/crate/bindings-cpp) to allow writing C++
20 modules.

- Emscripten WASM build system integration with CMake
- Macro-based code generation (SPACETIMEDB_TABLE, SPACETIMEDB_REDUCER,
etc)
- All SpacetimeDB types supported (primitives, Timestamp, Identity,
Uuid, etc)
- Product types via SPACETIMEDB_STRUCT
- Sum types via SPACETIMEDB_ENUM
- Constraints marked with FIELD* macros

# API and ABI breaking changes

None

# Expected complexity level and risk

2 - Doesn't heavily impact any other areas but is complex macro C++
structure to support a similar developer experience, did have a small
impact on init command

# Testing

- [x] modules/module-test-cpp - heavily tested every reducer
- [x] modules/benchmarks-cpp - tested through the standalone (~6x faster
than C#, ~6x slower than Rust)
- [x] modules/sdk-test-cpp
- [x] modules/sdk-test-procedure-cpp
- [x] modules/sdk-test-view-cpp  
- [x] Wrote several test modules myself
- [x] Quickstart smoketest [Currently in progress]
- [ ] Write Blackholio C++ server module

---------

Signed-off-by: Jason Larabie <jason@clockworklabs.io>
Co-authored-by: clockwork-labs-bot <clockwork-labs-bot@users.noreply.github.com>
Co-authored-by: Ryan <r.ekhoff@clockworklabs.io>
Co-authored-by: John Detter <4099508+jdetter@users.noreply.github.com>
2026-02-07 04:26:45 +00:00

53 lines
1.6 KiB
Rust

//! This script is used to generate the C++ bindings for the `RawModuleDef` type.
//! Run `cargo run --example regen-cpp-moduledef` to update C++ bindings whenever the module definition changes.
use fs_err as fs;
use spacetimedb_codegen::{cpp, generate, OutputFile};
use spacetimedb_lib::db::raw_def::v9::{RawModuleDefV9, RawModuleDefV9Builder};
use spacetimedb_schema::def::ModuleDef;
use std::path::Path;
fn main() -> anyhow::Result<()> {
let mut builder = RawModuleDefV9Builder::new();
builder.add_type::<RawModuleDefV9>();
let module = builder.finish();
// Build relative path from the codegen crate to the C++ Module Library autogen directory
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let dir = Path::new(manifest_dir)
.parent()
.unwrap()
.join("bindings-cpp/include/spacetimedb/internal/autogen");
println!("Target directory path: {}", dir.display());
// Create the autogen directory if it doesn't exist
if dir.exists() {
fs::remove_dir_all(&dir)?;
}
fs::create_dir_all(&dir)?;
let module: ModuleDef = module.try_into()?;
generate(
&module,
&cpp::Cpp {
namespace: "SpacetimeDB::Internal",
},
)
.into_iter()
.try_for_each(|OutputFile { filename, code }| {
// Remove any prefix and just use the filename
let filename = if let Some(name) = filename.strip_prefix("Types/") {
name
} else {
&filename
};
println!("Generating {}", filename);
fs::write(dir.join(filename), code)
})?;
println!("C++ autogen files written to: {}", dir.display());
Ok(())
}