Brian Silverman | 38658b0 | 2015-09-13 02:25:50 -0400 | [diff] [blame] | 1 | def _single_queue_file_impl(ctx): |
| 2 | args = [ |
| 3 | '-h_file_path', ctx.outputs.header.path, |
| 4 | '-cc_file_path', ctx.outputs.cc.path, |
| 5 | '-src_filename', ctx.file.src.short_path, |
| 6 | '-I', '.', |
| 7 | ctx.file.src.path, |
| 8 | ] |
| 9 | ctx.action( |
| 10 | outputs = [ |
| 11 | ctx.outputs.header, |
| 12 | ctx.outputs.cc, |
| 13 | ], |
| 14 | inputs = [ ctx.file.src ] + ctx.attr.q_deps.transitive_q_files, |
| 15 | executable = ctx.executable._queue_compiler, |
| 16 | arguments = args, |
| 17 | mnemonic = 'QGen', |
| 18 | progress_message = 'Generating C++ code for %s' % ctx.file.src.short_path, |
| 19 | ) |
| 20 | |
Brian Silverman | b200c17 | 2017-01-02 17:35:35 -0800 | [diff] [blame^] | 21 | def _single_queue_file_outputs(src): |
Brian Silverman | 38658b0 | 2015-09-13 02:25:50 -0400 | [diff] [blame] | 22 | return { |
Brian Silverman | b200c17 | 2017-01-02 17:35:35 -0800 | [diff] [blame^] | 23 | 'header': src.name + '.h', |
| 24 | 'cc': src.name + '.cc', |
Brian Silverman | 38658b0 | 2015-09-13 02:25:50 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | _single_queue_file = rule( |
| 28 | implementation = _single_queue_file_impl, |
| 29 | attrs = { |
| 30 | 'src': attr.label( |
| 31 | mandatory = True, |
| 32 | single_file = True, |
Brian Silverman | b200c17 | 2017-01-02 17:35:35 -0800 | [diff] [blame^] | 33 | allow_files = ['.q'], |
Brian Silverman | 38658b0 | 2015-09-13 02:25:50 -0400 | [diff] [blame] | 34 | ), |
| 35 | 'q_deps': attr.label( |
| 36 | providers = ['transitive_q_files'], |
| 37 | mandatory = True, |
| 38 | ), |
| 39 | 'package_name': attr.string( |
| 40 | mandatory = True, |
| 41 | ), |
| 42 | '_queue_compiler': attr.label( |
| 43 | executable = True, |
| 44 | default = Label('//aos/build/queues:compiler'), |
Brian Silverman | b200c17 | 2017-01-02 17:35:35 -0800 | [diff] [blame^] | 45 | cfg = 'host', |
Brian Silverman | 38658b0 | 2015-09-13 02:25:50 -0400 | [diff] [blame] | 46 | ), |
| 47 | }, |
| 48 | outputs = _single_queue_file_outputs, |
| 49 | output_to_genfiles = True, |
| 50 | ) |
| 51 | |
| 52 | def _q_deps_impl(ctx): |
| 53 | transitive_q_files = ctx.files.srcs |
| 54 | for dep in ctx.attr.deps: |
| 55 | transitive_q_files += dep.transitive_q_files |
| 56 | return struct(transitive_q_files = transitive_q_files) |
| 57 | |
| 58 | _q_deps = rule( |
| 59 | implementation = _q_deps_impl, |
| 60 | attrs = { |
| 61 | 'srcs': attr.label_list( |
| 62 | mandatory = True, |
| 63 | non_empty = True, |
Brian Silverman | b200c17 | 2017-01-02 17:35:35 -0800 | [diff] [blame^] | 64 | allow_files = ['.q'], |
Brian Silverman | 38658b0 | 2015-09-13 02:25:50 -0400 | [diff] [blame] | 65 | ), |
| 66 | 'deps': attr.label_list( |
| 67 | mandatory = True, |
| 68 | non_empty = False, |
| 69 | providers = ['transitive_q_files'], |
| 70 | ), |
| 71 | }, |
| 72 | ) |
| 73 | |
| 74 | '''Creates a C++ library from a set of .q files. |
| 75 | |
| 76 | Attrs: |
| 77 | srcs: A list of .q files. |
| 78 | deps: Other queue_library rules this one depends on. |
| 79 | ''' |
| 80 | def queue_library(name, srcs, deps = [], |
| 81 | visibility = None): |
| 82 | q_deps = _q_deps( |
| 83 | name = name + '__q_deps', |
| 84 | srcs = srcs, |
| 85 | deps = [dep + '__q_deps' for dep in deps], |
| 86 | visibility = visibility, |
| 87 | ) |
| 88 | |
| 89 | for src in srcs: |
| 90 | _single_queue_file( |
| 91 | name = name + '_' + src, |
| 92 | src = src, |
| 93 | q_deps = ':%s__q_deps' % name, |
| 94 | package_name = PACKAGE_NAME, |
| 95 | visibility = ['//visibility:private'], |
| 96 | ) |
| 97 | |
| 98 | native.cc_library( |
| 99 | name = name, |
| 100 | srcs = [src + '.cc' for src in srcs], |
| 101 | hdrs = [src + '.h' for src in srcs], |
| 102 | deps = deps + [ |
| 103 | '//aos/common:once', |
| 104 | '//aos/common:queues', |
| 105 | '//aos/common:queue_types', |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 106 | '//aos/common/logging:printf_formats', |
Brian Silverman | 38658b0 | 2015-09-13 02:25:50 -0400 | [diff] [blame] | 107 | ], |
| 108 | visibility = visibility, |
| 109 | ) |