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 | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame^] | 4 | if ctx.attr.include == None: |
| 5 | return "" |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 6 | if not ctx.attr.include: |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 7 | return ctx.label.package |
| 8 | if not ctx.label.package: |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 9 | return ctx.attr.include |
| 10 | return ctx.label.package + '/' + ctx.attr.include |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 11 | |
Jisi Liu | 25d75b5 | 2015-10-14 17:41:14 -0700 | [diff] [blame] | 12 | def _cc_outs(srcs): |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 13 | return [s[:-len(".proto")] + ".pb.h" for s in srcs] + \ |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 14 | [s[:-len(".proto")] + ".pb.cc" for s in srcs] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 15 | |
Jisi Liu | 25d75b5 | 2015-10-14 17:41:14 -0700 | [diff] [blame] | 16 | def _py_outs(srcs): |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 17 | return [s[:-len(".proto")] + "_pb2.py" for s in srcs] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 18 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 19 | def _proto_gen_impl(ctx): |
| 20 | """General implementation for generating protos""" |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 21 | srcs = ctx.files.srcs |
| 22 | deps = [] |
| 23 | deps += ctx.files.srcs |
| 24 | gen_dir = _gen_dir(ctx) |
| 25 | import_flags = ["-I" + gen_dir] |
| 26 | for dep in ctx.attr.deps: |
| 27 | import_flags += dep.proto.import_flags |
| 28 | deps += dep.proto.deps |
| 29 | |
| 30 | args = [] |
| 31 | if ctx.attr.gen_cc: |
| 32 | args += ["--cpp_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
| 33 | if ctx.attr.gen_py: |
| 34 | args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
| 35 | |
| 36 | if args: |
| 37 | ctx.action( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 38 | inputs=srcs + deps, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 39 | outputs=ctx.outputs.outs, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 40 | arguments=args + import_flags + [s.path for s in srcs], |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 41 | executable=ctx.executable.protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 42 | ) |
| 43 | |
| 44 | return struct( |
| 45 | proto=struct( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 46 | srcs=srcs, |
| 47 | import_flags=import_flags, |
| 48 | deps=deps, |
| 49 | ), |
| 50 | ) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 51 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 52 | _proto_gen = rule( |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 53 | attrs = { |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 54 | "srcs": attr.label_list(allow_files = True), |
| 55 | "deps": attr.label_list(providers = ["proto"]), |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 56 | "include": attr.string(), |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 57 | "protoc": attr.label( |
| 58 | executable = True, |
| 59 | single_file = True, |
| 60 | mandatory = True, |
| 61 | ), |
| 62 | "gen_cc": attr.bool(), |
| 63 | "gen_py": attr.bool(), |
| 64 | "outs": attr.output_list(), |
| 65 | }, |
| 66 | output_to_genfiles = True, |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 67 | implementation = _proto_gen_impl, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 68 | ) |
| 69 | |
| 70 | def cc_proto_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 71 | name, |
| 72 | srcs=[], |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 73 | deps=[], |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 74 | cc_libs=[], |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame^] | 75 | include=None, |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 76 | protoc=":protoc", |
| 77 | internal_bootstrap_hack=False, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 78 | **kargs): |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 79 | """Bazel rule to create a C++ protobuf library from proto source files |
| 80 | |
| 81 | Args: |
| 82 | name: the name of the cc_proto_library. |
| 83 | srcs: the .proto files of the cc_proto_library. |
| 84 | deps: a list of dependency labels; must be cc_proto_library. |
| 85 | cc_libs: a list of other cc_library targets depended by the generated |
| 86 | cc_library. |
| 87 | include: a string indicating the include path of the .proto files. |
| 88 | protoc: the label of the protocol compiler to generate the sources. |
| 89 | internal_bootstrap_hack: a flag indicate the cc_proto_library is used only |
| 90 | for bootstraping. When it is set to True, no files will be generated. |
| 91 | The rule will simply be a provider for .proto files, so that other |
| 92 | cc_proto_library can depend on it. |
| 93 | **kargs: other keyword arguments that are passed to cc_library. |
| 94 | |
| 95 | """ |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 96 | |
| 97 | if internal_bootstrap_hack: |
| 98 | # For pre-checked-in generated files, we add the internal_bootstrap_hack |
| 99 | # which will skip the codegen action. |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 100 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 101 | name=name + "_genproto", |
| 102 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 103 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 104 | include=include, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 105 | protoc=protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 106 | ) |
| 107 | # An empty cc_library to make rule dependency consistent. |
| 108 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 109 | name=name, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 110 | **kargs) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 111 | return |
| 112 | |
Jisi Liu | 25d75b5 | 2015-10-14 17:41:14 -0700 | [diff] [blame] | 113 | outs = _cc_outs(srcs) |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 114 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 115 | name=name + "_genproto", |
| 116 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 117 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 118 | include=include, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 119 | protoc=protoc, |
| 120 | gen_cc=1, |
| 121 | outs=outs, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 122 | ) |
| 123 | |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame^] | 124 | includes = [] |
| 125 | if include != None: |
| 126 | includes = [include] |
| 127 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 128 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 129 | name=name, |
| 130 | srcs=outs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 131 | deps=cc_libs + deps, |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame^] | 132 | includes=includes, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 133 | **kargs) |