Skip to content

Commit

Permalink
Migrate to Emscripten's native Bazel rules. (#132)
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
  • Loading branch information
PiotrSikora committed Mar 15, 2022
1 parent 898aaed commit e30535b
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 449 deletions.
14 changes: 0 additions & 14 deletions .bazelrc

This file was deleted.

9 changes: 7 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cc_library(
"proxy_wasm_api.h",
"proxy_wasm_externs.h",
],
copts = ["-std=c++17"],
deps = [
":common_lib",
"@com_google_protobuf//:protobuf_lite",
Expand All @@ -25,6 +26,7 @@ cc_library(
"proxy_wasm_common.h",
"proxy_wasm_enums.h",
],
copts = ["-std=c++17"],
)

cc_library(
Expand All @@ -39,6 +41,7 @@ cc_library(
"proxy_wasm_externs.h",
"proxy_wasm_intrinsics.h",
],
copts = ["-std=c++17"],
visibility = ["//visibility:public"],
)

Expand All @@ -62,7 +65,8 @@ proto_library(
cc_library(
name = "proxy_wasm_intrinsics_lite",
hdrs = ["proxy_wasm_intrinsics_lite.h"],
copts = ["-DPROXY_WASM_PROTOBUF_LITE=1"],
copts = ["-std=c++17"],
defines = ["PROXY_WASM_PROTOBUF_LITE"],
visibility = ["//visibility:public"],
deps = [
":proxy_wasm_intrinsics",
Expand All @@ -75,7 +79,8 @@ cc_library(
cc_library(
name = "proxy_wasm_intrinsics_full",
hdrs = ["proxy_wasm_intrinsics_full.h"],
copts = ["-DPROXY_WASM_PROTOBUF_FULL=1"],
copts = ["-std=c++17"],
defines = ["PROXY_WASM_PROTOBUF_FULL"],
visibility = ["//visibility:public"],
deps = [
":proxy_wasm_intrinsics",
Expand Down
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()
File renamed without changes.
116 changes: 116 additions & 0 deletions bazel/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# 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 _optimized_wasm_cc_binary_transition_impl(settings, attr):
# TODO(PiotrSikora): Add -flto to copts/linkopts when fixed in emsdk.
# See: https://github.com/emscripten-core/emsdk/issues/971
return {
"//command_line_option:copt": ["-O3"],
"//command_line_option:cxxopt": [],
"//command_line_option:linkopt": [],
"//command_line_option:collect_code_coverage": False,
}

_optimized_wasm_cc_binary_transition = transition(
implementation = _optimized_wasm_cc_binary_transition_impl,
inputs = [],
outputs = [
"//command_line_option:copt",
"//command_line_option:cxxopt",
"//command_line_option:linkopt",
"//command_line_option:collect_code_coverage",
],
)

def _optimized_wasm_cc_binary_impl(ctx):
input_binary = ctx.attr.wasm_cc_target[0][DefaultInfo].files_to_run.executable
input_runfiles = ctx.attr.wasm_cc_target[0][DefaultInfo].default_runfiles
copied_binary = ctx.actions.declare_file(ctx.attr.name)

ctx.actions.run(
mnemonic = "CopyFile",
executable = "cp",
arguments = [input_binary.path, copied_binary.path],
inputs = [input_binary],
outputs = [copied_binary],
)

return DefaultInfo(
executable = copied_binary,
runfiles = input_runfiles,
)

_optimized_wasm_cc_binary = rule(
implementation = _optimized_wasm_cc_binary_impl,
attrs = {
"wasm_cc_target": attr.label(
doc = "The wasm_cc_binary to extract files from.",
cfg = _optimized_wasm_cc_binary_transition,
mandatory = True,
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
executable = True,
)

def proxy_wasm_cc_binary(
name,
additional_linker_inputs = [],
linkopts = [],
tags = [],
deps = [],
protobuf = "",
**kwargs):
proxy_wasm_deps = ["@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics"]
if protobuf == "lite":
proxy_wasm_deps.append("@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_lite")
if protobuf == "full":
proxy_wasm_deps.append("@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_full")

cc_binary(
name = "proxy_wasm_" + name.rstrip(".wasm"),
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",
],
tags = tags + [
"manual",
],
deps = deps + proxy_wasm_deps,
**kwargs
)

wasm_cc_binary(
name = "wasm_" + name,
cc_target = ":proxy_wasm_" + name.rstrip(".wasm"),
tags = tags + [
"manual",
],
)

_optimized_wasm_cc_binary(
name = name,
wasm_cc_target = ":wasm_" + name,
tags = tags,
)
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", "emscripten_deps")

# Requires proxy_wasm_cpp_host_dependencies() to be loaded first.
def proxy_wasm_cpp_host_dependencies_extra():
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.7",
"./emsdk activate --embedded 3.1.7",
],
strip_prefix = "emsdk-3.1.7",
url = "https://github.com/emscripten-core/emsdk/archive/3.1.7.tar.gz",
sha256 = "bcceced0b7cad2e08375adf74ef20fa431230abbae8766bdad268c43e34f8d03",
name = "emsdk",
sha256 = "1ca0ff918d476c55707bb99bc0452be28ac5fb8f22a9260a8aae8a38d1bc0e27",
# v3.1.7 with Bazel fixes
strip_prefix = "emsdk-0ea8f8a8707070e9a7c83fbb4a3065683bcf1799/bazel",
url = "https://github.com/emscripten-core/emsdk/archive/0ea8f8a8707070e9a7c83fbb4a3065683bcf1799.tar.gz",
)

maybe(
Expand Down
90 changes: 3 additions & 87 deletions bazel/wasm/wasm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,8 @@
# 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),
)

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.

17 changes: 16 additions & 1 deletion example/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
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

proxy_wasm_cc_binary(
name = "http_wasm_example.wasm",
srcs = ["http_wasm_example.cc"],
copts = ["-std=c++17"],
)

proxy_wasm_cc_binary(
name = "http_wasm_example_with_protobuf_lite.wasm",
srcs = ["http_wasm_example.cc"],
copts = ["-std=c++17"],
protobuf = "lite",
)

proxy_wasm_cc_binary(
name = "http_wasm_example_with_protobuf_full.wasm",
srcs = ["http_wasm_example.cc"],
copts = ["-std=c++17"],
protobuf = "full",
)
10 changes: 10 additions & 0 deletions example/http_wasm_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleCon
"my_root_id");

bool ExampleRootContext::onStart(size_t) {
#if defined(PROXY_WASM_PROTOBUF_FULL)
LOG_TRACE("onStart with protobuf (full)");
google::protobuf::Value value;
value.set_string_value("unused");
#elif defined(PROXY_WASM_PROTOBUF_LITE)
LOG_TRACE("onStart with protobuf (lite)");
google::protobuf::Value value;
value.set_string_value("unused");
#else
LOG_TRACE("onStart");
#endif
return true;
}

Expand Down
Loading

0 comments on commit e30535b

Please sign in to comment.