blob: 11753c2582ac5ff2bc4c9a60fa4e20373b77eeb2 [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(
41 implementation = _do_proto_cc_library_impl,
42 attrs = {
Brian Silverman7f912222017-03-11 13:58:00 -080043 'src': attr.label(
Brian Silvermaneb16fa42016-02-20 15:29:56 -050044 allow_files = FileType(['.proto']),
Brian Silverman7f912222017-03-11 13:58:00 -080045 mandatory = True,
46 single_file = True,
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,
Brian Silverman7f912222017-03-11 13:58:00 -080073 src = src,
Parker Schuh971588a2017-03-01 22:15:04 -080074 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 )