forked from proxy-wasm/proxy-wasm-cpp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefs.bzl
116 lines (105 loc) · 3.74 KB
/
defs.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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,
)