blob: 840b4feebae0c486221b316df216ec6a081b4227 [file] [log] [blame]
Brian Silverman38658b02015-09-13 02:25:50 -04001def _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 Silvermanb200c172017-01-02 17:35:35 -080021def _single_queue_file_outputs(src):
Brian Silverman38658b02015-09-13 02:25:50 -040022 return {
Brian Silvermanb200c172017-01-02 17:35:35 -080023 'header': src.name + '.h',
24 'cc': src.name + '.cc',
Brian Silverman38658b02015-09-13 02:25:50 -040025 }
26
27_single_queue_file = rule(
Brian Silverman7a7c24d2018-09-01 17:49:09 -070028 attrs = {
29 "src": attr.label(
30 mandatory = True,
31 single_file = True,
32 allow_files = [".q"],
33 ),
34 "q_deps": attr.label(
35 providers = ["transitive_q_files"],
36 mandatory = True,
37 ),
38 "package_name": attr.string(
39 mandatory = True,
40 ),
41 "_queue_compiler": attr.label(
42 executable = True,
43 default = Label("//aos/build/queues:compiler"),
44 cfg = "host",
45 ),
46 },
47 output_to_genfiles = True,
48 outputs = _single_queue_file_outputs,
49 implementation = _single_queue_file_impl,
Brian Silverman38658b02015-09-13 02:25:50 -040050)
51
52def _q_deps_impl(ctx):
53 transitive_q_files = ctx.files.srcs
54 for dep in ctx.attr.deps:
Austin Schuhbe8c9b12017-11-25 15:53:12 -080055 transitive_q_files = transitive_q_files + dep.transitive_q_files
Brian Silverman38658b02015-09-13 02:25:50 -040056 return struct(transitive_q_files = transitive_q_files)
57
58_q_deps = rule(
Brian Silverman7a7c24d2018-09-01 17:49:09 -070059 attrs = {
60 "srcs": attr.label_list(
61 mandatory = True,
62 non_empty = True,
63 allow_files = [".q"],
64 ),
65 "deps": attr.label_list(
66 mandatory = True,
67 non_empty = False,
68 providers = ["transitive_q_files"],
69 ),
70 },
71 implementation = _q_deps_impl,
Brian Silverman38658b02015-09-13 02:25:50 -040072)
73
Brian Silverman7a7c24d2018-09-01 17:49:09 -070074"""Creates a C++ library from a set of .q files.
Brian Silverman38658b02015-09-13 02:25:50 -040075
76Attrs:
77 srcs: A list of .q files.
78 deps: Other queue_library rules this one depends on.
Brian Silverman7a7c24d2018-09-01 17:49:09 -070079"""
80
Brian Silverman38658b02015-09-13 02:25:50 -040081def queue_library(name, srcs, deps = [],
Brian Silverman7a7c24d2018-09-01 17:49:09 -070082 compatible_with = None, restricted_to = None,
Brian Silverman38658b02015-09-13 02:25:50 -040083 visibility = None):
84 q_deps = _q_deps(
85 name = name + '__q_deps',
86 srcs = srcs,
87 deps = [dep + '__q_deps' for dep in deps],
88 visibility = visibility,
Brian Silverman7a7c24d2018-09-01 17:49:09 -070089 compatible_with = compatible_with,
90 restricted_to = restricted_to,
Brian Silverman38658b02015-09-13 02:25:50 -040091 )
92
93 for src in srcs:
94 _single_queue_file(
95 name = name + '_' + src,
96 src = src,
97 q_deps = ':%s__q_deps' % name,
98 package_name = PACKAGE_NAME,
99 visibility = ['//visibility:private'],
Brian Silverman7a7c24d2018-09-01 17:49:09 -0700100 compatible_with = compatible_with,
101 restricted_to = restricted_to,
Brian Silverman38658b02015-09-13 02:25:50 -0400102 )
103
104 native.cc_library(
105 name = name,
106 srcs = [src + '.cc' for src in srcs],
107 hdrs = [src + '.h' for src in srcs],
108 deps = deps + [
Sabina Davis2ed5ea22017-09-26 22:27:42 -0700109 '//aos:once',
Brian Silverman38658b02015-09-13 02:25:50 -0400110 '//aos/common:queues',
111 '//aos/common:queue_types',
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500112 '//aos/common/logging:printf_formats',
Brian Silverman38658b02015-09-13 02:25:50 -0400113 ],
114 visibility = visibility,
Brian Silverman7a7c24d2018-09-01 17:49:09 -0700115 compatible_with = compatible_with,
116 restricted_to = restricted_to,
Brian Silverman38658b02015-09-13 02:25:50 -0400117 )