# Include from the client's CMakeLists; twidget_target_sources compiles every engine/render source into
# libtwidget.so (jni/CMakeLists.txt), the only library the isolated engine processes load. No .a.
#
# widget-sandbox (host, validator, QuickJS, protocol, bundles) comes from jni/twidget/widget-sandbox.
# The renderer is twidget's TwidgetCore on twidget's litehtml fork (TW_LITEHTML_TWIDGET), from
# jni/twidget/twidget: CSS animations, Web Animations, canvas and compositor layers need the fork.
# widget-sandbox's own renderer-litehtml and its upstream litehtml are not compiled.
set(WIDGET_SANDBOX_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/widget-sandbox")
set(TWIDGET_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/twidget")
set(_TWIDGET_BRIDGE_DIR "${CMAKE_CURRENT_LIST_DIR}")

function(twidget_target_sources target)
    if(NOT TARGET "${target}")
        message(FATAL_ERROR "Create target ${target} before attaching twidget")
    endif()
    get_target_property(attached "${target}" TWIDGET_ATTACHED)
    if(attached)
        return()
    endif()

    set(root "${WIDGET_SANDBOX_SOURCE_DIR}")
    set(bridge_dir "${_TWIDGET_BRIDGE_DIR}")
    set(qjs "${root}/packages/ios/Sources/CQuickJS")
    set(sandbox "${root}/packages/desktop/sandbox")
    set(core "${TWIDGET_SOURCE_DIR}/core/src")
    set(litehtml "${TWIDGET_SOURCE_DIR}/litehtml")
    set(gumbo "${litehtml}/src/gumbo")
    foreach(required "${qjs}/quickjs/quickjs.c" "${sandbox}/src/widget_host.cpp"
                     "${core}/renderer.cpp" "${litehtml}/src/document.cpp")
        if(NOT EXISTS "${required}")
            message(FATAL_ERROR "Missing ${required} in jni/twidget")
        endif()
    endforeach()

    # The profile's UA layer (tables.uaStylesheet in widget-sandbox's profile.json) as twidget's own CMake
    # generates it: tw_ua_stylesheet.h for the renderer's master CSS.
    set(profile_json "${root}/packages/core/profile.json")
    set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${profile_json}")
    file(READ "${profile_json}" profile)
    string(JSON ua_stylesheet GET "${profile}" tables uaStylesheet)
    set(generated "${CMAKE_CURRENT_BINARY_DIR}/twidget-generated")
    file(WRITE "${generated}/tw_ua_stylesheet.h.tmp"
        "// Generated by jni/twidget/twidget.cmake from widget-sandbox's packages/core/profile.json (tables.uaStylesheet).\n"
        "#pragma once\n"
        "inline constexpr const char* TW_UA_STYLESHEET = R\"tw-ua(${ua_stylesheet})tw-ua\";\n")
    configure_file("${generated}/tw_ua_stylesheet.h.tmp" "${generated}/tw_ua_stylesheet.h" COPYONLY)

    set(qjs_sources
        "${qjs}/quickjs/quickjs.c"
        "${qjs}/quickjs/dtoa.c"
        "${qjs}/quickjs/libregexp.c"
        "${qjs}/quickjs/libunicode.c"
        "${qjs}/shim/qjs_shim.c")
    file(GLOB sandbox_sources CONFIGURE_DEPENDS "${sandbox}/src/*.cpp")
    # Intl on Windows' own ICU (icu.h): Android answers Intl from Java (WidgetSandboxIntl).
    list(FILTER sandbox_sources EXCLUDE REGEX "/icu_intl\\.cpp$")
    file(GLOB litehtml_sources CONFIGURE_DEPENDS "${litehtml}/src/*.cpp")
    file(GLOB gumbo_sources CONFIGURE_DEPENDS "${gumbo}/*.c")
    set(core_sources
        "${core}/renderer.cpp"
        "${core}/computed_style.cpp"
        "${core}/css.cpp"
        "${core}/animations.cpp"
        "${core}/canvas.cpp"
        "${core}/compositor.cpp"
        "${core}/encode.cpp"
        "${core}/png.cpp"
        "${core}/filters.cpp"
        "${core}/paint_policy.cpp"
        "${bridge_dir}/twidget-bridge.cpp")

    target_sources("${target}" PRIVATE ${qjs_sources} ${gumbo_sources} ${sandbox_sources} ${litehtml_sources}
        ${core_sources})
    # Dependency include paths belong to these sources, not unrelated client components.
    set(cxx_options "-std=c++20;-O2;-fexceptions;-frtti;-fno-fast-math;-fvisibility=hidden")
    set_source_files_properties(${qjs_sources} PROPERTIES
        INCLUDE_DIRECTORIES "${qjs}/include;${qjs}/quickjs"
        COMPILE_DEFINITIONS "_GNU_SOURCE;QUICKJS_NG_BUILD"
        COMPILE_OPTIONS "-std=gnu11;-O2;-fno-strict-aliasing;-fno-fast-math;-fvisibility=hidden")
    set_source_files_properties(${gumbo_sources} PROPERTIES
        INCLUDE_DIRECTORIES "${litehtml}/gumbo-include;${litehtml}/gumbo-include/gumbo;${gumbo}"
        COMPILE_OPTIONS "-std=gnu99;-O2;-fno-fast-math;-fvisibility=hidden;-w")
    set_source_files_properties(${sandbox_sources} PROPERTIES
        INCLUDE_DIRECTORIES "${qjs}/include;${qjs}/quickjs;${sandbox}/include"
        COMPILE_OPTIONS "${cxx_options}")
    # The fork is compiled as it comes, without its warnings (as twidget's own CMake does).
    set_source_files_properties(${litehtml_sources} PROPERTIES
        INCLUDE_DIRECTORIES "${litehtml}/include;${litehtml}/include/litehtml;${litehtml}/gumbo-include;${litehtml}/src"
        COMPILE_OPTIONS "${cxx_options};-w")
    set_source_files_properties(${core_sources} PROPERTIES
        INCLUDE_DIRECTORIES "${qjs}/include;${qjs}/quickjs;${sandbox}/include;${core};${litehtml}/include;${litehtml}/include/litehtml;${litehtml}/gumbo-include;${bridge_dir};${generated}"
        COMPILE_DEFINITIONS "TW_LITEHTML_TWIDGET"
        COMPILE_OPTIONS "${cxx_options}")
    find_package(Threads REQUIRED)
    # Do not mix plain/keyword target_link_libraries signatures used by the client.
    set_property(TARGET "${target}" APPEND PROPERTY LINK_LIBRARIES Threads::Threads m)
    if(ANDROID)
        set_property(TARGET "${target}" APPEND PROPERTY LINK_LIBRARIES android log dl jnigraphics)
    endif()
    set_target_properties("${target}" PROPERTIES TWIDGET_ATTACHED TRUE)
endfunction()

# The client library's share (jni/CMakeLists.txt): widget-sandbox's gesture policy and twidget's CPU filter pass, with
# their JNI (twidget-client.cpp, WidgetClientNative), so the client process never maps libtwidget.so. An object
# library: its own flags rather than the client library's (-ffast-math -Oz), and no archive to drop the unreferenced
# Java_* exports. gesture_policy.cpp and filters.cpp are in libtwidget too, with the source properties set above.
function(twidget_client_sources target)
    set(sandbox "${WIDGET_SANDBOX_SOURCE_DIR}/packages/desktop/sandbox")
    set(core "${TWIDGET_SOURCE_DIR}/core/src")
    add_library(twidget_client OBJECT
        "${sandbox}/src/gesture_policy.cpp"
        "${core}/filters.cpp"
        "${_TWIDGET_BRIDGE_DIR}/twidget-client.cpp")
    target_include_directories(twidget_client PRIVATE "${sandbox}/include" "${core}")
    set_target_properties(twidget_client PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON)
    target_compile_options(twidget_client PRIVATE -O2 -fexceptions -frtti -fno-fast-math -fvisibility=hidden)
    target_sources("${target}" PRIVATE $<TARGET_OBJECTS:twidget_client>)
endfunction()
