Skip to content
Closed
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
9 changes: 9 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ build:tsan --linkopt=-fsanitize=thread
build:tsan --strip=never
build:tsan --features=dbg
build:tsan --compilation_mode=dbg

# WebAssembly (WASM) configuration
# Usage: bazel build --config=wasm //...
# Compiles using the hermetic emsdk toolchain downloaded automatically by Bazel.
build:wasm --platforms=@emsdk//:platform_wasm
build:wasm --cpu=wasm
build:wasm --cxxopt=-std=c++20
build:wasm --host_cxxopt=-std=c++20
build:wasm --compilation_mode=opt
7 changes: 7 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ config_setting(
define_values = {"asan": "true"},
)

# WebAssembly (WASM) build configuration
# Usage: bazel build --config=wasm //...
config_setting(
name = "build_wasm",
values = {"cpu": "wasm"},
)
Comment thread
ed2k marked this conversation as resolved.


load("@rules_cc//cc:defs.bzl", "cc_library")

Expand Down
11 changes: 9 additions & 2 deletions CPPVARIABLES.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ DDS_CPPOPTS = select({
"-fPIC",
"-Wpedantic",
"-Wall",
"-Wno-character-conversion",
"-Werror",
],
"//:debug_build_macos": [
Expand All @@ -16,7 +15,6 @@ DDS_CPPOPTS = select({
"-fPIC",
"-Wpedantic",
"-Wall",
"-Wno-character-conversion",
"-Werror",
],
"//:build_linux": [
Expand Down Expand Up @@ -51,6 +49,14 @@ DDS_CPPOPTS = select({
"/WX",
"/permissive-",
],
"//:build_wasm": [
"-O3",
"-flto",
"-Wpedantic",
"-Wall",
"-Werror",
"-fexceptions",
],
"//conditions:default": [
"-std=c++20"
],
Expand All @@ -64,6 +70,7 @@ DDS_LOCAL_DEFINES = select({
"//:debug_build_macos": [],
"//:build_linux": [],
"//:debug_build_linux": [],
"//:build_wasm": ["__WASM__"],
"//conditions:default": [],
}) + select({
"//:debug_all": ["DDS_DEBUG_ALL"],
Expand Down
4 changes: 3 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bazel_dep(name = "googletest", version = "1.17.0.bcr.2")
bazel_dep(name = "pybind11_bazel", version = "3.0.1")
bazel_dep(name = "rules_python", version = "2.0.1")
bazel_dep(name = "toolchains_llvm", version = "1.7.0")
bazel_dep(name = "emsdk", version = "5.0.7")

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm.toolchain(
Expand All @@ -29,4 +30,5 @@ python.toolchain(
)
use_repo(python, "python_3_14")

register_toolchains("@llvm_toolchain//:all")
register_toolchains("@llvm_toolchain//:all")
register_toolchains("@emsdk//:all")
228 changes: 228 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

136 changes: 136 additions & 0 deletions docs/wasm_build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# WebAssembly (WASM) Build Guide

This document explains how to build the DDS library and examples for WebAssembly using Bazel.

## Prerequisites

### Bazel

Bazel 7.x or later is required. Install using your package manager or download from:
https://bazel.build/install

The Emscripten SDK (emsdk) does NOT need to be manually installed. Bazel will automatically download, configure, and cache the appropriate hermetic Emscripten toolchain for your host platform as part of the build process.

## Building WASM Examples

### Build All Examples

```bash
cd /workspaces/dds
bazel build //examples:all_examples_wasm
```

### Build Specific Example

```bash
bazel build //examples:solve_board_wasm
```

Comment thread
ed2k marked this conversation as resolved.
### Output Files

the output files will be located in:
```
bazel-bin/examples/
```

Depending on the target, you'll get:
- `target.js` - JavaScript bindings
- `target.wasm` - WebAssembly binary


## Available WASM Targets

The following example targets are available for WASM builds:

### Implemented Examples
- `solve_board` - Solves a single board (produces .js and .wasm)

- `analyse_play_bin` - Analyze play from binary format


## WASM Build Configuration

The WASM build is configured through:

1. **BUILD.bazel** - Defines the WASM config_setting:
```python
config_setting(
name = "build_wasm",
values = {"cpu": "wasm"},
)
```

2. **CPPVARIABLES.bzl** - Specifies WASM-specific compiler flags:
- Optimization: `-O3 -flto`
- Exceptions: `-fexceptions`
- Define: `-D__WASM__`

3. **.bazelrc** - Contains the `wasm` profile:
```
build:wasm --platforms=@emsdk//:platform_wasm
build:wasm --cpu=wasm
build:wasm --cxxopt=-std=c++20
build:wasm --host_cxxopt=-std=c++20
build:wasm --compilation_mode=opt
```

Comment thread
ed2k marked this conversation as resolved.
## Running WASM Examples

After building, you can run the examples:

### Node.js (requires Node.js installed)

```bash
node bazel-bin/examples/solve_board.js
```

### Web Browser (TODO)

For HTML targets, open the generated HTML file in a web browser:
```bash
# Copy the output files to a web-accessible location
cp bazel-bin/examples/solve_board.* /path/to/webserver/

# Then open in browser at http://localhost:8000/solve_board.html
```

## Compilation Flags

The WASM build uses the following key flags:

| Flag | Purpose |
|------|---------|
| `-O3` | Aggressive optimization |
| `-flto` | Link-time optimization |
| `-fexceptions` | Enable C++ exceptions |
| `-D__WASM__` | Defines `__WASM__` preprocessor constant |
| `-sWASM=1` | Emscripten WASM output (link flag) |
| `-sALLOW_MEMORY_GROWTH=1` | Allow heap growth at runtime |
| `-sINITIAL_MEMORY=268435456` | Configure 256MB initial memory |

## C++ Standard

The WASM build uses C++20 as specified in `.bazelrc`:
```
build:wasm --cxxopt=-std=c++20
```

## Related Documentation

- [Emscripten Documentation](https://emscripten.org/docs/)
- [Bazel Build System](https://bazel.build/docs)
- [DDS C++ API](c++_interface.md)
- [Build System Overview](BUILD_SYSTEM.md)

## Next Steps

To integrate WASM builds into CI/CD:
1. See `.github/workflows/ci_linux.yml` and `.github/workflows/ci_macos.yml`
2. Store WASM artifacts for download/release

## Development Notes

- The `__WASM__` preprocessor constant is added initially to bypass error, need to check again whether we can remove
- Some threading and platform-specific features are disabled for WASM
- The build flow for a reusable library wasm
- A good HTML example
Loading