cmake_minimum_required(VERSION 3.20)
project(aladdin_web C)
if(NOT EMSCRIPTEN)
  message(FATAL_ERROR "Configure with emcmake cmake")
endif()
find_package(Python3 REQUIRED COMPONENTS Interpreter)
set(game ${CMAKE_CURRENT_SOURCE_DIR}/../upstream/aladdinsvhc/native/aladdin)
set(engine ${CMAKE_CURRENT_SOURCE_DIR}/../upstream/nativeshvc)
file(GLOB_RECURSE game_sources CONFIGURE_DEPENDS "${game}/src/*.c")
file(GLOB shader_sources CONFIGURE_DEPENDS "${engine}/src/engine/shaders/*")
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shaders.h
  COMMAND Python3::Interpreter ${CMAKE_CURRENT_SOURCE_DIR}/shaders.py ${engine}/src/engine/shaders ${CMAKE_CURRENT_BINARY_DIR}/shaders.h
  DEPENDS shaders.py ${shader_sources})
add_executable(aladdin ${game_sources} src/browser.c src/webgl.c ${CMAKE_CURRENT_BINARY_DIR}/shaders.h
  ${engine}/src/engine/compose.c ${engine}/src/engine/picture.c ${engine}/src/engine/input.c
  ${engine}/src/engine/files.c ${engine}/src/engine/report.c ${engine}/src/engine/inspect.c
  ${engine}/src/engine/sound.c ${engine}/src/dsp/resample.c)
target_include_directories(aladdin PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${game}/src ${engine}/include
  ${engine}/src/engine ${engine}/src/host ${engine}/src/render ${engine}/src/dsp)
target_compile_definitions(aladdin PRIVATE NSHVC_SINGLE_THREADED=1)
target_compile_options(aladdin PRIVATE -O2 -fno-strict-aliasing -ffp-contract=off -Wno-unused-command-line-argument)
target_link_options(aladdin PRIVATE -O2 -sMODULARIZE=1 -sEXPORT_ES6=1 -sENVIRONMENT=web
  -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sALLOW_MEMORY_GROWTH=1 -sSTACK_SIZE=2097152
  -sINITIAL_MEMORY=67108864 -sFORCE_FILESYSTEM=1 -lidbfs.js
  "-sEXPORTED_RUNTIME_METHODS=['FS','IDBFS','callMain','HEAPF32','ENV']"
  "-sEXPORTED_FUNCTIONS=['_main']")
set_target_properties(aladdin PROPERTIES SUFFIX ".mjs" RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/site)
