From 24c09a1e68404e1257e140d15d5de1a890ce3225 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc Date: Thu, 29 Oct 2026 19:27:10 +0100 Subject: [PATCH 1/3] Make native window detection available to consumers [#397] --- CMakeLists.txt | 4 +- README.md | 27 ++++- cmake/Frontends.cmake | 107 +++++++++++++++++- docs/CHANGELOG.md | 7 ++ docs/FRONTENDS.md | 16 ++- docs/PROJECT_STATUS.md | 7 +- test/cmake/CheckFrontendConsumer.cmake | 38 +++++++ test/cmake/consumer/CMakeLists.txt | 14 +++ test/cmake/consumer/main.cpp | 11 +- test/cmake/frontend-consumer/CMakeLists.txt | 72 ++++++++++++ .../frontend-consumer/provider/CMakeLists.txt | 45 ++++++++ .../provider/ConsumerSupport.cmake | 11 ++ .../frontend-consumer/sibling/CMakeLists.txt | 1 + test/unit/CMakeLists.txt | 36 +++++- 14 files changed, 384 insertions(+), 12 deletions(-) create mode 100644 test/cmake/CheckFrontendConsumer.cmake create mode 100644 test/cmake/frontend-consumer/CMakeLists.txt create mode 100644 test/cmake/frontend-consumer/provider/CMakeLists.txt create mode 100644 test/cmake/frontend-consumer/provider/ConsumerSupport.cmake create mode 100644 test/cmake/frontend-consumer/sibling/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 20970777..806b09c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,9 +27,9 @@ string(REGEX MATCH "([0-9]+\\.[0-9]+\\.[0-9]+)" VERSION_STRING "${VERSION_STRING project(WebFront LANGUAGES CXX VERSION ${VERSION_STRING}) include(cmake/Frontends.cmake) -# The examples need no -DWEBFRONT_EMBED_* option: unless the user decided, this platform's native window +# Applications need no -DWEBFRONT_EMBED_* option: unless the user decided, this platform's native window # engine is enabled when its dependencies are present. Test and fuzzing builds keep explicit selection. -if(WEBFRONT_IS_TOP_LEVEL AND WEBFRONT_BUILD_EXAMPLES AND NOT ENABLE_TESTING AND NOT ENABLE_FUZZING) +if(NOT ENABLE_TESTING AND NOT ENABLE_FUZZING) set(WEBFRONT_AUTODETECT_NATIVE_DEFAULT ON) else() set(WEBFRONT_AUTODETECT_NATIVE_DEFAULT OFF) diff --git a/README.md b/README.md index 9062cb75..64f0b730 100644 --- a/README.md +++ b/README.md @@ -258,7 +258,32 @@ target_link_libraries(MyApp PRIVATE WebFront::WebFront) As a subproject, examples and tests are off by default and compiler settings do not leak into your targets. `WEBFRONT_BUILD_EXAMPLES=OFF` disables examples in a standalone build too. -`webfront::WebFront` always uses the system browser. Native adapters remain optional: the examples enable the one of your platform when its dependencies are found, while your own application selects its frontend explicitly; CEF is available when you need an embedded Chromium distribution. See [WKWebView](docs/WKWEBVIEW.md), [WebKitGTK](docs/WEBKITGTK.md) and the [frontend architecture](docs/adr/README.md). +`webfront::WebFront` always uses the system browser. For a native window with the same browser fallback as the examples, add this after linking your target (also works after `CPMAddPackage`): + +```cmake +webfront_use_native_window(MyApp) +``` + +Select the application type in `main.cpp` using the definition supplied by the helper: + +```cpp +#include + +#if defined(WEBFRONT_NATIVE_WINDOW) +using App = webfront::WebFrontWithFrontend; +#else +using App = webfront::WebFront; +#endif + +int main() { + App app; + app.show(); +} +``` + +Native engines are detected by default for standalone and consumer builds, except with `ENABLE_TESTING=ON` or `ENABLE_FUZZING=ON`. The helper applies the native engine's target setup and defines `WEBFRONT_NATIVE_WINDOW` only when that engine is enabled; otherwise CMake prints installation advice and the application uses the default browser. On Windows, detection requires an MSVC-compatible compiler and downloads the pinned WebView2 SDK; the Evergreen runtime must be installed separately. Linux needs WebKitGTK 6.0 development files, and macOS needs Clang and macOS 13.3 or later. Explicit `WEBFRONT_EMBED_=ON|OFF` values take precedence; `WEBFRONT_AUTODETECT_NATIVE=OFF` disables detection. No option needs to be set before `add_subdirectory`/CPM for the default path. + +For explicit engine selection, `webfront_native_frontend_option(out)` and `webfront_setup_frontends_for_target(MyApp)` are also callable from the parent or other directories. The latter applies setup for all enabled engines; the native helper selects only the platform's native engine and never CEF. See [WKWebView](docs/WKWEBVIEW.md), [WebKitGTK](docs/WEBKITGTK.md) and the [frontend architecture](docs/adr/README.md). ## Build and test diff --git a/cmake/Frontends.cmake b/cmake/Frontends.cmake index fb65b9f2..30ba0555 100644 --- a/cmake/Frontends.cmake +++ b/cmake/Frontends.cmake @@ -1,13 +1,33 @@ include_guard(GLOBAL) -# Include this internal module first from WebFront's root CMakeLists.txt. Child directories inherit -# its registry variables; include_guard(GLOBAL) does not share them with unrelated or parent scopes. -# Standalone inclusion from such a scope is not supported. +# Metadata is shared across directory scopes, including parents of add_subdirectory/CPM. +# Keep ordinary variables available for existing internal callers and extension fixtures. +macro(_webfront_load_frontends) + get_property(WEBFRONT_FRONTEND_REGISTRY GLOBAL PROPERTY WEBFRONT_FRONTEND_REGISTRY) + foreach(_wf_entry IN LISTS WEBFRONT_FRONTEND_REGISTRY) + foreach(_wf_field OPTION POLICY DESCRIPTION SUPPORT SUPPORT_FILE SETUP TEST_SETUP TEST_NAME TEST_TIMEOUT PLATFORMS TEST_PLATFORMS XVFB_LINUX NATIVE) + get_property(WEBFRONT_FRONTEND_${_wf_entry}_${_wf_field} GLOBAL + PROPERTY WEBFRONT_FRONTEND_${_wf_entry}_${_wf_field}) + endforeach() + set(_wf_option "${WEBFRONT_FRONTEND_${_wf_entry}_OPTION}") + # Automatic values are not cache entries. Publish the configured result while preserving + # explicit normal/cache options and standalone registry callers' local selections. + get_property(_wf_configured GLOBAL PROPERTY WEBFRONT_FRONTEND_${_wf_entry}_ENABLED SET) + if(_wf_configured AND NOT _wf_option STREQUAL "" AND NOT DEFINED ${_wf_option}) + get_property(${_wf_option} GLOBAL PROPERTY WEBFRONT_FRONTEND_${_wf_entry}_ENABLED) + endif() + endforeach() + unset(_wf_entry) + unset(_wf_field) + unset(_wf_option) + unset(_wf_configured) +endmacro() # Registration order is the priority for the default embedded frontend. A new engine needs one # entry here and its support module. Options are always OFF by default and platform-checked before # dependencies are fetched. POLICY lenient preserves the CEF-off diagnostic build of webtest. function(webfront_register_frontend name) + _webfront_load_frontends() if(NOT "${name}" MATCHES "^[A-Za-z_][A-Za-z0-9_]*$") message(FATAL_ERROR "Frontend name '${name}' must be a C++ identifier") endif() @@ -75,12 +95,70 @@ function(webfront_register_frontend name) endif() endif() list(APPEND WEBFRONT_FRONTEND_REGISTRY "${name}") + set_property(GLOBAL PROPERTY WEBFRONT_FRONTEND_REGISTRY "${WEBFRONT_FRONTEND_REGISTRY}") set(WEBFRONT_FRONTEND_REGISTRY "${WEBFRONT_FRONTEND_REGISTRY}" PARENT_SCOPE) foreach(field OPTION POLICY DESCRIPTION SUPPORT SUPPORT_FILE SETUP TEST_SETUP TEST_NAME TEST_TIMEOUT PLATFORMS TEST_PLATFORMS XVFB_LINUX NATIVE) + set_property(GLOBAL PROPERTY WEBFRONT_FRONTEND_${name}_${field} "${ARG_${field}}") set(WEBFRONT_FRONTEND_${name}_${field} "${ARG_${field}}" PARENT_SCOPE) endforeach() endfunction() +# Opt an application into this platform's native window, with a browser fallback. The application +# selects NativeWebViewFrontend when WEBFRONT_NATIVE_WINDOW is defined, DefaultBrowserFrontend otherwise. +# Linking WebFront::WebFront remains explicit; CEF is never chosen by this helper. +function(webfront_use_native_window target) + _webfront_load_frontends() + if(NOT TARGET "${target}") + message(FATAL_ERROR "webfront_use_native_window requires an existing target (got '${target}')") + endif() + webfront_native_frontend_option(option) + set(enabled OFF) + set(detection "") + if(option) + set(enabled "${${option}}") + foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) + if(WEBFRONT_FRONTEND_${name}_OPTION STREQUAL option) + get_property(detection GLOBAL PROPERTY WEBFRONT_FRONTEND_${name}_DETECTION) + break() + endif() + endforeach() + endif() + if(enabled) + foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) + if(WEBFRONT_FRONTEND_${name}_OPTION STREQUAL option) + set(setup "${WEBFRONT_FRONTEND_${name}_SETUP}") + if(NOT setup STREQUAL "") + cmake_language(CALL ${setup} ${target}) + endif() + break() + endif() + endforeach() + target_compile_definitions(${target} PRIVATE WEBFRONT_NATIVE_WINDOW) + else() + if(NOT option) + set(advice "no native engine is registered for ${CMAKE_SYSTEM_NAME}") + elseif(NOT WEBFRONT_AUTODETECT_NATIVE) + set(advice "enable it with -D${option}=ON (automatic detection is off)") + elseif(detection STREQUAL "explicit") + set(advice "enable it with -D${option}=ON (it was explicitly disabled)") + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + set(advice "install the WebKitGTK 6.0 development package (libwebkitgtk-6.0-dev on Ubuntu/Debian, webkitgtk6.0-devel on Fedora; see docs/WEBKITGTK.md)") + elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(advice "use Clang on macOS 13.3 or later") + else() + set(advice "use an MSVC-compatible compiler") + endif() + if(option) + set(advice "For a native window, ${advice}, then run cmake again.") + else() + set(advice "${advice}; use the default browser on this platform.") + endif() + message(NOTICE "Native window: no native engine will be built.\n" + " ${target} will open the default browser instead (frontend DefaultBrowser).\n" + " ${advice}") + endif() +endfunction() + webfront_register_frontend(DefaultBrowser ALWAYS_AVAILABLE) webfront_register_frontend(WebView2 NATIVE OPTION WEBFRONT_EMBED_WEBVIEW2 POLICY strict @@ -108,6 +186,7 @@ webfront_register_frontend(CEF # Both option validation and standalone default selection use the registry's platform list. function(webfront_frontend_supports_platform name out) + _webfront_load_frontends() set(supported OFF) if(CMAKE_SYSTEM_NAME IN_LIST WEBFRONT_FRONTEND_${name}_PLATFORMS) set(supported ON) @@ -116,12 +195,14 @@ function(webfront_frontend_supports_platform name out) endfunction() function(webfront_declare_frontend_options) + _webfront_load_frontends() foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) set(option "${WEBFRONT_FRONTEND_${name}_OPTION}") if(option STREQUAL "") continue() endif() option(${option} "${WEBFRONT_FRONTEND_${name}_DESCRIPTION}" OFF) + set_property(GLOBAL PROPERTY WEBFRONT_FRONTEND_${name}_ENABLED "${${option}}") webfront_frontend_supports_platform("${name}" supported) if(${option} AND NOT supported) list(JOIN WEBFRONT_FRONTEND_${name}_PLATFORMS ", " platforms) @@ -175,6 +256,7 @@ endfunction() # configure probes again, while an explicit normal or cached value always wins, even when unchanged. # A missing engine only produces a status message; the application reports it at run time. function(webfront_autodetect_native_frontends) + _webfront_load_frontends() foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) set(option "${WEBFRONT_FRONTEND_${name}_OPTION}") if(option STREQUAL "" OR NOT WEBFRONT_FRONTEND_${name}_NATIVE) @@ -185,9 +267,11 @@ function(webfront_autodetect_native_frontends) continue() endif() if(DEFINED ${option}) + set_property(GLOBAL PROPERTY WEBFRONT_FRONTEND_${name}_DETECTION explicit) continue() endif() webfront_probe_native_frontend("${name}" available reason) + set_property(GLOBAL PROPERTY WEBFRONT_FRONTEND_${name}_DETECTION detected) set(${option} "${available}" PARENT_SCOPE) if(available) message(STATUS "Native window: ${name} detected, ${option}=ON") @@ -200,6 +284,7 @@ endfunction() # Hook functions are supplied by the support module. Validate them immediately after loading it, # rather than deferring an unknown-command error until target or test setup. function(webfront_validate_frontend_hooks name) + _webfront_load_frontends() foreach(field SETUP TEST_SETUP) set(hook "${WEBFRONT_FRONTEND_${name}_${field}}") if(NOT hook STREQUAL "" AND NOT COMMAND "${hook}") @@ -210,6 +295,7 @@ endfunction() # A macro keeps dependency variables (CEF_ROOT, CPM source directories, etc.) in the caller's scope. macro(webfront_include_frontend_support) + _webfront_load_frontends() foreach(_webfront_name IN LISTS WEBFRONT_FRONTEND_REGISTRY) set(_webfront_option "${WEBFRONT_FRONTEND_${_webfront_name}_OPTION}") if(NOT _webfront_option STREQUAL "") @@ -219,11 +305,21 @@ macro(webfront_include_frontend_support) endif() endif() endforeach() + # Support modules may adjust options while configuring dependencies. Publish the final + # configured values to other directories, while retaining the declaration-time snapshot + # for standalone registry users that do not load support modules. + foreach(_webfront_name IN LISTS WEBFRONT_FRONTEND_REGISTRY) + set(_webfront_option "${WEBFRONT_FRONTEND_${_webfront_name}_OPTION}") + if(NOT _webfront_option STREQUAL "") + set_property(GLOBAL PROPERTY WEBFRONT_FRONTEND_${_webfront_name}_ENABLED "${${_webfront_option}}") + endif() + endforeach() unset(_webfront_name) unset(_webfront_option) endmacro() function(webfront_setup_frontends_for_target target) + _webfront_load_frontends() foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) set(setup "${WEBFRONT_FRONTEND_${name}_SETUP}") if(NOT setup STREQUAL "") @@ -281,6 +377,7 @@ endfunction() # ARGUMENTS are passed to the Jasmine target (webtest runs `wf test` on a prepared project); SKIP_JASMINE registers the # lifecycle and engine cases only, when the target cannot run on this configuration. function(webfront_add_browser_tests target frontend assets_directory lifecycle_target) + _webfront_load_frontends() cmake_parse_arguments(ARG "SKIP_JASMINE" "" "ARGUMENTS" ${ARGN}) if(NOT frontend IN_LIST WEBFRONT_FRONTEND_REGISTRY) message(FATAL_ERROR "Unknown browser-test frontend '${frontend}'") @@ -339,6 +436,7 @@ endfunction() # Sets to the registered frontend names, optionally only the embedded ones. function(webfront_frontend_names out) + _webfront_load_frontends() cmake_parse_arguments(ARG "EMBEDDED_ONLY" "" "" ${ARGN}) set(names "") foreach(entry IN LISTS WEBFRONT_FRONTEND_REGISTRY) @@ -356,6 +454,7 @@ endfunction() # Production configuration rejects enabled unsupported engines when declaring options. Keep this # helper independently platform-aware for callers inspecting the registry, including test fixtures. function(webfront_default_embedded_frontend out) + _webfront_load_frontends() cmake_parse_arguments(ARG "" "FALLBACK" "" ${ARGN}) foreach(entry IN LISTS WEBFRONT_FRONTEND_REGISTRY) set(name "${entry}") @@ -375,6 +474,7 @@ endfunction() # with REQUIRE_AVAILABLE rejects a "strict" frontend whose availability option is OFF. Sets _TYPE in the caller to the C++ # type name, e.g. webfront::frontend::CEFFrontend. ALLOW_NATIVE adds the explicit platform alias. function(webfront_select_frontend var default description) + _webfront_load_frontends() cmake_parse_arguments(ARG "EMBEDDED_ONLY;REQUIRE_AVAILABLE;ALLOW_NATIVE" "" "" ${ARGN}) set(scope "") if(ARG_EMBEDDED_ONLY) @@ -427,6 +527,7 @@ endfunction() # Sets to the option of the native engine registered for this platform, or to "" when there is none. function(webfront_native_frontend_option out) + _webfront_load_frontends() set(result "") foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) webfront_frontend_supports_platform("${name}" supported) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8e0a4201..2226bbab 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +- Native-window detection is enabled by default for add_subdirectory/CPM consumers (#397). + On MSVC this downloads the pinned WebView2 SDK at configure time, including when no target + calls the helper; disable detection explicitly for offline or browser-only builds. + Frontend metadata and detected options are accessible from parent and sibling directories. + `webfront_use_native_window(target)` applies native setup and defines `WEBFRONT_NATIVE_WINDOW` + when available, with a configuration notice for the browser fallback otherwise. + - Let automation use an idle `wf dev` watcher started through nested `wf run` aliases, while retaining exclusive compilation/publication and module-script execution. Accept manifest entry labels such as `app:debug` and `app..debug` in native build/preview requests. diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 61106425..51c5fd2b 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -141,8 +141,8 @@ do not change the production HTTP/WebSocket transport. Enabling an engine makes its adapter available; select it explicitly in your application. `webfront::NativeApp` chooses the native engine for the current platform. -The options default to `OFF`. In a top-level build with examples and without `ENABLE_TESTING` or `ENABLE_FUZZING`, configuration first -probes the native engine of the platform (registered with `NATIVE`: WebKitGTK through `pkg-config webkitgtk-6.0`, WKWebView on Clang with +The options default to `OFF`. Without `ENABLE_TESTING` or `ENABLE_FUZZING`, standalone and add_subdirectory/CPM builds first +probe the native engine of the platform (registered with `NATIVE`: WebKitGTK through `pkg-config webkitgtk-6.0`, WKWebView on Clang with macOS 13.3+, WebView2 with an MSVC-compatible compiler) and sets `ON` or `OFF` in a normal variable, so `cmake ..` is enough to build the Life example with a native window. Automatic values do not create engine cache entries and are probed again at the next configuration. An explicit normal or cached option is never replaced, even if it equals the last detected value. Remove the cache entry with @@ -150,6 +150,18 @@ An explicit normal or cached option is never replaced, even if it equals the las A missing engine only prints a status; the Life examples then use the default browser and CMake explains what to install. Platforms without a registered native engine use the default browser as well. `-DWEBFRONT_AUTODETECT_NATIVE=OFF` turns the detection off. CEF is never detected. +These test and fuzzing variables are shared CMake options: a parent project that sets either to `ON` +also disables WebFront's automatic detection by default. Set `WEBFRONT_AUTODETECT_NATIVE=ON` +before adding WebFront to enable it explicitly. + +Consumers can call `webfront_use_native_window(target)` after linking `WebFront::WebFront`. +It applies the native engine's setup and defines `WEBFRONT_NATIVE_WINDOW` on that target when +enabled. Application code uses that definition to select `NativeWebViewFrontend` or the +default browser, as shown in the [README](../README.md#use-webfront-in-your-project). +The registry and configured options are shared across directories: the lower-level +`webfront_native_frontend_option(out)` and `webfront_setup_frontends_for_target(target)` +also work from a parent or sibling directory. The latter sets up every enabled engine; +the native helper never selects CEF. | Option | Default | Description | | --- | --- | --- | diff --git a/docs/PROJECT_STATUS.md b/docs/PROJECT_STATUS.md index c7244ce3..d78e9bba 100644 --- a/docs/PROJECT_STATUS.md +++ b/docs/PROJECT_STATUS.md @@ -9,8 +9,11 @@ WebFront is a header-only C++23 HTTP/WebSocket core plus a standalone developer - **C++ applications.** Link `WebFront::WebFront` from a CMake parent project. The default `webfront::WebFront` uses the system browser. Native applications explicitly select WebView2, - WKWebView, WebKitGTK or CEF; the standalone examples detect the platform's native engine - when its dependencies are available. See [frontends](FRONTENDS.md). + WKWebView, WebKitGTK or CEF. Standalone and consumer builds detect the platform's native + engine when its dependencies are available (outside test/fuzzing builds). + `webfront_use_native_window(target)` provides target setup and a definition for selecting + the native adapter with a browser fallback; see the [consumer example](../README.md#use-webfront-in-your-project) + and [frontends](FRONTENDS.md). - **Developer environment.** `NativeApp::bootstrap("developer")` loads the verified, pinned environment (0.6.2). Its terminal provides `wf init`, `create`, `install`, `restore`, `run`, `exec`, `check`, `test`, `build`, `dev`, `preview`, `embed`, `list` and `help`. The manifest and lock describe diff --git a/test/cmake/CheckFrontendConsumer.cmake b/test/cmake/CheckFrontendConsumer.cmake new file mode 100644 index 00000000..2e310913 --- /dev/null +++ b/test/cmake/CheckFrontendConsumer.cmake @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.31) +if(NOT ROOT OR NOT WORK_DIR) + message(FATAL_ERROR "ROOT and WORK_DIR are required") +endif() +foreach(probe ON OFF) + foreach(full_project OFF ON) + execute_process(COMMAND "${CMAKE_COMMAND}" --fresh + -S "${ROOT}/test/cmake/frontend-consumer" -B "${WORK_DIR}/${probe}-${full_project}" + "-DROOT=${ROOT}" "-DPROBE=${probe}" "-DFULL_PROJECT=${full_project}" + "-DCPM_MODULE=${CPM_MODULE}" ${DEPENDENCY_ARGS} + RESULT_VARIABLE result OUTPUT_VARIABLE output ERROR_VARIABLE error) + if(NOT result EQUAL 0) + message(FATAL_ERROR "Consumer native probe ${probe}: ${output}${error}") + endif() + if(NOT probe AND NOT "${output}${error}" MATCHES "parent_app_native will open the default browser instead") + message(FATAL_ERROR "Consumer fallback must explain the default browser: ${output}${error}") + endif() + endforeach() +endforeach() + +# Explicit OFF is distinct from a probe that cannot find the dependency. +execute_process(COMMAND "${CMAKE_COMMAND}" --fresh + -S "${ROOT}/test/cmake/frontend-consumer" -B "${WORK_DIR}/explicit-off" + "-DROOT=${ROOT}" -DPROBE=OFF -DFULL_PROJECT=ON "-DCPM_MODULE=${CPM_MODULE}" + -DWEBFRONT_EMBED_CONSUMER=OFF ${DEPENDENCY_ARGS} + RESULT_VARIABLE result OUTPUT_VARIABLE output ERROR_VARIABLE error) +if(NOT result EQUAL 0 OR NOT "${output}${error}" MATCHES "explicitly disabled") + message(FATAL_ERROR "Explicit OFF must be identified: ${output}${error}") +endif() + +# A support module's final option is what other directories observe. +execute_process(COMMAND "${CMAKE_COMMAND}" --fresh + -S "${ROOT}/test/cmake/frontend-consumer" -B "${WORK_DIR}/support-change" + "-DROOT=${ROOT}" -DPROBE=ON -DMUTATE_OTHER=ON + RESULT_VARIABLE result OUTPUT_VARIABLE output ERROR_VARIABLE error) +if(NOT result EQUAL 0) + message(FATAL_ERROR "Support module option change: ${output}${error}") +endif() diff --git a/test/cmake/consumer/CMakeLists.txt b/test/cmake/consumer/CMakeLists.txt index af20827c..fb831fcf 100644 --- a/test/cmake/consumer/CMakeLists.txt +++ b/test/cmake/consumer/CMakeLists.txt @@ -5,10 +5,24 @@ cmake_minimum_required(VERSION 3.31) project(WebFrontConsumer LANGUAGES C CXX) set(WEBFRONT_SOURCE_DIR "" CACHE PATH "Path of the WebFront checkout") +if(CPM_MODULE) + # A nested consumer configure reuses the already downloaded CPM module offline. + file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/cmake") + get_filename_component(cpm_filename "${CPM_MODULE}" NAME) + file(COPY_FILE "${CPM_MODULE}" "${CMAKE_BINARY_DIR}/cmake/${cpm_filename}" ONLY_IF_DIFFERENT) +endif() add_subdirectory("${WEBFRONT_SOURCE_DIR}" webfront) add_executable(consumer main.cpp) target_link_libraries(consumer PRIVATE WebFront::WebFront) +webfront_use_native_window(consumer) +if(EXPECT_NATIVE) + get_target_property(definitions consumer COMPILE_DEFINITIONS) + if(NOT "WEBFRONT_NATIVE_WINDOW" IN_LIST definitions) + message(FATAL_ERROR "The platform's native frontend was not selected") + endif() + target_compile_definitions(consumer PRIVATE WEBFRONT_CONSUMER_EXPECT_NATIVE) +endif() target_compile_definitions(consumer PRIVATE "$<$:WEBFRONT_CONSUMER_CXX=1>") # Registration from the parent scope must not rely on WebFront's private module path/registry. diff --git a/test/cmake/consumer/main.cpp b/test/cmake/consumer/main.cpp index 717850be..f7f388f8 100644 --- a/test/cmake/consumer/main.cpp +++ b/test/cmake/consumer/main.cpp @@ -14,9 +14,18 @@ #include #include +#if defined(WEBFRONT_CONSUMER_EXPECT_NATIVE) && !defined(WEBFRONT_NATIVE_WINDOW) +#error The native consumer must compile the native frontend branch +#endif +#if defined(WEBFRONT_NATIVE_WINDOW) +using ConsumerApp = webfront::WebFrontWithFrontend; +#else +using ConsumerApp = webfront::WebFront; +#endif + int main(int argc, char**) { if (argc > 1) { // never taken by the smoke test: it only proves that the library compiles and links - webfront::WebFront app; + ConsumerApp app; webfront::testing::JasmineTest tests{app}; app.show(); } diff --git a/test/cmake/frontend-consumer/CMakeLists.txt b/test/cmake/frontend-consumer/CMakeLists.txt new file mode 100644 index 00000000..1d6e6890 --- /dev/null +++ b/test/cmake/frontend-consumer/CMakeLists.txt @@ -0,0 +1,72 @@ +cmake_minimum_required(VERSION 3.31) +project(FrontendConsumer LANGUAGES CXX) + +# Register and detect inside a child directory, then use the API from its parent and a sibling. +add_subdirectory(provider) +include("${ROOT}/cmake/Frontends.cmake") +function(check_consumer target) + webfront_native_frontend_option(option) + if(NOT option STREQUAL "WEBFRONT_EMBED_CONSUMER") + message(FATAL_ERROR "Consumer lost native option: '${option}'") + endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${target}.cpp" "int main() { return 0; }\n") + add_executable(${target} "${CMAKE_CURRENT_BINARY_DIR}/${target}.cpp") + webfront_setup_frontends_for_target(${target}) + get_target_property(other_configured ${target} CONSUMER_OTHER_SETUP) + if(NOT MUTATE_OTHER AND NOT other_configured) + message(FATAL_ERROR "General frontend setup lost the other enabled engine") + elseif(MUTATE_OTHER AND other_configured) + message(FATAL_ERROR "Consumer setup ignored the support module's final option") + endif() + get_target_property(configured ${target} CONSUMER_SETUP) + if(PROBE AND NOT configured) + message(FATAL_ERROR "Consumer setup: expected ${PROBE}, got '${configured}'") + endif() + if(NOT PROBE AND configured) + message(FATAL_ERROR "Disabled engine setup was called") + endif() + add_executable(${target}_native "${CMAKE_CURRENT_BINARY_DIR}/${target}.cpp") + webfront_use_native_window(${target}_native) + get_target_property(other_configured ${target}_native CONSUMER_OTHER_SETUP) + if(other_configured) + message(FATAL_ERROR "Native helper must not set up other embedded engines") + endif() + get_target_property(configured ${target}_native CONSUMER_SETUP) + if(PROBE AND NOT configured) + message(FATAL_ERROR "Native helper did not apply the engine's setup") + elseif(NOT PROBE AND configured) + message(FATAL_ERROR "Native helper configured a disabled engine") + endif() + get_target_property(definitions ${target}_native COMPILE_DEFINITIONS) + if(PROBE AND NOT "WEBFRONT_NATIVE_WINDOW" IN_LIST definitions) + message(FATAL_ERROR "Consumer lost native window definition") + elseif(NOT PROBE AND "WEBFRONT_NATIVE_WINDOW" IN_LIST definitions) + message(FATAL_ERROR "Browser fallback defines native window") + endif() +endfunction() +check_consumer(parent_app) +add_subdirectory(sibling) + +if(PROBE) + function(check_override) + set(WEBFRONT_EMBED_CONSUMER OFF) + add_executable(override_app "${CMAKE_CURRENT_BINARY_DIR}/parent_app.cpp") + webfront_use_native_window(override_app) + get_target_property(definitions override_app COMPILE_DEFINITIONS) + if("WEBFRONT_NATIVE_WINDOW" IN_LIST definitions) + message(FATAL_ERROR "An explicit caller variable must override the published result") + endif() + endfunction() + check_override() +endif() + +function(check_unsupported) + set(CMAKE_SYSTEM_NAME UnsupportedOS) + add_executable(unsupported_app "${CMAKE_CURRENT_BINARY_DIR}/parent_app.cpp") + webfront_use_native_window(unsupported_app) + get_target_property(definitions unsupported_app COMPILE_DEFINITIONS) + if("WEBFRONT_NATIVE_WINDOW" IN_LIST definitions) + message(FATAL_ERROR "Unsupported platform must use the browser fallback") + endif() +endfunction() +check_unsupported() diff --git a/test/cmake/frontend-consumer/provider/CMakeLists.txt b/test/cmake/frontend-consumer/provider/CMakeLists.txt new file mode 100644 index 00000000..8e3fb07a --- /dev/null +++ b/test/cmake/frontend-consumer/provider/CMakeLists.txt @@ -0,0 +1,45 @@ +include("${ROOT}/cmake/Frontends.cmake") +# The fixture owns the native selection without changing the real target OS: the full +# project must still discover platform libraries (notably Windows threads) normally. +foreach(engine WebView2 WKWebView WebKitGTK) + set_property(GLOBAL PROPERTY WEBFRONT_FRONTEND_${engine}_NATIVE OFF) +endforeach() +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") +webfront_register_frontend(ConsumerNative + NATIVE OPTION WEBFRONT_EMBED_CONSUMER POLICY strict DESCRIPTION "Consumer test engine" + PLATFORMS "${CMAKE_SYSTEM_NAME}" SUPPORT ConsumerSupport SETUP consumer_setup + TEST_PLATFORMS "${CMAKE_SYSTEM_NAME}" TEST_NAME ConsumerTest TEST_TIMEOUT 5) +webfront_register_frontend(ConsumerOther + OPTION WEBFRONT_EMBED_CONSUMER_OTHER POLICY strict DESCRIPTION "Other embedded test engine" + PLATFORMS "${CMAKE_SYSTEM_NAME}" SUPPORT ConsumerSupport SETUP consumer_other_setup + TEST_PLATFORMS "${CMAKE_SYSTEM_NAME}" TEST_NAME ConsumerOtherTest TEST_TIMEOUT 5) +# Keep the fixture independent of the host's SDKs and all dependency downloads. +set(WEBFRONT_EMBED_WEBVIEW2 OFF) +set(WEBFRONT_EMBED_WKWEBVIEW OFF) +set(WEBFRONT_EMBED_WEBKITGTK OFF) +set(WEBFRONT_EMBED_CONSUMER_OTHER ON) +function(webfront_probe_native_frontend name out reason) + set(${out} "${PROBE}" PARENT_SCOPE) + set(${reason} "controlled consumer probe" PARENT_SCOPE) +endfunction() +if(FULL_PROJECT) + # Reuse the already provisioned CPM module too, not only dependency source trees. + # This configure must stay offline even when the outer build has no shared CPM cache. + if(CPM_MODULE) + set(CPM_SOURCE_CACHE "${CMAKE_CURRENT_BINARY_DIR}/cached-inputs") + file(MAKE_DIRECTORY "${CPM_SOURCE_CACHE}/cpm") + get_filename_component(cpm_filename "${CPM_MODULE}" NAME) + file(COPY_FILE "${CPM_MODULE}" "${CPM_SOURCE_CACHE}/cpm/${cpm_filename}" ONLY_IF_DIFFERENT) + endif() + set(ENABLE_TESTING OFF) + set(ENABLE_FUZZING OFF) + set(WEBFRONT_BUILD_EXAMPLES OFF) + add_subdirectory("${ROOT}" webfront) + if(NOT WEBFRONT_AUTODETECT_NATIVE) + message(FATAL_ERROR "Native detection must default to ON for consumers without examples") + endif() +else() + webfront_autodetect_native_frontends() + webfront_declare_frontend_options() + webfront_include_frontend_support() +endif() diff --git a/test/cmake/frontend-consumer/provider/ConsumerSupport.cmake b/test/cmake/frontend-consumer/provider/ConsumerSupport.cmake new file mode 100644 index 00000000..fa987f0c --- /dev/null +++ b/test/cmake/frontend-consumer/provider/ConsumerSupport.cmake @@ -0,0 +1,11 @@ +function(consumer_setup target) + set_property(TARGET ${target} PROPERTY CONSUMER_SETUP ON) +endfunction() + +function(consumer_other_setup target) + set_property(TARGET ${target} PROPERTY CONSUMER_OTHER_SETUP ON) +endfunction() + +if(MUTATE_OTHER) + set(WEBFRONT_EMBED_CONSUMER_OTHER OFF) +endif() diff --git a/test/cmake/frontend-consumer/sibling/CMakeLists.txt b/test/cmake/frontend-consumer/sibling/CMakeLists.txt new file mode 100644 index 00000000..72ebbf31 --- /dev/null +++ b/test/cmake/frontend-consumer/sibling/CMakeLists.txt @@ -0,0 +1 @@ +check_consumer(sibling_app) diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index eb8f9c64..2cdf96ee 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -166,18 +166,52 @@ endif() if(DEFINED CMAKE_CXX_SCAN_FOR_MODULES) list(APPEND _consumer_dependency_args -DCMAKE_CXX_SCAN_FOR_MODULES:BOOL=${CMAKE_CXX_SCAN_FOR_MODULES}) endif() +# Exercise public frontend helpers across directory scopes and the default detection in a real parent project. +add_test(NAME FrontendConsumerConfiguration + COMMAND ${CMAKE_COMMAND} -DROOT=${PROJECT_SOURCE_DIR} + -DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/frontend-consumer + -DCPM_MODULE=${CPM_DOWNLOAD_LOCATION} + "-DDEPENDENCY_ARGS=${_consumer_dependency_args}" + -P ${PROJECT_SOURCE_DIR}/test/cmake/CheckFrontendConsumer.cmake) +set_tests_properties(FrontendConsumerConfiguration PROPERTIES TIMEOUT 120) # Read the actual target's launcher at generation time, including an explicitly # configured or auto-discovered cache. Do not depend on Cache.cmake's local variable. +# The build smoke test opts out of detection to avoid SDK downloads after configuration; +# FrontendConsumerConfiguration tests the automatic path with deterministic support instead. add_test(NAME AddSubdirectoryConsumer COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test ${PROJECT_SOURCE_DIR}/test/cmake/consumer ${CMAKE_CURRENT_BINARY_DIR}/consumer-build --build-generator ${CMAKE_GENERATOR} --build-config $ - --build-options -DWEBFRONT_SOURCE_DIR=${PROJECT_SOURCE_DIR} ${_consumer_dependency_args} + --build-options -DWEBFRONT_SOURCE_DIR=${PROJECT_SOURCE_DIR} -DCPM_MODULE=${CPM_DOWNLOAD_LOCATION} ${_consumer_dependency_args} + -DWEBFRONT_AUTODETECT_NATIVE=OFF -DENABLE_CACHE=OFF "-DCMAKE_CXX_COMPILER_LAUNCHER:STRING=$" -DCMAKE_BUILD_TYPE=$ -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}) set_tests_properties(AddSubdirectoryConsumer PROPERTIES TIMEOUT 600 LABELS portability) +# Compile the real native frontend from a consuming project on the native desktop CI jobs. +# Reuse the configured SDK source (including WebView2) and CPM bootstrap without network access. +webfront_native_frontend_option(_consumer_native_option) +set(_consumer_native_enabled OFF) +if(_consumer_native_option) + set(_consumer_native_enabled "${${_consumer_native_option}}") +endif() +if(_consumer_native_enabled) + set(_consumer_native_dependency_args ${_consumer_dependency_args}) + if(WEBFRONT_EMBED_WEBVIEW2) + list(APPEND _consumer_native_dependency_args -DCPM_WebView2SDK_SOURCE=${WebView2SDK_SOURCE_DIR}) + endif() + add_test(NAME AddSubdirectoryNativeConsumer + COMMAND ${CMAKE_CTEST_COMMAND} + --build-and-test ${PROJECT_SOURCE_DIR}/test/cmake/consumer ${CMAKE_CURRENT_BINARY_DIR}/native-consumer-build + --build-generator ${CMAKE_GENERATOR} --build-config $ + --build-options -DWEBFRONT_SOURCE_DIR=${PROJECT_SOURCE_DIR} -DCPM_MODULE=${CPM_DOWNLOAD_LOCATION} + ${_consumer_native_dependency_args} -DEXPECT_NATIVE=ON + -DENABLE_CACHE=OFF "-DCMAKE_CXX_COMPILER_LAUNCHER:STRING=$" + -DCMAKE_BUILD_TYPE=$ -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}) + set_tests_properties(AddSubdirectoryNativeConsumer PROPERTIES TIMEOUT 600) +endif() + # Manual timing evidence for gzip cache/cold-decode work; never a timing-based test. add_executable(gzip_cache_benchmark EXCLUDE_FROM_ALL GzipCacheBenchmark.cpp) target_link_libraries(gzip_cache_benchmark PRIVATE WebFront_test_hardening WebFront_warnings WebFront_options WebFront) From cf4b1c7157d334cf3f653336b16d2e3edc282724 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc Date: Thu, 29 Oct 2026 21:15:41 +0100 Subject: [PATCH 2/3] Decouple consumer native detection from the parent's test options [#397] A parent's ENABLE_TESTING or ENABLE_FUZZING no longer disables native window detection for its consumers; only a top-level WebFront build keeps that exclusion. The registry loader leaves ordinary variables untouched when no registry was published, and the browser-fallback advice is printed once per configuration. Generic test option names are tracked in #412. --- CMakeLists.txt | 3 ++- README.md | 2 +- cmake/Frontends.cmake | 20 ++++++++++++++++---- docs/CHANGELOG.md | 5 +++-- docs/FRONTENDS.md | 7 +++---- test/cmake/CheckFrontendConsumer.cmake | 8 ++++++++ 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 806b09c0..1c129ff6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,8 @@ project(WebFront LANGUAGES CXX VERSION ${VERSION_STRING}) include(cmake/Frontends.cmake) # Applications need no -DWEBFRONT_EMBED_* option: unless the user decided, this platform's native window # engine is enabled when its dependencies are present. Test and fuzzing builds keep explicit selection. -if(NOT ENABLE_TESTING AND NOT ENABLE_FUZZING) +# An embedding parent's variables of those names do not decide for its consumers. +if(NOT WEBFRONT_IS_TOP_LEVEL OR (NOT ENABLE_TESTING AND NOT ENABLE_FUZZING)) set(WEBFRONT_AUTODETECT_NATIVE_DEFAULT ON) else() set(WEBFRONT_AUTODETECT_NATIVE_DEFAULT OFF) diff --git a/README.md b/README.md index 64f0b730..fab4f3f8 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,7 @@ int main() { } ``` -Native engines are detected by default for standalone and consumer builds, except with `ENABLE_TESTING=ON` or `ENABLE_FUZZING=ON`. The helper applies the native engine's target setup and defines `WEBFRONT_NATIVE_WINDOW` only when that engine is enabled; otherwise CMake prints installation advice and the application uses the default browser. On Windows, detection requires an MSVC-compatible compiler and downloads the pinned WebView2 SDK; the Evergreen runtime must be installed separately. Linux needs WebKitGTK 6.0 development files, and macOS needs Clang and macOS 13.3 or later. Explicit `WEBFRONT_EMBED_=ON|OFF` values take precedence; `WEBFRONT_AUTODETECT_NATIVE=OFF` disables detection. No option needs to be set before `add_subdirectory`/CPM for the default path. +Native engines are detected by default for consumer builds, whatever variables the parent project defines, and for standalone builds except with `ENABLE_TESTING=ON` or `ENABLE_FUZZING=ON`. The helper applies the native engine's target setup and defines `WEBFRONT_NATIVE_WINDOW` only when that engine is enabled; otherwise CMake prints installation advice and the application uses the default browser. On Windows, detection requires an MSVC-compatible compiler and downloads the pinned WebView2 SDK; the Evergreen runtime must be installed separately. Linux needs WebKitGTK 6.0 development files, and macOS needs Clang and macOS 13.3 or later. Explicit `WEBFRONT_EMBED_=ON|OFF` values take precedence; `WEBFRONT_AUTODETECT_NATIVE=OFF` disables detection. No option needs to be set before `add_subdirectory`/CPM for the default path. For explicit engine selection, `webfront_native_frontend_option(out)` and `webfront_setup_frontends_for_target(MyApp)` are also callable from the parent or other directories. The latter applies setup for all enabled engines; the native helper selects only the platform's native engine and never CEF. See [WKWebView](docs/WKWEBVIEW.md), [WebKitGTK](docs/WEBKITGTK.md) and the [frontend architecture](docs/adr/README.md). diff --git a/cmake/Frontends.cmake b/cmake/Frontends.cmake index 30ba0555..ea9d2bb0 100644 --- a/cmake/Frontends.cmake +++ b/cmake/Frontends.cmake @@ -3,7 +3,11 @@ include_guard(GLOBAL) # Metadata is shared across directory scopes, including parents of add_subdirectory/CPM. # Keep ordinary variables available for existing internal callers and extension fixtures. macro(_webfront_load_frontends) - get_property(WEBFRONT_FRONTEND_REGISTRY GLOBAL PROPERTY WEBFRONT_FRONTEND_REGISTRY) + # Without a published registry, ordinary variables set by a standalone caller stay untouched. + get_property(_wf_published GLOBAL PROPERTY WEBFRONT_FRONTEND_REGISTRY SET) + if(_wf_published) + get_property(WEBFRONT_FRONTEND_REGISTRY GLOBAL PROPERTY WEBFRONT_FRONTEND_REGISTRY) + endif() foreach(_wf_entry IN LISTS WEBFRONT_FRONTEND_REGISTRY) foreach(_wf_field OPTION POLICY DESCRIPTION SUPPORT SUPPORT_FILE SETUP TEST_SETUP TEST_NAME TEST_TIMEOUT PLATFORMS TEST_PLATFORMS XVFB_LINUX NATIVE) get_property(WEBFRONT_FRONTEND_${_wf_entry}_${_wf_field} GLOBAL @@ -21,6 +25,7 @@ macro(_webfront_load_frontends) unset(_wf_field) unset(_wf_option) unset(_wf_configured) + unset(_wf_published) endmacro() # Registration order is the priority for the default embedded frontend. A new engine needs one @@ -153,9 +158,16 @@ function(webfront_use_native_window target) else() set(advice "${advice}; use the default browser on this platform.") endif() - message(NOTICE "Native window: no native engine will be built.\n" - " ${target} will open the default browser instead (frontend DefaultBrowser).\n" - " ${advice}") + # The advice is identical for every target of a configuration: print it once. + get_property(noticed GLOBAL PROPERTY WEBFRONT_NATIVE_FALLBACK_NOTICED) + if(noticed) + message(STATUS "Native window: ${target} will open the default browser instead") + else() + set_property(GLOBAL PROPERTY WEBFRONT_NATIVE_FALLBACK_NOTICED ON) + message(NOTICE "Native window: no native engine will be built.\n" + " ${target} will open the default browser instead (frontend DefaultBrowser).\n" + " ${advice}") + endif() endif() endfunction() diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2226bbab..362d4ede 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,9 +5,10 @@ - Native-window detection is enabled by default for add_subdirectory/CPM consumers (#397). On MSVC this downloads the pinned WebView2 SDK at configure time, including when no target calls the helper; disable detection explicitly for offline or browser-only builds. - Frontend metadata and detected options are accessible from parent and sibling directories. + A parent's own `ENABLE_TESTING`/`ENABLE_FUZZING` do not disable it. Frontend metadata and detected + options are accessible from parent and sibling directories. `webfront_use_native_window(target)` applies native setup and defines `WEBFRONT_NATIVE_WINDOW` - when available, with a configuration notice for the browser fallback otherwise. + when available, with a configuration notice (printed once) for the browser fallback otherwise. - Let automation use an idle `wf dev` watcher started through nested `wf run` aliases, while retaining exclusive compilation/publication and module-script execution. Accept manifest entry diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 51c5fd2b..150a0bd8 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -141,7 +141,7 @@ do not change the production HTTP/WebSocket transport. Enabling an engine makes its adapter available; select it explicitly in your application. `webfront::NativeApp` chooses the native engine for the current platform. -The options default to `OFF`. Without `ENABLE_TESTING` or `ENABLE_FUZZING`, standalone and add_subdirectory/CPM builds first +The options default to `OFF`. Standalone builds without `ENABLE_TESTING` or `ENABLE_FUZZING`, and every add_subdirectory/CPM build, first probe the native engine of the platform (registered with `NATIVE`: WebKitGTK through `pkg-config webkitgtk-6.0`, WKWebView on Clang with macOS 13.3+, WebView2 with an MSVC-compatible compiler) and sets `ON` or `OFF` in a normal variable, so `cmake ..` is enough to build the Life example with a native window. Automatic values do not create engine cache entries and are probed again at the next configuration. @@ -150,9 +150,8 @@ An explicit normal or cached option is never replaced, even if it equals the las A missing engine only prints a status; the Life examples then use the default browser and CMake explains what to install. Platforms without a registered native engine use the default browser as well. `-DWEBFRONT_AUTODETECT_NATIVE=OFF` turns the detection off. CEF is never detected. -These test and fuzzing variables are shared CMake options: a parent project that sets either to `ON` -also disables WebFront's automatic detection by default. Set `WEBFRONT_AUTODETECT_NATIVE=ON` -before adding WebFront to enable it explicitly. +`ENABLE_TESTING` and `ENABLE_FUZZING` only disable the detection of a top-level WebFront build: a +parent project that defines them for its own purposes does not change the detection of its consumers. Consumers can call `webfront_use_native_window(target)` after linking `WebFront::WebFront`. It applies the native engine's setup and defines `WEBFRONT_NATIVE_WINDOW` on that target when diff --git a/test/cmake/CheckFrontendConsumer.cmake b/test/cmake/CheckFrontendConsumer.cmake index 2e310913..4f8c713d 100644 --- a/test/cmake/CheckFrontendConsumer.cmake +++ b/test/cmake/CheckFrontendConsumer.cmake @@ -15,6 +15,14 @@ foreach(probe ON OFF) if(NOT probe AND NOT "${output}${error}" MATCHES "parent_app_native will open the default browser instead") message(FATAL_ERROR "Consumer fallback must explain the default browser: ${output}${error}") endif() + if(NOT probe) + # parent, sibling and their native helper targets share one configuration: one advice only. + string(REGEX MATCHALL "no native engine will be built" advice "${output}${error}") + list(LENGTH advice advice_count) + if(NOT advice_count EQUAL 1) + message(FATAL_ERROR "Fallback advice must be printed once, got ${advice_count}: ${output}${error}") + endif() + endif() endforeach() endforeach() From e9babe780a1300ddd84cb2d399258fa4d48d67eb Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc Date: Thu, 29 Oct 2026 21:23:35 +0100 Subject: [PATCH 3/3] Share the native window advice between the helper and the examples [#397] webfront_native_window_advice builds the platform guidance once for webfront_use_native_window and the Life examples, which now also tell an explicitly disabled engine from a missing dependency. Fix the grammar of the detection paragraph in FRONTENDS.md. --- cmake/Frontends.cmake | 61 ++++++++++++++++++++++++----------------- docs/FRONTENDS.md | 5 ++-- examples/CMakeLists.txt | 17 +----------- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/cmake/Frontends.cmake b/cmake/Frontends.cmake index ea9d2bb0..36070f2b 100644 --- a/cmake/Frontends.cmake +++ b/cmake/Frontends.cmake @@ -108,6 +108,41 @@ function(webfront_register_frontend name) endforeach() endfunction() +# Sets to what the user can do for a native window when this platform's engine is not enabled. +# Shared by webfront_use_native_window and the examples so that the guidance exists once. +function(webfront_native_window_advice out) + _webfront_load_frontends() + webfront_native_frontend_option(option) + set(detection "") + if(option) + foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) + if(WEBFRONT_FRONTEND_${name}_OPTION STREQUAL option) + get_property(detection GLOBAL PROPERTY WEBFRONT_FRONTEND_${name}_DETECTION) + break() + endif() + endforeach() + endif() + if(NOT option) + set(advice "no native engine is registered for ${CMAKE_SYSTEM_NAME}") + elseif(NOT WEBFRONT_AUTODETECT_NATIVE) + set(advice "enable it with -D${option}=ON (automatic detection is off)") + elseif(detection STREQUAL "explicit") + set(advice "enable it with -D${option}=ON (it was explicitly disabled)") + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + set(advice "install the WebKitGTK 6.0 development package (libwebkitgtk-6.0-dev on Ubuntu/Debian, webkitgtk6.0-devel on Fedora; see docs/WEBKITGTK.md)") + elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(advice "use Clang on macOS 13.3 or later") + else() + set(advice "use an MSVC-compatible compiler") + endif() + if(option) + set(advice "For a native window, ${advice}, then run cmake again.") + else() + set(advice "${advice}; use the default browser on this platform.") + endif() + set(${out} "${advice}" PARENT_SCOPE) +endfunction() + # Opt an application into this platform's native window, with a browser fallback. The application # selects NativeWebViewFrontend when WEBFRONT_NATIVE_WINDOW is defined, DefaultBrowserFrontend otherwise. # Linking WebFront::WebFront remains explicit; CEF is never chosen by this helper. @@ -118,15 +153,8 @@ function(webfront_use_native_window target) endif() webfront_native_frontend_option(option) set(enabled OFF) - set(detection "") if(option) set(enabled "${${option}}") - foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) - if(WEBFRONT_FRONTEND_${name}_OPTION STREQUAL option) - get_property(detection GLOBAL PROPERTY WEBFRONT_FRONTEND_${name}_DETECTION) - break() - endif() - endforeach() endif() if(enabled) foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY) @@ -140,24 +168,7 @@ function(webfront_use_native_window target) endforeach() target_compile_definitions(${target} PRIVATE WEBFRONT_NATIVE_WINDOW) else() - if(NOT option) - set(advice "no native engine is registered for ${CMAKE_SYSTEM_NAME}") - elseif(NOT WEBFRONT_AUTODETECT_NATIVE) - set(advice "enable it with -D${option}=ON (automatic detection is off)") - elseif(detection STREQUAL "explicit") - set(advice "enable it with -D${option}=ON (it was explicitly disabled)") - elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") - set(advice "install the WebKitGTK 6.0 development package (libwebkitgtk-6.0-dev on Ubuntu/Debian, webkitgtk6.0-devel on Fedora; see docs/WEBKITGTK.md)") - elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") - set(advice "use Clang on macOS 13.3 or later") - else() - set(advice "use an MSVC-compatible compiler") - endif() - if(option) - set(advice "For a native window, ${advice}, then run cmake again.") - else() - set(advice "${advice}; use the default browser on this platform.") - endif() + webfront_native_window_advice(advice) # The advice is identical for every target of a configuration: print it once. get_property(noticed GLOBAL PROPERTY WEBFRONT_NATIVE_FALLBACK_NOTICED) if(noticed) diff --git a/docs/FRONTENDS.md b/docs/FRONTENDS.md index 150a0bd8..fe6aa8c1 100644 --- a/docs/FRONTENDS.md +++ b/docs/FRONTENDS.md @@ -141,9 +141,8 @@ do not change the production HTTP/WebSocket transport. Enabling an engine makes its adapter available; select it explicitly in your application. `webfront::NativeApp` chooses the native engine for the current platform. -The options default to `OFF`. Standalone builds without `ENABLE_TESTING` or `ENABLE_FUZZING`, and every add_subdirectory/CPM build, first -probe the native engine of the platform (registered with `NATIVE`: WebKitGTK through `pkg-config webkitgtk-6.0`, WKWebView on Clang with -macOS 13.3+, WebView2 with an MSVC-compatible compiler) and sets `ON` or `OFF` in a normal variable, so `cmake ..` is enough to build the Life +The options default to `OFF`. Configuration first probes the native engine of the platform (registered with `NATIVE`: WebKitGTK through +`pkg-config webkitgtk-6.0`, WKWebView on Clang with macOS 13.3+, WebView2 with an MSVC-compatible compiler) and sets `ON` or `OFF` in a normal variable, so `cmake ..` is enough to build the Life example with a native window. Automatic values do not create engine cache entries and are probed again at the next configuration. An explicit normal or cached option is never replaced, even if it equals the last detected value. Remove the cache entry with `-UWEBFRONT_EMBED_WKWEBVIEW` (or the corresponding platform option) to resume automatic detection. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5e193b62..bdd058f3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -11,22 +11,7 @@ if(WEBFRONT_LIFE_NATIVE_OPTION) endif() endif() if(NOT WEBFRONT_LIFE_NATIVE_WINDOW) - if(NOT WEBFRONT_LIFE_NATIVE_OPTION) - set(_life_install "no native engine is registered for ${CMAKE_SYSTEM_NAME}") - elseif(NOT WEBFRONT_AUTODETECT_NATIVE) - set(_life_install "enable it with -D${WEBFRONT_LIFE_NATIVE_OPTION}=ON (automatic detection is off)") - elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") - set(_life_install "install the WebKitGTK 6.0 development package (libwebkitgtk-6.0-dev on Ubuntu/Debian, webkitgtk6.0-devel on Fedora; see docs/WEBKITGTK.md)") - elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") - set(_life_install "use Clang on macOS 13.3 or later") - else() - set(_life_install "use an MSVC-compatible compiler") - endif() - if(WEBFRONT_LIFE_NATIVE_OPTION) - set(_life_advice "For a native window, ${_life_install}, then run cmake again.") - else() - set(_life_advice "${_life_install}; use the default browser on this platform.") - endif() + webfront_native_window_advice(_life_advice) message(NOTICE "Native window: no native engine will be built.\n" " The Life examples will open the default browser instead (frontend DefaultBrowser).\n"