Update examples collection
This commit is contained in:
parent
8bc9f30d87
commit
87d3066428
83 changed files with 21502 additions and 0 deletions
1
emsdk-cache/emsdk-main/bazel/.bazelrc
Normal file
1
emsdk-cache/emsdk-main/bazel/.bazelrc
Normal file
|
|
@ -0,0 +1 @@
|
|||
build --incompatible_enable_cc_toolchain_resolution
|
||||
1
emsdk-cache/emsdk-main/bazel/.gitignore
vendored
Normal file
1
emsdk-cache/emsdk-main/bazel/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/bazel-*
|
||||
79
emsdk-cache/emsdk-main/bazel/MODULE.bazel
Normal file
79
emsdk-cache/emsdk-main/bazel/MODULE.bazel
Normal 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",
|
||||
)
|
||||
3696
emsdk-cache/emsdk-main/bazel/MODULE.bazel.lock
generated
Normal file
3696
emsdk-cache/emsdk-main/bazel/MODULE.bazel.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
90
emsdk-cache/emsdk-main/bazel/README.md
Normal file
90
emsdk-cache/emsdk-main/bazel/README.md
Normal 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).
|
||||
2
emsdk-cache/emsdk-main/bazel/bazelrc
Normal file
2
emsdk-cache/emsdk-main/bazel/bazelrc
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
build:wasm --incompatible_enable_cc_toolchain_resolution
|
||||
build:wasm --platforms=@emsdk//:platform_wasm
|
||||
93
emsdk-cache/emsdk-main/bazel/emscripten_build_file.bzl
Normal file
93
emsdk-cache/emsdk-main/bazel/emscripten_build_file.bzl
Normal 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__/**",
|
||||
],
|
||||
),
|
||||
)
|
||||
"""
|
||||
113
emsdk-cache/emsdk-main/bazel/emscripten_cache.bzl
Normal file
113
emsdk-cache/emsdk-main/bazel/emscripten_cache.bzl
Normal 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,
|
||||
)
|
||||
99
emsdk-cache/emsdk-main/bazel/emscripten_deps.bzl
Normal file
99
emsdk-cache/emsdk-main/bazel/emscripten_deps.bzl
Normal 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,
|
||||
)
|
||||
|
|
@ -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"],
|
||||
)
|
||||
|
|
@ -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")]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
@ECHO OFF
|
||||
|
||||
call %~dp0\env.bat
|
||||
|
||||
py -3 %EMSCRIPTEN%\emar.py %*
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
source $(dirname $0)/env.sh
|
||||
|
||||
exec python3 $EMSCRIPTEN/emar.py "$@"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
@ECHO OFF
|
||||
|
||||
call %~dp0\env.bat
|
||||
|
||||
py -3 %EMSCRIPTEN%\emcc.py %*
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
source $(dirname $0)/env.sh
|
||||
|
||||
exec python3 $EMSCRIPTEN/emcc.py "$@"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
@ECHO OFF
|
||||
|
||||
call %~dp0\env.bat
|
||||
|
||||
py -3 %~dp0\link_wrapper.py %*
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
source $(dirname $0)/env.sh
|
||||
|
||||
exec python3 $(dirname $0)/link_wrapper.py "$@"
|
||||
|
|
@ -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%
|
||||
5
emsdk-cache/emsdk-main/bazel/emscripten_toolchain/env.sh
Normal file
5
emsdk-cache/emsdk-main/bazel/emscripten_toolchain/env.sh
Normal 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
|
||||
|
|
@ -0,0 +1,167 @@
|
|||
#!/usr/bin/env python
|
||||
"""wrapper around emcc link step.
|
||||
|
||||
This wrapper currently serves the following purposes.
|
||||
|
||||
1. When building with --config=wasm the final output is multiple files, usually
|
||||
at least one .js and one .wasm file. Since the cc_binary link step only
|
||||
allows a single output, we must tar up the outputs into a single file.
|
||||
|
||||
2. Add quotes around arguments that need them in the response file to work
|
||||
around a bazel quirk.
|
||||
|
||||
3. Ensure the external_debug_info section of the wasm points at the correct
|
||||
bazel path.
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
# Only argument should be @path/to/parameter/file
|
||||
assert sys.argv[1][0] == '@', sys.argv
|
||||
param_filename = sys.argv[1][1:]
|
||||
param_file_args = [line.strip() for line in open(param_filename, 'r').readlines()]
|
||||
|
||||
# Re-write response file if needed.
|
||||
if any(' ' in a for a in param_file_args):
|
||||
new_param_filename = param_filename + '.modified'
|
||||
with open(new_param_filename, 'w') as f:
|
||||
for param in param_file_args:
|
||||
if ' ' in param:
|
||||
f.write('"%s"' % param)
|
||||
else:
|
||||
f.write(param)
|
||||
f.write('\n')
|
||||
sys.argv[1] = '@' + new_param_filename
|
||||
|
||||
emcc_py = os.path.join(os.environ['EMSCRIPTEN'], 'emcc.py')
|
||||
rtn = subprocess.call([sys.executable, emcc_py] + sys.argv[1:])
|
||||
if rtn != 0:
|
||||
sys.exit(1)
|
||||
|
||||
# Parse the arguments that we gave to the linker to determine what the output
|
||||
# file is named and what the output format is.
|
||||
parser = argparse.ArgumentParser(add_help=False)
|
||||
parser.add_argument('-o')
|
||||
parser.add_argument('--oformat')
|
||||
options = parser.parse_known_args(param_file_args)[0]
|
||||
output_file = options.o
|
||||
oformat = options.oformat
|
||||
outdir = os.path.normpath(os.path.dirname(output_file))
|
||||
base_name = os.path.basename(output_file)
|
||||
|
||||
# The output file name is the name of the build rule that was built.
|
||||
# Add an appropriate file extension based on --oformat.
|
||||
if oformat is not None:
|
||||
base_name_split = os.path.splitext(base_name)
|
||||
|
||||
# If the output name has no extension, give it the appropriate extension.
|
||||
if not base_name_split[1]:
|
||||
os.replace(output_file, output_file + '.' + oformat)
|
||||
|
||||
# If the output name does have an extension and it matches the output format,
|
||||
# change the base_name so it doesn't have an extension.
|
||||
elif base_name_split[1] == '.' + oformat:
|
||||
base_name = base_name_split[0]
|
||||
|
||||
# If the output name does have an extension and it does not match the output
|
||||
# format, change the base_name so it doesn't have an extension and rename
|
||||
# the output_file so it has the proper extension.
|
||||
# Note that if you do something like name your build rule "foo.js" and pass
|
||||
# "--oformat=html", emscripten will write to the same file for both the js and
|
||||
# html output, overwriting the js output entirely with the html.
|
||||
# Please don't do that.
|
||||
else:
|
||||
base_name = base_name_split[0]
|
||||
os.replace(output_file, os.path.join(outdir, base_name + '.' + oformat))
|
||||
|
||||
files = []
|
||||
extensions = [
|
||||
'.js',
|
||||
'.wasm',
|
||||
'.wasm.map',
|
||||
'.js.mem',
|
||||
'.fetch.js',
|
||||
'.worker.js',
|
||||
'.data',
|
||||
'.js.symbols',
|
||||
'.wasm.debug.wasm',
|
||||
'.html',
|
||||
'.aw.js'
|
||||
]
|
||||
|
||||
for ext in extensions:
|
||||
filename = base_name + ext
|
||||
if os.path.exists(os.path.join(outdir, filename)):
|
||||
files.append(filename)
|
||||
|
||||
wasm_base = os.path.join(outdir, base_name + '.wasm')
|
||||
if os.path.exists(wasm_base + '.debug.wasm') and os.path.exists(wasm_base):
|
||||
# If we have a .wasm.debug.wasm file and a .wasm file, we need to rewrite the
|
||||
# section in the .wasm file that refers to it. The path that's in there
|
||||
# is the blaze output path; we want it to be just the filename.
|
||||
|
||||
llvm_objcopy = os.path.join(
|
||||
os.environ['EM_BIN_PATH'], 'bin/llvm-objcopy')
|
||||
# First, check to make sure the .wasm file has the header that needs to be
|
||||
# rewritten.
|
||||
rtn = subprocess.call([
|
||||
llvm_objcopy,
|
||||
'--dump-section=external_debug_info=/dev/null',
|
||||
wasm_base], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if rtn == 0:
|
||||
# If llvm-objcopy did not return an error, the external_debug_info section
|
||||
# must exist, so we're good to continue.
|
||||
|
||||
# Next we need to convert length of the filename to LEB128.
|
||||
# Start by converting the length of the filename to a bit string.
|
||||
bit_string = '{0:b}'.format(len(base_name + '.wasm.debug.wasm'))
|
||||
|
||||
# Pad the bit string with 0s so that its length is a multiple of 7.
|
||||
while len(bit_string) % 7 != 0:
|
||||
bit_string = '0' + bit_string
|
||||
|
||||
# Break up our bit string into chunks of 7.
|
||||
# We do this backwards because the final format is little-endian.
|
||||
final_bytes = bytearray()
|
||||
for i in reversed(range(0, len(bit_string), 7)):
|
||||
binary_part = bit_string[i:i + 7]
|
||||
if i != 0:
|
||||
# Every chunk except the last one needs to be prepended with '1'.
|
||||
# The length of each chunk is 7, so that one has an implicit '0'.
|
||||
binary_part = '1' + binary_part
|
||||
final_bytes.append(int(binary_part, 2))
|
||||
# Finally, add the actual filename.
|
||||
final_bytes.extend((base_name + '.wasm.debug.wasm').encode())
|
||||
|
||||
# Write our length + filename bytes to a temp file.
|
||||
with open(base_name + '_debugsection.tmp', 'wb+') as f:
|
||||
f.write(final_bytes)
|
||||
f.close()
|
||||
|
||||
# First delete the old section.
|
||||
subprocess.check_call([
|
||||
llvm_objcopy,
|
||||
wasm_base,
|
||||
'--remove-section=external_debug_info'])
|
||||
# Rewrite section with the new size and filename from the temp file.
|
||||
subprocess.check_call([
|
||||
llvm_objcopy,
|
||||
wasm_base,
|
||||
'--add-section=external_debug_info=' + base_name + '_debugsection.tmp'])
|
||||
|
||||
# Make sure we have at least one output file.
|
||||
if not len(files):
|
||||
print('emcc.py did not appear to output any known files!')
|
||||
sys.exit(1)
|
||||
|
||||
# cc_binary must output exactly one file; put all the output files in a tarball.
|
||||
cmd = ['tar', 'cf', base_name + '.tar'] + files
|
||||
subprocess.check_call(cmd, cwd=outdir)
|
||||
os.replace(os.path.join(outdir, base_name + '.tar'), output_file)
|
||||
|
||||
sys.exit(0)
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue