From 7eafc8b09635c914be1fe8c936e501ed43e58d1f Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc Date: Sun, 25 Oct 2026 19:15:34 +0100 Subject: [PATCH 1/4] Start the Life example without arguments and detect the native window [#366] WebFrontLifeDeveloper uses ./life-project (created when missing, reused untouched) and the pinned environment when run without argument; the explicit [ ] form remains. Configuration enables the native engine of the platform when its dependencies are found (top-level builds with examples and without tests; an explicit WEBFRONT_EMBED_* value always wins, WEBFRONT_AUTODETECT_NATIVE=OFF disables it). Without a native engine the Life examples are built for the default browser and CMake explains what to install. The terminal no longer prints its redundant "browser-powered" banner, the README walkthrough builds with mkdir/cd/cmake/cmake --build, and the bundle checksum is refreshed. --- CMakeLists.txt | 11 +++ README.md | 58 ++++++++------- cmake/Frontends.cmake | 97 +++++++++++++++++++++++-- docs/CHANGELOG.md | 9 ++- docs/FRONTENDS.md | 7 ++ docs/PROJECT_STATUS.md | 2 +- docs/adr/README.md | 2 +- examples/CMakeLists.txt | 30 ++++++++ examples/life/LifeDeveloper.cpp | 18 +++-- examples/life/LifeDeveloperOptions.hpp | 42 +++++++++++ examples/life/LifeEmbedded.cpp | 5 +- examples/life/LifeFrontend.hpp | 27 +++++++ include/webfront/DeveloperApp.hpp | 2 +- js/developer/environment/bundles.json | 2 +- js/developer/environment/runtime.mjs | 1 - test/cmake/CheckFrontendSelection.cmake | 64 +++++++++++++++- test/cmake/CheckNewcomerDocs.cmake | 12 ++- test/unit/LifeTests.cpp | 75 +++++++++++++++++++ 18 files changed, 416 insertions(+), 48 deletions(-) create mode 100644 examples/life/LifeDeveloperOptions.hpp create mode 100644 examples/life/LifeFrontend.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7121eb19..5f3c361d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,17 @@ 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 +# 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) + set(WEBFRONT_AUTODETECT_NATIVE_DEFAULT ON) +else() + set(WEBFRONT_AUTODETECT_NATIVE_DEFAULT OFF) +endif() +option(WEBFRONT_AUTODETECT_NATIVE "Enable the native window engine of this platform when its dependencies are found" ${WEBFRONT_AUTODETECT_NATIVE_DEFAULT}) +if(WEBFRONT_AUTODETECT_NATIVE) + webfront_autodetect_native_frontends() +endif() webfront_declare_frontend_options() # The library requires threads; consumers inherit the flag through the WebFront target. diff --git a/README.md b/README.md index be5c32c5..a48bfb86 100644 --- a/README.md +++ b/README.md @@ -64,20 +64,23 @@ The example starts with a C++ simulation, adds a React interface, then embeds th ```bash git clone https://github.com/ambroise-leclerc/WebFront.git cd WebFront -cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -cmake --build build --parallel 2 +mkdir build +cd build +cmake .. +cmake --build . ``` +No option is needed: the build enables this platform's native window engine when its dependencies are found (see [step 2](#2-add-a-window-and-bootstrap-the-tools)). The commands below start from this `build` directory. With a multi-configuration generator such as Visual Studio, executables are in `examples\Debug\` (`--config Release` selects `examples\Release\`). + ### 1. Start with the C++ simulation [Life.hpp](examples/life/Life.hpp) implements Conway's B3/S23 rules on a wrapping 32 × 32 grid. Each step creates a new `std::bitset<1024>`. [LifeConsole.cpp](examples/life/LifeConsole.cpp) runs it without a GUI: ```bash -# From the repository root: -./build/examples/WebFrontLifeConsole +examples/WebFrontLifeConsole ``` -On Windows the executable is `build\examples\Release\WebFrontLifeConsole.exe`. +On Windows with Visual Studio run `examples\Debug\WebFrontLifeConsole.exe`. ### 2. Add a window and bootstrap the tools @@ -92,24 +95,30 @@ app.show(); The project directory gives the application access to its development files. `bootstrap("developer")` opens the integrated terminal and loads the version-pinned developer environment, checking its SHA-512 integrity. Downloads are cached, so later launches work offline. The terminal runs `wf` commands; no system shell is exposed and nothing is installed on your machine. -Enable **one** engine for your platform: +The build you made above is enough. Start the example from the `build` directory: + +```bash +examples/WebFrontLifeDeveloper +``` + +On Windows with Visual Studio run `examples\Debug\WebFrontLifeDeveloper.exe`. Without argument, the example uses `life-project` in the current directory (`build/life-project`), creates it when it is missing and reuses an existing one without touching its files; every `life-project/...` path below is relative to it. The pinned environment is used. + +**Native window.** At configuration, CMake enables the native engine of your platform when it finds its dependencies, with no option to pass: -| Platform | CMake option | Runtime | +| Platform | Engine | Detected when | | --- | --- | --- | -| Windows | `-DWEBFRONT_EMBED_WEBVIEW2=ON` | Microsoft Edge WebView2 Evergreen | -| macOS 13.3+ | `-DWEBFRONT_EMBED_WKWEBVIEW=ON` | System Cocoa / WebKit | -| Linux | `-DWEBFRONT_EMBED_WEBKITGTK=ON` | WebKitGTK 6.0; [installation](docs/WEBKITGTK.md) | +| Windows | Microsoft Edge WebView2 Evergreen | an MSVC-compatible compiler is used (the SDK is downloaded; the Evergreen runtime is checked at start) | +| macOS 13.3+ | System Cocoa / WebKit | Clang and a deployment target of at least 13.3 | +| Linux | WebKitGTK 6.0 | `pkg-config webkitgtk-6.0` succeeds; [installation](docs/WEBKITGTK.md) | -Then, on macOS, from the repository root, build and start the example on a new project directory: +When the engine is missing, configuration still succeeds. CMake prints a `Native window: no native engine will be built` notice that says what to install, and `WebFrontLifeDeveloper` is compiled for the default browser: at start it prints that notice, serves the same application and opens it in your system browser. Install the dependency and run `cmake ..` again to get the native window. CEF (`-DWEBFRONT_EMBED_CEF=ON`) makes `CEFFrontend` available to your own applications; the examples do not select it. -```bash -cmake -S . -B build-native -DCMAKE_BUILD_TYPE=Release -DWEBFRONT_EMBED_WKWEBVIEW=ON -cmake --build build-native --target WebFrontLifeDeveloper --parallel 2 -mkdir -p life-project -./build-native/examples/WebFrontLifeDeveloper "$PWD/life-project" -``` +**Advanced options.** + +- Choose the engine yourself with `-DWEBFRONT_EMBED_WEBVIEW2=ON|OFF`, `-DWEBFRONT_EMBED_WKWEBVIEW=ON|OFF` or `-DWEBFRONT_EMBED_WEBKITGTK=ON|OFF`; an explicit value is never replaced by the detection. `-DWEBFRONT_AUTODETECT_NATIVE=OFF` disables it. Test builds (`-DENABLE_TESTING=ON`) keep the explicit selection. +- Another project directory, and another environment archive, can be passed: `examples/WebFrontLifeDeveloper [ ]`, for instance an environment [built from the sources](docs/DEVELOPER_ENVIRONMENT.md#environment-distribution-and-offline-tests-maintainers). -Replace the engine option on Linux or Windows. With Visual Studio, add `--config Release` when building and start it with the project directory, `build-native\examples\Release\WebFrontLifeDeveloper.exe life-project` (after `mkdir life-project`). The example reports startup failures with a nonzero exit status. Two extra arguments, ` `, select another environment archive, for instance one [built from the sources](docs/DEVELOPER_ENVIRONMENT.md#environment-distribution-and-offline-tests-maintainers). +The example reports startup failures with a nonzero exit status. ### 3. Create the project and install its dependencies @@ -199,16 +208,15 @@ Declare the entry and the provider in `webfront.project.json` (`"entries": {"mai wf embed ui/main.tsx --output generated/LifeUI.hpp ``` -This builds the interface and packages its resources and dependency notices into a compressed C++ header. Close the development window, then reconfigure with the **same engine option** used above. On macOS: +This builds the interface and packages its resources and dependency notices into a compressed C++ header. Close the development window, then reconfigure the same `build` directory (the engine is kept): ```bash -cmake -S . -B build-native -DWEBFRONT_EMBED_WKWEBVIEW=ON \ - -DWEBFRONT_LIFE_ASSETS="$PWD/life-project/generated/LifeUI.hpp" -cmake --build build-native --target WebFrontLifeEmbedded --parallel 2 -./build-native/examples/WebFrontLifeEmbedded +cmake .. -DWEBFRONT_LIFE_ASSETS="$PWD/life-project/generated/LifeUI.hpp" +cmake --build . --target WebFrontLifeEmbedded +examples/WebFrontLifeEmbedded ``` -For Visual Studio add `--config Release` and launch `build-native\examples\Release\WebFrontLifeEmbedded.exe`. +For Visual Studio launch `examples\Debug\WebFrontLifeEmbedded.exe`. [LifeEmbedded.cpp](examples/life/LifeEmbedded.cpp) replaces the development project and bootstrap with the generated provider: @@ -232,7 +240,7 @@ 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 are explicit choices and remain optional; 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. 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). ## Build and test diff --git a/cmake/Frontends.cmake b/cmake/Frontends.cmake index 742a9f5c..42fd9fa8 100644 --- a/cmake/Frontends.cmake +++ b/cmake/Frontends.cmake @@ -11,7 +11,7 @@ function(webfront_register_frontend name) if(NOT "${name}" MATCHES "^[A-Za-z_][A-Za-z0-9_]*$") message(FATAL_ERROR "Frontend name '${name}' must be a C++ identifier") endif() - cmake_parse_arguments(PARSE_ARGV 1 ARG "XVFB_LINUX;ALWAYS_AVAILABLE" "OPTION;POLICY;DESCRIPTION;SUPPORT;SETUP;TEST_SETUP;TEST_NAME;TEST_TIMEOUT" "PLATFORMS;TEST_PLATFORMS") + cmake_parse_arguments(PARSE_ARGV 1 ARG "XVFB_LINUX;ALWAYS_AVAILABLE;NATIVE" "OPTION;POLICY;DESCRIPTION;SUPPORT;SETUP;TEST_SETUP;TEST_NAME;TEST_TIMEOUT" "PLATFORMS;TEST_PLATFORMS") if(name IN_LIST WEBFRONT_FRONTEND_REGISTRY) message(FATAL_ERROR "Frontend '${name}' is already registered") endif() @@ -70,29 +70,32 @@ function(webfront_register_frontend name) if(ARG_XVFB_LINUX) message(FATAL_ERROR "Frontend '${name}' cannot use XVFB_LINUX without OPTION") endif() + if(ARG_NATIVE) + message(FATAL_ERROR "Frontend '${name}' cannot use NATIVE without OPTION") + endif() endif() list(APPEND WEBFRONT_FRONTEND_REGISTRY "${name}") 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) + foreach(field OPTION POLICY DESCRIPTION SUPPORT SUPPORT_FILE SETUP TEST_SETUP TEST_NAME TEST_TIMEOUT PLATFORMS TEST_PLATFORMS XVFB_LINUX NATIVE) set(WEBFRONT_FRONTEND_${name}_${field} "${ARG_${field}}" PARENT_SCOPE) endforeach() endfunction() webfront_register_frontend(DefaultBrowser ALWAYS_AVAILABLE) webfront_register_frontend(WebView2 - OPTION WEBFRONT_EMBED_WEBVIEW2 POLICY strict + NATIVE OPTION WEBFRONT_EMBED_WEBVIEW2 POLICY strict DESCRIPTION "Download the pinned WebView2 SDK and link its static loader (Windows only)" PLATFORMS Windows SUPPORT WebView2Support TEST_PLATFORMS Windows TEST_NAME WebFrontWebView2Integration TEST_TIMEOUT 90 TEST_SETUP webfront_add_webview2_tests) webfront_register_frontend(WKWebView - OPTION WEBFRONT_EMBED_WKWEBVIEW POLICY strict + NATIVE OPTION WEBFRONT_EMBED_WKWEBVIEW POLICY strict DESCRIPTION "Link the system Cocoa and WebKit frameworks for WKWebViewFrontend (macOS 13.3+ only)" PLATFORMS Darwin SUPPORT WKWebViewSupport TEST_PLATFORMS Darwin TEST_NAME WebFrontWKWebViewIntegration TEST_TIMEOUT 90 TEST_SETUP webfront_add_wkwebview_tests) webfront_register_frontend(WebKitGTK - OPTION WEBFRONT_EMBED_WEBKITGTK POLICY strict + NATIVE OPTION WEBFRONT_EMBED_WEBKITGTK POLICY strict DESCRIPTION "Enable the system WebKitGTK 6.0 embedded frontend (Linux only)" PLATFORMS Linux SUPPORT WebKitGTKSupport TEST_PLATFORMS Linux TEST_NAME WebFrontWebKitGTKIntegration TEST_TIMEOUT 90 XVFB_LINUX @@ -127,6 +130,77 @@ function(webfront_declare_frontend_options) endforeach() endfunction() + +# Probes whether the native engine can be built here. Sets to ON or OFF and to a +# diagnostic for the OFF case. Only the registry's native engines are probed, never CEF. Kept +# separate so that configure-only tests can supply a deterministic answer. +function(webfront_probe_native_frontend name out reason) + set(available OFF) + set(why "") + if(name STREQUAL "WebKitGTK") + find_package(PkgConfig QUIET) + if(PkgConfig_FOUND) + pkg_check_modules(WEBFRONT_AUTODETECT_WEBKITGTK QUIET webkitgtk-6.0) + endif() + if(WEBFRONT_AUTODETECT_WEBKITGTK_FOUND) + set(available ON) + else() + set(why "pkg-config webkitgtk-6.0 was not found; install libwebkitgtk-6.0-dev (Ubuntu/Debian) or webkitgtk6.0-devel (Fedora), see docs/WEBKITGTK.md") + endif() + elseif(name STREQUAL "WKWebView") + if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(why "WKWebViewFrontend requires Clang with Apple Blocks support") + elseif(CMAKE_OSX_DEPLOYMENT_TARGET AND CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS "13.3") + set(why "CMAKE_OSX_DEPLOYMENT_TARGET ${CMAKE_OSX_DEPLOYMENT_TARGET} is older than 13.3") + elseif(NOT CMAKE_OSX_DEPLOYMENT_TARGET AND CMAKE_HOST_SYSTEM_VERSION VERSION_LESS "22.4") + set(why "macOS 13.3 or later is required (Darwin ${CMAKE_HOST_SYSTEM_VERSION})") + else() + set(available ON) + endif() + elseif(name STREQUAL "WebView2") + if(MSVC) + set(available ON) + else() + set(why "WebView2Frontend requires an MSVC-compatible compiler (MSVC or clang-cl)") + endif() + else() + set(why "no probe is defined for ${name}") + endif() + set(${out} "${available}" PARENT_SCOPE) + set(${reason} "${why}" PARENT_SCOPE) +endfunction() + +# Enables, for this platform's native engine, an option the user has not set. The result is stored in +# the cache with a marker recording the automatic value: a later configure probes again as long as the +# value is still the automatic one, while an explicit -D