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

70 lines
2.3 KiB
CMake

cmake_minimum_required(VERSION 3.16)
project(module-test-cpp)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Module source file
if(NOT DEFINED MODULE_SOURCE)
set(MODULE_SOURCE "src/lib.cpp")
endif()
# Output name
if(NOT DEFINED OUTPUT_NAME)
set(OUTPUT_NAME "lib")
endif()
# Set the path to the SpacetimeDB C++ library
set(SPACETIMEDB_CPP_LIBRARY_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../crates/bindings-cpp")
# Create the module executable
add_executable(${OUTPUT_NAME} ${MODULE_SOURCE})
# Include directories
target_include_directories(${OUTPUT_NAME} PRIVATE
${SPACETIMEDB_CPP_LIBRARY_PATH}/include
)
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
target_compile_options(${OUTPUT_NAME} PRIVATE -fno-exceptions -O2 -g0)
# Enable unstable features for transaction support
target_compile_definitions(${OUTPUT_NAME} PRIVATE SPACETIMEDB_UNSTABLE_FEATURES)
# Also define for the library so it includes unstable features
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSPACETIMEDB_UNSTABLE_FEATURES")
endif()
# Link the SpacetimeDB library
add_subdirectory(${SPACETIMEDB_CPP_LIBRARY_PATH} ${CMAKE_CURRENT_BINARY_DIR}/spacetimedb_cpp_library)
target_link_libraries(${OUTPUT_NAME} PRIVATE spacetimedb_cpp_library)
# Emscripten specific settings
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
# Export list as a single variable (no extra quoting per-platform)
set(EXPORTED_FUNCS "['_malloc','_free','___describe_module__','___call_reducer__','___call_procedure__','___call_view__','___call_view_anon__']")
# Produce a standalone .wasm (no JS harness) with no entry point
target_link_options(lib PRIVATE
"SHELL:-sSTANDALONE_WASM=1"
"SHELL:-sWASM=1"
"SHELL:--no-entry"
# Exports
"SHELL:-sEXPORTED_FUNCTIONS=${EXPORTED_FUNCS}"
# Keep this ON for correctness; you can flip to 0 temporarily to probe
"SHELL:-sERROR_ON_UNDEFINED_SYMBOLS=1"
# Trim runtime
"SHELL:-sFILESYSTEM=0"
"SHELL:-sDISABLE_EXCEPTION_CATCHING=1"
"SHELL:-sALLOW_MEMORY_GROWTH=0"
"SHELL:-sINITIAL_MEMORY=16MB"
"SHELL:-sSUPPORT_LONGJMP=0"
"SHELL:-sSUPPORT_ERRNO=0"
# C++20 and O2 at link step (Emscripten accepts them here too)
"SHELL:-std=c++20"
"SHELL:-O2"
"SHELL:-g0"
)
# Name the output lib.wasm
set_target_properties(lib PROPERTIES OUTPUT_NAME "lib" SUFFIX ".wasm")
endif()