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
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ jobs:
- checkout
- run: brew install grep
- run:
name: install bazel
name: install bazelisk
command: |
brew install bazel
brew install bazelisk
- run: test/test_bazel_mac.sh

test-bazel-windows:
Expand Down
31 changes: 21 additions & 10 deletions bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,34 @@ config_setting(
filegroup(name = "empty")

alias(
name = "binaries",
name = "compiler_files",
actual = select({
":linux": "@emscripten_bin_linux//:all",
":macos": "@emscripten_bin_mac//:all",
":macos_arm64": "@emscripten_bin_mac_arm64//:all",
":windows": "@emscripten_bin_win//:all",
":linux": "@emscripten_bin_linux//:compiler_files",
":macos": "@emscripten_bin_mac//:compiler_files",
":macos_arm64": "@emscripten_bin_mac_arm64//:compiler_files",
":windows": "@emscripten_bin_win//:compiler_files",
"//conditions:default": ":empty",
}),
)

alias(
name = "node_modules",
name = "linker_files",
actual = select({
":linux": "@emscripten_npm_linux//:node_modules",
":macos": "@emscripten_npm_mac//:node_modules",
":macos_arm64": "@emscripten_npm_mac//:node_modules",
":windows": "@emscripten_npm_win//:node_modules",
":linux": "@emscripten_bin_linux//:linker_files",
":macos": "@emscripten_bin_mac//:linker_files",
":macos_arm64": "@emscripten_bin_mac_arm64//:linker_files",
":windows": "@emscripten_bin_win//:linker_files",
"//conditions:default": ":empty",
}),
)

alias(
name = "ar_files",
actual = select({
":linux": "@emscripten_bin_linux//:ar_files",
":macos": "@emscripten_bin_mac//:ar_files",
":macos_arm64": "@emscripten_bin_mac_arm64//:ar_files",
":windows": "@emscripten_bin_win//:ar_files",
"//conditions:default": ":empty",
}),
)
57 changes: 52 additions & 5 deletions bazel/emscripten_deps.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install", "node_repositories")
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "npm_install")
load(":revisions.bzl", "EMSCRIPTEN_TAGS")

def _parse_version(v):
return [int(u) for u in v.split(".")]

BUILD_FILE_CONTENT_TEMPLATE = """
package(default_visibility = ['//visibility:public'])

filegroup(
name = "includes",
srcs = glob([
"emscripten/cache/sysroot/include/c++/v1/**",
"emscripten/cache/sysroot/include/compat/**",
"emscripten/cache/sysroot/include/**",
"lib/clang/15.0.0/include/**",
]),
)

filegroup(
name = "compiler_files",
srcs = [
"emscripten/emcc.py",
"bin/clang{bin_extension}",
"bin/clang++{bin_extension}",
":includes",
],
)

filegroup(
name = "linker_files",
srcs = [
"emscripten/emcc.py",
"bin/clang{bin_extension}",
"bin/llc{bin_extension}",
"bin/llvm-ar{bin_extension}",
"bin/llvm-nm{bin_extension}",
"bin/llvm-objcopy{bin_extension}",
"bin/wasm-emscripten-finalize{bin_extension}",
"bin/wasm-ld{bin_extension}",
"bin/wasm-opt{bin_extension}",
] + glob(["emscripten/node_modules/**"]),
)

filegroup(
name = "ar_files",
srcs = [
"emscripten/emar.py",
"bin/llvm-ar{bin_extension}",
],
)
"""

def emscripten_deps(emscripten_version = "latest"):
version = emscripten_version

Expand Down Expand Up @@ -36,7 +83,7 @@ def emscripten_deps(emscripten_version = "latest"):
strip_prefix = "install",
url = emscripten_url.format("linux", revision.hash, "", "tbz2"),
sha256 = revision.sha_linux,
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ""),
type = "tar.bz2",
)

Expand All @@ -46,7 +93,7 @@ def emscripten_deps(emscripten_version = "latest"):
strip_prefix = "install",
url = emscripten_url.format("mac", revision.hash, "", "tbz2"),
sha256 = revision.sha_mac,
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ""),
type = "tar.bz2",
)

Expand All @@ -56,7 +103,7 @@ def emscripten_deps(emscripten_version = "latest"):
strip_prefix = "install",
url = emscripten_url.format("mac", revision.hash, "-arm64", "tbz2"),
sha256 = revision.sha_mac_arm64,
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ""),
type = "tar.bz2",
)

Expand All @@ -66,7 +113,7 @@ def emscripten_deps(emscripten_version = "latest"):
strip_prefix = "install",
url = emscripten_url.format("win", revision.hash, "", "zip"),
sha256 = revision.sha_win,
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ".exe"),
type = "zip",
)

Expand Down
44 changes: 21 additions & 23 deletions bazel/emscripten_toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,43 @@ package(default_visibility = ["//visibility:public"])
node_files = "@nodejs_host//:node_files" if existing_rule("@nodejs_host//:node_files") else "@nodejs//:node_files"

filegroup(
name = "common-script-includes",
name = "common_files",
srcs = [
"emar.sh",
"emar.bat",
"emcc.sh",
"emcc.bat",
"emscripten_config",
"env.sh",
"env.bat",
"@emsdk//:binaries",
"@emsdk//:node_modules",
node_files,
],
)

filegroup(
name = "compile-emscripten",
srcs = [":common-script-includes"],
name = "compiler_files",
srcs = [
"emcc.sh",
"emcc.bat",
"@emsdk//:compiler_files",
":common_files",
],
)

filegroup(
name = "link-emscripten",
name = "linker_files",
srcs = [
"emcc_link.sh",
"emcc_link.bat",
"link_wrapper.py",
":common-script-includes",
"@emsdk//:binaries",
node_files,
"@emsdk//:linker_files",
":common_files",
],
)

filegroup(
name = "every-file",
name = "ar_files",
srcs = [
":compile-emscripten",
":link-emscripten",
"@emsdk//:binaries",
node_files,
"emar.sh",
"emar.bat",
"@emsdk//:ar_files",
":common_files",
],
)

Expand All @@ -59,7 +57,7 @@ emscripten_cc_toolchain_config_rule(
name = "wasm",
cpu = "wasm",
em_config = "emscripten_config",
emscripten_binaries = "@emsdk//:binaries",
emscripten_binaries = "@emsdk//:compiler_files",
script_extension = select({
"@bazel_tools//src/conditions:host_windows": "bat",
"//conditions:default": "sh",
Expand All @@ -68,12 +66,12 @@ emscripten_cc_toolchain_config_rule(

cc_toolchain(
name = "cc-compiler-wasm",
all_files = ":every-file",
ar_files = ":common-script-includes",
all_files = ":empty",
ar_files = ":ar_files",
as_files = ":empty",
compiler_files = ":compile-emscripten",
compiler_files = ":compiler_files",
dwp_files = ":empty",
linker_files = ":link-emscripten",
linker_files = ":linker_files",
objcopy_files = ":empty",
strip_files = ":empty",
toolchain_config = "wasm",
Expand Down
6 changes: 0 additions & 6 deletions bazel/emscripten_toolchain/emscripten.BUILD

This file was deleted.

13 changes: 11 additions & 2 deletions bazel/emscripten_toolchain/emscripten_config
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ import platform

ROOT_DIR = os.environ["ROOT_DIR"]
EMSCRIPTEN_ROOT = os.environ["EMSCRIPTEN"]
BINARYEN_ROOT = ROOT_DIR + "/" + os.environ["EM_BIN_PATH"]
LLVM_ROOT = BINARYEN_ROOT + "/bin"
BINARYEN_ROOT = os.path.join(ROOT_DIR, os.environ["EM_BIN_PATH"])
LLVM_ROOT = os.path.join(BINARYEN_ROOT, "bin")
FROZEN_CACHE = True

system = platform.system()

machine = "arm64" if platform.machine() == "arm64" else "amd64"
nodejs_binary = "bin/nodejs/node.exe" if(system =="Windows") else "bin/node"
NODE_JS = ROOT_DIR + "/external/nodejs_{}_{}/{}".format(system.lower(), machine, nodejs_binary)


# 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
if system != "Windows":
CLOSURE_COMPILER = [NODE_JS, os.path.join(EMSCRIPTEN_ROOT, "node_modules",
"google-closure-compiler", "cli.js")]
54 changes: 21 additions & 33 deletions bazel/emscripten_toolchain/wasm_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
This script will take a tar archive containing the output of the emscripten
toolchain. This file contains any output files produced by a wasm_cc_binary or a
cc_binary built with --config=wasm. The files are extracted into the given
output path.
output paths.

The name of archive is expected to be of the format `foo` or `foo.XXX` and
the contents are expected to be foo.js and foo.wasm.

Several optional files may also be in the archive, including but not limited to
foo.js.mem, pthread-main.js, and foo.wasm.map.

If the file is not a tar archive, the passed file will simply be copied to its
destination.
The contents of the archive are expected to match the given outputs extnames.

This script and its accompanying Bazel rule should allow you to extract a
WebAssembly binary into a larger web application.
Expand All @@ -29,39 +22,34 @@ def ensure(f):
pass


def check(f):
if not os.path.exists(f):
raise Exception('Expected file in archive: %s' % f)


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--archive', help='The archive to extract from.')
parser.add_argument('--output_path', help='The path to extract into.')
parser.add_argument('--outputs', help='Comma separated list of files that should be extracted from the archive. Only the extname has to match a file in the archive.')
parser.add_argument('--allow_empty_outputs', help='If an output listed in --outputs does not exist, create it anyways.', action='store_true')
args = parser.parse_args()

args.archive = os.path.normpath(args.archive)
args.outputs = args.outputs.split(",")

basename = os.path.basename(args.archive)
stem = basename.split('.')[0]

# Extract all files from the tarball.
tar = tarfile.open(args.archive)
tar.extractall(args.output_path)

# At least one of these two files should exist at this point.
ensure(os.path.join(args.output_path, stem + '.js'))
ensure(os.path.join(args.output_path, stem + '.wasm'))

# And can optionally contain these extra files.
ensure(os.path.join(args.output_path, stem + '.wasm.map'))
ensure(os.path.join(args.output_path, stem + '.worker.js'))
ensure(os.path.join(args.output_path, stem + '.js.mem'))
ensure(os.path.join(args.output_path, stem + '.data'))
ensure(os.path.join(args.output_path, stem + '.fetch.js'))
ensure(os.path.join(args.output_path, stem + '.js.symbols'))
ensure(os.path.join(args.output_path, stem + '.wasm.debug.wasm'))
ensure(os.path.join(args.output_path, stem + '.html'))
for member in tar.getmembers():
extname = '.' + member.name.split('.', 1)[1]
for idx, output in enumerate(args.outputs):
if output.endswith(extname):
member_file = tar.extractfile(member)
with open(output, "wb") as output_file:
output_file.write(member_file.read())
args.outputs.pop(idx)
break

for output in args.outputs:
extname = '.' + output.split('.', 1)[1]
if args.allow_empty_outputs:
ensure(output)
else:
print("[ERROR] Archive does not contain file with extname: %s" % extname)


if __name__ == '__main__':
Expand Down
Loading