blob: d5f4284d5c93c7e760a3a95e9c928483f23a694b [file] [log] [blame]
Brian Silvermaneb16fa42016-02-20 15:29:56 -05001def _do_proto_cc_library_impl(ctx):
2 message = 'Building %s and %s from %s' % (ctx.outputs.pb_h.short_path,
3 ctx.outputs.pb_cc.short_path,
4 ctx.file.src.short_path)
5 ctx.action(
6 inputs = ctx.files.src + ctx.files._well_known_protos,
7 executable = ctx.executable._protoc,
8 arguments = [
9 '--cpp_out=%s' % ctx.configuration.genfiles_dir.path,
10 '-I.',
11 '-Ithird_party/protobuf/src',
12 ctx.file.src.path,
13 ],
14 mnemonic = 'ProtocCc',
15 progress_message = message,
16 outputs = [
17 ctx.outputs.pb_h,
18 ctx.outputs.pb_cc,
19 ],
20 )
21
Brian Silvermanb200c172017-01-02 17:35:35 -080022def _do_proto_cc_library_outputs(src):
23 basename = src.name[:-len('.proto')]
Brian Silvermaneb16fa42016-02-20 15:29:56 -050024 return {
25 'pb_h': '%s.pb.h' % basename,
26 'pb_cc': '%s.pb.cc' % basename,
27 }
28
29_do_proto_cc_library = rule(
30 implementation = _do_proto_cc_library_impl,
31 attrs = {
32 'src': attr.label(
33 allow_files = FileType(['.proto']),
34 mandatory = True,
35 single_file = True,
36 ),
37 '_protoc': attr.label(
38 default = Label('//third_party/protobuf:protoc'),
39 executable = True,
Brian Silvermanb200c172017-01-02 17:35:35 -080040 cfg = 'host',
Brian Silvermaneb16fa42016-02-20 15:29:56 -050041 ),
42 '_well_known_protos': attr.label(
43 default = Label('//third_party/protobuf:well_known_protos'),
44 ),
45 },
46 outputs = _do_proto_cc_library_outputs,
47 output_to_genfiles = True,
48)
49
50def proto_cc_library(name, src, visibility = None):
51 '''Generates a cc_library from a single .proto file. Does not support
52 dependencies on any .proto files except the well-known ones protobuf comes
53 with (which are unconditionally depended on).
54
55 Attrs:
56 src: The .proto file.
57 '''
58
59 _do_proto_cc_library(
60 name = '%s__proto_srcs' % name,
61 src = src,
62 visibility = ['//visibility:private'],
63 )
64 basename = src[:-len('.proto')]
65 native.cc_library(
66 name = name,
67 srcs = [ '%s.pb.cc' % basename ],
68 hdrs = [ '%s.pb.h' % basename ],
69 deps = [ '//third_party/protobuf' ],
70 visibility = visibility,
71 )