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