blob: 79dabd02468bddff02da521445db06e1bdad6906 [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):
Jisi Liu3101e732015-10-16 12:46:26 -07004 if not ctx.attr.include:
Jisi Liu39362b32015-10-14 17:12:11 -07005 return ctx.label.package
6 if not ctx.label.package:
Jisi Liu3101e732015-10-16 12:46:26 -07007 return ctx.attr.include
8 return ctx.label.package + '/' + ctx.attr.include
Jisi Liu39362b32015-10-14 17:12:11 -07009
Jisi Liu25d75b52015-10-14 17:41:14 -070010def _cc_outs(srcs):
Jisi Liu39362b32015-10-14 17:12:11 -070011 return [s[:-len(".proto")] + ".pb.h" for s in srcs] + \
Jisi Liu125a91b2015-10-14 17:37:39 -070012 [s[:-len(".proto")] + ".pb.cc" for s in srcs]
Jisi Liu39362b32015-10-14 17:12:11 -070013
Jisi Liu25d75b52015-10-14 17:41:14 -070014def _py_outs(srcs):
Jisi Liu125a91b2015-10-14 17:37:39 -070015 return [s[:-len(".proto")] + "_pb2.py" for s in srcs]
Jisi Liu39362b32015-10-14 17:12:11 -070016
Jisi Liu9c7d9c02015-10-15 10:51:32 -070017def _proto_gen_impl(ctx):
18 """General implementation for generating protos"""
Jisi Liu39362b32015-10-14 17:12:11 -070019 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(
Jisi Liu125a91b2015-10-14 17:37:39 -070036 inputs=srcs + deps,
Jisi Liu39362b32015-10-14 17:12:11 -070037 outputs=ctx.outputs.outs,
Jisi Liu125a91b2015-10-14 17:37:39 -070038 arguments=args + import_flags + [s.path for s in srcs],
Jisi Liu9c7d9c02015-10-15 10:51:32 -070039 executable=ctx.executable.protoc,
Jisi Liu39362b32015-10-14 17:12:11 -070040 )
41
42 return struct(
43 proto=struct(
Jisi Liu125a91b2015-10-14 17:37:39 -070044 srcs=srcs,
45 import_flags=import_flags,
46 deps=deps,
47 ),
48 )
Jisi Liu39362b32015-10-14 17:12:11 -070049
Jisi Liu9c7d9c02015-10-15 10:51:32 -070050_proto_gen = 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"]),
Jisi Liu3101e732015-10-16 12:46:26 -070054 "include": attr.string(),
Jisi Liuee8131a2015-10-14 17:20:05 -070055 "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,
Jisi Liu9c7d9c02015-10-15 10:51:32 -070065 implementation = _proto_gen_impl,
Jisi Liu39362b32015-10-14 17:12:11 -070066)
67
68def cc_proto_library(
Jisi Liu125a91b2015-10-14 17:37:39 -070069 name,
70 srcs=[],
Jisi Liu125a91b2015-10-14 17:37:39 -070071 deps=[],
Jisi Liud8701b52015-10-16 11:44:21 -070072 cc_libs=[],
Jisi Liu3101e732015-10-16 12:46:26 -070073 include="",
74 protoc=":protoc",
75 internal_bootstrap_hack=False,
Jisi Liu125a91b2015-10-14 17:37:39 -070076 **kargs):
Jisi Liu3101e732015-10-16 12:46:26 -070077 """Bazel rule to create a C++ protobuf library from proto source files
78
79 Args:
80 name: the name of the cc_proto_library.
81 srcs: the .proto files of the cc_proto_library.
82 deps: a list of dependency labels; must be cc_proto_library.
83 cc_libs: a list of other cc_library targets depended by the generated
84 cc_library.
85 include: a string indicating the include path of the .proto files.
86 protoc: the label of the protocol compiler to generate the sources.
87 internal_bootstrap_hack: a flag indicate the cc_proto_library is used only
88 for bootstraping. When it is set to True, no files will be generated.
89 The rule will simply be a provider for .proto files, so that other
90 cc_proto_library can depend on it.
91 **kargs: other keyword arguments that are passed to cc_library.
92
93 """
Jisi Liu39362b32015-10-14 17:12:11 -070094
95 if internal_bootstrap_hack:
96 # For pre-checked-in generated files, we add the internal_bootstrap_hack
97 # which will skip the codegen action.
Jisi Liu9c7d9c02015-10-15 10:51:32 -070098 _proto_gen(
Jisi Liu125a91b2015-10-14 17:37:39 -070099 name=name + "_genproto",
100 srcs=srcs,
Jisi Liud8701b52015-10-16 11:44:21 -0700101 deps=[s + "_genproto" for s in deps],
Jisi Liu3101e732015-10-16 12:46:26 -0700102 include=include,
Jisi Liu125a91b2015-10-14 17:37:39 -0700103 protoc=protoc,
Jisi Liu39362b32015-10-14 17:12:11 -0700104 )
105 # An empty cc_library to make rule dependency consistent.
106 native.cc_library(
Jisi Liu125a91b2015-10-14 17:37:39 -0700107 name=name,
Jisi Liud8701b52015-10-16 11:44:21 -0700108 **kargs)
Jisi Liu39362b32015-10-14 17:12:11 -0700109 return
110
Jisi Liu25d75b52015-10-14 17:41:14 -0700111 outs = _cc_outs(srcs)
Jisi Liu9c7d9c02015-10-15 10:51:32 -0700112 _proto_gen(
Jisi Liu125a91b2015-10-14 17:37:39 -0700113 name=name + "_genproto",
114 srcs=srcs,
Jisi Liud8701b52015-10-16 11:44:21 -0700115 deps=[s + "_genproto" for s in deps],
Jisi Liu3101e732015-10-16 12:46:26 -0700116 include=include,
Jisi Liu125a91b2015-10-14 17:37:39 -0700117 protoc=protoc,
118 gen_cc=1,
119 outs=outs,
Jisi Liu39362b32015-10-14 17:12:11 -0700120 )
121
122 native.cc_library(
Jisi Liu125a91b2015-10-14 17:37:39 -0700123 name=name,
124 srcs=outs,
Jisi Liud8701b52015-10-16 11:44:21 -0700125 deps=cc_libs + deps,
Jisi Liu3101e732015-10-16 12:46:26 -0700126 includes=[include],
Jisi Liud8701b52015-10-16 11:44:21 -0700127 **kargs)