Brian Silverman | eb16fa4 | 2016-02-20 15:29:56 -0500 | [diff] [blame^] | 1 | def _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 | |
| 22 | def _do_proto_cc_library_outputs(attr): |
| 23 | basename = attr.src.name[:-len('.proto')] |
| 24 | 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, |
| 40 | cfg = HOST_CFG, |
| 41 | ), |
| 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 | |
| 50 | def 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 | ) |