Philipp Schrader | 15b92e0 | 2020-11-08 10:35:57 -0800 | [diff] [blame] | 1 | load("@//tools/build_rules:select.bzl", "compiler_select") |
| 2 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3 | def _GetPath(ctx, path): |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 4 | if ctx.label.workspace_root: |
| 5 | return ctx.label.workspace_root + '/' + path |
| 6 | else: |
| 7 | return path |
| 8 | |
| 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("../") |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 15 | |
| 16 | def _GenDir(ctx): |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [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): |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 27 | if not ctx.attr.includes: |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 28 | return ctx.label.workspace_root |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 29 | if not ctx.attr.includes[0]: |
| 30 | return _GetPath(ctx, ctx.label.package) |
| 31 | if not ctx.label.package: |
| 32 | return _GetPath(ctx, ctx.attr.includes[0]) |
| 33 | return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0]) |
| 34 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 35 | def _CcHdrs(srcs, use_grpc_plugin=False): |
| 36 | ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] |
| 37 | if use_grpc_plugin: |
| 38 | ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] |
| 39 | return ret |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 40 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [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 |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 46 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 47 | def _CcOuts(srcs, use_grpc_plugin=False): |
| 48 | return _CcHdrs(srcs, use_grpc_plugin) + _CcSrcs(srcs, use_grpc_plugin) |
| 49 | |
| 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 |
| 55 | |
| 56 | def _RelativeOutputPath(path, include, dest=""): |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [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 + '/' |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 65 | if dest and dest[-1] != '/': |
| 66 | dest = dest + '/' |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 67 | |
| 68 | path = path[len(include):] |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 69 | return dest + path |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 70 | |
| 71 | def _proto_gen_impl(ctx): |
| 72 | """General implementation for generating protos""" |
| 73 | srcs = ctx.files.srcs |
| 74 | deps = [] |
| 75 | deps += ctx.files.srcs |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 76 | source_dir = _SourceDir(ctx) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 77 | gen_dir = _GenDir(ctx) |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 78 | if source_dir: |
| 79 | import_flags = ["-I" + source_dir, "-I" + gen_dir] |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 80 | else: |
| 81 | import_flags = ["-I."] |
| 82 | |
| 83 | for dep in ctx.attr.deps: |
| 84 | import_flags += dep.proto.import_flags |
| 85 | deps += dep.proto.deps |
| 86 | |
| 87 | args = [] |
| 88 | if ctx.attr.gen_cc: |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 89 | args += ["--cpp_out=" + gen_dir] |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 90 | if ctx.attr.gen_py: |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 91 | args += ["--python_out=" + gen_dir] |
| 92 | |
| 93 | inputs = srcs + deps |
| 94 | if ctx.executable.plugin: |
| 95 | plugin = ctx.executable.plugin |
| 96 | lang = ctx.attr.plugin_language |
| 97 | if not lang and plugin.basename.startswith('protoc-gen-'): |
| 98 | lang = plugin.basename[len('protoc-gen-'):] |
| 99 | if not lang: |
| 100 | fail("cannot infer the target language of plugin", "plugin_language") |
| 101 | |
| 102 | outdir = gen_dir |
| 103 | if ctx.attr.plugin_options: |
| 104 | outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir |
| 105 | args += ["--plugin=protoc-gen-%s=%s" % (lang, plugin.path)] |
| 106 | args += ["--%s_out=%s" % (lang, outdir)] |
| 107 | inputs += [plugin] |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 108 | |
| 109 | if args: |
Philipp Schrader | 5dd9eda | 2020-11-08 10:16:35 -0800 | [diff] [blame] | 110 | ctx.actions.run( |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 111 | inputs=inputs, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 112 | outputs=ctx.outputs.outs, |
| 113 | arguments=args + import_flags + [s.path for s in srcs], |
| 114 | executable=ctx.executable.protoc, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 115 | mnemonic="ProtoCompile", |
| 116 | use_default_shell_env=True, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 117 | ) |
| 118 | |
| 119 | return struct( |
| 120 | proto=struct( |
| 121 | srcs=srcs, |
| 122 | import_flags=import_flags, |
| 123 | deps=deps, |
| 124 | ), |
| 125 | ) |
| 126 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 127 | proto_gen = rule( |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 128 | attrs = { |
| 129 | "srcs": attr.label_list(allow_files = True), |
| 130 | "deps": attr.label_list(providers = ["proto"]), |
| 131 | "includes": attr.string_list(), |
| 132 | "protoc": attr.label( |
Brian Silverman | 7b8899e | 2018-06-30 19:19:24 -0700 | [diff] [blame] | 133 | cfg = "host", |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 134 | executable = True, |
Philipp Schrader | 5dd9eda | 2020-11-08 10:16:35 -0800 | [diff] [blame] | 135 | allow_single_file = True, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 136 | mandatory = True, |
| 137 | ), |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 138 | "plugin": attr.label( |
| 139 | cfg = "host", |
| 140 | allow_files = True, |
| 141 | executable = True, |
| 142 | ), |
| 143 | "plugin_language": attr.string(), |
| 144 | "plugin_options": attr.string_list(), |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 145 | "gen_cc": attr.bool(), |
| 146 | "gen_py": attr.bool(), |
| 147 | "outs": attr.output_list(), |
| 148 | }, |
| 149 | output_to_genfiles = True, |
| 150 | implementation = _proto_gen_impl, |
| 151 | ) |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 152 | """Generates codes from Protocol Buffers definitions. |
| 153 | |
| 154 | This rule helps you to implement Skylark macros specific to the target |
| 155 | language. You should prefer more specific `cc_proto_library `, |
| 156 | `py_proto_library` and others unless you are adding such wrapper macros. |
| 157 | |
| 158 | Args: |
| 159 | srcs: Protocol Buffers definition files (.proto) to run the protocol compiler |
| 160 | against. |
| 161 | deps: a list of dependency labels; must be other proto libraries. |
| 162 | includes: a list of include paths to .proto files. |
| 163 | protoc: the label of the protocol compiler to generate the sources. |
| 164 | plugin: the label of the protocol compiler plugin to be passed to the protocol |
| 165 | compiler. |
| 166 | plugin_language: the language of the generated sources |
| 167 | plugin_options: a list of options to be passed to the plugin |
| 168 | gen_cc: generates C++ sources in addition to the ones from the plugin. |
| 169 | gen_py: generates Python sources in addition to the ones from the plugin. |
| 170 | outs: a list of labels of the expected outputs from the protocol compiler. |
| 171 | """ |
| 172 | |
| 173 | MSVC_COPTS = [ |
| 174 | "/DHAVE_PTHREAD", |
| 175 | "/wd4018", # -Wno-sign-compare |
| 176 | "/wd4514", # -Wno-unused-function |
| 177 | ] |
| 178 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 179 | COPTS = [ |
| 180 | "-DHAVE_PTHREAD", |
| 181 | "-DGOOGLE_THIRD_PARTY_PROTOBUF", |
| 182 | "-Wall", |
| 183 | "-Wwrite-strings", |
| 184 | "-Woverloaded-virtual", |
| 185 | "-Wno-sign-compare", |
| 186 | "-Wno-unused-function", |
| 187 | "-Wno-unused-parameter", |
| 188 | "-Wno-format-nonliteral", |
| 189 | "-Wno-switch-enum", |
| 190 | "-Wno-missing-field-initializers", |
| 191 | "-Wno-ignored-qualifiers", |
| 192 | ] + compiler_select({ |
| 193 | "gcc": [ |
| 194 | "-Wno-error=cast-align", |
| 195 | ], |
| 196 | "clang": [ |
| 197 | "-Wno-unused-const-variable", |
| 198 | "-Wno-unused-private-field", |
Brian Silverman | 4c7235a | 2021-11-17 19:04:37 -0800 | [diff] [blame^] | 199 | "-Wno-tautological-type-limit-compare", |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 200 | ], |
| 201 | }) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 202 | |
| 203 | def cc_proto_library( |
| 204 | name, |
| 205 | srcs=[], |
| 206 | deps=[], |
| 207 | cc_libs=[], |
| 208 | include=None, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 209 | protoc="@com_google_protobuf//:protoc", |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 210 | internal_bootstrap_hack=False, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 211 | use_grpc_plugin=False, |
| 212 | default_runtime="@com_google_protobuf//:protobuf", |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 213 | target_compatible_with = None, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 214 | copts = [], |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 215 | **kargs): |
| 216 | """Bazel rule to create a C++ protobuf library from proto source files |
| 217 | |
| 218 | NOTE: the rule is only an internal workaround to generate protos. The |
| 219 | interface may change and the rule may be removed when bazel has introduced |
| 220 | the native rule. |
| 221 | |
| 222 | Args: |
| 223 | name: the name of the cc_proto_library. |
| 224 | srcs: the .proto files of the cc_proto_library. |
| 225 | deps: a list of dependency labels; must be cc_proto_library. |
| 226 | cc_libs: a list of other cc_library targets depended by the generated |
| 227 | cc_library. |
| 228 | include: a string indicating the include path of the .proto files. |
| 229 | protoc: the label of the protocol compiler to generate the sources. |
| 230 | internal_bootstrap_hack: a flag indicate the cc_proto_library is used only |
| 231 | for bootstraping. When it is set to True, no files will be generated. |
| 232 | The rule will simply be a provider for .proto files, so that other |
| 233 | cc_proto_library can depend on it. |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 234 | use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin |
| 235 | when processing the proto files. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 236 | default_runtime: the implicitly default runtime which will be depended on by |
| 237 | the generated cc_library target. |
| 238 | **kargs: other keyword arguments that are passed to cc_library. |
| 239 | |
| 240 | """ |
| 241 | |
| 242 | includes = [] |
| 243 | if include != None: |
| 244 | includes = [include] |
| 245 | |
| 246 | if internal_bootstrap_hack: |
| 247 | # For pre-checked-in generated files, we add the internal_bootstrap_hack |
| 248 | # which will skip the codegen action. |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 249 | proto_gen( |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 250 | name=name + "_genproto", |
| 251 | srcs=srcs, |
| 252 | deps=[s + "_genproto" for s in deps], |
| 253 | includes=includes, |
| 254 | protoc=protoc, |
| 255 | visibility=["//visibility:public"], |
| 256 | ) |
| 257 | # An empty cc_library to make rule dependency consistent. |
| 258 | native.cc_library( |
| 259 | name=name, |
| 260 | **kargs) |
| 261 | return |
| 262 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 263 | grpc_cpp_plugin = None |
| 264 | if use_grpc_plugin: |
| 265 | grpc_cpp_plugin = "//external:grpc_cpp_plugin" |
| 266 | |
| 267 | gen_srcs = _CcSrcs(srcs, use_grpc_plugin) |
| 268 | gen_hdrs = _CcHdrs(srcs, use_grpc_plugin) |
| 269 | outs = gen_srcs + gen_hdrs |
| 270 | |
| 271 | proto_gen( |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 272 | name=name + "_genproto", |
| 273 | srcs=srcs, |
| 274 | deps=[s + "_genproto" for s in deps], |
| 275 | includes=includes, |
| 276 | protoc=protoc, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 277 | plugin=grpc_cpp_plugin, |
| 278 | plugin_language="grpc", |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 279 | gen_cc=1, |
| 280 | outs=outs, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 281 | target_compatible_with = target_compatible_with, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 282 | visibility=["//visibility:public"], |
| 283 | ) |
| 284 | |
| 285 | if default_runtime and not default_runtime in cc_libs: |
Austin Schuh | be8c9b1 | 2017-11-25 15:53:12 -0800 | [diff] [blame] | 286 | cc_libs = cc_libs + [default_runtime] |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 287 | if use_grpc_plugin: |
| 288 | cc_libs = cc_libs + ["//external:grpc_lib"] |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 289 | |
| 290 | native.cc_library( |
| 291 | name=name, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 292 | srcs=gen_srcs, |
| 293 | hdrs=gen_hdrs, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 294 | deps=cc_libs + deps, |
| 295 | includes=includes, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 296 | copts = COPTS + copts, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 297 | target_compatible_with = target_compatible_with, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 298 | **kargs) |
| 299 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 300 | def internal_gen_well_known_protos_java(srcs): |
| 301 | """Bazel rule to generate the gen_well_known_protos_java genrule |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 302 | |
| 303 | Args: |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 304 | srcs: the well known protos |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 305 | """ |
Philipp Schrader | 15b92e0 | 2020-11-08 10:35:57 -0800 | [diff] [blame] | 306 | root = Label("%s//protobuf_java" % (native.repository_name())).workspace_root |
| 307 | pkg = native.package_name() + "/" if native.package_name() else "" |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 308 | if root == "": |
| 309 | include = " -I%ssrc " % pkg |
| 310 | else: |
| 311 | include = " -I%s/%ssrc " % (root, pkg) |
| 312 | native.genrule( |
| 313 | name = "gen_well_known_protos_java", |
| 314 | srcs = srcs, |
| 315 | outs = [ |
| 316 | "wellknown.srcjar", |
| 317 | ], |
| 318 | cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" + |
| 319 | " %s $(SRCS) " % include + |
| 320 | " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar", |
| 321 | tools = [":protoc"], |
| 322 | ) |
| 323 | |
| 324 | def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs): |
| 325 | """Macro to copy files to a different directory and then create a filegroup. |
| 326 | |
| 327 | This is used by the //:protobuf_python py_proto_library target to work around |
| 328 | an issue caused by Python source files that are part of the same Python |
| 329 | package being in separate directories. |
| 330 | |
| 331 | Args: |
| 332 | srcs: The source files to copy and add to the filegroup. |
| 333 | strip_prefix: Path to the root of the files to copy. |
| 334 | dest: The directory to copy the source files into. |
| 335 | **kwargs: extra arguments that will be passesd to the filegroup. |
| 336 | """ |
| 337 | outs = [_RelativeOutputPath(s, strip_prefix, dest) for s in srcs] |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 338 | |
| 339 | native.genrule( |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 340 | name = name + "_genrule", |
| 341 | srcs = srcs, |
| 342 | outs = outs, |
| 343 | cmd = " && ".join( |
| 344 | ["cp $(location %s) $(location %s)" % |
| 345 | (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs]), |
| 346 | ) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 347 | |
| 348 | native.filegroup( |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 349 | name = name, |
| 350 | srcs = outs, |
| 351 | **kwargs) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 352 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 353 | def py_proto_library( |
| 354 | name, |
| 355 | srcs=[], |
| 356 | deps=[], |
| 357 | py_libs=[], |
| 358 | py_extra_srcs=[], |
Brian Silverman | 7b8899e | 2018-06-30 19:19:24 -0700 | [diff] [blame] | 359 | py_imports=[], |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 360 | include=None, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 361 | default_runtime="@com_google_protobuf//:protobuf_python", |
| 362 | protoc="@com_google_protobuf//:protoc", |
| 363 | use_grpc_plugin=False, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 364 | **kargs): |
| 365 | """Bazel rule to create a Python protobuf library from proto source files |
| 366 | |
| 367 | NOTE: the rule is only an internal workaround to generate protos. The |
| 368 | interface may change and the rule may be removed when bazel has introduced |
| 369 | the native rule. |
| 370 | |
| 371 | Args: |
| 372 | name: the name of the py_proto_library. |
| 373 | srcs: the .proto files of the py_proto_library. |
| 374 | deps: a list of dependency labels; must be py_proto_library. |
| 375 | py_libs: a list of other py_library targets depended by the generated |
| 376 | py_library. |
| 377 | py_extra_srcs: extra source files that will be added to the output |
| 378 | py_library. This attribute is used for internal bootstrapping. |
| 379 | include: a string indicating the include path of the .proto files. |
| 380 | default_runtime: the implicitly default runtime which will be depended on by |
| 381 | the generated py_library target. |
| 382 | protoc: the label of the protocol compiler to generate the sources. |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 383 | use_grpc_plugin: a flag to indicate whether to call the Python C++ plugin |
| 384 | when processing the proto files. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 385 | **kargs: other keyword arguments that are passed to cc_library. |
| 386 | |
| 387 | """ |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 388 | outs = _PyOuts(srcs, use_grpc_plugin) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 389 | |
| 390 | includes = [] |
| 391 | if include != None: |
| 392 | includes = [include] |
| 393 | |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 394 | grpc_python_plugin = None |
| 395 | if use_grpc_plugin: |
| 396 | grpc_python_plugin = "//external:grpc_python_plugin" |
| 397 | # Note: Generated grpc code depends on Python grpc module. This dependency |
| 398 | # is not explicitly listed in py_libs. Instead, host system is assumed to |
| 399 | # have grpc installed. |
| 400 | |
| 401 | proto_gen( |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 402 | name=name + "_genproto", |
| 403 | srcs=srcs, |
| 404 | deps=[s + "_genproto" for s in deps], |
| 405 | includes=includes, |
| 406 | protoc=protoc, |
| 407 | gen_py=1, |
| 408 | outs=outs, |
| 409 | visibility=["//visibility:public"], |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 410 | plugin=grpc_python_plugin, |
| 411 | plugin_language="grpc" |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 412 | ) |
| 413 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 414 | if default_runtime and not default_runtime in py_libs + deps: |
Austin Schuh | be8c9b1 | 2017-11-25 15:53:12 -0800 | [diff] [blame] | 415 | py_libs = py_libs + [default_runtime] |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 416 | |
| 417 | native.py_library( |
| 418 | name=name, |
| 419 | srcs=outs+py_extra_srcs, |
| 420 | deps=py_libs+deps, |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 421 | imports=includes, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 422 | **kargs) |
| 423 | |
| 424 | def internal_protobuf_py_tests( |
| 425 | name, |
| 426 | modules=[], |
| 427 | **kargs): |
| 428 | """Bazel rules to create batch tests for protobuf internal. |
| 429 | |
| 430 | Args: |
| 431 | name: the name of the rule. |
| 432 | modules: a list of modules for tests. The macro will create a py_test for |
| 433 | each of the parameter with the source "google/protobuf/%s.py" |
| 434 | kargs: extra parameters that will be passed into the py_test. |
| 435 | |
| 436 | """ |
| 437 | for m in modules: |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 438 | s = "python/google/protobuf/internal/%s.py" % m |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 439 | native.py_test( |
| 440 | name="py_%s" % m, |
| 441 | srcs=[s], |
| 442 | main=s, |
| 443 | **kargs) |
Austin Schuh | f972444 | 2018-10-28 20:30:21 -0700 | [diff] [blame] | 444 | |
| 445 | |
| 446 | def check_protobuf_required_bazel_version(): |
| 447 | """For WORKSPACE files, to check the installed version of bazel. |
| 448 | |
| 449 | This ensures bazel supports our approach to proto_library() depending on a |
| 450 | copied filegroup. (Fixed in bazel 0.5.4) |
| 451 | """ |
| 452 | expected = apple_common.dotted_version("0.5.4") |
| 453 | current = apple_common.dotted_version(native.bazel_version) |
| 454 | if current.compare_to(expected) < 0: |
| 455 | fail("Bazel must be newer than 0.5.4") |