Skip to content

Latest commit

 

History

History
308 lines (248 loc) · 18.2 KB

README.md

File metadata and controls

308 lines (248 loc) · 18.2 KB

webcpp

webcpp brings to C++ the libraries that other languages use for the web. Each is a header-only C++20 library, ported with the help of coding agents from a widely used original at an exact version, and proven against that original; or a library of webcpp's own. webcpp follows Boost's guidelines and model: this repository is the superproject, the equivalent of boostorg/boost, each library has a repository of its own under webcpporg, checked out as a submodule under libs/<name>, and everything is built, tested and documented with b2.

webcpp builds infrastructure for every developer, in both directions: the best of C++ within easy reach of the web, compiled to WebAssembly, and the libraries web developers use most brought to C++. It is for C++, crypto and AI developers who want to build decentralized applications for the web. Each target has its purpose: the browser, through Emscripten; WASI components, run by wasmtime and by clouds as containers lighter than Docker; and native programs, for close integration with an operating system.

The libraries

Library What it is Targets
asio Boost.Asio and Cobalt with browser event-loop suspension, WASIp3 networking and optional source-built TLS. native, emscripten, wasip3
beast Beast-compatible HTTP and WebSocket applications, with native/WASIp3 sockets, browser WebSocket and fetch, WASI host HTTP and supplied streams. Its guide records compatibility and results for all 53 upstream applications. native, emscripten, wasip2, wasip3, by backend
crypto Authenticated encryption, randomness and secp256k1 for C++20 on every target, over BearSSL, OpenSSL, ctaes and libsecp256k1. native, wasip2, wasip3, emscripten
xactor A header-only, deterministic actor system: actors that handle one message at a time, with fuel per execution, timers and a lifecycle. native, wasip2, wasip3
xstate A header-only port of XState's state machines and actors: machines read from XState's JSON config, stepped by XState's pure functions or run as actors on xactor. Ports XState 5.33.2, and is proven against it. native, wasip2, wasip3
pratt A header-only Pratt parser engine, generic through concepts, with a calculator built on it, exact in decimal by default; a library of webcpp's own. native, emscripten, wasip2, wasip3
wasi Header-only helpers for C++ built as WebAssembly components: the HTTP handler a component exports on wasip2 and wasip3, and the macros a program writes its main between; a library of webcpp's own. wasip2, wasip3 (its response also natively)
document A header-only JSON document that behaves as JavaScript does: the same key order, JSON.stringify's bytes and the array methods, exact in decimal, in one relocatable image the same on every target; proven against Node, a library of webcpp's own. native, wasip2, wasip3, emscripten
idna A header-only library of internationalized domain names: Punycode, UTS 46's ToASCII and ToUnicode, conformant to IdnaTestV2, and IDNA2008's validity rules, over Unicode 17.0.0 tables generated into its headers; a library of webcpp's own. native, wasip2, wasip3, emscripten
lexical Header-only validators of the string formats JSON Schema names (dates and times, e-mail addresses, host names, IP addresses, URIs, IRIs, URI templates, JSON Pointers, UUIDs, regular expressions), each allocating nothing and each a Boost.URL grammar rule; its regex check is an ECMA-262 syntax validator of its own, checked against V8; a library of webcpp's own. native, wasip2, wasip3, emscripten
trystero Serverless WebRTC rooms, wire compatible with Trystero: peers meet over public Nostr relays, natively over libdatachannel and in the browser over its own WebRTC. Ports Trystero 0.26.0, and is proven against it. native, emscripten

Each library's page, with its API reference, and the test matrix of every library on every target are published at https://webcpporg.github.io/.

Prerequisites

  • Boost 1.92 or newer, installed, and for a library that links Boost.URL, Boost.Charconv, Boost.Locale, Boost.Cobalt or Boost.Context its sources, which bash tools/ci/actions/boost/install.sh source .local/boost-source fetches;
  • b2;
  • a C++20 compiler;
  • Python 3.9 or newer, which compares the examples with their expected output and runs the tests of the build itself;
  • for WebAssembly, wasi-sdk 34, whose clang builds for wasm32-wasip2 and wasm32-wasip3, with the release's platform-independent sysroot (below), and wasmtime 50.0.1, which runs what it builds;
  • for emscripten, Emscripten 6.0.11 from emsdk, and Node, which runs what it builds;
  • for trystero's native backend, CMake, which builds libdatachannel 0.24.6 and OpenSSL 3 from pinned sources (below);
  • for WebAssembly components, such as wasi's, wit-bindgen 0.62.0, which generates the C bindings of a component's world, and the wasi:http WIT of WASI 0.2.12 and 0.3.0, against which a world resolves its packages; and wasmtime, which serves the components;
  • for the documentation, Node, which runs Asciidoctor.js, MrDocs 2026.9.29, which writes each library's API reference, and clang++; wit-bindgen and the WIT, since wasi's reference reads its bindings; and emsdk's headers, since trystero's reference reads its browser backend;
  • for a library's oracle lane, which runs the original it ports against the same cases and examples, Node and npm;
  • for Beast's browser contracts, Node 26 and full Chrome for Testing with a disposable CA profile; its WASI HTTPS host fixture builds with Rust 1.97.1 and the checked-in Cargo lockfile, as its verification guide describes;
  • for the lint, wasi-sdk 34, whose clang-format and clang-tidy it runs, the wasip2, wasip3 and emscripten toolsets below, wit-bindgen and the WIT, since it reads what those toolsets compile, and Node, with which it installs Pyright.

b2 refuses to run while CPATH, CPLUS_INCLUDE_PATH or C_INCLUDE_PATH is set, since the compiler would read another Boost from them before the configured one. Unset them in the shell you build from:

unset CPATH CPLUS_INCLUDE_PATH C_INCLUDE_PATH

Getting started

Clone the superproject with its libraries, and test them:

git clone --recursive https://github.com/webcpporg/webcpp
cd webcpp
b2 test

trystero needs no library installed on the machine: libsecp256k1 is webcpp's own build of it, /third_party//secp256k1 ("trystero's libraries", below), and its native backend builds libdatachannel and OpenSSL from pinned sources. webcpp-require-external=on on b2's command line, which the CI gives, stops the build when a library that is not built from third_party/ is missing; no library of libs/ has one today.

When Boost is not on the compiler's default include path, as with Homebrew's on macOS, the build stops at once and says what to configure:

error: Boost 1.92 or newer was not found: no `using boost` names its location, and none is on the compiler's default include path
error: Boost 1.92.0 is installed in /opt/homebrew/opt/boost: configure it in user-config.jam with this line, in place of any other `using boost`:
error:     using boost : 1.92 : <include>/opt/homebrew/opt/boost/include <library>/opt/homebrew/opt/boost/lib ;

b2 reads user-config.jam from your home directory. Write that line there, after the toolset of your compiler (using clang ;, using gcc ; or using msvc ;), so that ~/user-config.jam reads, for Homebrew's Boost and clang:

using clang ;
using boost : 1.92 : <include>/opt/homebrew/opt/boost/include <library>/opt/homebrew/opt/boost/lib ;

and run b2 test again. It checks the Boost it found, which it reports on a line of its configuration checks,

- Boost 1.92 or newer in /opt/homebrew/opt/boost/include (1.92.0) : yes [1]

then builds and runs every library's tests.

The other aggregates work the same way:

Command What it does
b2 test builds and runs every library's tests
b2 example builds and runs every library's examples, and compares each one's output with the .expected file beside it
b2 doc builds the index page, doc/html/index.html, and each library's page, libs/<name>/doc/html/index.html; a library left out, as trystero without its libraries, is named on the index without a link, and has no page
b2 install --prefix=<dir> copies every library's headers to <dir>/include/webcpp/, for CMake or a plain compiler, and its licence files beside them, in <dir>/include/webcpp/<name>/
b2 libs/<name>/test one library's tests; libs/<name>/example and libs/<name>/doc likewise
b2 libs/<name>/test/oracle//oracle a port's oracle lane: the original it ports runs the same cases and a twin of each example, and the results must be the port's
b2 -a ... the same, from scratch: b2 compares timestamps, so only a build from scratch is a result

A b2 project uses a library through /webcpp/<name>//<name>, which adds its headers and Boost's.

The installation also includes include/webcpp_ca_certificates.hpp and the matching PEM source, manifest and MPL notices under include/webcpp/ca_certificates/. This keeps the embedded trust data usable by an installed TLS client header. External build systems must still configure Boost and link matching OpenSSL libraries; installation does not supply their compiled archives or the Cobalt runner.

Asio's optional TLS target builds pinned OpenSSL sources through /webcpp/third_party//openssl, for native and WASIp3 variants. It works from a Git checkout or a WebCPP source distribution: the builder verifies the distributable source digest before invoking upstream Configure and Make. Updating that pin is explicit; maintainers regenerate its integrity metadata with python3 tools/openssl/update.py after changing the source commit.

WebAssembly

Each library declares the targets it is built for. To build them for wasm32-wasip2 and wasm32-wasip3, install wasi-sdk 34 in .local/wasi-sdk from the superproject's root, as the CI does:

bash tools/ci/actions/wasi-sdk/install.sh

It installs the build of wasi-sdk for your system and processor, with the release's platform-independent sysroot and compiler-rt builtins in place of that build's own, each archive checked against its SHA-256. A per-host archive's sysroot carries the paths of the machine that built it in its libraries, so the same WASI program would differ from one host to another; with the shared sysroot it is the same bytes on every host. Then register two clang toolsets against it in ~/user-config.jam:

local wasi-sdk = /path/to/wasi-sdk ;
modules.poke : WASI_SDK : $(wasi-sdk) ;
using clang : wasip2 : $(wasi-sdk)/bin/clang++
  : <cflags>--target=wasm32-wasip2 <cxxflags>--target=wasm32-wasip2
    <linkflags>--target=wasm32-wasip2
    <archiver>$(wasi-sdk)/bin/llvm-ar <ranlib>$(wasi-sdk)/bin/llvm-ranlib ;
using clang : wasip3 : $(wasi-sdk)/bin/clang++
  : <cflags>--target=wasm32-wasip3 <cxxflags>--target=wasm32-wasip3
    <linkflags>--target=wasm32-wasip3
    <archiver>$(wasi-sdk)/bin/llvm-ar <ranlib>$(wasi-sdk)/bin/llvm-ranlib ;

and run the tests and the examples with wasmtime, one toolset per command:

b2 toolset=clang-wasip2 testing.launcher=wasmtime test example
b2 toolset=clang-wasip3 testing.launcher=tools/boost/asio-wasip3/run.sh test example

Every target builds with exceptions: whether a program uses them is the choice of whoever builds it, every library supports a build without them, and webcpp builds none without RTTI. Each library has its own WEBCPP_<NAME>_NO_EXCEPTIONS, in <webcpp/<name>/config.hpp>, as Boost.Asio has BOOST_ASIO_NO_EXCEPTIONS: defined in a build without exceptions, and definable by a program to turn a library's exceptions off in a build that has them. A library raises an exception through boost::throw_exception alone. A program that a library does not declare for a target is skipped there. b2 declared-targets -d0 lists the targets each library declares.

webcpp has one version, WEBCPP_VERSION in <webcpp/version.hpp>, part of libs/config. Each library's page ends with its changelog, and the index page shows each version's release notes. Each library moves onto <webcpp/config.hpp> once webcpporg/config exists (the roadmap, CODING-GUIDELINES.md chapter 16).

A library may also run some of its programs in lanes of its own, which test and example leave out: the HTTP components wasmtime serves, for one. b2 declared-lanes -d0 lists them, each with the target it runs on and its kind, original when it runs the original's language, which needs Node, else programs, and one runs as

b2 toolset=clang-wasip2 testing.launcher=wasmtime libs/<name>/test//<lane>

A component's bindings are generated by wit-bindgen when a WASI toolset builds it, or when a native build names the explicit target <bindings>-headers, as wasi's API reference does to parse them; so b2 test with the host's toolset never needs wit-bindgen or the WIT, and b2 doc does. b2 finds wit-bindgen where -sWIT_BINDGEN=<path> says, else at .local/wit-bindgen/wit-bindgen, else on PATH; the WIT of each version where -sWASI_WIT_P2=<dir> and -sWASI_WIT_P3=<dir> say, each the wit/deps directory of the Rust crate wasip2 1.0.4 or wasip3 0.9.0, else in .local/wasi-wit/p2 and .local/wasi-wit/p3; and the wasmtime that serves a component where -sWASMTIME=<path> says, else on PATH. When one is needed and not there, the build stops, naming it and every place it looked.

Emscripten

A library that declares emscripten, as trystero does, is built for it with b2's emscripten toolset, registered against Emscripten 6.0.11 from emsdk in ~/user-config.jam, where /path/to/emsdk is the emsdk's directory and /path/to/node the node that runs a program:

local emsdk = /path/to/emsdk ;
local node = /path/to/node ;
using emscripten : : $(emsdk)/upstream/emscripten/em++ : <nodejs>$(node) ;

It builds wasm32, single-threaded and static, and runs each test and example with node itself, so it takes no testing.launcher:

b2 toolset=emscripten libs/trystero/test libs/trystero/example

A program for a browser, which node cannot run, is linked and never run.

trystero's libraries

trystero links three libraries that webcpp builds from pinned sources, each in the build directory of the program that uses it: libsecp256k1 0.8.0 as /third_party//secp256k1 (its tables, small on emscripten and large natively, are chosen by the feature webcpp-secp256k1-tables: auto, small or large), and, for its native backend, libdatachannel 0.24.6 and OpenSSL 3. With the pinned submodules initialised, nothing is installed in .local/ or found on the system, and no -s option names them. Its page says how each is built.

Documentation

b2 doc

builds the index page and each library's page, with its API reference from MrDocs. MrDocs is found at .local/mrdocs/bin/mrdocs, on PATH, or where -sMRDOCS=<path> says, and the build stops, naming where it looked, when it is in none of them; clang++ is found on PATH, or where -sCLANG=<path> says. The Node packages are installed by npm ci on first use, once per package-lock.json, under tools/doc/.node-modules/, and linked at tools/doc/node_modules, so that doc builds at once share them.

The build is strict: a public symbol without a Doc Comment, a template parameter without @tparam, a detail symbol without a brief, and a Doc Comment's @see "<title>" that names no section of its library's page each fail it, naming the symbol and the file.

Linting

tools/lint/lint.sh --clang-format <wasi-sdk>/bin/clang-format --clang-tidy <wasi-sdk>/bin/clang-tidy

lints the superproject and every library under libs/, and names each rule that fails. --shard K/N analyses one of N slices of the files with clang-tidy, which takes most of the time, and runs every other rule.

The tests of the tools

The build and its tools have tests of their own, each a Python script run from the root, such as python3 tools/test/jamroot_test.py: tools/test/*_test.py for the Jamroot, the compiled Boost libraries, tools/webcpp.jam, third-party code, the oracle's rules, the check of generated files, the rules of a WebAssembly component and the documentation build, and tools/<tool>/*_test.py for the oracle, the lint, the report, the documentation, the example runner, the server of a component, the driver of generated files, the installer of the Node packages and the CI's scripts.

Contributing

Every library is developed inside a checkout of the superproject, as a Boost library is developed inside Boost. CONTRIBUTING.md is the way in: what webcpp asks of a library, how to propose one, and how a change is made and accepted. CODING-GUIDELINES.md holds every rule a contributor, human or agent, needs to port a library or change one, and every decision of webcpp's design with its reason: the names, the C++ rules, the tests, the evidence a port gives and how a change is made. AGENTS.md says how to work in the tree: the layout, the build, the lanes, the documentation and the releases.

Related projects

Crossbind is an independent project with a complementary goal: making C++ and Rust libraries accessible through WebAssembly and other runtimes. Its source-based build recipes and WASI ports are useful references when investigating how to bring low-level libraries to the web. In particular, its OpenSSL recipe demonstrates a WASIp3 build and records its target configuration. Consult those recipes as engineering references; validate the toolchain, entropy source and supported features for the WebCPP configuration being built.

License

Distributed under the Boost Software License, Version 1.0. Third-party code under third_party/libs/ keeps its own licence: the JSON Schema Test Suite is MIT.