Skip to content

Migrate to Emscripten's native Bazel rules. #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 1 addition & 14 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
# https://github.com/bazelbuild/bazel/issues/9451
# ideally we want this, but it doesn't work...
# build --cxxopt=-fno-canonical-system-headers
# as a work around we use `sed` to "fix" the *.d files.

build --crosstool_top=//toolchain:emscripten

# Use --cpu as a differentiator.
build --cpu=wasm32

# Use the default Bazel C++ toolchain to build the tools used during the
# build.

build --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
build --cxxopt=-std=c++17
12 changes: 8 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
workspace(name = "proxy_wasm_cpp_sdk")

load("@proxy_wasm_cpp_sdk//bazel/dep:deps.bzl", "wasm_dependencies")
load("@proxy_wasm_cpp_sdk//bazel:repositories.bzl", "proxy_wasm_cpp_host_repositories")

wasm_dependencies()
proxy_wasm_cpp_host_repositories()

load("@proxy_wasm_cpp_sdk//bazel/dep:deps_extra.bzl", "wasm_dependencies_extra")
load("@proxy_wasm_cpp_sdk//bazel:dependencies.bzl", "proxy_wasm_cpp_host_dependencies")

wasm_dependencies_extra()
proxy_wasm_cpp_host_dependencies()

load("@proxy_wasm_cpp_sdk//bazel:dependencies_extra.bzl", "proxy_wasm_cpp_host_dependencies_extra")

proxy_wasm_cpp_host_dependencies_extra()
6 changes: 6 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
licenses(["notice"]) # Apache 2

config_setting(
name = "is_opt",
values = {"compilation_mode": "opt"},
)
61 changes: 61 additions & 0 deletions bazel/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
load("@rules_cc//cc:defs.bzl", "cc_binary")

def proxy_wasm_cc_binary(
name,
additional_linker_inputs = [],
copts = [],
linkopts = [],
tags = [],
deps = [],
**kwargs):
cc_binary(
name = "proxy_wasm_" + name.rstrip(".wasm"),
additional_linker_inputs = additional_linker_inputs + [
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js",
],
copts = copts + [
"-std=c++17",
] + select({
"@proxy_wasm_cpp_sdk//bazel:is_opt": [
"-fdata-sections",
"-ffunction-sections",
"-flto",
"-O3",
],
"//conditions:default": [],
}),
linkopts = linkopts + [
"--no-entry",
"--js-library=$(location @proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js)",
"-sSTANDALONE_WASM",
"-sEXPORTED_FUNCTIONS=_malloc",
],
tags = tags + [
"manual",
],
deps = deps + [
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics",
],
**kwargs
)

wasm_cc_binary(
name = name,
cc_target = ":proxy_wasm_" + name.rstrip(".wasm"),
tags = tags,
)
1 change: 0 additions & 1 deletion bazel/dep/BUILD

This file was deleted.

6 changes: 4 additions & 2 deletions bazel/dep/deps_extra.bzl → bazel/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
load("@emsdk//:deps.bzl", emsdk_deps = "deps")

# Wasm deps that rely on a first stage of dependency loading in wasm_dependencies().
def wasm_dependencies_extra():
# Requires proxy_wasm_cpp_host_repositories() to be loaded first.
def proxy_wasm_cpp_host_dependencies():
protobuf_deps()
emsdk_deps()
19 changes: 19 additions & 0 deletions bazel/dependencies_extra.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")

# Requires proxy_wasm_cpp_host_dependencies() to be loaded first.
def proxy_wasm_cpp_host_dependencies_extra():
emsdk_emscripten_deps()
16 changes: 6 additions & 10 deletions bazel/dep/deps.bzl → bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

def wasm_dependencies():
def proxy_wasm_cpp_host_repositories():
maybe(
http_archive,
name = "emscripten_toolchain",
build_file = "@proxy_wasm_cpp_sdk//:emscripten-toolchain.BUILD",
patch_cmds = [
"./emsdk install 3.1.1",
"./emsdk activate --embedded 3.1.1",
],
strip_prefix = "emsdk-3.1.1",
url = "https://github.com/emscripten-core/emsdk/archive/3.1.1.tar.gz",
sha256 = "3a4893f0bb8203469e1197aa235fc49ed6f5dd2d490e9244a6899a8ad860f3e6",
name = "emsdk",
sha256 = "19851efd2616a66f9abd84f55f1d094e03252d87b1d78ae0f5d992121d1dd9cc",
# v3.1.1 with Bazel fixes
strip_prefix = "emsdk-7eecde8afcb89fbdff2597d6cce61a24ae2c34f3/bazel",
url = "https://github.com/emscripten-core/emsdk/archive/7eecde8afcb89fbdff2597d6cce61a24ae2c34f3.tar.gz",
)

maybe(
Expand Down
117 changes: 16 additions & 101 deletions bazel/wasm/wasm.bzl
Original file line number Diff line number Diff line change
@@ -1,104 +1,19 @@
# Copyright 2016-2020 Envoy Project Authors
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

def _wasm_cc_transition_impl(settings, attr):
return {
"//command_line_option:cpu": "wasm32",
"//command_line_option:crosstool_top": "@proxy_wasm_cpp_sdk//toolchain:emscripten",

# Overriding copt/cxxopt/linkopt to prevent sanitizers/coverage options leak
# into Wasm build configuration
"//command_line_option:copt": [],
"//command_line_option:cxxopt": [],
"//command_line_option:linkopt": [],
"//command_line_option:collect_code_coverage": "false",
"//command_line_option:fission": "no",
}

wasm_cc_transition = transition(
implementation = _wasm_cc_transition_impl,
inputs = [],
outputs = [
"//command_line_option:cpu",
"//command_line_option:crosstool_top",
"//command_line_option:copt",
"//command_line_option:cxxopt",
"//command_line_option:fission",
"//command_line_option:linkopt",
"//command_line_option:collect_code_coverage",
],
)

def wasm_binary_impl(ctx):
out = ctx.actions.declare_file(ctx.label.name)
ctx.actions.run(
executable = "cp",
arguments = [ctx.files.binary[0].path, out.path],
outputs = [out],
inputs = ctx.files.binary,
)

return [DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))]

def _wasm_attrs(transition):
return {
"binary": attr.label(mandatory = True, cfg = transition),
"_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
}

# Wasm binary rule implementation.
# This copies the binary specified in binary attribute in Wasm configuration to
# target configuration, so a binary in non-Wasm configuration can depend on them.
wasm_cc_binary_rule = rule(
implementation = wasm_binary_impl,
attrs = _wasm_attrs(wasm_cc_transition),
)
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def wasm_cc_binary(**kwargs):
fail("`wasm_cc_binary` is deprecated. Please use `proxy_wasm_cc_binary`.")

def proxy_wasm_cc_binary(name, additional_linker_inputs = [], linkopts = [], tags = [], deps = [], **kwargs):
wasm_name = "_wasm_" + name
kwargs.setdefault("visibility", ["//visibility:public"])
cc_binary(
name = wasm_name,
additional_linker_inputs = additional_linker_inputs + [
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js",
],
linkopts = linkopts + [
"--no-entry",
"--js-library=$(location @proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js)",
"-sSTANDALONE_WASM",
"-sEXPORTED_FUNCTIONS=_malloc",
],
# Adding manual tag it won't be built in non-Wasm (e.g. x86_64 config)
# when an wildcard is specified, but it will be built in Wasm configuration
# when the wasm_binary below is built.
tags = tags + [
"manual",
],
deps = deps + [
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics",
],
**kwargs
)
fail("`wasm_cc_binary` is deprecated. Please use `proxy_wasm_cc_binary` from `@proxy_wasm_cpp_sdk//bazel:defs.bzl`.")

wasm_cc_binary_rule(
name = name,
binary = ":" + wasm_name,
tags = tags,
)
def proxy_wasm_cc_binary(**kwargs):
fail("Please use `proxy_wasm_cc_binary` from `@proxy_wasm_cpp_sdk//bazel:defs.bzl`.")
8 changes: 0 additions & 8 deletions emscripten-toolchain.BUILD

This file was deleted.

2 changes: 1 addition & 1 deletion example/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//bazel/wasm:wasm.bzl", "proxy_wasm_cc_binary")
load("@proxy_wasm_cpp_sdk//bazel:defs.bzl", "proxy_wasm_cc_binary")

licenses(["notice"]) # Apache 2

Expand Down
46 changes: 0 additions & 46 deletions toolchain/BUILD

This file was deleted.

Loading