cmake_minimum_required(VERSION 3.10)
project(smart_wasm_json)

# 设置 Emscripten 工具链
if(NOT DEFINED EMSCRIPTEN_ROOT_PATH)
    set(EMSCRIPTEN_ROOT_PATH "E:/emsdk/upstream/emscripten")
endif()

if(EXISTS "${EMSCRIPTEN_ROOT_PATH}/cmake/Modules/Platform/Emscripten.cmake")
    set(CMAKE_TOOLCHAIN_FILE "${EMSCRIPTEN_ROOT_PATH}/cmake/Modules/Platform/Emscripten.cmake")
else()
    message(FATAL_ERROR "Emscripten toolchain file not found")
endif()

# C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 源文件
set(SOURCES
    src/main.cpp
    src/math_utils.cpp
    src/smart_json_wrapper.cpp
)

# 头文件目录
include_directories(
    include
    third_party
    third_party/kdl_parser/include
    third_party/orocos_kdl/install_wasm/include
    third_party/eigen3
)

# 创建可执行文件
add_executable(smart_wasm ${SOURCES})

# 设置编译选项
target_compile_options(smart_wasm PRIVATE
    -O3
    -s WASM=1
    -s EXPORTED_FUNCTIONS='["_smart_process_json","_smart_get_function_list","_smart_get_function_info","_smart_test_match","_smart_get_version","_smart_free_string","_malloc","_free"]'
    -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap","UTF8ToString","stringToUTF8","lengthBytesUTF8"]'
    -s ALLOW_MEMORY_GROWTH=1
    -s MODULARIZE=1
    -s EXPORT_NAME='createSmartMathModule'
    -s ENVIRONMENT='web'
    -s ASSERTIONS=1
)

# 输出设置
set_target_properties(smart_wasm PROPERTIES
    SUFFIX ".js"
    OUTPUT_NAME "smart_math"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/public/wasm"
)
