OLD | NEW |
(Empty) | |
| 1 # -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED |
| 2 |
| 3 def _GenDir(ctx): |
| 4 if not ctx.attr.includes: |
| 5 return "" |
| 6 if not ctx.attr.includes[0]: |
| 7 return ctx.label.package |
| 8 if not ctx.label.package: |
| 9 return ctx.attr.includes[0] |
| 10 return ctx.label.package + '/' + ctx.attr.includes[0] |
| 11 |
| 12 def _CcOuts(srcs): |
| 13 return [s[:-len(".proto")] + ".pb.h" for s in srcs] + \ |
| 14 [s[:-len(".proto")] + ".pb.cc" for s in srcs] |
| 15 |
| 16 def _PyOuts(srcs): |
| 17 return [s[:-len(".proto")] + "_pb2.py" for s in srcs] |
| 18 |
| 19 def _RelativeOutputPath(path, include): |
| 20 if include == None: |
| 21 return path |
| 22 |
| 23 if not path.startswith(include): |
| 24 fail("Include path %s isn't part of the path %s." % (include, path)) |
| 25 |
| 26 if include and include[-1] != '/': |
| 27 include = include + '/' |
| 28 |
| 29 path = path[len(include):] |
| 30 |
| 31 if not path.startswith(PACKAGE_NAME): |
| 32 fail("The package %s is not within the path %s" % (PACKAGE_NAME, path)) |
| 33 |
| 34 if not PACKAGE_NAME: |
| 35 return path |
| 36 |
| 37 return path[len(PACKAGE_NAME)+1:] |
| 38 |
| 39 |
| 40 |
| 41 def _proto_gen_impl(ctx): |
| 42 """General implementation for generating protos""" |
| 43 srcs = ctx.files.srcs |
| 44 deps = [] |
| 45 deps += ctx.files.srcs |
| 46 gen_dir = _GenDir(ctx) |
| 47 if gen_dir: |
| 48 import_flags = ["-I" + gen_dir] |
| 49 else: |
| 50 import_flags = ["-I."] |
| 51 |
| 52 for dep in ctx.attr.deps: |
| 53 import_flags += dep.proto.import_flags |
| 54 deps += dep.proto.deps |
| 55 |
| 56 args = [] |
| 57 if ctx.attr.gen_cc: |
| 58 args += ["--cpp_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
| 59 if ctx.attr.gen_py: |
| 60 args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
| 61 |
| 62 if args: |
| 63 ctx.action( |
| 64 inputs=srcs + deps, |
| 65 outputs=ctx.outputs.outs, |
| 66 arguments=args + import_flags + [s.path for s in srcs], |
| 67 executable=ctx.executable.protoc, |
| 68 ) |
| 69 |
| 70 return struct( |
| 71 proto=struct( |
| 72 srcs=srcs, |
| 73 import_flags=import_flags, |
| 74 deps=deps, |
| 75 ), |
| 76 ) |
| 77 |
| 78 _proto_gen = rule( |
| 79 attrs = { |
| 80 "srcs": attr.label_list(allow_files = True), |
| 81 "deps": attr.label_list(providers = ["proto"]), |
| 82 "includes": attr.string_list(), |
| 83 "protoc": attr.label( |
| 84 cfg = HOST_CFG, |
| 85 executable = True, |
| 86 single_file = True, |
| 87 mandatory = True, |
| 88 ), |
| 89 "gen_cc": attr.bool(), |
| 90 "gen_py": attr.bool(), |
| 91 "outs": attr.output_list(), |
| 92 }, |
| 93 output_to_genfiles = True, |
| 94 implementation = _proto_gen_impl, |
| 95 ) |
| 96 |
| 97 def cc_proto_library( |
| 98 name, |
| 99 srcs=[], |
| 100 deps=[], |
| 101 cc_libs=[], |
| 102 include=None, |
| 103 protoc="//google/protobuf:protoc", |
| 104 internal_bootstrap_hack=False, |
| 105 default_runtime="//google/protobuf:protobuf", |
| 106 **kargs): |
| 107 """Bazel rule to create a C++ protobuf library from proto source files |
| 108 |
| 109 NOTE: the rule is only an internal workaround to generate protos. The |
| 110 interface may change and the rule may be removed when bazel has introduced |
| 111 the native rule. |
| 112 |
| 113 Args: |
| 114 name: the name of the cc_proto_library. |
| 115 srcs: the .proto files of the cc_proto_library. |
| 116 deps: a list of dependency labels; must be cc_proto_library. |
| 117 cc_libs: a list of other cc_library targets depended by the generated |
| 118 cc_library. |
| 119 include: a string indicating the include path of the .proto files. |
| 120 protoc: the label of the protocol compiler to generate the sources. |
| 121 internal_bootstrap_hack: a flag indicate the cc_proto_library is used only |
| 122 for bootstraping. When it is set to True, no files will be generated. |
| 123 The rule will simply be a provider for .proto files, so that other |
| 124 cc_proto_library can depend on it. |
| 125 default_runtime: the implicitly default runtime which will be depended on by |
| 126 the generated cc_library target. |
| 127 **kargs: other keyword arguments that are passed to cc_library. |
| 128 |
| 129 """ |
| 130 |
| 131 includes = [] |
| 132 if include != None: |
| 133 includes = [include] |
| 134 |
| 135 if internal_bootstrap_hack: |
| 136 # For pre-checked-in generated files, we add the internal_bootstrap_hack |
| 137 # which will skip the codegen action. |
| 138 _proto_gen( |
| 139 name=name + "_genproto", |
| 140 srcs=srcs, |
| 141 deps=[s + "_genproto" for s in deps], |
| 142 includes=includes, |
| 143 protoc=protoc, |
| 144 visibility=["//visibility:public"], |
| 145 ) |
| 146 # An empty cc_library to make rule dependency consistent. |
| 147 native.cc_library( |
| 148 name=name, |
| 149 **kargs) |
| 150 return |
| 151 |
| 152 outs = _CcOuts(srcs) |
| 153 _proto_gen( |
| 154 name=name + "_genproto", |
| 155 srcs=srcs, |
| 156 deps=[s + "_genproto" for s in deps], |
| 157 includes=includes, |
| 158 protoc=protoc, |
| 159 gen_cc=1, |
| 160 outs=outs, |
| 161 visibility=["//visibility:public"], |
| 162 ) |
| 163 |
| 164 if default_runtime and not default_runtime in cc_libs: |
| 165 cc_libs += [default_runtime] |
| 166 |
| 167 native.cc_library( |
| 168 name=name, |
| 169 srcs=outs, |
| 170 deps=cc_libs + deps, |
| 171 includes=includes, |
| 172 **kargs) |
| 173 |
| 174 |
| 175 def internal_copied_filegroup( |
| 176 name, |
| 177 srcs, |
| 178 include, |
| 179 **kargs): |
| 180 """Bazel rule to fix sources file to workaround with python path issues. |
| 181 |
| 182 Args: |
| 183 name: the name of the internal_copied_filegroup rule, which will be the |
| 184 name of the generated filegroup. |
| 185 srcs: the source files to be copied. |
| 186 include: the expected import root of the source. |
| 187 **kargs: extra arguments that will be passed into the filegroup. |
| 188 """ |
| 189 outs = [_RelativeOutputPath(s, include) for s in srcs] |
| 190 |
| 191 native.genrule( |
| 192 name=name+"_genrule", |
| 193 srcs=srcs, |
| 194 outs=outs, |
| 195 cmd=" && ".join(["cp $(location %s) $(location %s)" % |
| 196 (s, _RelativeOutputPath(s, include)) |
| 197 for s in srcs])) |
| 198 |
| 199 native.filegroup( |
| 200 name=name, |
| 201 srcs=outs, |
| 202 **kargs) |
| 203 |
| 204 |
| 205 def py_proto_library( |
| 206 name, |
| 207 srcs=[], |
| 208 deps=[], |
| 209 py_libs=[], |
| 210 py_extra_srcs=[], |
| 211 include=None, |
| 212 default_runtime="//google/protobuf:protobuf_python", |
| 213 protoc="//google/protobuf:protoc", |
| 214 **kargs): |
| 215 """Bazel rule to create a Python protobuf library from proto source files |
| 216 |
| 217 NOTE: the rule is only an internal workaround to generate protos. The |
| 218 interface may change and the rule may be removed when bazel has introduced |
| 219 the native rule. |
| 220 |
| 221 Args: |
| 222 name: the name of the py_proto_library. |
| 223 srcs: the .proto files of the py_proto_library. |
| 224 deps: a list of dependency labels; must be py_proto_library. |
| 225 py_libs: a list of other py_library targets depended by the generated |
| 226 py_library. |
| 227 py_extra_srcs: extra source files that will be added to the output |
| 228 py_library. This attribute is used for internal bootstrapping. |
| 229 include: a string indicating the include path of the .proto files. |
| 230 default_runtime: the implicitly default runtime which will be depended on by |
| 231 the generated py_library target. |
| 232 protoc: the label of the protocol compiler to generate the sources. |
| 233 **kargs: other keyword arguments that are passed to cc_library. |
| 234 |
| 235 """ |
| 236 outs = _PyOuts(srcs) |
| 237 |
| 238 includes = [] |
| 239 if include != None: |
| 240 includes = [include] |
| 241 |
| 242 _proto_gen( |
| 243 name=name + "_genproto", |
| 244 srcs=srcs, |
| 245 deps=[s + "_genproto" for s in deps], |
| 246 includes=includes, |
| 247 protoc=protoc, |
| 248 gen_py=1, |
| 249 outs=outs, |
| 250 visibility=["//visibility:public"], |
| 251 ) |
| 252 |
| 253 if include != None: |
| 254 # Copy the output files to the desired location to make the import work. |
| 255 internal_copied_filegroup_name=name + "_internal_copied_filegroup" |
| 256 internal_copied_filegroup( |
| 257 name=internal_copied_filegroup_name, |
| 258 srcs=outs, |
| 259 include=include) |
| 260 outs=[internal_copied_filegroup_name] |
| 261 |
| 262 if default_runtime and not default_runtime in py_libs + deps: |
| 263 py_libs += [default_runtime] |
| 264 |
| 265 native.py_library( |
| 266 name=name, |
| 267 srcs=outs+py_extra_srcs, |
| 268 deps=py_libs+deps, |
| 269 **kargs) |
| 270 |
| 271 def internal_protobuf_py_tests( |
| 272 name, |
| 273 modules=[], |
| 274 **kargs): |
| 275 """Bazel rules to create batch tests for protobuf internal. |
| 276 |
| 277 Args: |
| 278 name: the name of the rule. |
| 279 modules: a list of modules for tests. The macro will create a py_test for |
| 280 each of the parameter with the source "google/protobuf/%s.py" |
| 281 kargs: extra parameters that will be passed into the py_test. |
| 282 |
| 283 """ |
| 284 for m in modules: |
| 285 s = _RelativeOutputPath( |
| 286 "python/google/protobuf/internal/%s.py" % m, "python") |
| 287 native.py_test( |
| 288 name="py_%s" % m, |
| 289 srcs=[s], |
| 290 main=s, |
| 291 **kargs) |
OLD | NEW |