Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 1 | # -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED |
| 2 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 3 | def _GenDir(ctx): |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 4 | if not ctx.attr.includes: |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 5 | return "" |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 6 | if not ctx.attr.includes[0]: |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 7 | return ctx.label.package |
| 8 | if not ctx.label.package: |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 9 | return ctx.attr.includes[0] |
| 10 | return ctx.label.package + '/' + ctx.attr.includes[0] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 11 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 12 | def _CcOuts(srcs): |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 13 | return [s[:-len(".proto")] + ".pb.h" for s in srcs] + \ |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 14 | [s[:-len(".proto")] + ".pb.cc" for s in srcs] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 15 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 16 | def _PyOuts(srcs): |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 17 | return [s[:-len(".proto")] + "_pb2.py" for s in srcs] |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 18 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 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 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 41 | def _proto_gen_impl(ctx): |
| 42 | """General implementation for generating protos""" |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 43 | srcs = ctx.files.srcs |
| 44 | deps = [] |
| 45 | deps += ctx.files.srcs |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 46 | gen_dir = _GenDir(ctx) |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 47 | if gen_dir: |
| 48 | import_flags = ["-I" + gen_dir] |
| 49 | else: |
| 50 | import_flags = ["-I."] |
| 51 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 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( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 64 | inputs=srcs + deps, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 65 | outputs=ctx.outputs.outs, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 66 | arguments=args + import_flags + [s.path for s in srcs], |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 67 | executable=ctx.executable.protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 68 | ) |
| 69 | |
| 70 | return struct( |
| 71 | proto=struct( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 72 | srcs=srcs, |
| 73 | import_flags=import_flags, |
| 74 | deps=deps, |
| 75 | ), |
| 76 | ) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 77 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 78 | _proto_gen = rule( |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 79 | attrs = { |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 80 | "srcs": attr.label_list(allow_files = True), |
| 81 | "deps": attr.label_list(providers = ["proto"]), |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 82 | "includes": attr.string_list(), |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 83 | "protoc": attr.label( |
Andrew Harp | 38f131f | 2015-11-03 16:39:32 -0500 | [diff] [blame] | 84 | cfg = HOST_CFG, |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 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, |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 94 | implementation = _proto_gen_impl, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 95 | ) |
| 96 | |
| 97 | def cc_proto_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 98 | name, |
| 99 | srcs=[], |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 100 | deps=[], |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 101 | cc_libs=[], |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 102 | include=None, |
Jisi Liu | 04658a3 | 2015-10-20 15:00:13 -0700 | [diff] [blame] | 103 | protoc="//google/protobuf:protoc", |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 104 | internal_bootstrap_hack=False, |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 105 | default_runtime="//google/protobuf:protobuf", |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 106 | **kargs): |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 107 | """Bazel rule to create a C++ protobuf library from proto source files |
| 108 | |
Jisi Liu | d4bef7d | 2015-11-02 12:24:32 -0800 | [diff] [blame] | 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 | |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 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. |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 125 | default_runtime: the implicitly default runtime which will be depended on by |
| 126 | the generated cc_library target. |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 127 | **kargs: other keyword arguments that are passed to cc_library. |
| 128 | |
| 129 | """ |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 130 | |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 131 | includes = [] |
| 132 | if include != None: |
| 133 | includes = [include] |
| 134 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 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. |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 138 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 139 | name=name + "_genproto", |
| 140 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 141 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 142 | includes=includes, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 143 | protoc=protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 144 | ) |
| 145 | # An empty cc_library to make rule dependency consistent. |
| 146 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 147 | name=name, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 148 | **kargs) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 149 | return |
| 150 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 151 | outs = _CcOuts(srcs) |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 152 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 153 | name=name + "_genproto", |
| 154 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 155 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 156 | includes=includes, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 157 | protoc=protoc, |
| 158 | gen_cc=1, |
| 159 | outs=outs, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 160 | ) |
| 161 | |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 162 | if default_runtime and not default_runtime in cc_libs: |
| 163 | cc_libs += [default_runtime] |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 164 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 165 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 166 | name=name, |
| 167 | srcs=outs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 168 | deps=cc_libs + deps, |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 169 | includes=includes, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 170 | **kargs) |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 171 | |
| 172 | |
Jisi Liu | bc4fd15 | 2015-10-20 16:02:58 -0700 | [diff] [blame] | 173 | def internal_copied_filegroup( |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 174 | name, |
| 175 | srcs, |
| 176 | include, |
| 177 | **kargs): |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 178 | """Bazel rule to fix sources file to workaround with python path issues. |
| 179 | |
| 180 | Args: |
Jisi Liu | bc4fd15 | 2015-10-20 16:02:58 -0700 | [diff] [blame] | 181 | name: the name of the internal_copied_filegroup rule, which will be the |
| 182 | name of the generated filegroup. |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 183 | srcs: the source files to be copied. |
| 184 | include: the expected import root of the source. |
| 185 | **kargs: extra arguments that will be passed into the filegroup. |
| 186 | """ |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 187 | outs = [_RelativeOutputPath(s, include) for s in srcs] |
| 188 | |
| 189 | native.genrule( |
| 190 | name=name+"_genrule", |
| 191 | srcs=srcs, |
| 192 | outs=outs, |
Jisi Liu | 6ddcae2 | 2015-10-21 10:48:33 -0700 | [diff] [blame] | 193 | cmd=" && ".join(["cp $(location %s) $(location %s)" % |
| 194 | (s, _RelativeOutputPath(s, include)) |
| 195 | for s in srcs])) |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 196 | |
| 197 | native.filegroup( |
| 198 | name=name, |
| 199 | srcs=outs, |
| 200 | **kargs) |
| 201 | |
| 202 | |
| 203 | def py_proto_library( |
| 204 | name, |
| 205 | srcs=[], |
| 206 | deps=[], |
| 207 | py_libs=[], |
| 208 | py_extra_srcs=[], |
| 209 | include=None, |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 210 | default_runtime="//google/protobuf:protobuf_python", |
Jisi Liu | 04658a3 | 2015-10-20 15:00:13 -0700 | [diff] [blame] | 211 | protoc="//google/protobuf:protoc", |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 212 | **kargs): |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 213 | """Bazel rule to create a Python protobuf library from proto source files |
| 214 | |
Jisi Liu | d4bef7d | 2015-11-02 12:24:32 -0800 | [diff] [blame] | 215 | NOTE: the rule is only an internal workaround to generate protos. The |
| 216 | interface may change and the rule may be removed when bazel has introduced |
| 217 | the native rule. |
| 218 | |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 219 | Args: |
| 220 | name: the name of the py_proto_library. |
| 221 | srcs: the .proto files of the py_proto_library. |
| 222 | deps: a list of dependency labels; must be py_proto_library. |
| 223 | py_libs: a list of other py_library targets depended by the generated |
| 224 | py_library. |
| 225 | py_extra_srcs: extra source files that will be added to the output |
| 226 | py_library. This attribute is used for internal bootstrapping. |
| 227 | include: a string indicating the include path of the .proto files. |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 228 | default_runtime: the implicitly default runtime which will be depended on by |
| 229 | the generated py_library target. |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 230 | protoc: the label of the protocol compiler to generate the sources. |
| 231 | **kargs: other keyword arguments that are passed to cc_library. |
| 232 | |
| 233 | """ |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 234 | outs = _PyOuts(srcs) |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 235 | |
| 236 | includes = [] |
| 237 | if include != None: |
| 238 | includes = [include] |
| 239 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 240 | _proto_gen( |
| 241 | name=name + "_genproto", |
| 242 | srcs=srcs, |
| 243 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 53a56be | 2015-10-20 15:18:20 -0700 | [diff] [blame] | 244 | includes=includes, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 245 | protoc=protoc, |
| 246 | gen_py=1, |
| 247 | outs=outs, |
| 248 | ) |
| 249 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 250 | if include != None: |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 251 | # Copy the output files to the desired location to make the import work. |
Jisi Liu | bc4fd15 | 2015-10-20 16:02:58 -0700 | [diff] [blame] | 252 | internal_copied_filegroup_name=name + "_internal_copied_filegroup" |
| 253 | internal_copied_filegroup( |
| 254 | name=internal_copied_filegroup_name, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 255 | srcs=outs, |
| 256 | include=include) |
Jisi Liu | bc4fd15 | 2015-10-20 16:02:58 -0700 | [diff] [blame] | 257 | outs=[internal_copied_filegroup_name] |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 258 | |
Jisi Liu | be92ffb | 2015-10-27 15:11:38 -0700 | [diff] [blame] | 259 | if default_runtime and not default_runtime in py_libs + deps: |
| 260 | py_libs += [default_runtime] |
| 261 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 262 | native.py_library( |
| 263 | name=name, |
Jisi Liu | a33fa8e | 2015-10-20 15:30:44 -0700 | [diff] [blame] | 264 | srcs=outs+py_extra_srcs, |
| 265 | deps=py_libs+deps, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 266 | **kargs) |
| 267 | |
| 268 | def internal_protobuf_py_tests( |
| 269 | name, |
| 270 | modules=[], |
| 271 | **kargs): |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 272 | """Bazel rules to create batch tests for protobuf internal. |
| 273 | |
| 274 | Args: |
| 275 | name: the name of the rule. |
| 276 | modules: a list of modules for tests. The macro will create a py_test for |
| 277 | each of the parameter with the source "google/protobuf/%s.py" |
| 278 | kargs: extra parameters that will be passed into the py_test. |
| 279 | |
| 280 | """ |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 281 | for m in modules: |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 282 | s = _RelativeOutputPath( |
| 283 | "python/google/protobuf/internal/%s.py" % m, "python") |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 284 | native.py_test( |
| 285 | name="py_%s" % m, |
Jisi Liu | 7b948cc | 2015-10-19 17:56:27 -0700 | [diff] [blame] | 286 | srcs=[s], |
| 287 | main=s, |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame] | 288 | **kargs) |