blob: f6a98b86988d52a1849d75dff7edd7a26c7fb0c1 [file] [log] [blame]
Brian Silvermaneb16fa42016-02-20 15:29:56 -05001def _do_proto_cc_library_impl(ctx):
Parker Schuh971588a2017-03-01 22:15:04 -08002 srcs = ctx.files.srcs
3 deps = []
4 deps += ctx.files.srcs
5
6 for dep in ctx.attr.deps:
7 deps += dep.proto.deps
8
Brian Silvermaneb16fa42016-02-20 15:29:56 -05009 message = 'Building %s and %s from %s' % (ctx.outputs.pb_h.short_path,
10 ctx.outputs.pb_cc.short_path,
Parker Schuh971588a2017-03-01 22:15:04 -080011 ctx.files.srcs[0].short_path)
Brian Silvermaneb16fa42016-02-20 15:29:56 -050012 ctx.action(
Parker Schuh971588a2017-03-01 22:15:04 -080013 inputs = deps + ctx.files._well_known_protos,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050014 executable = ctx.executable._protoc,
15 arguments = [
16 '--cpp_out=%s' % ctx.configuration.genfiles_dir.path,
17 '-I.',
18 '-Ithird_party/protobuf/src',
Parker Schuh971588a2017-03-01 22:15:04 -080019 ] + [s.path for s in srcs],
Brian Silvermaneb16fa42016-02-20 15:29:56 -050020 mnemonic = 'ProtocCc',
21 progress_message = message,
22 outputs = [
23 ctx.outputs.pb_h,
24 ctx.outputs.pb_cc,
25 ],
26 )
27
Parker Schuh971588a2017-03-01 22:15:04 -080028 return struct(
29 proto = struct(
30 srcs = srcs,
31 deps = deps,
32 )
33 )
34
35def _do_proto_cc_library_outputs(srcs):
36 basename = srcs[0].name[:-len('.proto')]
Brian Silvermaneb16fa42016-02-20 15:29:56 -050037 return {
38 'pb_h': '%s.pb.h' % basename,
39 'pb_cc': '%s.pb.cc' % basename,
40 }
41
42_do_proto_cc_library = rule(
43 implementation = _do_proto_cc_library_impl,
44 attrs = {
Parker Schuh971588a2017-03-01 22:15:04 -080045 'srcs': attr.label_list(
Brian Silvermaneb16fa42016-02-20 15:29:56 -050046 allow_files = FileType(['.proto']),
Brian Silvermaneb16fa42016-02-20 15:29:56 -050047 ),
Parker Schuh971588a2017-03-01 22:15:04 -080048 'deps': attr.label_list(providers = ["proto"]),
Brian Silvermaneb16fa42016-02-20 15:29:56 -050049 '_protoc': attr.label(
50 default = Label('//third_party/protobuf:protoc'),
51 executable = True,
Brian Silvermanb200c172017-01-02 17:35:35 -080052 cfg = 'host',
Brian Silvermaneb16fa42016-02-20 15:29:56 -050053 ),
54 '_well_known_protos': attr.label(
55 default = Label('//third_party/protobuf:well_known_protos'),
56 ),
57 },
58 outputs = _do_proto_cc_library_outputs,
59 output_to_genfiles = True,
60)
61
Parker Schuh971588a2017-03-01 22:15:04 -080062def proto_cc_library(name, src, deps = [], visibility = None):
Brian Silvermaneb16fa42016-02-20 15:29:56 -050063 '''Generates a cc_library from a single .proto file. Does not support
64 dependencies on any .proto files except the well-known ones protobuf comes
65 with (which are unconditionally depended on).
66
67 Attrs:
68 src: The .proto file.
69 '''
70
71 _do_proto_cc_library(
72 name = '%s__proto_srcs' % name,
Parker Schuh971588a2017-03-01 22:15:04 -080073 srcs = [src],
74 deps = [('%s__proto_srcs' % o_name) for o_name in deps],
Parker Schuh24ee58d2017-03-11 16:13:23 -080075 visibility = visibility,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050076 )
77 basename = src[:-len('.proto')]
78 native.cc_library(
79 name = name,
80 srcs = [ '%s.pb.cc' % basename ],
81 hdrs = [ '%s.pb.h' % basename ],
Parker Schuh971588a2017-03-01 22:15:04 -080082 deps = [ '//third_party/protobuf' ] + deps,
Brian Silvermaneb16fa42016-02-20 15:29:56 -050083 visibility = visibility,
84 )