| 1 | # Include from the client's CMakeLists; twidget_target_sources compiles every engine/render source into |
| 2 | # libtwidget.so (jni/CMakeLists.txt), the only library the isolated engine processes load. No .a. |
| 3 | # |
| 4 | # widget-sandbox (host, validator, QuickJS, protocol, bundles) comes from jni/twidget/widget-sandbox. |
| 5 | # 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. |
| 7 | # widget-sandbox's own renderer-litehtml and its upstream litehtml are not compiled. |
| 8 | set(WIDGET_SANDBOX_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/widget-sandbox") |
| 9 | set(TWIDGET_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/twidget") |
| 10 | set(_TWIDGET_BRIDGE_DIR "${CMAKE_CURRENT_LIST_DIR}") |
| 11 |
| 12 | function(twidget_target_sources target) |
| 13 | if(NOT TARGET "${target}") |
| 14 | message(FATAL_ERROR "Create target ${target} before attaching twidget") |
| 15 | endif() |
| 16 | get_target_property(attached "${target}" TWIDGET_ATTACHED) |
| 17 | if(attached) |
| 18 | return() |
| 19 | endif() |
| 20 |
| 21 | set(root "${WIDGET_SANDBOX_SOURCE_DIR}") |
| 22 | set(bridge_dir "${_TWIDGET_BRIDGE_DIR}") |
| 23 | set(qjs "${root}/packages/ios/Sources/CQuickJS") |
| 24 | set(sandbox "${root}/packages/desktop/sandbox") |
| 25 | set(core "${TWIDGET_SOURCE_DIR}/core/src") |
| 26 | set(litehtml "${TWIDGET_SOURCE_DIR}/litehtml") |
| 27 | set(gumbo "${litehtml}/src/gumbo") |
| 28 | foreach(required "${qjs}/quickjs/quickjs.c" "${sandbox}/src/widget_host.cpp" |
| 29 | "${core}/renderer.cpp" "${litehtml}/src/document.cpp") |
| 30 | if(NOT EXISTS "${required}") |
| 31 | message(FATAL_ERROR "Missing ${required} in jni/twidget") |
| 32 | endif() |
| 33 | endforeach() |
| 34 |
| 35 | # The profile's UA layer (tables.uaStylesheet in widget-sandbox's profile.json) as twidget's own CMake |
| 36 | # generates it: tw_ua_stylesheet.h for the renderer's master CSS. |
| 37 | set(profile_json "${root}/packages/core/profile.json") |
| 38 | set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${profile_json}") |
| 39 | file(READ "${profile_json}" profile) |
| 40 | string(JSON ua_stylesheet GET "${profile}" tables uaStylesheet) |
| 41 | set(generated "${CMAKE_CURRENT_BINARY_DIR}/twidget-generated") |
| 42 | file(WRITE "${generated}/tw_ua_stylesheet.h.tmp" |
| 43 | "// Generated by jni/twidget/twidget.cmake from widget-sandbox's packages/core/profile.json (tables.uaStylesheet).\n" |
| 44 | "#pragma once\n" |
| 45 | "inline constexpr const char* TW_UA_STYLESHEET = R\"tw-ua(${ua_stylesheet})tw-ua\";\n") |
| 46 | configure_file("${generated}/tw_ua_stylesheet.h.tmp" "${generated}/tw_ua_stylesheet.h" COPYONLY) |
| 47 |
| 48 | set(qjs_sources |
| 49 | "${qjs}/quickjs/quickjs.c" |
| 50 | "${qjs}/quickjs/dtoa.c" |
| 51 | "${qjs}/quickjs/libregexp.c" |
| 52 | "${qjs}/quickjs/libunicode.c" |
| 53 | "${qjs}/shim/qjs_shim.c") |
| 54 | file(GLOB sandbox_sources CONFIGURE_DEPENDS "${sandbox}/src/*.cpp") |
| 55 | # Intl on Windows' own ICU (icu.h): Android answers Intl from Java (WidgetSandboxIntl). |
| 56 | list(FILTER sandbox_sources EXCLUDE REGEX "/icu_intl\\.cpp$") |
| 57 | file(GLOB litehtml_sources CONFIGURE_DEPENDS "${litehtml}/src/*.cpp") |
| 58 | file(GLOB gumbo_sources CONFIGURE_DEPENDS "${gumbo}/*.c") |
| 59 | set(core_sources |
| 60 | "${core}/renderer.cpp" |
| 61 | "${core}/computed_style.cpp" |
| 62 | "${core}/css.cpp" |
| 63 | "${core}/animations.cpp" |
| 64 | "${core}/canvas.cpp" |
| 65 | "${core}/compositor.cpp" |
| 66 | "${core}/encode.cpp" |
| 67 | "${core}/png.cpp" |
| 68 | "${core}/filters.cpp" |
| 69 | "${core}/paint_policy.cpp" |
| 70 | "${bridge_dir}/twidget-bridge.cpp") |
| 71 |
| 72 | target_sources("${target}" PRIVATE ${qjs_sources} ${gumbo_sources} ${sandbox_sources} ${litehtml_sources} |
| 73 | ${core_sources}) |
| 74 | # Dependency include paths belong to these sources, not unrelated client components. |
| 75 | set(cxx_options "-std=c++20;-O2;-fexceptions;-frtti;-fno-fast-math;-fvisibility=hidden") |
| 76 | set_source_files_properties(${qjs_sources} PROPERTIES |
| 77 | INCLUDE_DIRECTORIES "${qjs}/include;${qjs}/quickjs" |
| 78 | COMPILE_DEFINITIONS "_GNU_SOURCE;QUICKJS_NG_BUILD" |
| 79 | COMPILE_OPTIONS "-std=gnu11;-O2;-fno-strict-aliasing;-fno-fast-math;-fvisibility=hidden") |
| 80 | set_source_files_properties(${gumbo_sources} PROPERTIES |
| 81 | INCLUDE_DIRECTORIES "${litehtml}/gumbo-include;${litehtml}/gumbo-include/gumbo;${gumbo}" |
| 82 | COMPILE_OPTIONS "-std=gnu99;-O2;-fno-fast-math;-fvisibility=hidden;-w") |
| 83 | set_source_files_properties(${sandbox_sources} PROPERTIES |
| 84 | INCLUDE_DIRECTORIES "${qjs}/include;${qjs}/quickjs;${sandbox}/include" |
| 85 | COMPILE_OPTIONS "${cxx_options}") |
| 86 | # The fork is compiled as it comes, without its warnings (as twidget's own CMake does). |
| 87 | set_source_files_properties(${litehtml_sources} PROPERTIES |
| 88 | INCLUDE_DIRECTORIES "${litehtml}/include;${litehtml}/include/litehtml;${litehtml}/gumbo-include;${litehtml}/src" |
| 89 | COMPILE_OPTIONS "${cxx_options};-w") |
| 90 | set_source_files_properties(${core_sources} PROPERTIES |
| 91 | INCLUDE_DIRECTORIES "${qjs}/include;${qjs}/quickjs;${sandbox}/include;${core};${litehtml}/include;${litehtml}/include/litehtml;${litehtml}/gumbo-include;${bridge_dir};${generated}" |
| 92 | COMPILE_DEFINITIONS "TW_LITEHTML_TWIDGET" |
| 93 | COMPILE_OPTIONS "${cxx_options}") |
| 94 | find_package(Threads REQUIRED) |
| 95 | # Do not mix plain/keyword target_link_libraries signatures used by the client. |
| 96 | set_property(TARGET "${target}" APPEND PROPERTY LINK_LIBRARIES Threads::Threads m) |
| 97 | if(ANDROID) |
| 98 | set_property(TARGET "${target}" APPEND PROPERTY LINK_LIBRARIES android log dl jnigraphics) |
| 99 | endif() |
| 100 | set_target_properties("${target}" PROPERTIES TWIDGET_ATTACHED TRUE) |
| 101 | endfunction() |
| 102 |
| 103 | # The client library's share (jni/CMakeLists.txt): widget-sandbox's gesture policy and twidget's CPU filter pass, with |
| 104 | # their JNI (twidget-client.cpp, WidgetClientNative), so the client process never maps libtwidget.so. An object |
| 105 | # library: its own flags rather than the client library's (-ffast-math -Oz), and no archive to drop the unreferenced |
| 106 | # Java_* exports. gesture_policy.cpp and filters.cpp are in libtwidget too, with the source properties set above. |
| 107 | function(twidget_client_sources target) |
| 108 | set(sandbox "${WIDGET_SANDBOX_SOURCE_DIR}/packages/desktop/sandbox") |
| 109 | set(core "${TWIDGET_SOURCE_DIR}/core/src") |
| 110 | add_library(twidget_client OBJECT |
| 111 | "${sandbox}/src/gesture_policy.cpp" |
| 112 | "${core}/filters.cpp" |
| 113 | "${_TWIDGET_BRIDGE_DIR}/twidget-client.cpp") |
| 114 | target_include_directories(twidget_client PRIVATE "${sandbox}/include" "${core}") |
| 115 | set_target_properties(twidget_client PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON) |
| 116 | target_compile_options(twidget_client PRIVATE -O2 -fexceptions -frtti -fno-fast-math -fvisibility=hidden) |
| 117 | target_sources("${target}" PRIVATE $<TARGET_OBJECTS:twidget_client>) |
| 118 | endfunction() |
| 119 |