Files
SpacetimeDB/crates/codegen/examples/regen-cpp-moduledef.rs
Jason Larabie c59ee1ddc3 [2.0 Breaking] Add --include-private and default private tables to not generate (#4241)
# Description of Changes
Updated the codegen table/function iteration functions to take in a
parameter to check visibility in all locations for the supported
languages.
- Updated the util.rs functions for iterating tables/functions to check
for a CodegenVisibility enum (IncludePrivate, or OnlyPublic)
- Added a new CodegenOptions struct to pass around the CodegenVisibility
and future flags, defaulted visibility to OnlyPublic
- Updated the CLI to return a list of all private tables not included
(added a TODO to check the --include-private opt):
```bash
Optimising module with wasm-opt...
Build finished successfully.
Skipping private tables during codegen: secret_note, secret_order, secret_person.
Generate finished successfully.
```

# API and ABI breaking changes

Technically API breaking as the private tables will no longer be
available. (GitHub labels are not working at the moment)

# Expected complexity level and risk

1 - Simple change the testing took longer

# Testing

Turns out when you remove private tables you invalidate most of the
module_bindings across the system!

- [x] Rust test SDK for all languages
- [x] C# SDK tests
- [x] C# dotnet tests
- [x] Updated and checked snap files
- [x] Updated Blackholio (Unreal + Unity) module_bindings and tested
- [x] Ran Unreal SDK tests

---------

Signed-off-by: Jason Larabie <jason@clockworklabs.io>
2026-02-12 03:08:54 +00:00

54 lines
1.7 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, CodegenOptions, 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",
},
&CodegenOptions::default(),
)
.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(())
}