OLD | NEW |
1 # -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED | 1 # -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED |
2 | 2 |
3 def _GetPath(ctx, path): | 3 def _GetPath(ctx, path): |
4 if ctx.label.workspace_root: | 4 if ctx.label.workspace_root: |
5 return ctx.label.workspace_root + '/' + path | 5 return ctx.label.workspace_root + '/' + path |
6 else: | 6 else: |
7 return path | 7 return path |
8 | 8 |
9 def _GenDir(ctx): | 9 def _GenDir(ctx): |
10 if not ctx.attr.includes: | 10 if not ctx.attr.includes: |
11 return ctx.label.workspace_root | 11 return ctx.label.workspace_root |
12 if not ctx.attr.includes[0]: | 12 if not ctx.attr.includes[0]: |
13 return _GetPath(ctx, ctx.label.package) | 13 return _GetPath(ctx, ctx.label.package) |
14 if not ctx.label.package: | 14 if not ctx.label.package: |
15 return _GetPath(ctx, ctx.attr.includes[0]) | 15 return _GetPath(ctx, ctx.attr.includes[0]) |
16 return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0]) | 16 return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0]) |
17 | 17 |
| 18 def _CcHdrs(srcs, use_grpc_plugin=False): |
| 19 ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] |
| 20 if use_grpc_plugin: |
| 21 ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] |
| 22 return ret |
| 23 |
| 24 def _CcSrcs(srcs, use_grpc_plugin=False): |
| 25 ret = [s[:-len(".proto")] + ".pb.cc" for s in srcs] |
| 26 if use_grpc_plugin: |
| 27 ret += [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs] |
| 28 return ret |
| 29 |
18 def _CcOuts(srcs, use_grpc_plugin=False): | 30 def _CcOuts(srcs, use_grpc_plugin=False): |
19 ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] + \ | 31 return _CcHdrs(srcs, use_grpc_plugin) + _CcSrcs(srcs, use_grpc_plugin) |
20 [s[:-len(".proto")] + ".pb.cc" for s in srcs] | |
21 if use_grpc_plugin: | |
22 ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] + \ | |
23 [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs] | |
24 return ret | |
25 | 32 |
26 def _PyOuts(srcs): | 33 def _PyOuts(srcs): |
27 return [s[:-len(".proto")] + "_pb2.py" for s in srcs] | 34 return [s[:-len(".proto")] + "_pb2.py" for s in srcs] |
28 | 35 |
29 def _RelativeOutputPath(path, include): | 36 def _RelativeOutputPath(path, include, dest=""): |
30 if include == None: | 37 if include == None: |
31 return path | 38 return path |
32 | 39 |
33 if not path.startswith(include): | 40 if not path.startswith(include): |
34 fail("Include path %s isn't part of the path %s." % (include, path)) | 41 fail("Include path %s isn't part of the path %s." % (include, path)) |
35 | 42 |
36 if include and include[-1] != '/': | 43 if include and include[-1] != '/': |
37 include = include + '/' | 44 include = include + '/' |
| 45 if dest and dest[-1] != '/': |
| 46 dest = dest + '/' |
38 | 47 |
39 path = path[len(include):] | 48 path = path[len(include):] |
40 | 49 return dest + path |
41 if not path.startswith(PACKAGE_NAME): | |
42 fail("The package %s is not within the path %s" % (PACKAGE_NAME, path)) | |
43 | |
44 if not PACKAGE_NAME: | |
45 return path | |
46 | |
47 return path[len(PACKAGE_NAME)+1:] | |
48 | 50 |
49 def _proto_gen_impl(ctx): | 51 def _proto_gen_impl(ctx): |
50 """General implementation for generating protos""" | 52 """General implementation for generating protos""" |
51 srcs = ctx.files.srcs | 53 srcs = ctx.files.srcs |
52 deps = [] | 54 deps = [] |
53 deps += ctx.files.srcs | 55 deps += ctx.files.srcs |
54 gen_dir = _GenDir(ctx) | 56 gen_dir = _GenDir(ctx) |
55 if gen_dir: | 57 if gen_dir: |
56 import_flags = ["-I" + gen_dir] | 58 import_flags = ["-I" + gen_dir, "-I" + ctx.var["GENDIR"] + "/" + gen_dir] |
57 else: | 59 else: |
58 import_flags = ["-I."] | 60 import_flags = ["-I."] |
59 | 61 |
60 for dep in ctx.attr.deps: | 62 for dep in ctx.attr.deps: |
61 import_flags += dep.proto.import_flags | 63 import_flags += dep.proto.import_flags |
62 deps += dep.proto.deps | 64 deps += dep.proto.deps |
63 | 65 |
64 args = [] | 66 args = [] |
65 if ctx.attr.gen_cc: | 67 if ctx.attr.gen_cc: |
66 args += ["--cpp_out=" + ctx.var["GENDIR"] + "/" + gen_dir] | 68 args += ["--cpp_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
67 if ctx.attr.gen_py: | 69 if ctx.attr.gen_py: |
68 args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir] | 70 args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
69 | 71 |
70 if ctx.executable.grpc_cpp_plugin: | 72 inputs = srcs + deps |
71 args += ["--plugin=protoc-gen-grpc=" + ctx.executable.grpc_cpp_plugin.path] | 73 if ctx.executable.plugin: |
72 args += ["--grpc_out=" + ctx.var["GENDIR"] + "/" + gen_dir] | 74 plugin = ctx.executable.plugin |
| 75 lang = ctx.attr.plugin_language |
| 76 if not lang and plugin.basename.startswith('protoc-gen-'): |
| 77 lang = plugin.basename[len('protoc-gen-'):] |
| 78 if not lang: |
| 79 fail("cannot infer the target language of plugin", "plugin_language") |
| 80 |
| 81 outdir = ctx.var["GENDIR"] + "/" + gen_dir |
| 82 if ctx.attr.plugin_options: |
| 83 outdir = ",".join(ctx.attr.plugin_options) + ":" + outdir |
| 84 args += ["--plugin=protoc-gen-%s=%s" % (lang, plugin.path)] |
| 85 args += ["--%s_out=%s" % (lang, outdir)] |
| 86 inputs += [plugin] |
73 | 87 |
74 if args: | 88 if args: |
75 ctx.action( | 89 ctx.action( |
76 inputs=srcs + deps, | 90 inputs=inputs, |
77 outputs=ctx.outputs.outs, | 91 outputs=ctx.outputs.outs, |
78 arguments=args + import_flags + [s.path for s in srcs], | 92 arguments=args + import_flags + [s.path for s in srcs], |
79 executable=ctx.executable.protoc, | 93 executable=ctx.executable.protoc, |
| 94 mnemonic="ProtoCompile", |
80 ) | 95 ) |
81 | 96 |
82 return struct( | 97 return struct( |
83 proto=struct( | 98 proto=struct( |
84 srcs=srcs, | 99 srcs=srcs, |
85 import_flags=import_flags, | 100 import_flags=import_flags, |
86 deps=deps, | 101 deps=deps, |
87 ), | 102 ), |
88 ) | 103 ) |
89 | 104 |
90 _proto_gen = rule( | 105 proto_gen = rule( |
91 attrs = { | 106 attrs = { |
92 "srcs": attr.label_list(allow_files = True), | 107 "srcs": attr.label_list(allow_files = True), |
93 "deps": attr.label_list(providers = ["proto"]), | 108 "deps": attr.label_list(providers = ["proto"]), |
94 "includes": attr.string_list(), | 109 "includes": attr.string_list(), |
95 "protoc": attr.label( | 110 "protoc": attr.label( |
96 cfg = HOST_CFG, | 111 cfg = "host", |
97 executable = True, | 112 executable = True, |
98 single_file = True, | 113 single_file = True, |
99 mandatory = True, | 114 mandatory = True, |
100 ), | 115 ), |
101 "grpc_cpp_plugin": attr.label( | 116 "plugin": attr.label( |
102 cfg = HOST_CFG, | 117 cfg = "host", |
| 118 allow_files = True, |
103 executable = True, | 119 executable = True, |
104 single_file = True, | |
105 ), | 120 ), |
| 121 "plugin_language": attr.string(), |
| 122 "plugin_options": attr.string_list(), |
106 "gen_cc": attr.bool(), | 123 "gen_cc": attr.bool(), |
107 "gen_py": attr.bool(), | 124 "gen_py": attr.bool(), |
108 "outs": attr.output_list(), | 125 "outs": attr.output_list(), |
109 }, | 126 }, |
110 output_to_genfiles = True, | 127 output_to_genfiles = True, |
111 implementation = _proto_gen_impl, | 128 implementation = _proto_gen_impl, |
112 ) | 129 ) |
| 130 """Generates codes from Protocol Buffers definitions. |
| 131 |
| 132 This rule helps you to implement Skylark macros specific to the target |
| 133 language. You should prefer more specific `cc_proto_library `, |
| 134 `py_proto_library` and others unless you are adding such wrapper macros. |
| 135 |
| 136 Args: |
| 137 srcs: Protocol Buffers definition files (.proto) to run the protocol compiler |
| 138 against. |
| 139 deps: a list of dependency labels; must be other proto libraries. |
| 140 includes: a list of include paths to .proto files. |
| 141 protoc: the label of the protocol compiler to generate the sources. |
| 142 plugin: the label of the protocol compiler plugin to be passed to the protocol |
| 143 compiler. |
| 144 plugin_language: the language of the generated sources |
| 145 plugin_options: a list of options to be passed to the plugin |
| 146 gen_cc: generates C++ sources in addition to the ones from the plugin. |
| 147 gen_py: generates Python sources in addition to the ones from the plugin. |
| 148 outs: a list of labels of the expected outputs from the protocol compiler. |
| 149 """ |
113 | 150 |
114 def cc_proto_library( | 151 def cc_proto_library( |
115 name, | 152 name, |
116 srcs=[], | 153 srcs=[], |
117 deps=[], | 154 deps=[], |
118 cc_libs=[], | 155 cc_libs=[], |
119 include=None, | 156 include=None, |
120 protoc="//:protoc", | 157 protoc="//:protoc", |
121 internal_bootstrap_hack=False, | 158 internal_bootstrap_hack=False, |
122 use_grpc_plugin=False, | 159 use_grpc_plugin=False, |
(...skipping 25 matching lines...) Expand all Loading... |
148 | 185 |
149 """ | 186 """ |
150 | 187 |
151 includes = [] | 188 includes = [] |
152 if include != None: | 189 if include != None: |
153 includes = [include] | 190 includes = [include] |
154 | 191 |
155 if internal_bootstrap_hack: | 192 if internal_bootstrap_hack: |
156 # For pre-checked-in generated files, we add the internal_bootstrap_hack | 193 # For pre-checked-in generated files, we add the internal_bootstrap_hack |
157 # which will skip the codegen action. | 194 # which will skip the codegen action. |
158 _proto_gen( | 195 proto_gen( |
159 name=name + "_genproto", | 196 name=name + "_genproto", |
160 srcs=srcs, | 197 srcs=srcs, |
161 deps=[s + "_genproto" for s in deps], | 198 deps=[s + "_genproto" for s in deps], |
162 includes=includes, | 199 includes=includes, |
163 protoc=protoc, | 200 protoc=protoc, |
164 visibility=["//visibility:public"], | 201 visibility=["//visibility:public"], |
165 ) | 202 ) |
166 # An empty cc_library to make rule dependency consistent. | 203 # An empty cc_library to make rule dependency consistent. |
167 native.cc_library( | 204 native.cc_library( |
168 name=name, | 205 name=name, |
169 **kargs) | 206 **kargs) |
170 return | 207 return |
171 | 208 |
172 grpc_cpp_plugin = None | 209 grpc_cpp_plugin = None |
173 if use_grpc_plugin: | 210 if use_grpc_plugin: |
174 grpc_cpp_plugin = "//external:grpc_cpp_plugin" | 211 grpc_cpp_plugin = "//external:grpc_cpp_plugin" |
175 | 212 |
176 outs = _CcOuts(srcs, use_grpc_plugin) | 213 gen_srcs = _CcSrcs(srcs, use_grpc_plugin) |
| 214 gen_hdrs = _CcHdrs(srcs, use_grpc_plugin) |
| 215 outs = gen_srcs + gen_hdrs |
177 | 216 |
178 _proto_gen( | 217 proto_gen( |
179 name=name + "_genproto", | 218 name=name + "_genproto", |
180 srcs=srcs, | 219 srcs=srcs, |
181 deps=[s + "_genproto" for s in deps], | 220 deps=[s + "_genproto" for s in deps], |
182 includes=includes, | 221 includes=includes, |
183 protoc=protoc, | 222 protoc=protoc, |
184 grpc_cpp_plugin=grpc_cpp_plugin, | 223 plugin=grpc_cpp_plugin, |
| 224 plugin_language="grpc", |
185 gen_cc=1, | 225 gen_cc=1, |
186 outs=outs, | 226 outs=outs, |
187 visibility=["//visibility:public"], | 227 visibility=["//visibility:public"], |
188 ) | 228 ) |
189 | 229 |
190 if default_runtime and not default_runtime in cc_libs: | 230 if default_runtime and not default_runtime in cc_libs: |
191 cc_libs += [default_runtime] | 231 cc_libs += [default_runtime] |
192 if use_grpc_plugin: | 232 if use_grpc_plugin: |
193 cc_libs += ["//external:grpc_lib"] | 233 cc_libs += ["//external:grpc_lib"] |
194 | 234 |
195 native.cc_library( | 235 native.cc_library( |
196 name=name, | 236 name=name, |
197 srcs=outs, | 237 srcs=gen_srcs, |
| 238 hdrs=gen_hdrs, |
198 deps=cc_libs + deps, | 239 deps=cc_libs + deps, |
199 includes=includes, | 240 includes=includes, |
200 **kargs) | 241 **kargs) |
201 | 242 |
202 | 243 |
203 def internal_gen_well_known_protos_java(srcs): | 244 def internal_gen_well_known_protos_java(srcs): |
204 """Bazel rule to generate the gen_well_known_protos_java genrule | 245 """Bazel rule to generate the gen_well_known_protos_java genrule |
205 | 246 |
206 Args: | 247 Args: |
207 srcs: the well known protos | 248 srcs: the well known protos |
208 """ | 249 """ |
209 root = Label("%s//protobuf_java" % (REPOSITORY_NAME)).workspace_root | 250 root = Label("%s//protobuf_java" % (REPOSITORY_NAME)).workspace_root |
210 if root == "": | 251 if root == "": |
211 include = " -Isrc " | 252 include = " -Isrc " |
212 else: | 253 else: |
213 include = " -I%s/src " % root | 254 include = " -I%s/src " % root |
214 native.genrule( | 255 native.genrule( |
215 name = "gen_well_known_protos_java", | 256 name = "gen_well_known_protos_java", |
216 srcs = srcs, | 257 srcs = srcs, |
217 outs = [ | 258 outs = [ |
218 "wellknown.srcjar", | 259 "wellknown.srcjar", |
219 ], | 260 ], |
220 cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" + | 261 cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" + |
221 " %s $(SRCS) " % include + | 262 " %s $(SRCS) " % include + |
222 " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar", | 263 " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar", |
223 tools = [":protoc"], | 264 tools = [":protoc"], |
224 ) | 265 ) |
225 | 266 |
226 | 267 |
| 268 def internal_copied_filegroup(name, srcs, strip_prefix, dest, **kwargs): |
| 269 """Macro to copy files to a different directory and then create a filegroup. |
| 270 |
| 271 This is used by the //:protobuf_python py_proto_library target to work around |
| 272 an issue caused by Python source files that are part of the same Python |
| 273 package being in separate directories. |
| 274 |
| 275 Args: |
| 276 srcs: The source files to copy and add to the filegroup. |
| 277 strip_prefix: Path to the root of the files to copy. |
| 278 dest: The directory to copy the source files into. |
| 279 **kwargs: extra arguments that will be passesd to the filegroup. |
| 280 """ |
| 281 outs = [_RelativeOutputPath(s, strip_prefix, dest) for s in srcs] |
| 282 |
| 283 native.genrule( |
| 284 name = name + "_genrule", |
| 285 srcs = srcs, |
| 286 outs = outs, |
| 287 cmd = " && ".join( |
| 288 ["cp $(location %s) $(location %s)" % |
| 289 (s, _RelativeOutputPath(s, strip_prefix, dest)) for s in srcs]), |
| 290 ) |
| 291 |
| 292 native.filegroup( |
| 293 name = name, |
| 294 srcs = outs, |
| 295 **kwargs) |
| 296 |
| 297 |
227 def py_proto_library( | 298 def py_proto_library( |
228 name, | 299 name, |
229 srcs=[], | 300 srcs=[], |
230 deps=[], | 301 deps=[], |
231 py_libs=[], | 302 py_libs=[], |
232 py_extra_srcs=[], | 303 py_extra_srcs=[], |
233 include=None, | 304 include=None, |
234 default_runtime="//:protobuf_python", | 305 default_runtime="//:protobuf_python", |
235 protoc="//:protoc", | 306 protoc="//:protoc", |
236 **kargs): | 307 **kargs): |
(...skipping 17 matching lines...) Expand all Loading... |
254 protoc: the label of the protocol compiler to generate the sources. | 325 protoc: the label of the protocol compiler to generate the sources. |
255 **kargs: other keyword arguments that are passed to cc_library. | 326 **kargs: other keyword arguments that are passed to cc_library. |
256 | 327 |
257 """ | 328 """ |
258 outs = _PyOuts(srcs) | 329 outs = _PyOuts(srcs) |
259 | 330 |
260 includes = [] | 331 includes = [] |
261 if include != None: | 332 if include != None: |
262 includes = [include] | 333 includes = [include] |
263 | 334 |
264 _proto_gen( | 335 proto_gen( |
265 name=name + "_genproto", | 336 name=name + "_genproto", |
266 srcs=srcs, | 337 srcs=srcs, |
267 deps=[s + "_genproto" for s in deps], | 338 deps=[s + "_genproto" for s in deps], |
268 includes=includes, | 339 includes=includes, |
269 protoc=protoc, | 340 protoc=protoc, |
270 gen_py=1, | 341 gen_py=1, |
271 outs=outs, | 342 outs=outs, |
272 visibility=["//visibility:public"], | 343 visibility=["//visibility:public"], |
273 ) | 344 ) |
274 | 345 |
(...skipping 20 matching lines...) Expand all Loading... |
295 kargs: extra parameters that will be passed into the py_test. | 366 kargs: extra parameters that will be passed into the py_test. |
296 | 367 |
297 """ | 368 """ |
298 for m in modules: | 369 for m in modules: |
299 s = "python/google/protobuf/internal/%s.py" % m | 370 s = "python/google/protobuf/internal/%s.py" % m |
300 native.py_test( | 371 native.py_test( |
301 name="py_%s" % m, | 372 name="py_%s" % m, |
302 srcs=[s], | 373 srcs=[s], |
303 main=s, | 374 main=s, |
304 **kargs) | 375 **kargs) |
OLD | NEW |