|
| 1 | +cmake_minimum_required(VERSION 3.20) |
| 2 | +project(spacetime_cpp_module LANGUAGES C CXX) |
| 3 | + |
| 4 | +# Set C++ standard |
| 5 | +set(CMAKE_CXX_STANDARD 20) |
| 6 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 7 | + |
| 8 | +# Set the module source file |
| 9 | +set(MODULE_SOURCE "src/lib.cpp" CACHE STRING "Source file for the SpacetimeDB module") |
| 10 | + |
| 11 | +# Export compile commands for better IDE support |
| 12 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 13 | + |
| 14 | +# Set optimization level for Release builds |
| 15 | +if(CMAKE_BUILD_TYPE STREQUAL "Release") |
| 16 | + set(CMAKE_CXX_FLAGS_RELEASE "-O2") |
| 17 | +endif() |
| 18 | + |
| 19 | +# ------------------------------------------------------------------------------ |
| 20 | +# SpacetimeDB C++ SDK - Use local SDK from repository |
| 21 | +# ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +set(SPACETIMEDB_CPP_SDK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../crates/bindings-cpp") |
| 24 | +message(STATUS "Using local SpacetimeDB C++ SDK: ${SPACETIMEDB_CPP_SDK_DIR}") |
| 25 | +add_subdirectory("${SPACETIMEDB_CPP_SDK_DIR}" "${CMAKE_BINARY_DIR}/_deps/spacetime_cpp_sdk-build") |
| 26 | + |
| 27 | +# Create the main module executable |
| 28 | +add_executable(lib ${MODULE_SOURCE}) |
| 29 | + |
| 30 | +# Link against the SDK target (preferred alias) |
| 31 | +target_link_libraries(lib PRIVATE spacetimedb_cpp_library) |
| 32 | + |
| 33 | +# Configure Emscripten-specific settings for WASM output |
| 34 | +if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") |
| 35 | + set(EXPORTED_FUNCS "['_malloc','_free','___describe_module__','___call_reducer__','___call_view__','___call_view_anon__','___call_procedure__']") |
| 36 | + |
| 37 | + # Compile options/defines for WASM |
| 38 | + # (apply to both the module and the SDK, so everything sees the same feature flags) |
| 39 | + target_compile_options(lib PRIVATE -fno-exceptions) |
| 40 | + target_compile_options(spacetimedb_cpp_library PRIVATE -fno-exceptions) |
| 41 | + |
| 42 | + # Uncomment the following to allow unstable features of SpacetimeDB |
| 43 | + #target_compile_definitions(lib PRIVATE SPACETIMEDB_UNSTABLE_FEATURES) |
| 44 | + #target_compile_definitions(spacetimedb_cpp_library PUBLIC SPACETIMEDB_UNSTABLE_FEATURES) |
| 45 | + |
| 46 | + target_link_options(lib PRIVATE |
| 47 | + "SHELL:-sSTANDALONE_WASM=1" |
| 48 | + "SHELL:-sWASM=1" |
| 49 | + "SHELL:--no-entry" |
| 50 | + "SHELL:-sEXPORTED_FUNCTIONS=${EXPORTED_FUNCS}" |
| 51 | + "SHELL:-sERROR_ON_UNDEFINED_SYMBOLS=1" |
| 52 | + "SHELL:-sFILESYSTEM=0" |
| 53 | + "SHELL:-sDISABLE_EXCEPTION_CATCHING=1" |
| 54 | + "SHELL:-sALLOW_MEMORY_GROWTH=0" |
| 55 | + "SHELL:-sINITIAL_MEMORY=16MB" |
| 56 | + "SHELL:-sSUPPORT_LONGJMP=0" |
| 57 | + "SHELL:-sSUPPORT_ERRNO=0" |
| 58 | + "SHELL:-std=c++20" |
| 59 | + "SHELL:-O2" |
| 60 | + ) |
| 61 | + |
| 62 | + set_target_properties(lib PROPERTIES OUTPUT_NAME "lib" SUFFIX ".wasm") |
| 63 | +endif() |
0 commit comments