blob: a6ef20e10d9ed5ac7019813ade85852148ca1589 [file] [log] [blame]
Jisi Liu39362b32015-10-14 17:12:11 -07001# -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED
2
Jisi Liu39362b32015-10-14 17:12:11 -07003def _gen_dir(ctx):
4 if not ctx.attr.prefix:
5 return ctx.label.package
6 if not ctx.label.package:
7 return ctx.attr.prefix
8 return ctx.label.package + '/' + ctx.attr.prefix
9
10def CcOuts(srcs):
11 return [s[:-len(".proto")] + ".pb.h" for s in srcs] + \
12 [s[:-len(".proto")] + ".pb.cc" for s in srcs]
13
14def PyOuts(srcs):
15 return [s[:-len(".proto")] + "_pb2.py" for s in srcs]
16
17def _proto_srcs_impl(ctx):
18 """General implementation for calculating proto srcs"""
19 srcs = ctx.files.srcs
20 deps = []
21 deps += ctx.files.srcs
22 gen_dir = _gen_dir(ctx)
23 import_flags = ["-I" + gen_dir]
24 for dep in ctx.attr.deps:
25 import_flags += dep.proto.import_flags
26 deps += dep.proto.deps
27
28 args = []
29 if ctx.attr.gen_cc:
30 args += ["--cpp_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
31 if ctx.attr.gen_py:
32 args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
33
34 if args:
35 ctx.action(
36 inputs= srcs + deps,
37 outputs=ctx.outputs.outs,
38 arguments= args + import_flags + [s.path for s in srcs],
39 executable=ctx.executable.protoc
40 )
41
42 return struct(
43 proto=struct(
44 srcs = srcs,
45 import_flags = import_flags,
46 deps = deps,
47 ),
48 )
49
50_proto_srcs = rule(
Jisi Liu39362b32015-10-14 17:12:11 -070051 attrs = {
Jisi Liuee8131a2015-10-14 17:20:05 -070052 "srcs": attr.label_list(allow_files = True),
53 "deps": attr.label_list(providers = ["proto"]),
54 "prefix": attr.string(),
55 "protoc": attr.label(
56 executable = True,
57 single_file = True,
58 mandatory = True,
59 ),
60 "gen_cc": attr.bool(),
61 "gen_py": attr.bool(),
62 "outs": attr.output_list(),
63 },
64 output_to_genfiles = True,
65 implementation = _proto_srcs_impl,
Jisi Liu39362b32015-10-14 17:12:11 -070066)
67
68def cc_proto_library(
69 name,
70 srcs=[],
71 protoc=":protoc",
72 internal_bootstrap_hack=False,
73 prefix="",
74 proto_deps=[],
75 deps=[],
76 **kargs):
77
78 if internal_bootstrap_hack:
79 # For pre-checked-in generated files, we add the internal_bootstrap_hack
80 # which will skip the codegen action.
81 _proto_srcs(
82 name = name + "_genproto",
83 srcs = srcs,
84 deps = [s + "_genproto" for s in proto_deps],
85 prefix = prefix,
86 protoc = protoc,
87 )
88 # An empty cc_library to make rule dependency consistent.
89 native.cc_library(
90 name = name,
91 **kargs)
92 return
93
94 outs = CcOuts(srcs)
95 _proto_srcs(
96 name = name + "_genproto",
97 srcs = srcs,
98 deps = [s + "_genproto" for s in proto_deps],
99 prefix = prefix,
100 protoc = protoc,
101 gen_cc = 1,
102 outs = outs,
103 )
104
105 native.cc_library(
106 name = name,
107 srcs = outs,
108 deps = deps + proto_deps,
109 includes = [prefix],
110 **kargs)