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
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
75 changes: 48 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,52 +64,69 @@ 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

The essential change in [LifeDeveloper.cpp](examples/life/LifeDeveloper.cpp) is:

```cpp
webfront::NativeApp app{"life-project"};
#include "LifeFrontend.hpp"

webfront::demo::LifeApp app{"life-project"};
app.cppFunction("life.step", &webfront::demo::nextLife);
app.bootstrap("developer");
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.

| Platform | CMake option | Runtime |
**Native window.** At configuration, CMake enables the native engine of your platform when it finds its dependencies, with no option to pass:

| 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:
On macOS, this opens a WKWebView window using the system Cocoa and WebKit frameworks, with no engine download.
The example constructs the application and calls `show()` on the main thread; the HTTP/WebSocket server runs on its own thread.
Closing the window stops the server and exits the example. An explicit deployment target below 13.3 disables automatic detection;
explicitly enabling WKWebView with such a target fails configuration. See [the macOS guide](docs/WKWEBVIEW.md).

```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"
```
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.

**Advanced options.**

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, `<environment-url> <sha512-integrity>`, select another environment archive, for instance one [built from the sources](docs/DEVELOPER_ENVIRONMENT.md#environment-distribution-and-offline-tests-maintainers).
- 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.
- Automatic engine choices are recalculated on each configuration and do not create an engine option in the cache. To return from an explicit macOS choice to automatic detection, run `cmake .. -UWEBFRONT_EMBED_WKWEBVIEW` (or remove the corresponding engine option on another platform).
- Another project directory, and another environment archive, can be passed: `examples/WebFrontLifeDeveloper <project> [<environment-url> <sha512-integrity>]`, for instance an environment [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

Expand Down Expand Up @@ -199,27 +216,31 @@ 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:

```cpp
webfront::NativeAppWithAssets<webfront::fs::LifeUI> app;
#include "LifeFrontend.hpp"

webfront::demo::LifeAppWithAssets<webfront::fs::LifeUI> app;
app.cppFunction("life.step", &webfront::demo::nextLife);
app.show();
```

Recompilation is explicit. The resulting application runs offline without the project directory, developer callbacks or downloaded tool cache. The selected native engine's deployment requirements still apply; this is not an application installer.

The examples' [LifeFrontend.hpp](examples/life/LifeFrontend.hpp) selects the native engine or the default browser for both stages.
Your own application can select `webfront::NativeApp` or `webfront::NativeAppWithAssets` explicitly when it requires a native window.

## Use WebFront in your project

WebFront's implementation lives under `include/`. There is no WebFront library binary to build; CMake supplies its C++ dependencies and platform settings:
Expand All @@ -232,7 +253,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

Expand Down
6 changes: 6 additions & 0 deletions cmake/CompilerWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ function(set_project_warnings project_name)
-Wlogical-op # warn about logical operations being used where bitwise were probably wanted
)

# GCC 16 at -O2/-O3 reports a stringop-overflow inside stdexec's task_scheduler (std::optional of a type-erased
# scheduler) for every coroutine awaiting continues_on. The code is not ours: keep the warning, never fail on it.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND GCC_WARNINGS -Wno-error=stringop-overflow)
endif()

# Vérifier si -Wdangling-reference est supporté (GCC 13+ ou certaines versions de GCC 12)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-Wdangling-reference" COMPILER_SUPPORTS_DANGLING_REF)
Expand Down
93 changes: 88 additions & 5 deletions cmake/Frontends.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -127,6 +130,73 @@ function(webfront_declare_frontend_options)
endforeach()
endfunction()


# Probes whether the native engine <name> can be built here. Sets <out> to ON or OFF and <reason> 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. Automatic values stay
# in normal variables: option() honors them (CMP0077), without creating a cache entry. A later
# 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)
foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY)
set(option "${WEBFRONT_FRONTEND_${name}_OPTION}")
if(option STREQUAL "" OR NOT WEBFRONT_FRONTEND_${name}_NATIVE)
continue()
endif()
webfront_frontend_supports_platform("${name}" supported)
if(NOT supported)
continue()
endif()
if(DEFINED ${option})
continue()
endif()
webfront_probe_native_frontend("${name}" available reason)
set(${option} "${available}" PARENT_SCOPE)
if(available)
message(STATUS "Native window: ${name} detected, ${option}=ON")
else()
message(STATUS "Native window: ${name} unavailable (${reason}), so applications using NativeApp will report it when started. Set -D${option}=ON once it is installed")
endif()
endforeach()
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)
Expand Down Expand Up @@ -323,3 +393,16 @@ function(webfront_select_frontend var default description)
endforeach()
set(${var}_TYPE "webfront::frontend::${selected}Frontend" PARENT_SCOPE)
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)
set(result "")
foreach(name IN LISTS WEBFRONT_FRONTEND_REGISTRY)
webfront_frontend_supports_platform("${name}" supported)
if(WEBFRONT_FRONTEND_${name}_NATIVE AND supported)
set(result "${WEBFRONT_FRONTEND_${name}_OPTION}")
break()
endif()
endforeach()
set(${out} "${result}" PARENT_SCOPE)
endfunction()
Loading
Loading