# Web-only Blender data engine target. This is intentionally separate from the
# desktop creator/window-manager entry point and only exposes the C ABI.

set(SRC
  web_engine_blend_reader.cpp
  web_engine_depsgraph.cpp
  web_engine_main_state.cpp
  web_engine_api.cpp
  web_engine_decimate.cpp
  web_engine_headless_fallback.cpp
  web_gpu_fallback.cpp
  web_rna_fallback.cpp
)

set(INC
  PUBLIC .
  ${CMAKE_SOURCE_DIR}/source/blender/blenkernel
  ${CMAKE_SOURCE_DIR}/source/blender/blenfont
  ${CMAKE_SOURCE_DIR}/source/blender/animrig
  ${CMAKE_SOURCE_DIR}/source/blender/blenloader
  ${CMAKE_SOURCE_DIR}/source/blender/depsgraph
  ${CMAKE_SOURCE_DIR}/source/blender/gpu
  ${CMAKE_SOURCE_DIR}/source/blender/imbuf
  ${CMAKE_SOURCE_DIR}/source/blender/makesrna
  ${CMAKE_SOURCE_DIR}/source/blender/makesrna/intern
  ${CMAKE_SOURCE_DIR}/source/blender/nodes
  ${CMAKE_SOURCE_DIR}/source/blender/sequencer
  ${CMAKE_SOURCE_DIR}/source/blender/render
  ${CMAKE_SOURCE_DIR}/source/blender/windowmanager
  ${CMAKE_SOURCE_DIR}/source/blender/editors/include
  ${CMAKE_SOURCE_DIR}/source/blender/editors/asset
  ${CMAKE_SOURCE_DIR}/source/blender/functions
  ${CMAKE_SOURCE_DIR}/source/blender/makesdna/intern
  ${CMAKE_SOURCE_DIR}/intern/clog
  ${CMAKE_SOURCE_DIR}/extern/json/include
)

set(LIB
  PRIVATE bf::blenlib
  PRIVATE bf::dna
  PRIVATE bf::blenloader_core
  PRIVATE bf::blenloader
  PRIVATE bf::depsgraph
  PRIVATE bf::bmesh
  PRIVATE bf::intern::guardedalloc
  PRIVATE bf::dependencies::zstd
)

blender_add_lib(blender_web_engine_core "${SRC}" "${INC}" "" "${LIB}")
add_library(bf::web_engine ALIAS blender_web_engine_core)

option(WEB_ENGINE_ENABLE_UNSAFE_DEPSGRAPH
  "Enable verbose native Depsgraph diagnostics" OFF)
mark_as_advanced(WEB_ENGINE_ENABLE_UNSAFE_DEPSGRAPH)
if(WEB_ENGINE_ENABLE_UNSAFE_DEPSGRAPH)
  target_compile_definitions(blender_web_engine_core PRIVATE WEB_ENGINE_ENABLE_UNSAFE_DEPSGRAPH)
endif()

add_executable(web_engine
  "${CMAKE_SOURCE_DIR}/../web/engine/web_engine_smoke.cpp"
)
target_include_directories(web_engine PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")

# Link archive files instead of CMake targets here. Blender's static targets
# intentionally expose their implementation dependencies to the desktop
# executable; following those edges would pull the entire editor and image
# stack into this data-only WebAssembly module.
set(WEB_ENGINE_ARCHIVE_TARGETS
  blender_web_engine_core
  bf_bmesh
  bf_blenkernel
  bf_blenloader_core
  bf_blenloader
  bf_depsgraph
  bf_rna
  bf_blentranslation
  bf_animrig
  bf_asset_system
  bf_imbuf
  bf_imbuf_movie
  bf_functions
  bf_geometry
  bf_nodes
  bf_modifiers
  bf_blenfont
  bf_editor_datafiles
  bf_imbuf_opencolorio
  bf_imbuf_openexr
  bf_simulation
  bf_ikplugin
  bf_intern_iksolver
  bf_intern_eigen
  bf_intern_memutil
  bf_intern_ghost
  extern_xxhash
  extern_wcwidth
  bf_intern_libmv
  bf_intern_opensubdiv
  bf_blenlib
  bf_dna
  bf_intern_clog
  bf_intern_guardedalloc
  extern_rangetree
  zlibstatic
  libzstd_static
  osd_static_cpu
)
set(WEB_ENGINE_ARCHIVES "")
foreach(archive_target IN LISTS WEB_ENGINE_ARCHIVE_TARGETS)
  list(APPEND WEB_ENGINE_ARCHIVES "$<TARGET_FILE:${archive_target}>")
endforeach()
if(WITH_GMP)
  list(APPEND WEB_ENGINE_ARCHIVES ${GMP_LIBRARIES})
endif()
add_dependencies(web_engine ${WEB_ENGINE_ARCHIVE_TARGETS})
# The libraries contain a few intentional cycles. Repeating the small archive
# group is equivalent to Blender's LINK_INTERFACE_MULTIPLICITY policy without
# restoring the desktop-only dependency graph.
target_link_libraries(web_engine PRIVATE
  ${WEB_ENGINE_ARCHIVES}
  ${WEB_ENGINE_ARCHIVES}
  ${WEB_ENGINE_ARCHIVES}
)
target_link_options(web_engine PRIVATE
  "-Wl,--error-limit=0"
  "-sUSE_FREETYPE=1"
  "-sENVIRONMENT=web,node,worker"
  "-sALLOW_MEMORY_GROWTH=1"
  "-sINITIAL_MEMORY=67108864"
  "-sMAXIMUM_MEMORY=2147483648"
  "-sSTACK_SIZE=8388608"
  "-sEXPORTED_FUNCTIONS=['_malloc','_free','_web_engine_create','_web_engine_destroy','_web_engine_get_memory_stats','_web_engine_get_live_handles','_web_engine_get_allocated_bytes','_web_engine_open_blend','_web_engine_apply_command','_web_engine_undo','_web_engine_redo','_web_engine_get_scene_snapshot','_web_engine_get_scene_metadata','_web_engine_get_scene_geometry','_web_engine_get_scene_delta','_web_engine_get_packed_asset','_web_engine_evaluate_depsgraph','_web_engine_save_blend','_web_engine_free_buffer','_web_engine_last_error_code','_web_engine_last_error_message','_web_engine_decimate_apply']"
  "-sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','UTF8ToString']"
)
if(WEB_ENGINE_ENABLE_UNSAFE_DEPSGRAPH)
  target_link_options(web_engine PRIVATE
    "-sASSERTIONS=2"
    "-sSTACK_OVERFLOW_CHECK=2"
    "--profiling-funcs"
  )
endif()
set_target_properties(web_engine PROPERTIES OUTPUT_NAME "web_engine")
