blob: 9c92158b1be76b46c5edb33685b370b6b70cb895 [file] [log] [blame]
Brian Silvermaneb16fa42016-02-20 15:29:56 -05001def _do_proto_cc_library_impl(ctx):
Brian Silverman7f912222017-03-11 13:58:00 -08002 deps = [ctx.file.src]
Parker Schuh971588a2017-03-01 22:15:04 -08003
4 for dep in ctx.attr.deps:
5 deps += dep.proto.deps
6
Brian Silvermaneb16fa42016-02-20 15:29:56 -05007 message = 'Building %s and %s from %s' % (ctx.outputs.pb_h.short_path,
8 ctx.outputs.pb_cc.short_path,
Brian Silverman7f912222017-03-11 13:58:00 -08009 ctx.file.src.short_path)
Brian Silvermaneb16fa42016-02-20 15:29:56 -050010 ctx.action(
Parker Schuh971588a2017-03-01 22:15:04 -080011 inputs = deps + ctx.files._well_known_protos,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050012 executable = ctx.executable._protoc,
13 arguments = [
14 '--cpp_out=%s' % ctx.configuration.genfiles_dir.path,
15 '-I.',
16 '-Ithird_party/protobuf/src',
Brian Silverman7f912222017-03-11 13:58:00 -080017 ctx.file.src.path,
18 ],
Brian Silvermaneb16fa42016-02-20 15:29:56 -050019 mnemonic = 'ProtocCc',
20 progress_message = message,
21 outputs = [
22 ctx.outputs.pb_h,
23 ctx.outputs.pb_cc,
24 ],
25 )
26
Parker Schuh971588a2017-03-01 22:15:04 -080027 return struct(
28 proto = struct(
Parker Schuh971588a2017-03-01 22:15:04 -080029 deps = deps,
30 )
31 )
32
Brian Silverman7f912222017-03-11 13:58:00 -080033def _do_proto_cc_library_outputs(src):
34 basename = src.name[:-len('.proto')]
Brian Silvermaneb16fa42016-02-20 15:29:56 -050035 return {
36 'pb_h': '%s.pb.h' % basename,
37 'pb_cc': '%s.pb.cc' % basename,
38 }
39
40_do_proto_cc_library = rule(
Brian Silverman7a7c24d2018-09-01 17:49:09 -070041 attrs = {
42 "src": attr.label(
43 allow_files = FileType([".proto"]),
44 mandatory = True,
45 single_file = True,
46 ),
47 "deps": attr.label_list(providers = ["proto"]),
48 "_protoc": attr.label(
49 default = Label("//third_party/protobuf:protoc"),
50 executable = True,
51 cfg = "host",
52 ),
53 "_well_known_protos": attr.label(
54 default = Label("//third_party/protobuf:well_known_protos"),
55 ),
56 },
57 output_to_genfiles = True,
58 outputs = _do_proto_cc_library_outputs,
59 implementation = _do_proto_cc_library_impl,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050060)
61
Brian Silverman7a7c24d2018-09-01 17:49:09 -070062def proto_cc_library(name, src, deps = [],
63 compatible_with = None, restricted_to = None,
64 visibility = None):
Brian Silvermaneb16fa42016-02-20 15:29:56 -050065 '''Generates a cc_library from a single .proto file. Does not support
66 dependencies on any .proto files except the well-known ones protobuf comes
67 with (which are unconditionally depended on).
68
69 Attrs:
70 src: The .proto file.
71 '''
72
73 _do_proto_cc_library(
74 name = '%s__proto_srcs' % name,
Brian Silverman7f912222017-03-11 13:58:00 -080075 src = src,
Parker Schuh971588a2017-03-01 22:15:04 -080076 deps = [('%s__proto_srcs' % o_name) for o_name in deps],
Parker Schuh24ee58d2017-03-11 16:13:23 -080077 visibility = visibility,
Brian Silverman7a7c24d2018-09-01 17:49:09 -070078 compatible_with = compatible_with,
79 restricted_to = restricted_to,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050080 )
81 basename = src[:-len('.proto')]
82 native.cc_library(
83 name = name,
84 srcs = [ '%s.pb.cc' % basename ],
85 hdrs = [ '%s.pb.h' % basename ],
Parker Schuh971588a2017-03-01 22:15:04 -080086 deps = [ '//third_party/protobuf' ] + deps,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050087 visibility = visibility,
Brian Silverman7a7c24d2018-09-01 17:49:09 -070088 compatible_with = compatible_with,
89 restricted_to = restricted_to,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050090 )