forked from buildbarn/go-xdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdr.bzl
55 lines (50 loc) · 1.69 KB
/
xdr.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
XDRLibrary = provider()
def _go_xdr_library_impl(ctx):
import_root = (
ctx.file.src.root.path + ctx.label.workspace_root + ctx.attr.strip_import_prefix
)
import_root_prefix = import_root
if import_root_prefix:
import_root_prefix += "/"
if not ctx.file.src.path.startswith(import_root_prefix):
fail(
"Source path %s does not start with import root %s" %
(ctx.file.src.path, import_root_prefix),
)
short_src_path = ctx.file.src.path[len(import_root_prefix):]
all_srcs = depset(
direct = [ctx.file.src],
transitive = [dep[XDRLibrary].srcs for dep in ctx.attr.deps],
)
import_roots = depset(
direct = [import_root],
transitive = [dep[XDRLibrary].import_roots for dep in ctx.attr.deps],
)
out = ctx.actions.declare_file(ctx.attr.name + ".go")
args = ctx.actions.args()
args.add_all(import_roots, before_each = "-I")
args.add(short_src_path)
args.add(out.path)
ctx.actions.run(
inputs = all_srcs,
outputs = [out],
executable = ctx.executable._xdr_compiler,
arguments = [args],
)
return [
DefaultInfo(files = depset([out])),
XDRLibrary(srcs = all_srcs, import_roots = import_roots),
]
go_xdr_library = rule(
implementation = _go_xdr_library_impl,
attrs = {
"_xdr_compiler": attr.label(
default = "@com_github_buildbarn_go_xdr//cmd/xdr_compiler",
executable = True,
cfg = "host",
),
"src": attr.label(allow_single_file = True),
"deps": attr.label_list(providers = [XDRLibrary]),
"strip_import_prefix": attr.string(default = ""),
},
)