# Wrapper-side TGUI 0.9 build (plan task 4).
#
# TGUI 0.9's own CMake has no emscripten branch (cmake/Config.cmake
# FATAL_ERRORs "Unsupported operating system or environment" on
# CMAKE_SYSTEM_NAME=Emscripten), and TGUI lives inside the oboe/ submodule,
# which the web build must not modify. So we build TGUI from its sources
# here, UNMODIFIED: the explicit source list from
# oboe/deps/TGUI/src/CMakeLists.txt (TGUI_SRC) + the SFML backend files from
# oboe/deps/TGUI/src/Backends/SFML/CMakeLists.txt, minus the two
# platform-specific icon loaders (Windows/Linux only; neither compiles on
# emscripten and neither is referenced there).
#
# TGUI's CMake also generates include/TGUI/Config.hpp from Config.hpp.in via
# configure_file(@ONLY) — into its own source tree, which we avoid. We
# generate ours into the build tree instead, with one splice: the platform
# detection would classify emscripten as TGUI_SYSTEM_LINUX (emcc defines
# __unix__ + __linux__), which makes BackendSFML.cpp #include <X11/Xlib.h>.
# The spliced copy adds an __EMSCRIPTEN__ arm that defines no system macro.
cmake_minimum_required(VERSION 3.22)
project(tgui-web CXX)

set(TGUI_SRC_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../oboe/deps/TGUI)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ---- generated Config.hpp (build tree only; values match TGUI 0.9.5) ----
set(TGUI_CXX_STANDARD 14)
set(TGUI_VERSION_MAJOR 0)
set(TGUI_VERSION_MINOR 9)
set(TGUI_VERSION_PATCH 5)
set(TGUI_USE_GLES 0)
set(TGUI_HAS_BACKEND_SFML 1)
set(TGUI_HAS_BACKEND_SDL 0)

set(CONFIG_HPP_IN ${TGUI_SRC_ROOT}/include/TGUI/Config.hpp.in)
set(CONFIG_HPP_WORK ${CMAKE_BINARY_DIR}/Config.hpp.in)  # file(COPY) keeps the source name
set(CONFIG_HPP_OUT ${CMAKE_BINARY_DIR}/include/TGUI/Config.hpp)
file(COPY ${CONFIG_HPP_IN} DESTINATION ${CMAKE_BINARY_DIR})
# splice: stop emscripten being detected as Linux (X11 headers unavailable)
file(READ ${CONFIG_HPP_WORK} _cfg)
string(REPLACE "#elif defined(__unix__)"
               "#elif defined(__EMSCRIPTEN__)\n    // Web build: no platform-specific code paths\n#elif defined(__unix__)"
               _cfg "${_cfg}")
file(WRITE ${CONFIG_HPP_WORK} "${_cfg}")
configure_file(${CONFIG_HPP_WORK} ${CONFIG_HPP_OUT} @ONLY)

# ---- sources (explicit list from TGUI's own CMake, minus platform loaders) ----
file(GLOB TGUI_CORE ${TGUI_SRC_ROOT}/src/*.cpp)
file(GLOB TGUI_SUB  ${TGUI_SRC_ROOT}/src/Loading/*.cpp
                    ${TGUI_SRC_ROOT}/src/Renderers/*.cpp
                    ${TGUI_SRC_ROOT}/src/Widgets/*.cpp)
file(GLOB TGUI_SFML ${TGUI_SRC_ROOT}/src/Backends/SFML/*.cpp)
list(REMOVE_ITEM TGUI_CORE
  ${TGUI_SRC_ROOT}/src/FileDialogIconLoaderWindows.cpp
  ${TGUI_SRC_ROOT}/src/FileDialogIconLoaderLinux.cpp)

add_library(tgui STATIC ${TGUI_CORE} ${TGUI_SUB} ${TGUI_SFML})
# -pthread: the game links -sUSE_PTHREADS (shared memory); emcc 6.x wasm-ld
# requires the atomics/bulk-memory features in every input object, so this
# is a compile flag too. TGUI's code stays single-threaded.
# -fexceptions: MUST match the game's -fexceptions (JS EH). Emscripten changes
# the exception-handling codegen/ABI with -fexceptions; a TGUI object compiled
# without it, when called from the exception-instrumented game, yields
# "function signature mismatch" in destructors (corrupted EH/vtable).
target_compile_options(tgui PRIVATE -pthread -fexceptions)
# build-tree generated headers FIRST so they shadow nothing in-tree
target_include_directories(tgui PUBLIC
  $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
  ${TGUI_SRC_ROOT}/include)
target_compile_definitions(tgui PUBLIC TGUI_STATIC)
# SFML headers from the shared prefix (task 3 install)
target_include_directories(tgui SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../prefix/include)

install(TARGETS tgui ARCHIVE DESTINATION lib)
install(DIRECTORY ${TGUI_SRC_ROOT}/include/TGUI DESTINATION include)
install(FILES ${CONFIG_HPP_OUT} DESTINATION include/TGUI)
