Update examples collection

This commit is contained in:
github-actions[bot] 2025-08-20 09:41:41 +00:00
commit e0920a1d65
87 changed files with 21612 additions and 0 deletions

View file

@ -0,0 +1 @@
build --incompatible_enable_cc_toolchain_resolution

View file

@ -0,0 +1 @@
/bazel-*

View file

@ -0,0 +1,19 @@
package(default_visibility = ["//visibility:public"])
filegroup(name = "empty")
platform(
name = "platform_wasm",
constraint_values = [
"@platforms//cpu:wasm32",
"@platforms//os:emscripten",
],
)
platform(
name = "platform_wasi",
constraint_values = [
"@platforms//cpu:wasm32",
"@platforms//os:wasi",
],
)

View file

@ -0,0 +1,79 @@
module(
name = "emsdk",
version = "4.0.13",
)
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "aspect_rules_js", version = "1.42.0")
bazel_dep(name = "rules_nodejs", version = "6.3.2")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "rules_python", version = "1.3.0")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
python_version = "3.13",
)
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
node.toolchain(node_version = "20.18.0")
use_repo(node, "nodejs")
emscripten_deps = use_extension(
"//:emscripten_deps.bzl",
"emscripten_deps",
)
use_repo(emscripten_deps, "emscripten_bin_linux")
use_repo(emscripten_deps, "emscripten_bin_linux_arm64")
use_repo(emscripten_deps, "emscripten_bin_mac")
use_repo(emscripten_deps, "emscripten_bin_mac_arm64")
use_repo(emscripten_deps, "emscripten_bin_win")
npm = use_extension(
"@aspect_rules_js//npm:extensions.bzl",
"npm",
)
npm.npm_translate_lock(
name = "emscripten_npm_linux",
data = ["@emscripten_bin_linux//:emscripten/package.json"],
npm_package_lock = "@emscripten_bin_linux//:emscripten/package-lock.json",
)
npm.npm_translate_lock(
name = "emscripten_npm_linux_arm64",
data = ["@emscripten_bin_linux_arm64//:emscripten/package.json"],
npm_package_lock = "@emscripten_bin_linux_arm64//:emscripten/package-lock.json",
)
npm.npm_translate_lock(
name = "emscripten_npm_mac",
data = ["@emscripten_bin_mac//:emscripten/package.json"],
npm_package_lock = "@emscripten_bin_mac//:emscripten/package-lock.json",
)
npm.npm_translate_lock(
name = "emscripten_npm_mac_arm64",
data = ["@emscripten_bin_mac_arm64//:emscripten/package.json"],
npm_package_lock = "@emscripten_bin_mac_arm64//:emscripten/package-lock.json",
)
npm.npm_translate_lock(
name = "emscripten_npm_win",
data = ["@emscripten_bin_win//:emscripten/package.json"],
npm_package_lock = "@emscripten_bin_win//:emscripten/package-lock.json",
)
use_repo(
npm,
"emscripten_npm_linux",
"emscripten_npm_linux_arm64",
"emscripten_npm_mac",
"emscripten_npm_mac_arm64",
"emscripten_npm_win",
)
emscripten_cache = use_extension("//:emscripten_cache.bzl", "emscripten_cache")
use_repo(emscripten_cache, "emscripten_cache")
register_toolchains(
"//emscripten_toolchain:cc-toolchain-wasm-emscripten_linux",
"//emscripten_toolchain:cc-toolchain-wasm-emscripten_linux_arm64",
"//emscripten_toolchain:cc-toolchain-wasm-emscripten_mac",
"//emscripten_toolchain:cc-toolchain-wasm-emscripten_mac_arm64",
"//emscripten_toolchain:cc-toolchain-wasm-emscripten_win",
)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,90 @@
# Bazel Emscripten toolchain
## Setup Instructions
Support for depending on emsdk with a WORKSPACE file was removed and last available in [emsdk version 4.0.6](https://github.com/emscripten-core/emsdk/tree/24fc909c0da13ef641d5ae75e89b5a97f25e37aa). Now we only support inclusion as a bzlmod module.
In your `MODULE.bazel` file, put:
```starlark
emsdk_version = "4.0.6"
bazel_dep(name = "emsdk", version = emsdk_version)
git_override(
module_name = "emsdk",
remote = "https://github.com/emscripten-core/emsdk.git",
strip_prefix = "bazel",
tag = emsdk_version,
)
```
You can use a different version of this SDK by changing it in your `MODULE.bazel` file. The Emscripten version is by default the same as the SDK version, but you can use a different one as well by adding to your `MODULE.bazel`:
```
emscripten_deps = use_extension(
"@emsdk//:emscripten_deps.bzl",
"emscripten_deps",
)
emscripten_deps.config(version = "4.0.1")
```
## Building
Put the following line into your `.bazelrc`:
```
build --incompatible_enable_cc_toolchain_resolution
```
Then write a new rule wrapping your `cc_binary`.
```starlark
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
)
wasm_cc_binary(
name = "hello-world-wasm",
cc_target = ":hello-world",
)
```
Now you can run `bazel build :hello-world-wasm`. The result of this build will
be the individual files produced by emscripten. Note that some of these files
may be empty. This is because bazel has no concept of optional outputs for
rules.
`wasm_cc_binary` uses transition to use emscripten toolchain on `cc_target`
and all of its dependencies, and does not require amending `.bazelrc`. This
is the preferred way, since it also unpacks the resulting tarball.
The Emscripten cache shipped by default does not include LTO, 64-bit or PIC
builds of the system libraries and ports. If you wish to use these features you
will need to declare the cache in your `MODULE.bazel` as follows. Note
that the configuration consists of the same flags that can be passed to
embuilder. If `targets` is not set, all system libraries and ports will be
built, i.e., the `ALL` option to embuilder.
```starlark
emscripten_cache = use_extension(
"@emsdk//:emscripten_cache.bzl",
"emscripten_cache",
)
emscripten_cache.configuration(flags = ["--lto"])
emscripten_cache.targets(targets = [
"crtbegin",
"libprintf_long_double-debug",
"libstubs-debug",
"libnoexit",
"libc-debug",
"libdlmalloc",
"libcompiler_rt",
"libc++-noexcept",
"libc++abi-debug-noexcept",
"libsockets"
])
```
See `test_external/` for an example using [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html).

View file

@ -0,0 +1,2 @@
build:wasm --incompatible_enable_cc_toolchain_resolution
build:wasm --platforms=@emsdk//:platform_wasm

View file

@ -0,0 +1,93 @@
"""A templated build file for emscripten repositories"""
EMSCRIPTEN_BUILD_FILE_CONTENT_TEMPLATE = """
package(default_visibility = ['//visibility:public'])
filegroup(
name = "all",
srcs = glob(["**"]),
)
filegroup(
name = "includes",
srcs = glob([
"emscripten/cache/sysroot/include/c++/v1/**",
"emscripten/cache/sysroot/include/compat/**",
"emscripten/cache/sysroot/include/**",
"lib/clang/**/include/**",
]),
)
filegroup(
name = "emcc_common",
srcs = [
"emscripten/emcc.py",
"emscripten/embuilder.py",
"emscripten/emscripten-version.txt",
"emscripten/cache/sysroot_install.stamp",
"emscripten/src/settings.js",
"emscripten/src/settings_internal.js",
] + glob(
include = [
"emscripten/third_party/**",
"emscripten/tools/**",
],
exclude = [
"**/__pycache__/**",
],
),
)
filegroup(
name = "compiler_files",
srcs = [
"bin/clang{bin_extension}",
"bin/clang++{bin_extension}",
":emcc_common",
":includes",
],
)
filegroup(
name = "linker_files",
srcs = [
"bin/clang{bin_extension}",
"bin/llvm-ar{bin_extension}",
"bin/llvm-dwarfdump{bin_extension}",
"bin/llvm-nm{bin_extension}",
"bin/llvm-objcopy{bin_extension}",
"bin/wasm-ctor-eval{bin_extension}",
"bin/wasm-emscripten-finalize{bin_extension}",
"bin/wasm-ld{bin_extension}",
"bin/wasm-metadce{bin_extension}",
"bin/wasm-opt{bin_extension}",
"bin/wasm-split{bin_extension}",
"bin/wasm2js{bin_extension}",
":emcc_common",
] + glob(
include = [
"emscripten/cache/sysroot/lib/**",
"emscripten/node_modules/**",
"emscripten/src/**",
],
),
)
filegroup(
name = "ar_files",
srcs = [
"bin/llvm-ar{bin_extension}",
"emscripten/emar.py",
"emscripten/emscripten-version.txt",
"emscripten/src/settings.js",
"emscripten/src/settings_internal.js",
] + glob(
include = [
"emscripten/tools/**",
],
exclude = [
"**/__pycache__/**",
],
),
)
"""

View file

@ -0,0 +1,113 @@
BUILD_FILE_CONTENT_TEMPLATE = """
package(default_visibility = ['//visibility:public'])
exports_files(['emscripten_config'])
"""
EMBUILDER_CONFIG_TEMPLATE = """
CACHE = '{cache}'
BINARYEN_ROOT = '{binaryen_root}'
LLVM_ROOT = '{llvm_root}'
"""
def get_root_and_script_ext(repository_ctx):
if repository_ctx.os.name.startswith("linux"):
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_linux//:BUILD.bazel")).dirname, "")
elif "aarch64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_linux_arm64//:BUILD.bazel")).dirname, "")
else:
fail("Unsupported architecture for Linux")
elif repository_ctx.os.name.startswith("mac"):
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_mac//:BUILD.bazel")).dirname, "")
elif "aarch64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_mac_arm64//:BUILD.bazel")).dirname, "")
else:
fail("Unsupported architecture for MacOS")
elif repository_ctx.os.name.startswith("windows"):
return (repository_ctx.path(Label("@emscripten_bin_win//:BUILD.bazel")).dirname, ".bat")
else:
fail("Unsupported operating system")
def _emscripten_cache_repository_impl(repository_ctx):
# Read the default emscripten configuration file
default_config = repository_ctx.read(
repository_ctx.path(
Label("@emsdk//emscripten_toolchain:default_config"),
),
)
if repository_ctx.attr.targets or repository_ctx.attr.configuration:
root, script_ext = get_root_and_script_ext(repository_ctx)
llvm_root = root.get_child("bin")
cache = repository_ctx.path("cache")
# Create configuration file
embuilder_config_content = EMBUILDER_CONFIG_TEMPLATE.format(
cache = cache,
binaryen_root = root,
llvm_root = llvm_root,
)
repository_ctx.file("embuilder_config", embuilder_config_content)
embuilder_config_path = repository_ctx.path("embuilder_config")
embuilder_path = "{}{}".format(root.get_child("emscripten").get_child("embuilder"), script_ext)
# Prepare the command line
if repository_ctx.attr.targets:
targets = repository_ctx.attr.targets
else:
# If no targets are requested, build everything
targets = ["ALL"]
flags = ["--em-config", embuilder_config_path] + repository_ctx.attr.configuration
embuilder_args = [embuilder_path] + flags + ["build"] + targets
# Run embuilder
repository_ctx.report_progress("Building secondary cache")
result = repository_ctx.execute(
embuilder_args,
quiet = True,
environment = {
"EM_IGNORE_SANITY": "1",
"EM_NODE_JS": "empty",
},
)
if result.return_code != 0:
fail("Embuilder exited with a non-zero return code")
# Override Emscripten's cache with the secondary cache
default_config += "CACHE = '{}'\n".format(cache)
# Create the configuration file for the toolchain and export
repository_ctx.file("emscripten_config", default_config)
repository_ctx.file("BUILD.bazel", BUILD_FILE_CONTENT_TEMPLATE)
_emscripten_cache_repository = repository_rule(
implementation = _emscripten_cache_repository_impl,
attrs = {
"configuration": attr.string_list(),
"targets": attr.string_list(),
},
)
def _emscripten_cache_impl(ctx):
all_configuration = []
all_targets = []
for mod in ctx.modules:
for configuration in mod.tags.configuration:
all_configuration += configuration.flags
for targets in mod.tags.targets:
all_targets += targets.targets
_emscripten_cache_repository(
name = "emscripten_cache",
configuration = all_configuration,
targets = all_targets,
)
emscripten_cache = module_extension(
tag_classes = {
"configuration": tag_class(attrs = {"flags": attr.string_list()}),
"targets": tag_class(attrs = {"targets": attr.string_list()}),
},
implementation = _emscripten_cache_impl,
)

View file

@ -0,0 +1,99 @@
load(":remote_emscripten_repository.bzl", "remote_emscripten_repository")
load(":revisions.bzl", "EMSCRIPTEN_TAGS")
def _parse_version(v):
return [int(u) for u in v.split(".")]
def _empty_repository_impl(ctx):
ctx.file("MODULE.bazel", """module(name = "{}")""".format(ctx.name))
ctx.file("BUILD.bazel", "")
_empty_repository = repository_rule(
implementation = _empty_repository_impl,
)
def emscripten_repo_name(name):
return "emscripten_bin_{}".format(name)
def _emscripten_deps_impl(ctx):
version = None
for mod in ctx.modules:
for config in mod.tags.config:
if config.version and version != None:
fail("More than one emscripten version specified!")
version = config.version
if version == None:
version = "latest"
if version == "latest":
version = reversed(sorted(EMSCRIPTEN_TAGS.keys(), key = _parse_version))[0]
revision = EMSCRIPTEN_TAGS[version]
emscripten_url = "https://storage.googleapis.com/webassembly/emscripten-releases-builds/{}/{}/wasm-binaries{}.{}"
remote_emscripten_repository(
name = emscripten_repo_name("linux"),
bin_extension = "",
sha256 = revision.sha_linux,
strip_prefix = "install",
type = "tar.xz",
url = emscripten_url.format("linux", revision.hash, "", "tar.xz"),
)
# Not all versions have a linux/arm64 release: https://github.com/emscripten-core/emsdk/issues/547
if hasattr(revision, "sha_linux_arm64"):
remote_emscripten_repository(
name = emscripten_repo_name("linux_arm64"),
bin_extension = "",
sha256 = revision.sha_linux_arm64,
strip_prefix = "install",
type = "tar.xz",
url = emscripten_url.format("linux", revision.hash, "-arm64", "tar.xz"),
)
else:
_empty_repository(
name = emscripten_repo_name("linux_arm64"),
)
remote_emscripten_repository(
name = emscripten_repo_name("mac"),
bin_extension = "",
sha256 = revision.sha_mac,
strip_prefix = "install",
type = "tar.xz",
url = emscripten_url.format("mac", revision.hash, "", "tar.xz"),
)
remote_emscripten_repository(
name = emscripten_repo_name("mac_arm64"),
bin_extension = "",
sha256 = revision.sha_mac_arm64,
strip_prefix = "install",
type = "tar.xz",
url = emscripten_url.format("mac", revision.hash, "-arm64", "tar.xz"),
)
remote_emscripten_repository(
name = emscripten_repo_name("win"),
bin_extension = ".exe",
sha256 = revision.sha_win,
strip_prefix = "install",
type = "zip",
url = emscripten_url.format("win", revision.hash, "", "zip"),
)
emscripten_deps = module_extension(
tag_classes = {
"config": tag_class(
attrs = {
"version": attr.string(
doc = "Version to use. 'latest' to use latest.",
values = ["latest"] + EMSCRIPTEN_TAGS.keys(),
),
},
),
},
implementation = _emscripten_deps_impl,
)

View file

@ -0,0 +1,43 @@
load("//:emscripten_deps.bzl", "emscripten_repo_name")
load("//:remote_emscripten_repository.bzl", "create_toolchains", "emscripten_toolchain_name")
load("@rules_python//python:py_binary.bzl", "py_binary")
package(default_visibility = ["//visibility:public"])
# dlmalloc.bc is implicitly added by the emscripten toolchain
cc_library(name = "malloc")
create_toolchains(
name = emscripten_toolchain_name("linux"),
repo_name = emscripten_repo_name("linux"),
exec_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64"],
)
create_toolchains(
name = emscripten_toolchain_name("linux_arm64"),
repo_name = emscripten_repo_name("linux_arm64"),
exec_compatible_with = ["@platforms//os:linux", "@platforms//cpu:arm64"],
)
create_toolchains(
name = emscripten_toolchain_name("mac"),
repo_name = emscripten_repo_name("mac"),
exec_compatible_with = ["@platforms//os:macos", "@platforms//cpu:x86_64"],
)
create_toolchains(
name = emscripten_toolchain_name("mac_arm64"),
repo_name = emscripten_repo_name("mac_arm64"),
exec_compatible_with = ["@platforms//os:macos", "@platforms//cpu:arm64"],
)
create_toolchains(
name = emscripten_toolchain_name("win"),
repo_name = emscripten_repo_name("win"),
exec_compatible_with = ["@platforms//os:windows", "@platforms//cpu:x86_64"],
)
py_binary(
name = "wasm_binary",
srcs = ["wasm_binary.py"],
)

View file

@ -0,0 +1,16 @@
import os
import platform
ROOT_DIR = os.environ["ROOT_DIR"]
EMSCRIPTEN_ROOT = os.environ["EMSCRIPTEN"]
BINARYEN_ROOT = os.path.join(ROOT_DIR, os.environ["EM_BIN_PATH"])
LLVM_ROOT = os.path.join(BINARYEN_ROOT, "bin")
NODE_JS = os.path.join(ROOT_DIR, os.environ["NODE_JS_PATH"])
FROZEN_CACHE = True
# This works around an issue with Bazel RBE where the symlinks in node_modules/.bin
# are uploaded as the linked files, which means the cli.js cannot load its
# dependencies from the expected locations.
# See https://github.com/emscripten-core/emscripten/pull/16640 for more
CLOSURE_COMPILER = [NODE_JS, os.path.join(EMSCRIPTEN_ROOT, "node_modules",
"google-closure-compiler", "cli.js")]

View file

@ -0,0 +1,5 @@
@ECHO OFF
call %~dp0\env.bat
py -3 %EMSCRIPTEN%\emar.py %*

View file

@ -0,0 +1,5 @@
#!/bin/bash
source $(dirname $0)/env.sh
exec python3 $EMSCRIPTEN/emar.py "$@"

View file

@ -0,0 +1,5 @@
@ECHO OFF
call %~dp0\env.bat
py -3 %EMSCRIPTEN%\emcc.py %*

View file

@ -0,0 +1,5 @@
#!/bin/bash
source $(dirname $0)/env.sh
exec python3 $EMSCRIPTEN/emcc.py "$@"

View file

@ -0,0 +1,5 @@
@ECHO OFF
call %~dp0\env.bat
py -3 %~dp0\link_wrapper.py %*

View file

@ -0,0 +1,5 @@
#!/bin/bash
source $(dirname $0)/env.sh
exec python3 $(dirname $0)/link_wrapper.py "$@"

View file

@ -0,0 +1,6 @@
@ECHO OFF
set ROOT_DIR=%EXT_BUILD_ROOT%
if "%ROOT_DIR%"=="" set ROOT_DIR=%CD%
set EMSCRIPTEN=%ROOT_DIR%\%EM_BIN_PATH%\emscripten
set EM_CONFIG=%ROOT_DIR%\%EM_CONFIG_PATH%

View file

@ -0,0 +1,5 @@
#!/bin/bash
export ROOT_DIR=${EXT_BUILD_ROOT:-$(pwd -P)}
export EMSCRIPTEN=$ROOT_DIR/$EM_BIN_PATH/emscripten
export EM_CONFIG=$ROOT_DIR/$EM_CONFIG_PATH

Some files were not shown because too many files have changed in this diff Show more