Skip to content

Commit e30535b

Browse files
authored
Migrate to Emscripten's native Bazel rules. (#132)
Signed-off-by: Piotr Sikora <[email protected]>
1 parent 898aaed commit e30535b

17 files changed

+189
-449
lines changed

.bazelrc

-14
This file was deleted.

BUILD

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cc_library(
1313
"proxy_wasm_api.h",
1414
"proxy_wasm_externs.h",
1515
],
16+
copts = ["-std=c++17"],
1617
deps = [
1718
":common_lib",
1819
"@com_google_protobuf//:protobuf_lite",
@@ -25,6 +26,7 @@ cc_library(
2526
"proxy_wasm_common.h",
2627
"proxy_wasm_enums.h",
2728
],
29+
copts = ["-std=c++17"],
2830
)
2931

3032
cc_library(
@@ -39,6 +41,7 @@ cc_library(
3941
"proxy_wasm_externs.h",
4042
"proxy_wasm_intrinsics.h",
4143
],
44+
copts = ["-std=c++17"],
4245
visibility = ["//visibility:public"],
4346
)
4447

@@ -62,7 +65,8 @@ proto_library(
6265
cc_library(
6366
name = "proxy_wasm_intrinsics_lite",
6467
hdrs = ["proxy_wasm_intrinsics_lite.h"],
65-
copts = ["-DPROXY_WASM_PROTOBUF_LITE=1"],
68+
copts = ["-std=c++17"],
69+
defines = ["PROXY_WASM_PROTOBUF_LITE"],
6670
visibility = ["//visibility:public"],
6771
deps = [
6872
":proxy_wasm_intrinsics",
@@ -75,7 +79,8 @@ cc_library(
7579
cc_library(
7680
name = "proxy_wasm_intrinsics_full",
7781
hdrs = ["proxy_wasm_intrinsics_full.h"],
78-
copts = ["-DPROXY_WASM_PROTOBUF_FULL=1"],
82+
copts = ["-std=c++17"],
83+
defines = ["PROXY_WASM_PROTOBUF_FULL"],
7984
visibility = ["//visibility:public"],
8085
deps = [
8186
":proxy_wasm_intrinsics",

WORKSPACE

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
workspace(name = "proxy_wasm_cpp_sdk")
22

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

5-
wasm_dependencies()
5+
proxy_wasm_cpp_host_repositories()
66

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

9-
wasm_dependencies_extra()
9+
proxy_wasm_cpp_host_dependencies()
10+
11+
load("@proxy_wasm_cpp_sdk//bazel:dependencies_extra.bzl", "proxy_wasm_cpp_host_dependencies_extra")
12+
13+
proxy_wasm_cpp_host_dependencies_extra()
File renamed without changes.

bazel/defs.bzl

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
16+
load("@rules_cc//cc:defs.bzl", "cc_binary")
17+
18+
def _optimized_wasm_cc_binary_transition_impl(settings, attr):
19+
# TODO(PiotrSikora): Add -flto to copts/linkopts when fixed in emsdk.
20+
# See: https://github.com/emscripten-core/emsdk/issues/971
21+
return {
22+
"//command_line_option:copt": ["-O3"],
23+
"//command_line_option:cxxopt": [],
24+
"//command_line_option:linkopt": [],
25+
"//command_line_option:collect_code_coverage": False,
26+
}
27+
28+
_optimized_wasm_cc_binary_transition = transition(
29+
implementation = _optimized_wasm_cc_binary_transition_impl,
30+
inputs = [],
31+
outputs = [
32+
"//command_line_option:copt",
33+
"//command_line_option:cxxopt",
34+
"//command_line_option:linkopt",
35+
"//command_line_option:collect_code_coverage",
36+
],
37+
)
38+
39+
def _optimized_wasm_cc_binary_impl(ctx):
40+
input_binary = ctx.attr.wasm_cc_target[0][DefaultInfo].files_to_run.executable
41+
input_runfiles = ctx.attr.wasm_cc_target[0][DefaultInfo].default_runfiles
42+
copied_binary = ctx.actions.declare_file(ctx.attr.name)
43+
44+
ctx.actions.run(
45+
mnemonic = "CopyFile",
46+
executable = "cp",
47+
arguments = [input_binary.path, copied_binary.path],
48+
inputs = [input_binary],
49+
outputs = [copied_binary],
50+
)
51+
52+
return DefaultInfo(
53+
executable = copied_binary,
54+
runfiles = input_runfiles,
55+
)
56+
57+
_optimized_wasm_cc_binary = rule(
58+
implementation = _optimized_wasm_cc_binary_impl,
59+
attrs = {
60+
"wasm_cc_target": attr.label(
61+
doc = "The wasm_cc_binary to extract files from.",
62+
cfg = _optimized_wasm_cc_binary_transition,
63+
mandatory = True,
64+
),
65+
"_allowlist_function_transition": attr.label(
66+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
67+
),
68+
},
69+
executable = True,
70+
)
71+
72+
def proxy_wasm_cc_binary(
73+
name,
74+
additional_linker_inputs = [],
75+
linkopts = [],
76+
tags = [],
77+
deps = [],
78+
protobuf = "",
79+
**kwargs):
80+
proxy_wasm_deps = ["@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics"]
81+
if protobuf == "lite":
82+
proxy_wasm_deps.append("@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_lite")
83+
if protobuf == "full":
84+
proxy_wasm_deps.append("@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_full")
85+
86+
cc_binary(
87+
name = "proxy_wasm_" + name.rstrip(".wasm"),
88+
additional_linker_inputs = additional_linker_inputs + [
89+
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js",
90+
],
91+
linkopts = linkopts + [
92+
"--no-entry",
93+
"--js-library=$(location @proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js)",
94+
"-sSTANDALONE_WASM",
95+
"-sEXPORTED_FUNCTIONS=_malloc",
96+
],
97+
tags = tags + [
98+
"manual",
99+
],
100+
deps = deps + proxy_wasm_deps,
101+
**kwargs
102+
)
103+
104+
wasm_cc_binary(
105+
name = "wasm_" + name,
106+
cc_target = ":proxy_wasm_" + name.rstrip(".wasm"),
107+
tags = tags + [
108+
"manual",
109+
],
110+
)
111+
112+
_optimized_wasm_cc_binary(
113+
name = name,
114+
wasm_cc_target = ":wasm_" + name,
115+
tags = tags,
116+
)

bazel/dep/deps_extra.bzl renamed to bazel/dependencies.bzl

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# limitations under the License.
1414

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

17-
# Wasm deps that rely on a first stage of dependency loading in wasm_dependencies().
18-
def wasm_dependencies_extra():
18+
# Requires proxy_wasm_cpp_host_repositories() to be loaded first.
19+
def proxy_wasm_cpp_host_dependencies():
1920
protobuf_deps()
21+
emsdk_deps()

bazel/dependencies_extra.bzl

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@emsdk//:emscripten_deps.bzl", "emscripten_deps")
16+
17+
# Requires proxy_wasm_cpp_host_dependencies() to be loaded first.
18+
def proxy_wasm_cpp_host_dependencies_extra():
19+
emscripten_deps()

bazel/dep/deps.bzl renamed to bazel/repositories.bzl

+6-10
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1616
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
1717

18-
def wasm_dependencies():
18+
def proxy_wasm_cpp_host_repositories():
1919
maybe(
2020
http_archive,
21-
name = "emscripten_toolchain",
22-
build_file = "@proxy_wasm_cpp_sdk//:emscripten-toolchain.BUILD",
23-
patch_cmds = [
24-
"./emsdk install 3.1.7",
25-
"./emsdk activate --embedded 3.1.7",
26-
],
27-
strip_prefix = "emsdk-3.1.7",
28-
url = "https://github.com/emscripten-core/emsdk/archive/3.1.7.tar.gz",
29-
sha256 = "bcceced0b7cad2e08375adf74ef20fa431230abbae8766bdad268c43e34f8d03",
21+
name = "emsdk",
22+
sha256 = "1ca0ff918d476c55707bb99bc0452be28ac5fb8f22a9260a8aae8a38d1bc0e27",
23+
# v3.1.7 with Bazel fixes
24+
strip_prefix = "emsdk-0ea8f8a8707070e9a7c83fbb4a3065683bcf1799/bazel",
25+
url = "https://github.com/emscripten-core/emsdk/archive/0ea8f8a8707070e9a7c83fbb4a3065683bcf1799.tar.gz",
3026
)
3127

3228
maybe(

bazel/wasm/wasm.bzl

+3-87
Original file line numberDiff line numberDiff line change
@@ -13,92 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
load("@rules_cc//cc:defs.bzl", "cc_binary")
17-
18-
def _wasm_cc_transition_impl(settings, attr):
19-
return {
20-
"//command_line_option:cpu": "wasm32",
21-
"//command_line_option:crosstool_top": "@proxy_wasm_cpp_sdk//toolchain:emscripten",
22-
23-
# Overriding copt/cxxopt/linkopt to prevent sanitizers/coverage options leak
24-
# into Wasm build configuration
25-
"//command_line_option:copt": [],
26-
"//command_line_option:cxxopt": [],
27-
"//command_line_option:linkopt": [],
28-
"//command_line_option:collect_code_coverage": "false",
29-
"//command_line_option:fission": "no",
30-
}
31-
32-
wasm_cc_transition = transition(
33-
implementation = _wasm_cc_transition_impl,
34-
inputs = [],
35-
outputs = [
36-
"//command_line_option:cpu",
37-
"//command_line_option:crosstool_top",
38-
"//command_line_option:copt",
39-
"//command_line_option:cxxopt",
40-
"//command_line_option:fission",
41-
"//command_line_option:linkopt",
42-
"//command_line_option:collect_code_coverage",
43-
],
44-
)
45-
46-
def wasm_binary_impl(ctx):
47-
out = ctx.actions.declare_file(ctx.label.name)
48-
ctx.actions.run(
49-
executable = "cp",
50-
arguments = [ctx.files.binary[0].path, out.path],
51-
outputs = [out],
52-
inputs = ctx.files.binary,
53-
)
54-
55-
return [DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))]
56-
57-
def _wasm_attrs(transition):
58-
return {
59-
"binary": attr.label(mandatory = True, cfg = transition),
60-
"_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
61-
}
62-
63-
# Wasm binary rule implementation.
64-
# This copies the binary specified in binary attribute in Wasm configuration to
65-
# target configuration, so a binary in non-Wasm configuration can depend on them.
66-
wasm_cc_binary_rule = rule(
67-
implementation = wasm_binary_impl,
68-
attrs = _wasm_attrs(wasm_cc_transition),
69-
)
70-
7116
def wasm_cc_binary(**kwargs):
72-
fail("`wasm_cc_binary` is deprecated. Please use `proxy_wasm_cc_binary`.")
73-
74-
def proxy_wasm_cc_binary(name, additional_linker_inputs = [], linkopts = [], tags = [], deps = [], **kwargs):
75-
wasm_name = "_wasm_" + name
76-
kwargs.setdefault("visibility", ["//visibility:public"])
77-
cc_binary(
78-
name = wasm_name,
79-
additional_linker_inputs = additional_linker_inputs + [
80-
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js",
81-
],
82-
linkopts = linkopts + [
83-
"--no-entry",
84-
"--js-library=$(location @proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_js)",
85-
"-sSTANDALONE_WASM",
86-
"-sEXPORTED_FUNCTIONS=_malloc",
87-
],
88-
# Adding manual tag it won't be built in non-Wasm (e.g. x86_64 config)
89-
# when an wildcard is specified, but it will be built in Wasm configuration
90-
# when the wasm_binary below is built.
91-
tags = tags + [
92-
"manual",
93-
],
94-
deps = deps + [
95-
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics",
96-
],
97-
**kwargs
98-
)
17+
fail("`wasm_cc_binary` is deprecated. Please use `proxy_wasm_cc_binary` from `@proxy_wasm_cpp_sdk//bazel:defs.bzl`.")
9918

100-
wasm_cc_binary_rule(
101-
name = name,
102-
binary = ":" + wasm_name,
103-
tags = tags,
104-
)
19+
def proxy_wasm_cc_binary(**kwargs):
20+
fail("Please use `proxy_wasm_cc_binary` from `@proxy_wasm_cpp_sdk//bazel:defs.bzl`.")

emscripten-toolchain.BUILD

-8
This file was deleted.

example/BUILD

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
load("//bazel/wasm:wasm.bzl", "proxy_wasm_cc_binary")
1+
load("@proxy_wasm_cpp_sdk//bazel:defs.bzl", "proxy_wasm_cc_binary")
22

33
licenses(["notice"]) # Apache 2
44

55
proxy_wasm_cc_binary(
66
name = "http_wasm_example.wasm",
77
srcs = ["http_wasm_example.cc"],
8+
copts = ["-std=c++17"],
9+
)
10+
11+
proxy_wasm_cc_binary(
12+
name = "http_wasm_example_with_protobuf_lite.wasm",
13+
srcs = ["http_wasm_example.cc"],
14+
copts = ["-std=c++17"],
15+
protobuf = "lite",
16+
)
17+
18+
proxy_wasm_cc_binary(
19+
name = "http_wasm_example_with_protobuf_full.wasm",
20+
srcs = ["http_wasm_example.cc"],
21+
copts = ["-std=c++17"],
22+
protobuf = "full",
823
)

example/http_wasm_example.cc

+10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,17 @@ static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleCon
4646
"my_root_id");
4747

4848
bool ExampleRootContext::onStart(size_t) {
49+
#if defined(PROXY_WASM_PROTOBUF_FULL)
50+
LOG_TRACE("onStart with protobuf (full)");
51+
google::protobuf::Value value;
52+
value.set_string_value("unused");
53+
#elif defined(PROXY_WASM_PROTOBUF_LITE)
54+
LOG_TRACE("onStart with protobuf (lite)");
55+
google::protobuf::Value value;
56+
value.set_string_value("unused");
57+
#else
4958
LOG_TRACE("onStart");
59+
#endif
5060
return true;
5161
}
5262

0 commit comments

Comments
 (0)