Skip to content
This repository was archived by the owner on Dec 16, 2020. It is now read-only.

Commit 19589f5

Browse files
authored
Build rust wasm data with Bazel (#516)
Signed-off-by: Shikugawa <[email protected]> Signed-off-by: Lizan Zhou <[email protected]>
1 parent 5b099fb commit 19589f5

File tree

9 files changed

+105
-30
lines changed

9 files changed

+105
-30
lines changed

.bazelrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ build:libc++ --config=clang
104104
build:libc++ --action_env=CXXFLAGS=-stdlib=libc++
105105
build:libc++ --action_env=LDFLAGS=-stdlib=libc++
106106
build:libc++ --action_env=BAZEL_CXXOPTS=-stdlib=libc++
107-
build:libc++ --action_env=BAZEL_LINKLIBS=-l%:libc++.a:-l%:libc++abi.a:-lm
107+
build:libc++ --action_env=BAZEL_LINKLIBS=-l%:libc++.a:-l%:libc++abi.a
108+
build:libc++ --action_env=BAZEL_LINKOPTS=-lm:-pthread
108109
build:libc++ --define force_libcpp=enabled
109110

110111
# Optimize build for binary size reduction.
@@ -180,6 +181,8 @@ build:remote --spawn_strategy=remote,sandboxed,local
180181
build:remote --strategy=Javac=remote,sandboxed,local
181182
build:remote --strategy=Closure=remote,sandboxed,local
182183
build:remote --strategy=Genrule=remote,sandboxed,local
184+
# rules_rust is not remote runnable (yet)
185+
build:remote --strategy_regexp=_wasm_.*_rust=sandboxed,local
183186
build:remote --remote_timeout=7200
184187
build:remote --auth_enabled=true
185188
build:remote --remote_download_toplevel

bazel/dependency_imports.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ load("@bazel_toolchains//rules/exec_properties:exec_properties.bzl", "create_rbe
55
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
66
load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies")
77
load("@upb//bazel:repository_defs.bzl", upb_bazel_version_repository = "bazel_version_repository")
8+
load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")
9+
load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
810
load("@config_validation_pip3//:requirements.bzl", config_validation_pip_install = "pip_install")
911
load("@protodoc_pip3//:requirements.bzl", protodoc_pip_install = "pip_install")
1012
load("@rules_antlr//antlr:deps.bzl", "antlr_dependencies")
@@ -19,6 +21,8 @@ def envoy_dependency_imports(go_version = GO_VERSION):
1921
rbe_toolchains_config()
2022
gazelle_dependencies()
2123
apple_rules_dependencies()
24+
rust_repositories()
25+
bazel_version(name = "bazel_version")
2226
upb_bazel_version_repository(name = "upb_bazel_version")
2327
antlr_dependencies(471)
2428

bazel/repositories.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ def _go_deps(skip_targets):
136136
)
137137
_repository_impl("bazel_gazelle")
138138

139+
def _rust_deps():
140+
_repository_impl("io_bazel_rules_rust")
141+
139142
def envoy_dependencies(skip_targets = []):
140143
# Setup Envoy developer tools.
141144
envoy_dev_binding()
@@ -212,6 +215,7 @@ def envoy_dependencies(skip_targets = []):
212215
_python_deps()
213216
_cc_deps()
214217
_go_deps(skip_targets)
218+
_rust_deps()
215219
_kafka_deps()
216220

217221
_org_llvm_llvm()

bazel/repository_locations.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ DEPENDENCY_REPOSITORIES = dict(
529529
urls = ["https://github.com/emscripten-core/emsdk/archive/1.39.6.tar.gz"],
530530
use_category = ["build"],
531531
),
532+
io_bazel_rules_rust = dict(
533+
sha256 = "484a2b2b67cd2d1fa1054876de7f8d291c4b203fd256bc8cbea14d749bb864ce",
534+
# Last commit where "out_binary = True" works.
535+
# See: https://github.com/bazelbuild/rules_rust/issues/386
536+
strip_prefix = "rules_rust-fda9a1ce6482973adfda022cadbfa6b300e269c3",
537+
urls = ["https://github.com/bazelbuild/rules_rust/archive/fda9a1ce6482973adfda022cadbfa6b300e269c3.tar.gz"],
538+
use_category = ["test"],
539+
),
532540
rules_antlr = dict(
533541
sha256 = "7249d1569293d9b239e23c65f6b4c81a07da921738bde0dfeb231ed98be40429",
534542
strip_prefix = "rules_antlr-3cc2f9502a54ceb7b79b37383316b23c4da66f9a",

bazel/wasm/wasm.bzl

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")
12
load("@rules_cc//cc:defs.bzl", "cc_binary")
23

3-
def _wasm_transition_impl(settings, attr):
4+
def _wasm_cc_transition_impl(settings, attr):
45
return {
56
"//command_line_option:cpu": "wasm32",
67
"//command_line_option:crosstool_top": "@proxy_wasm_cpp_sdk//toolchain:emscripten",
@@ -13,8 +14,13 @@ def _wasm_transition_impl(settings, attr):
1314
"//command_line_option:collect_code_coverage": "false",
1415
}
1516

16-
wasm_transition = transition(
17-
implementation = _wasm_transition_impl,
17+
def _wasm_rust_transition_impl(settings, attr):
18+
return {
19+
"//command_line_option:platforms": "@io_bazel_rules_rust//rust/platform:wasm",
20+
}
21+
22+
wasm_cc_transition = transition(
23+
implementation = _wasm_cc_transition_impl,
1824
inputs = [],
1925
outputs = [
2026
"//command_line_option:cpu",
@@ -26,25 +32,53 @@ wasm_transition = transition(
2632
],
2733
)
2834

35+
wasm_rust_transition = transition(
36+
implementation = _wasm_rust_transition_impl,
37+
inputs = [],
38+
outputs = [
39+
"//command_line_option:platforms",
40+
],
41+
)
42+
2943
def _wasm_binary_impl(ctx):
3044
out = ctx.actions.declare_file(ctx.label.name)
31-
ctx.actions.run_shell(
32-
command = 'cp "{}" "{}"'.format(ctx.files.binary[0].path, out.path),
33-
outputs = [out],
34-
inputs = ctx.files.binary,
35-
)
45+
if ctx.attr.precompile:
46+
ctx.actions.run(
47+
executable = ctx.executable._compile_tool,
48+
arguments = [ctx.files.binary[0].path, out.path],
49+
outputs = [out],
50+
inputs = ctx.files.binary,
51+
)
52+
else:
53+
ctx.actions.run(
54+
executable = "cp",
55+
arguments = [ctx.files.binary[0].path, out.path],
56+
outputs = [out],
57+
inputs = ctx.files.binary,
58+
)
3659

37-
return [DefaultInfo(runfiles = ctx.runfiles([out]))]
60+
return [DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))]
61+
62+
def _wasm_attrs(transition):
63+
return {
64+
"binary": attr.label(mandatory = True, cfg = transition),
65+
"precompile": attr.bool(default = False),
66+
# This is deliberately in target configuration to avoid compiling v8 twice.
67+
"_compile_tool": attr.label(default = "@envoy//test/tools/wee8_compile:wee8_compile_tool", executable = True, cfg = "target"),
68+
"_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
69+
}
3870

3971
# WASM binary rule implementation.
4072
# This copies the binary specified in binary attribute in WASM configuration to
4173
# target configuration, so a binary in non-WASM configuration can depend on them.
42-
wasm_binary = rule(
74+
wasm_cc_binary_rule = rule(
4375
implementation = _wasm_binary_impl,
44-
attrs = {
45-
"binary": attr.label(mandatory = True, cfg = wasm_transition),
46-
"_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
47-
},
76+
attrs = _wasm_attrs(wasm_cc_transition),
77+
)
78+
79+
wasm_rust_binary_rule = rule(
80+
implementation = _wasm_binary_impl,
81+
attrs = _wasm_attrs(wasm_rust_transition),
4882
)
4983

5084
def wasm_cc_binary(name, **kwargs):
@@ -61,7 +95,29 @@ def wasm_cc_binary(name, **kwargs):
6195
**kwargs
6296
)
6397

64-
wasm_binary(
98+
wasm_cc_binary_rule(
99+
name = name,
100+
binary = ":" + wasm_name,
101+
)
102+
103+
def wasm_rust_binary(name, **kwargs):
104+
wasm_name = "_wasm_" + (name if not ".wasm" in name else name.strip(".wasm"))
105+
kwargs.setdefault("visibility", ["//visibility:public"])
106+
107+
rust_binary(
108+
name = wasm_name,
109+
edition = "2018",
110+
crate_type = "cdylib",
111+
out_binary = True,
112+
tags = ["manual"],
113+
**kwargs
114+
)
115+
116+
wasm_rust_binary_rule(
65117
name = name,
118+
precompile = select({
119+
"@envoy//bazel:linux_x86_64": True,
120+
"//conditions:default": False,
121+
}),
66122
binary = ":" + wasm_name,
67123
)

test/extensions/common/wasm/BUILD

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ envoy_cc_test(
1212
name = "wasm_vm_test",
1313
srcs = ["wasm_vm_test.cc"],
1414
data = [
15-
"//test/extensions/common/wasm/test_data:modules",
15+
"//test/extensions/common/wasm/test_data:test_rust.wasm",
16+
],
17+
tags = [
18+
# wasm (wee v8 etc) will not compile on Windows
19+
"skip_on_windows",
1620
],
17-
# wasm (wee v8 etc) will not compile on Windows
18-
tags = ["skip_on_windows"],
1921
deps = [
2022
"//source/extensions/common/wasm:wasm_lib",
2123
"//test/test_common:environment_lib",

test/extensions/common/wasm/test_data/BUILD

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ load(
33
"envoy_cc_library",
44
"envoy_package",
55
)
6-
load("//bazel/wasm:wasm.bzl", "wasm_cc_binary")
6+
load("//bazel/wasm:wasm.bzl", "wasm_cc_binary", "wasm_rust_binary")
77

88
licenses(["notice"]) # Apache 2
99

1010
envoy_package()
1111

12-
filegroup(
13-
name = "modules",
14-
srcs = glob(["*.wasm"]),
15-
data = [
16-
":test_cpp.wasm",
17-
],
12+
wasm_rust_binary(
13+
name = "test_rust.wasm",
14+
srcs = ["test_rust.rs"],
15+
rustc_flags = ["-Clink-arg=-zstack-size=32768"],
1816
)
1917

2018
envoy_cc_library(
Binary file not shown.

test/tools/wee8_compile/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
load(
22
"//bazel:envoy_build_system.bzl",
3-
"envoy_cc_test_binary",
4-
"envoy_cc_test_library",
3+
"envoy_cc_binary",
4+
"envoy_cc_library",
55
"envoy_package",
66
)
77

88
licenses(["notice"]) # Apache 2
99

1010
envoy_package()
1111

12-
envoy_cc_test_binary(
12+
envoy_cc_binary(
1313
name = "wee8_compile_tool",
1414
deps = [":wee8_compile_lib"],
1515
)
1616

17-
envoy_cc_test_library(
17+
envoy_cc_library(
1818
name = "wee8_compile_lib",
1919
srcs = ["wee8_compile.cc"],
2020
external_deps = ["wee8"],

0 commit comments

Comments
 (0)