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 | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 4 | if ctx.attr.include == None: |
| 5 | return "" |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 6 | if not ctx.attr.include: |
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 | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 9 | return ctx.attr.include |
| 10 | return ctx.label.package + '/' + ctx.attr.include |
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 | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 47 | import_flags = ["-I" + gen_dir] |
| 48 | for dep in ctx.attr.deps: |
| 49 | import_flags += dep.proto.import_flags |
| 50 | deps += dep.proto.deps |
| 51 | |
| 52 | args = [] |
| 53 | if ctx.attr.gen_cc: |
| 54 | args += ["--cpp_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
| 55 | if ctx.attr.gen_py: |
| 56 | args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir] |
| 57 | |
| 58 | if args: |
| 59 | ctx.action( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 60 | inputs=srcs + deps, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 61 | outputs=ctx.outputs.outs, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 62 | arguments=args + import_flags + [s.path for s in srcs], |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 63 | executable=ctx.executable.protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 64 | ) |
| 65 | |
| 66 | return struct( |
| 67 | proto=struct( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 68 | srcs=srcs, |
| 69 | import_flags=import_flags, |
| 70 | deps=deps, |
| 71 | ), |
| 72 | ) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 73 | |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 74 | _proto_gen = rule( |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 75 | attrs = { |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 76 | "srcs": attr.label_list(allow_files = True), |
| 77 | "deps": attr.label_list(providers = ["proto"]), |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 78 | "include": attr.string(), |
Jisi Liu | ee8131a | 2015-10-14 17:20:05 -0700 | [diff] [blame] | 79 | "protoc": attr.label( |
| 80 | executable = True, |
| 81 | single_file = True, |
| 82 | mandatory = True, |
| 83 | ), |
| 84 | "gen_cc": attr.bool(), |
| 85 | "gen_py": attr.bool(), |
| 86 | "outs": attr.output_list(), |
| 87 | }, |
| 88 | output_to_genfiles = True, |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 89 | implementation = _proto_gen_impl, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 90 | ) |
| 91 | |
| 92 | def cc_proto_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 93 | name, |
| 94 | srcs=[], |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 95 | deps=[], |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 96 | cc_libs=[], |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 97 | include=None, |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 98 | protoc=":protoc", |
| 99 | internal_bootstrap_hack=False, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 100 | **kargs): |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 101 | """Bazel rule to create a C++ protobuf library from proto source files |
| 102 | |
| 103 | Args: |
| 104 | name: the name of the cc_proto_library. |
| 105 | srcs: the .proto files of the cc_proto_library. |
| 106 | deps: a list of dependency labels; must be cc_proto_library. |
| 107 | cc_libs: a list of other cc_library targets depended by the generated |
| 108 | cc_library. |
| 109 | include: a string indicating the include path of the .proto files. |
| 110 | protoc: the label of the protocol compiler to generate the sources. |
| 111 | internal_bootstrap_hack: a flag indicate the cc_proto_library is used only |
| 112 | for bootstraping. When it is set to True, no files will be generated. |
| 113 | The rule will simply be a provider for .proto files, so that other |
| 114 | cc_proto_library can depend on it. |
| 115 | **kargs: other keyword arguments that are passed to cc_library. |
| 116 | |
| 117 | """ |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 118 | |
| 119 | if internal_bootstrap_hack: |
| 120 | # For pre-checked-in generated files, we add the internal_bootstrap_hack |
| 121 | # which will skip the codegen action. |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 122 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 123 | name=name + "_genproto", |
| 124 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 125 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 126 | include=include, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 127 | protoc=protoc, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 128 | ) |
| 129 | # An empty cc_library to make rule dependency consistent. |
| 130 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 131 | name=name, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 132 | **kargs) |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 133 | return |
| 134 | |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame^] | 135 | outs = _CcOuts(srcs) |
Jisi Liu | 9c7d9c0 | 2015-10-15 10:51:32 -0700 | [diff] [blame] | 136 | _proto_gen( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 137 | name=name + "_genproto", |
| 138 | srcs=srcs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 139 | deps=[s + "_genproto" for s in deps], |
Jisi Liu | 3101e73 | 2015-10-16 12:46:26 -0700 | [diff] [blame] | 140 | include=include, |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 141 | protoc=protoc, |
| 142 | gen_cc=1, |
| 143 | outs=outs, |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 144 | ) |
| 145 | |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 146 | includes = [] |
| 147 | if include != None: |
| 148 | includes = [include] |
| 149 | |
Jisi Liu | 39362b3 | 2015-10-14 17:12:11 -0700 | [diff] [blame] | 150 | native.cc_library( |
Jisi Liu | 125a91b | 2015-10-14 17:37:39 -0700 | [diff] [blame] | 151 | name=name, |
| 152 | srcs=outs, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 153 | deps=cc_libs + deps, |
Jisi Liu | 6dac082 | 2015-10-19 14:41:00 -0700 | [diff] [blame] | 154 | includes=includes, |
Jisi Liu | d8701b5 | 2015-10-16 11:44:21 -0700 | [diff] [blame] | 155 | **kargs) |
Jisi Liu | 993fb70 | 2015-10-19 17:19:49 -0700 | [diff] [blame^] | 156 | |
| 157 | |
| 158 | def copied_srcs( |
| 159 | name, |
| 160 | srcs, |
| 161 | include, |
| 162 | **kargs): |
| 163 | outs = [_RelativeOutputPath(s, include) for s in srcs] |
| 164 | |
| 165 | native.genrule( |
| 166 | name=name+"_genrule", |
| 167 | srcs=srcs, |
| 168 | outs=outs, |
| 169 | cmd=";".join(["cp $(location %s) $(location %s)" % \ |
| 170 | (s, _RelativeOutputPath(s, include)) \ |
| 171 | for s in srcs])) |
| 172 | |
| 173 | native.filegroup( |
| 174 | name=name, |
| 175 | srcs=outs, |
| 176 | **kargs) |
| 177 | |
| 178 | |
| 179 | def py_proto_library( |
| 180 | name, |
| 181 | srcs=[], |
| 182 | deps=[], |
| 183 | py_libs=[], |
| 184 | py_extra_srcs=[], |
| 185 | include=None, |
| 186 | protoc=":protoc", |
| 187 | **kargs): |
| 188 | outs = _PyOuts(srcs) |
| 189 | _proto_gen( |
| 190 | name=name + "_genproto", |
| 191 | srcs=srcs, |
| 192 | deps=[s + "_genproto" for s in deps], |
| 193 | include=include, |
| 194 | protoc=protoc, |
| 195 | gen_py=1, |
| 196 | outs=outs, |
| 197 | ) |
| 198 | |
| 199 | copied_srcs_name=name + "_copied_srcs" |
| 200 | if include != None: |
| 201 | copied_srcs( |
| 202 | name=copied_srcs_name, |
| 203 | srcs=outs, |
| 204 | include=include) |
| 205 | srcs=[copied_srcs_name] |
| 206 | |
| 207 | native.py_library( |
| 208 | name=name, |
| 209 | srcs=srcs+py_extra_srcs, |
| 210 | deps=py_libs, |
| 211 | **kargs) |
| 212 | |
| 213 | def internal_protobuf_py_tests( |
| 214 | name, |
| 215 | modules=[], |
| 216 | **kargs): |
| 217 | for m in modules: |
| 218 | native.py_test( |
| 219 | name="py_%s" % m, |
| 220 | srcs=["google/protobuf/internal/%s.py" % m], |
| 221 | main="google/protobuf/internal/%s.py" % m, |
| 222 | **kargs) |