Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 1 | # -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED |
| 2 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 3 | def _gen_dir(ctx): |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 4 | if not ctx.attr.include: |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 5 | return ctx.label.package |
| 6 | if not ctx.label.package: |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 7 | return ctx.attr.include |
| 8 | return ctx.label.package + '/' + ctx.attr.include |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 9 | |
Jisi Liu | 25d75b5 | 2015-10-14 17:41:14 -0700 | [diff] [blame] | 10 | def _cc_outs(srcs): |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 11 | return [s[:-len(".proto")] + ".pb.h" for s in srcs] + \ |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 12 | [s[:-len(".proto")] + ".pb.cc" for s in srcs] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 13 | |
Jisi Liu | 25d75b5 | 2015-10-14 17:41:14 -0700 | [diff] [blame] | 14 | def _py_outs(srcs): |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 15 | return [s[:-len(".proto")] + "_pb2.py" for s in srcs] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 16 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 17 | def _proto_gen_impl(ctx): |
| 18 | """General implementation for generating protos""" |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 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( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 36 | inputs=srcs + deps, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 37 | outputs=ctx.outputs.outs, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 38 | arguments=args + import_flags + [s.path for s in srcs], |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 39 | executable=ctx.executable.protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | return struct( |
| 43 | proto=struct( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 44 | srcs=srcs, |
| 45 | import_flags=import_flags, |
| 46 | deps=deps, |
| 47 | ), |
| 48 | ) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 49 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 50 | _proto_gen = rule( |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 51 | attrs = { |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 52 | "srcs": attr.label_list(allow_files = True), |
| 53 | "deps": attr.label_list(providers = ["proto"]), |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 54 | "include": attr.string(), |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 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, |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 65 | implementation = _proto_gen_impl, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 66 | ) |
| 67 | |
| 68 | def cc_proto_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 69 | name, |
| 70 | srcs=[], |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 71 | deps=[], |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 72 | cc_libs=[], |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 73 | include="", |
| 74 | protoc=":protoc", |
| 75 | internal_bootstrap_hack=False, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 76 | **kargs): |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 77 | """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 Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 94 | |
| 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 Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 98 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 99 | name=name + "_genproto", |
| 100 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 101 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 102 | include=include, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 103 | protoc=protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 104 | ) |
| 105 | # An empty cc_library to make rule dependency consistent. |
| 106 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 107 | name=name, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 108 | **kargs) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 109 | return |
| 110 | |
Jisi Liu | 25d75b5 | 2015-10-14 17:41:14 -0700 | [diff] [blame] | 111 | outs = _cc_outs(srcs) |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 112 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 113 | name=name + "_genproto", |
| 114 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 115 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 116 | include=include, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 117 | protoc=protoc, |
| 118 | gen_cc=1, |
| 119 | outs=outs, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 120 | ) |
| 121 | |
| 122 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 123 | name=name, |
| 124 | srcs=outs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 125 | deps=cc_libs + deps, |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame^] | 126 | includes=[include], |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 127 | **kargs) |