Skip to content

Commit 2ccab8d

Browse files
authored
Add wasm_cc_binary rule. (#68)
Signed-off-by: Pengyuan Bian <[email protected]>
1 parent 4204345 commit 2ccab8d

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

bazel/wasm/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
licenses(["notice"]) # Apache 2

bazel/wasm/wasm.bzl

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2016-2020 Envoy Project Authors
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
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+
71+
def wasm_cc_binary(name, tags = [], **kwargs):
72+
wasm_name = "_wasm_" + name
73+
kwargs.setdefault("visibility", ["//visibility:public"])
74+
cc_binary(
75+
name = wasm_name,
76+
# Adding manual tag it won't be built in non-Wasm (e.g. x86_64 config)
77+
# when an wildcard is specified, but it will be built in Wasm configuration
78+
# when the wasm_binary below is built.
79+
tags = ["manual"],
80+
**kwargs
81+
)
82+
83+
wasm_cc_binary_rule(
84+
name = name,
85+
binary = ":" + wasm_name,
86+
tags = tags,
87+
)

example/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("//bazel/wasm:wasm.bzl", "wasm_cc_binary")
2+
3+
licenses(["notice"]) # Apache 2
4+
5+
wasm_cc_binary(
6+
name = "http_wasm_example.wasm",
7+
srcs = ["http_wasm_example.cc"],
8+
deps = [
9+
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics",
10+
],
11+
)

example/http_wasm_example.cc

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2016-2020 Envoy Project Authors
2+
// Copyright 2020 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include <string>
17+
#include <string_view>
18+
#include <unordered_map>
19+
20+
#include "proxy_wasm_intrinsics.h"
21+
22+
class ExampleRootContext : public RootContext {
23+
public:
24+
explicit ExampleRootContext(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {}
25+
26+
bool onStart(size_t) override;
27+
bool onConfigure(size_t) override;
28+
void onTick() override;
29+
};
30+
31+
class ExampleContext : public Context {
32+
public:
33+
explicit ExampleContext(uint32_t id, RootContext *root) : Context(id, root) {}
34+
35+
void onCreate() override;
36+
FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) override;
37+
FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) override;
38+
FilterHeadersStatus onResponseHeaders(uint32_t headers, bool end_of_stream) override;
39+
FilterDataStatus onResponseBody(size_t body_buffer_length, bool end_of_stream) override;
40+
void onDone() override;
41+
void onLog() override;
42+
void onDelete() override;
43+
};
44+
static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleContext),
45+
ROOT_FACTORY(ExampleRootContext),
46+
"my_root_id");
47+
48+
bool ExampleRootContext::onStart(size_t) {
49+
LOG_TRACE("onStart");
50+
return true;
51+
}
52+
53+
bool ExampleRootContext::onConfigure(size_t) {
54+
LOG_TRACE("onConfigure");
55+
proxy_set_tick_period_milliseconds(1000); // 1 sec
56+
return true;
57+
}
58+
59+
void ExampleRootContext::onTick() { LOG_TRACE("onTick"); }
60+
61+
void ExampleContext::onCreate() { LOG_WARN(std::string("onCreate " + std::to_string(id()))); }
62+
63+
FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t, bool) {
64+
LOG_DEBUG(std::string("onRequestHeaders ") + std::to_string(id()));
65+
auto result = getRequestHeaderPairs();
66+
auto pairs = result->pairs();
67+
LOG_INFO(std::string("headers: ") + std::to_string(pairs.size()));
68+
for (auto &p : pairs) {
69+
LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second));
70+
}
71+
return FilterHeadersStatus::Continue;
72+
}
73+
74+
FilterHeadersStatus ExampleContext::onResponseHeaders(uint32_t, bool) {
75+
LOG_DEBUG(std::string("onResponseHeaders ") + std::to_string(id()));
76+
auto result = getResponseHeaderPairs();
77+
auto pairs = result->pairs();
78+
LOG_INFO(std::string("headers: ") + std::to_string(pairs.size()));
79+
for (auto &p : pairs) {
80+
LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second));
81+
}
82+
addResponseHeader("X-Wasm-custom", "FOO");
83+
replaceResponseHeader("content-type", "text/plain; charset=utf-8");
84+
removeResponseHeader("content-length");
85+
return FilterHeadersStatus::Continue;
86+
}
87+
88+
FilterDataStatus ExampleContext::onRequestBody(size_t body_buffer_length,
89+
bool /* end_of_stream */) {
90+
auto body = getBufferBytes(WasmBufferType::HttpRequestBody, 0, body_buffer_length);
91+
LOG_ERROR(std::string("onRequestBody ") + std::string(body->view()));
92+
return FilterDataStatus::Continue;
93+
}
94+
95+
FilterDataStatus ExampleContext::onResponseBody(size_t /* body_buffer_length */,
96+
bool /* end_of_stream */) {
97+
setBuffer(WasmBufferType::HttpResponseBody, 0, 12, "Hello, world");
98+
return FilterDataStatus::Continue;
99+
}
100+
101+
void ExampleContext::onDone() { LOG_WARN(std::string("onDone " + std::to_string(id()))); }
102+
103+
void ExampleContext::onLog() { LOG_WARN(std::string("onLog " + std::to_string(id()))); }
104+
105+
void ExampleContext::onDelete() { LOG_WARN(std::string("onDelete " + std::to_string(id()))); }

0 commit comments

Comments
 (0)