Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ 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)
# 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)
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <webfront/WebFront.hpp>

#if defined(WEBFRONT_NATIVE_WINDOW)
using App = webfront::WebFrontWithFrontend<webfront::frontend::NativeWebViewFrontend>;
#else
using App = webfront::WebFront;
#endif

int main() {
App app;
app.show();
}
```

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_<ENGINE>=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

Expand Down
130 changes: 127 additions & 3 deletions cmake/Frontends.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
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)
# 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
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)
unset(_wf_published)
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()
Expand Down Expand Up @@ -75,12 +100,88 @@ 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()

# Sets <out> 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.
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)
if(option)
set(enabled "${${option}}")
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()
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)
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()

webfront_register_frontend(DefaultBrowser ALWAYS_AVAILABLE)
webfront_register_frontend(WebView2
NATIVE OPTION WEBFRONT_EMBED_WEBVIEW2 POLICY strict
Expand Down Expand Up @@ -108,6 +209,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)
Expand All @@ -116,12 +218,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)
Expand Down Expand Up @@ -175,6 +279,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)
Expand All @@ -185,9 +290,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")
Expand All @@ -200,6 +307,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}")
Expand All @@ -210,6 +318,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 "")
Expand All @@ -219,11 +328,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 "")
Expand Down Expand Up @@ -281,6 +400,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}'")
Expand Down Expand Up @@ -339,6 +459,7 @@ endfunction()

# Sets <out> 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)
Expand All @@ -356,6 +477,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}")
Expand All @@ -375,6 +497,7 @@ endfunction()
# with REQUIRE_AVAILABLE rejects a "strict" frontend whose availability option is OFF. Sets <var>_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)
Expand Down Expand Up @@ -427,6 +550,7 @@ endfunction()

# Sets <out> 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)
Expand Down
8 changes: 8 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## 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.
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 (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
labels such as `app:debug` and `app..debug` in native build/preview requests.
Expand Down
16 changes: 13 additions & 3 deletions docs/FRONTENDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,25 @@ 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
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.
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.
`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
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 |
| --- | --- | --- |
Expand Down
Loading
Loading