# sim-driver: the C++ led-driver built for the browser (led-simulator's strips and pattern gallery) with Emscripten (build.sh runs this in Docker).
#   led_driver  one whole driver per strip, for web/driver-worker.js. No threads: the worker runs the pattern frame
#               and the ESP-NOW link pass from a timer (sim_tick), so the page needs no SharedArrayBuffer and
#               works on any address (a phone on the LAN), not only on a cross-origin isolated one
#   led_fx      the pattern engine alone, no threads, stepped by the page, for web/gallery.js
# The driver sources are led-driver/main's, unchanged; the platform comes from desktop/port and src/.
cmake_minimum_required(VERSION 3.16)
project(led_simulator_wasm C CXX)

set(ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
set(DRIVER ${ROOT}/led-driver/main)
set(DESK ${ROOT}/desktop)
set(OUT ${ROOT}/led-simulator/web/wasm)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

set(ENGINE_SRC
    ${DRIVER}/patterns.cpp
    ${DRIVER}/effects_a.cpp
    ${DRIVER}/effects_b.cpp
    ${DRIVER}/effects_rig.cpp
    ${DRIVER}/effects_util.cpp
    ${DRIVER}/beat_sync.cpp
    ${DRIVER}/presets.cpp
    ${DRIVER}/settings.cpp
    ${DESK}/port/src/freertos.c
    ${DESK}/third_party/cjson/cJSON.c
    src/sim_glue.c
    src/nvs_sim.cpp
    src/ws2812_sim.cpp)

# musl (Emscripten's libc) has no static initializer for a recursive mutex; its type word is the first int
set_source_files_properties(${DESK}/port/src/freertos.c PROPERTIES
    COMPILE_DEFINITIONS "PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP={{{PTHREAD_MUTEX_RECURSIVE}}}")

set(INCLUDES
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${DESK}/port/include
    ${DESK}/third_party/cjson
    ${DRIVER})

set(COMMON_LINK
    -sMODULARIZE=1
    -sEXPORT_ES6=1
    -sALLOW_MEMORY_GROWTH=1
    -sEXPORTED_RUNTIME_METHODS=HEAPU8,UTF8ToString,stringToNewUTF8)

function(sim_target name)
    target_include_directories(${name} PRIVATE ${INCLUDES})
    # ESP-IDF force-includes sdkconfig.h into every source; so does the desktop port
    # (SHELL: keeps each -include with its file: CMake would merge the repeated flag)
    target_compile_options(${name} PRIVATE "SHELL:-include ${DESK}/port/include/sdkconfig.h"
                           -Wall -Wno-unused-function -Wno-missing-field-initializers
                           "$<$<COMPILE_LANGUAGE:CXX>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/include/sim_prelude.h>")
    set_target_properties(${name} PROPERTIES SUFFIX ".mjs" RUNTIME_OUTPUT_DIRECTORY ${OUT})
endfunction()

# ---- one driver ----------------------------------------------------------------------------------
add_executable(led_driver ${ENGINE_SRC}
    ${DRIVER}/ctrl_msg.cpp
    ${DRIVER}/espnow_link.cpp
    src/espnow_sim.cpp
    src/sim_main.cpp)
sim_target(led_driver)
target_link_options(led_driver PRIVATE ${COMMON_LINK}
    -sENVIRONMENT=worker
    -sEXPORT_NAME=LedDriver
    -sINITIAL_MEMORY=4MB
    -sSTACK_SIZE=256KB
    -sEXPORTED_FUNCTIONS=_sim_boot,_sim_tick,_sim_rx,_sim_frame,_malloc,_free)

# ---- pattern gallery -----------------------------------------------------------------------------
add_executable(led_fx ${ENGINE_SRC} src/gallery_main.cpp)
sim_target(led_fx)
target_compile_definitions(led_fx PRIVATE SIM_MANUAL_CLOCK=1)
target_link_options(led_fx PRIVATE ${COMMON_LINK}
    -sENVIRONMENT=web
    -sEXPORT_NAME=LedFx
    -sINITIAL_MEMORY=2MB
    -sSTACK_SIZE=256KB
    -sEXPORTED_FUNCTIONS=_fx_count,_fx_name,_fx_init,_fx_defaults_json,_fx_select_json,_fx_step,_sim_frame,_malloc,_free)
