Damien Martin-Guillerez | 76547e5 | 2016-01-15 14:01:37 +0100 | [diff] [blame] | 1 | def _GetPath(ctx, path): |
| 2 | if ctx.label.workspace_root: |
| 3 | return ctx.label.workspace_root + '/' + path |
| 4 | else: |
| 5 | return path |
| 6 | |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 7 | def _IsNewExternal(ctx): |
| 8 | # Bazel 0.4.4 and older have genfiles paths that look like: |
| 9 | # bazel-out/local-fastbuild/genfiles/external/repo/foo |
| 10 | # After the exec root rearrangement, they look like: |
| 11 | # ../repo/bazel-out/local-fastbuild/genfiles/foo |
| 12 | return ctx.label.workspace_root.startswith("../") |
| 13 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 14 | def _GenDir(ctx): |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 15 | if _IsNewExternal(ctx): |
| 16 | # We are using the fact that Bazel 0.4.4+ provides repository-relative paths |
| 17 | # for ctx.genfiles_dir. |
| 18 | return ctx.genfiles_dir.path + ( |
| 19 | "/" + ctx.attr.includes[0] if ctx.attr.includes and ctx.attr.includes[0] else "") |
| 20 | # This means that we're either in the old version OR the new version in the local repo. |
| 21 | # Either way, appending the source path to the genfiles dir works. |
| 22 | return ctx.var["GENDIR"] + "/" + _SourceDir(ctx) |
| 23 | |
| 24 | def _SourceDir(ctx): |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 25 | if not ctx.attr.includes: |
Damien Martin-Guillerez | 76547e5 | 2016-01-15 14:01:37 +0100 | [diff] [blame] | 26 | return ctx.label.workspace_root |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 27 | if not ctx.attr.includes[0]: |
Damien Martin-Guillerez | 76547e5 | 2016-01-15 14:01:37 +0100 | [diff] [blame] | 28 | return _GetPath(ctx, ctx.label.package) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 29 | if not ctx.label.package: |
Damien Martin-Guillerez | 76547e5 | 2016-01-15 14:01:37 +0100 | [diff] [blame] | 30 | return _GetPath(ctx, ctx.attr.includes[0]) |
| 31 | return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0]) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 32 | |
Andreas Bergmeier | bbeb983 | 2016-08-15 16:57:30 +0200 | [diff] [blame] | 33 | def _CcHdrs(srcs, use_grpc_plugin=False): |
| 34 | ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] |
Manjunath Kudlur | f5c7363 | 2016-02-25 08:50:50 -0800 | [diff] [blame] | 35 | if use_grpc_plugin: |
Andreas Bergmeier | bbeb983 | 2016-08-15 16:57:30 +0200 | [diff] [blame] | 36 | ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] |
Manjunath Kudlur | f5c7363 | 2016-02-25 08:50:50 -0800 | [diff] [blame] | 37 | return ret |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 38 | |
Andreas Bergmeier | bbeb983 | 2016-08-15 16:57:30 +0200 | [diff] [blame] | 39 | def _CcSrcs(srcs, use_grpc_plugin=False): |
| 40 | ret = [s[:-len(".proto")] + ".pb.cc" for s in srcs] |
| 41 | if use_grpc_plugin: |
| 42 | ret += [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs] |
| 43 | return ret |
| 44 | |
| 45 | def _CcOuts(srcs, use_grpc_plugin=False): |
| 46 | return _CcHdrs(srcs, use_grpc_plugin) + _CcSrcs(srcs, use_grpc_plugin) |
| 47 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 48 | def _PyOuts(srcs): |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 49 | return [s[:-len(".proto")] + "_pb2.py" for s in srcs] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 50 | |
David Z. Chen | 02cd45c | 2016-05-20 16:49:04 -0700 | [diff] [blame] | 51 | def _RelativeOutputPath(path, include, dest=""): |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 52 | if include == None: |
| 53 | return path |
| 54 | |
| 55 | if not path.startswith(include): |
| 56 | fail("Include path %s isn't part of the path %s." % (include, path)) |
| 57 | |
| 58 | if include and include[-1] != '/': |
| 59 | include = include + '/' |
David Z. Chen | 02cd45c | 2016-05-20 16:49:04 -0700 | [diff] [blame] | 60 | if dest and dest[-1] != '/': |
| 61 | dest = dest + '/' |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 62 | |
| 63 | path = path[len(include):] |
David Z. Chen | 02cd45c | 2016-05-20 16:49:04 -0700 | [diff] [blame] | 64 | return dest + path |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 65 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 66 | def _proto_gen_impl(ctx): |
| 67 | """General implementation for generating protos""" |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 68 | srcs = ctx.files.srcs |
| 69 | deps = [] |
| 70 | deps += ctx.files.srcs |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 71 | source_dir = _SourceDir(ctx) |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 72 | gen_dir = _GenDir(ctx) |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 73 | if source_dir: |
| 74 | import_flags = ["-I" + source_dir, "-I" + gen_dir] |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 75 | else: |
| 76 | import_flags = ["-I."] |
| 77 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 78 | for dep in ctx.attr.deps: |
| 79 | import_flags += dep.proto.import_flags |
| 80 | deps += dep.proto.deps |
| 81 | |
| 82 | args = [] |
| 83 | if ctx.attr.gen_cc: |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 84 | args += ["--cpp_out=" + gen_dir] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 85 | if ctx.attr.gen_py: |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 86 | args += ["--python_out=" + gen_dir] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 87 | |
Florian Weikert | c2b3e70 | 2016-10-12 14:03:07 +0200 | [diff] [blame] | 88 | inputs = srcs + deps |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 89 | if ctx.executable.plugin: |
| 90 | plugin = ctx.executable.plugin |
| 91 | lang = ctx.attr.plugin_language |
| 92 | if not lang and plugin.basename.startswith('protoc-gen-'): |
| 93 | lang = plugin.basename[len('protoc-gen-'):] |
| 94 | if not lang: |
| 95 | fail("cannot infer the target language of plugin", "plugin_language") |
| 96 | |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 97 | outdir = gen_dir |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 98 | if ctx.attr.plugin_options: |
| 99 | outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir |
| 100 | args += ["--plugin=protoc-gen-%s=%s" % (lang, plugin.path)] |
| 101 | args += ["--%s_out=%s" % (lang, outdir)] |
Florian Weikert | c2b3e70 | 2016-10-12 14:03:07 +0200 | [diff] [blame] | 102 | inputs += [plugin] |
Manjunath Kudlur | f0966a7 | 2016-02-22 14:30:43 -0800 | [diff] [blame] | 103 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 104 | if args: |
| 105 | ctx.action( |
Florian Weikert | c2b3e70 | 2016-10-12 14:03:07 +0200 | [diff] [blame] | 106 | inputs=inputs, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 107 | outputs=ctx.outputs.outs, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 108 | arguments=args + import_flags + [s.path for s in srcs], |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 109 | executable=ctx.executable.protoc, |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 110 | mnemonic="ProtoCompile", |
Marco A. Harrendorf | dd04ffb | 2017-04-03 17:01:36 +0200 | [diff] [blame] | 111 | use_default_shell_env=True, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 112 | ) |
| 113 | |
| 114 | return struct( |
| 115 | proto=struct( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 116 | srcs=srcs, |
| 117 | import_flags=import_flags, |
| 118 | deps=deps, |
| 119 | ), |
| 120 | ) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 121 | |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 122 | proto_gen = rule( |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 123 | attrs = { |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 124 | "srcs": attr.label_list(allow_files = True), |
| 125 | "deps": attr.label_list(providers = ["proto"]), |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 126 | "includes": attr.string_list(), |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 127 | "protoc": attr.label( |
Vladimir Moskva | a86e6d8 | 2016-09-09 13:21:35 +0200 | [diff] [blame] | 128 | cfg = "host", |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 129 | executable = True, |
| 130 | single_file = True, |
| 131 | mandatory = True, |
| 132 | ), |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 133 | "plugin": attr.label( |
Vladimir Moskva | a86e6d8 | 2016-09-09 13:21:35 +0200 | [diff] [blame] | 134 | cfg = "host", |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 135 | allow_files = True, |
Manjunath Kudlur | f0966a7 | 2016-02-22 14:30:43 -0800 | [diff] [blame] | 136 | executable = True, |
Manjunath Kudlur | f0966a7 | 2016-02-22 14:30:43 -0800 | [diff] [blame] | 137 | ), |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 138 | "plugin_language": attr.string(), |
| 139 | "plugin_options": attr.string_list(), |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 140 | "gen_cc": attr.bool(), |
| 141 | "gen_py": attr.bool(), |
| 142 | "outs": attr.output_list(), |
| 143 | }, |
| 144 | output_to_genfiles = True, |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 145 | implementation = _proto_gen_impl, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 146 | ) |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 147 | """Generates codes from Protocol Buffers definitions. |
| 148 | |
| 149 | This rule helps you to implement Skylark macros specific to the target |
| 150 | language. You should prefer more specific `cc_proto_library `, |
| 151 | `py_proto_library` and others unless you are adding such wrapper macros. |
| 152 | |
| 153 | Args: |
| 154 | srcs: Protocol Buffers definition files (.proto) to run the protocol compiler |
| 155 | against. |
| 156 | deps: a list of dependency labels; must be other proto libraries. |
| 157 | includes: a list of include paths to .proto files. |
| 158 | protoc: the label of the protocol compiler to generate the sources. |
| 159 | plugin: the label of the protocol compiler plugin to be passed to the protocol |
| 160 | compiler. |
| 161 | plugin_language: the language of the generated sources |
| 162 | plugin_options: a list of options to be passed to the plugin |
Kristina Chodorow | 4e7ecde | 2017-01-25 14:10:56 -0500 | [diff] [blame] | 163 | gen_cc: generates C++ sources in addition to the ones from the plugin. |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 164 | gen_py: generates Python sources in addition to the ones from the plugin. |
| 165 | outs: a list of labels of the expected outputs from the protocol compiler. |
| 166 | """ |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 167 | |
| 168 | def cc_proto_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 169 | name, |
| 170 | srcs=[], |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 171 | deps=[], |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 172 | cc_libs=[], |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 173 | include=None, |
David Z. Chen | 985c968 | 2016-02-11 18:11:10 -0800 | [diff] [blame] | 174 | protoc="//:protoc", |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 175 | internal_bootstrap_hack=False, |
Manjunath Kudlur | f0966a7 | 2016-02-22 14:30:43 -0800 | [diff] [blame] | 176 | use_grpc_plugin=False, |
David Z. Chen | 985c968 | 2016-02-11 18:11:10 -0800 | [diff] [blame] | 177 | default_runtime="//:protobuf", |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 178 | **kargs): |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 179 | """Bazel rule to create a C++ protobuf library from proto source files |
| 180 | |
Jisi Liu | d4bef7d | 2015-11-02 12:24:32 -0800 | [diff] [blame] | 181 | NOTE: the rule is only an internal workaround to generate protos. The |
| 182 | interface may change and the rule may be removed when bazel has introduced |
| 183 | the native rule. |
| 184 | |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 185 | Args: |
| 186 | name: the name of the cc_proto_library. |
| 187 | srcs: the .proto files of the cc_proto_library. |
| 188 | deps: a list of dependency labels; must be cc_proto_library. |
| 189 | cc_libs: a list of other cc_library targets depended by the generated |
| 190 | cc_library. |
| 191 | include: a string indicating the include path of the .proto files. |
| 192 | protoc: the label of the protocol compiler to generate the sources. |
| 193 | internal_bootstrap_hack: a flag indicate the cc_proto_library is used only |
| 194 | for bootstraping. When it is set to True, no files will be generated. |
| 195 | The rule will simply be a provider for .proto files, so that other |
| 196 | cc_proto_library can depend on it. |
Manjunath Kudlur | f0966a7 | 2016-02-22 14:30:43 -0800 | [diff] [blame] | 197 | use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin |
| 198 | when processing the proto files. |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 199 | default_runtime: the implicitly default runtime which will be depended on by |
| 200 | the generated cc_library target. |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 201 | **kargs: other keyword arguments that are passed to cc_library. |
| 202 | |
| 203 | """ |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 204 | |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 205 | includes = [] |
| 206 | if include != None: |
| 207 | includes = [include] |
| 208 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 209 | if internal_bootstrap_hack: |
| 210 | # For pre-checked-in generated files, we add the internal_bootstrap_hack |
| 211 | # which will skip the codegen action. |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 212 | proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 213 | name=name + "_genproto", |
| 214 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 215 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 216 | includes=includes, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 217 | protoc=protoc, |
Martin Maly | 8e0c9a3 | 2015-12-04 17:44:58 -0800 | [diff] [blame] | 218 | visibility=["//visibility:public"], |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 219 | ) |
| 220 | # An empty cc_library to make rule dependency consistent. |
| 221 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 222 | name=name, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 223 | **kargs) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 224 | return |
| 225 | |
Manjunath Kudlur | f0966a7 | 2016-02-22 14:30:43 -0800 | [diff] [blame] | 226 | grpc_cpp_plugin = None |
| 227 | if use_grpc_plugin: |
| 228 | grpc_cpp_plugin = "//external:grpc_cpp_plugin" |
| 229 | |
Andreas Bergmeier | bbeb983 | 2016-08-15 16:57:30 +0200 | [diff] [blame] | 230 | gen_srcs = _CcSrcs(srcs, use_grpc_plugin) |
| 231 | gen_hdrs = _CcHdrs(srcs, use_grpc_plugin) |
| 232 | outs = gen_srcs + gen_hdrs |
Manjunath Kudlur | f5c7363 | 2016-02-25 08:50:50 -0800 | [diff] [blame] | 233 | |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 234 | proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 235 | name=name + "_genproto", |
| 236 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 237 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 238 | includes=includes, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 239 | protoc=protoc, |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 240 | plugin=grpc_cpp_plugin, |
| 241 | plugin_language="grpc", |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 242 | gen_cc=1, |
| 243 | outs=outs, |
Martin Maly | 8e0c9a3 | 2015-12-04 17:44:58 -0800 | [diff] [blame] | 244 | visibility=["//visibility:public"], |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 245 | ) |
| 246 | |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 247 | if default_runtime and not default_runtime in cc_libs: |
Vladimir Moskva | 4fc9304 | 2017-08-07 13:33:03 +0200 | [diff] [blame] | 248 | cc_libs = cc_libs + [default_runtime] |
Manjunath Kudlur | f5c7363 | 2016-02-25 08:50:50 -0800 | [diff] [blame] | 249 | if use_grpc_plugin: |
Vladimir Moskva | 4fc9304 | 2017-08-07 13:33:03 +0200 | [diff] [blame] | 250 | cc_libs = cc_libs + ["//external:grpc_lib"] |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 251 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 252 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 253 | name=name, |
Andreas Bergmeier | bbeb983 | 2016-08-15 16:57:30 +0200 | [diff] [blame] | 254 | srcs=gen_srcs, |
| 255 | hdrs=gen_hdrs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 256 | deps=cc_libs + deps, |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 257 | includes=includes, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 258 | **kargs) |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 259 | |
Steven Parkes | ea18866 | 2016-02-25 07:53:19 -0800 | [diff] [blame] | 260 | def internal_gen_well_known_protos_java(srcs): |
| 261 | """Bazel rule to generate the gen_well_known_protos_java genrule |
| 262 | |
| 263 | Args: |
| 264 | srcs: the well known protos |
| 265 | """ |
| 266 | root = Label("%s//protobuf_java" % (REPOSITORY_NAME)).workspace_root |
cgrushko | 6fffd4a | 2017-02-08 12:19:40 -0500 | [diff] [blame] | 267 | pkg = PACKAGE_NAME + "/" if PACKAGE_NAME else "" |
Steven Parkes | ea18866 | 2016-02-25 07:53:19 -0800 | [diff] [blame] | 268 | if root == "": |
cgrushko | 6fffd4a | 2017-02-08 12:19:40 -0500 | [diff] [blame] | 269 | include = " -I%ssrc " % pkg |
Steven Parkes | ea18866 | 2016-02-25 07:53:19 -0800 | [diff] [blame] | 270 | else: |
cgrushko | 6fffd4a | 2017-02-08 12:19:40 -0500 | [diff] [blame] | 271 | include = " -I%s/%ssrc " % (root, pkg) |
Steven Parkes | ea18866 | 2016-02-25 07:53:19 -0800 | [diff] [blame] | 272 | native.genrule( |
| 273 | name = "gen_well_known_protos_java", |
| 274 | srcs = srcs, |
| 275 | outs = [ |
| 276 | "wellknown.srcjar", |
| 277 | ], |
| 278 | cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" + |
| 279 | " %s $(SRCS) " % include + |
| 280 | " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar", |
| 281 | tools = [":protoc"], |
| 282 | ) |
| 283 | |
David Z. Chen | 02cd45c | 2016-05-20 16:49:04 -0700 | [diff] [blame] | 284 | def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs): |
| 285 | """Macro to copy files to a different directory and then create a filegroup. |
| 286 | |
| 287 | This is used by the //:protobuf_python py_proto_library target to work around |
| 288 | an issue caused by Python source files that are part of the same Python |
| 289 | package being in separate directories. |
| 290 | |
| 291 | Args: |
| 292 | srcs: The source files to copy and add to the filegroup. |
| 293 | strip_prefix: Path to the root of the files to copy. |
| 294 | dest: The directory to copy the source files into. |
| 295 | **kwargs: extra arguments that will be passesd to the filegroup. |
| 296 | """ |
| 297 | outs = [_RelativeOutputPath(s, strip_prefix, dest) for s in srcs] |
| 298 | |
| 299 | native.genrule( |
| 300 | name = name + "_genrule", |
| 301 | srcs = srcs, |
| 302 | outs = outs, |
| 303 | cmd = " && ".join( |
| 304 | ["cp $(location %s) $(location %s)" % |
| 305 | (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs]), |
| 306 | ) |
| 307 | |
| 308 | native.filegroup( |
| 309 | name = name, |
| 310 | srcs = outs, |
| 311 | **kwargs) |
| 312 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 313 | def py_proto_library( |
| 314 | name, |
| 315 | srcs=[], |
| 316 | deps=[], |
| 317 | py_libs=[], |
| 318 | py_extra_srcs=[], |
| 319 | include=None, |
David Z. Chen | 985c968 | 2016-02-11 18:11:10 -0800 | [diff] [blame] | 320 | default_runtime="//:protobuf_python", |
| 321 | protoc="//:protoc", |
Wiktor Tomczak | 0fa31b2 | 2016-11-22 20:18:46 +0100 | [diff] [blame] | 322 | use_grpc_plugin=False, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 323 | **kargs): |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 324 | """Bazel rule to create a Python protobuf library from proto source files |
| 325 | |
Jisi Liu | d4bef7d | 2015-11-02 12:24:32 -0800 | [diff] [blame] | 326 | NOTE: the rule is only an internal workaround to generate protos. The |
| 327 | interface may change and the rule may be removed when bazel has introduced |
| 328 | the native rule. |
| 329 | |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 330 | Args: |
| 331 | name: the name of the py_proto_library. |
| 332 | srcs: the .proto files of the py_proto_library. |
| 333 | deps: a list of dependency labels; must be py_proto_library. |
| 334 | py_libs: a list of other py_library targets depended by the generated |
| 335 | py_library. |
| 336 | py_extra_srcs: extra source files that will be added to the output |
| 337 | py_library. This attribute is used for internal bootstrapping. |
| 338 | include: a string indicating the include path of the .proto files. |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 339 | default_runtime: the implicitly default runtime which will be depended on by |
| 340 | the generated py_library target. |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 341 | protoc: the label of the protocol compiler to generate the sources. |
Wiktor Tomczak | 0fa31b2 | 2016-11-22 20:18:46 +0100 | [diff] [blame] | 342 | use_grpc_plugin: a flag to indicate whether to call the Python C++ plugin |
| 343 | when processing the proto files. |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 344 | **kargs: other keyword arguments that are passed to cc_library. |
| 345 | |
| 346 | """ |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 347 | outs = _PyOuts(srcs) |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 348 | |
| 349 | includes = [] |
| 350 | if include != None: |
| 351 | includes = [include] |
| 352 | |
Wiktor Tomczak | 0fa31b2 | 2016-11-22 20:18:46 +0100 | [diff] [blame] | 353 | grpc_python_plugin = None |
| 354 | if use_grpc_plugin: |
| 355 | grpc_python_plugin = "//external:grpc_python_plugin" |
| 356 | # Note: Generated grpc code depends on Python grpc module. This dependency |
| 357 | # is not explicitly listed in py_libs. Instead, host system is assumed to |
| 358 | # have grpc installed. |
| 359 | |
Yuki Yugui Sonoda | 5977fb0 | 2016-06-01 16:23:15 +0900 | [diff] [blame] | 360 | proto_gen( |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 361 | name=name + "_genproto", |
| 362 | srcs=srcs, |
| 363 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 364 | includes=includes, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 365 | protoc=protoc, |
| 366 | gen_py=1, |
| 367 | outs=outs, |
Martin Maly | 8e0c9a3 | 2015-12-04 17:44:58 -0800 | [diff] [blame] | 368 | visibility=["//visibility:public"], |
Wiktor Tomczak | 0fa31b2 | 2016-11-22 20:18:46 +0100 | [diff] [blame] | 369 | plugin=grpc_python_plugin, |
| 370 | plugin_language="grpc" |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 371 | ) |
| 372 | |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 373 | if default_runtime and not default_runtime in py_libs + deps: |
Vladimir Moskva | 4fc9304 | 2017-08-07 13:33:03 +0200 | [diff] [blame] | 374 | py_libs = py_libs + [default_runtime] |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 375 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 376 | native.py_library( |
| 377 | name=name, |
Jisi Liu | a33fa8e | 2015-10-20 15:30:44 -0700 | [diff] [blame] | 378 | srcs=outs+py_extra_srcs, |
| 379 | deps=py_libs+deps, |
David Z. Chen | 985c968 | 2016-02-11 18:11:10 -0800 | [diff] [blame] | 380 | imports=includes, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 381 | **kargs) |
| 382 | |
| 383 | def internal_protobuf_py_tests( |
| 384 | name, |
| 385 | modules=[], |
| 386 | **kargs): |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 387 | """Bazel rules to create batch tests for protobuf internal. |
| 388 | |
| 389 | Args: |
| 390 | name: the name of the rule. |
| 391 | modules: a list of modules for tests. The macro will create a py_test for |
| 392 | each of the parameter with the source "google/protobuf/%s.py" |
| 393 | kargs: extra parameters that will be passed into the py_test. |
| 394 | |
| 395 | """ |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 396 | for m in modules: |
David Z. Chen | 985c968 | 2016-02-11 18:11:10 -0800 | [diff] [blame] | 397 | s = "python/google/protobuf/internal/%s.py" % m |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 398 | native.py_test( |
| 399 | name="py_%s" % m, |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 400 | srcs=[s], |
| 401 | main=s, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 402 | **kargs) |
Fahrzin Hemmati | 35119e3 | 2017-11-28 14:24:53 -0800 | [diff] [blame^] | 403 | |
| 404 | |
| 405 | def check_protobuf_required_bazel_version(): |
| 406 | """For WORKSPACE files, to check the installed version of bazel. |
| 407 | |
| 408 | This ensures bazel supports our approach to proto_library() depending on a |
| 409 | copied filegroup. (Fixed in bazel 0.5.4) |
| 410 | """ |
| 411 | expected = apple_common.dotted_version("0.5.4") |
| 412 | current = apple_common.dotted_version(native.bazel_version) |
| 413 | if current.compare_to(expected) < 0: |
| 414 | fail("Bazel must be newer than 0.5.4") |