-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathjs_capnp_library.bzl
123 lines (107 loc) · 4.11 KB
/
js_capnp_library.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
117
118
119
120
121
122
123
"""
Bazel rule to compile .capnp files into JavaScript using capnp-ts.
Based on https://github.com/capnproto/capnproto/blob/3b2e368cecc4b1419b40c5970d74a7a342224fac/c++/src/capnp/cc_capnp_library.bzl.
"""
load("@aspect_rules_js//js:defs.bzl", "js_library")
capnp_provider = provider("Capnproto Provider", fields = {
"includes": "includes for this target (transitive)",
"inputs": "src + data for the target",
"src_prefix": "src_prefix of the target",
})
def _workspace_path(label, path):
if label.workspace_root == "":
return path
return label.workspace_root + "/" + path
def _capnp_gen_impl(ctx):
label = ctx.label
src_prefix = _workspace_path(label, ctx.attr.src_prefix)
includes = []
inputs = ctx.files.srcs + ctx.files.data
for dep_target in ctx.attr.deps:
includes += dep_target[capnp_provider].includes
inputs += dep_target[capnp_provider].inputs
if src_prefix != "":
includes.append(src_prefix)
system_include = ctx.files._capnp_system[0].dirname.removesuffix("/capnp")
out_dir = ctx.var["GENDIR"]
if src_prefix != "":
out_dir = out_dir + "/" + src_prefix
js_out = "-o%s:%s" % (ctx.executable._capnpc_es.path, out_dir)
args = ctx.actions.args()
args.add_all(["compile", "--verbose", js_out])
args.add_all(["-I" + inc for inc in includes])
args.add_all(["-I", system_include])
if src_prefix != "":
args.add_all(["--src-prefix", src_prefix])
args.add_all([s for s in ctx.files.srcs])
ctx.actions.run(
inputs = inputs + ctx.files._capnpc_es + ctx.files._capnpc_capnp + ctx.files._capnp_system,
tools = [ctx.executable._capnpc_es], # Include required js_binary runfiles
outputs = ctx.outputs.outs,
executable = ctx.executable._capnpc,
arguments = [args],
mnemonic = "GenCapnp",
)
return [
capnp_provider(
includes = includes,
inputs = inputs,
src_prefix = src_prefix,
),
]
_capnp_gen = rule(
attrs = {
"srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(providers = [capnp_provider]),
"data": attr.label_list(allow_files = True),
"outs": attr.output_list(),
"src_prefix": attr.string(),
"_capnpc": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "@capnp-cpp//src/capnp:capnp_tool"),
"_capnpc_es": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "//:capnpc_es"),
"_capnpc_capnp": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "@capnp-cpp//src/capnp:capnpc-capnp"),
"_capnp_system": attr.label(default = "@capnp-cpp//src/capnp:capnp_system_library"),
},
output_to_genfiles = True,
implementation = _capnp_gen_impl,
)
def js_capnp_library(
name,
srcs = [],
data = [],
outs = [],
deps = [],
src_prefix = "",
visibility = None,
target_compatible_with = None,
**kwargs):
"""Bazel rule to create a JavaScript capnproto library from capnp source files
Args:
name: library name
srcs: list of files to compile
data: additional files to provide to the compiler - data files and includes that need not to
be compiled
outs: expected output files
deps: other js_capnp_library rules to depend on
src_prefix: src_prefix for capnp compiler to the source root
visibility: rule visibility
target_compatible_with: target compatibility
**kwargs: rest of the arguments to js_library rule
"""
_capnp_gen(
name = name + "_gen",
srcs = srcs,
deps = [s + "_gen" for s in deps],
data = data,
outs = outs,
src_prefix = src_prefix,
visibility = visibility,
target_compatible_with = target_compatible_with,
)
js_library(
name = name,
srcs = outs,
deps = deps + ["//:node_modules/capnp-es"],
visibility = visibility,
target_compatible_with = target_compatible_with,
**kwargs
)