blob: baeb0d3faa3e34f3451f8cc84503c4f52973bb8c [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001load("//tools/build_rules:label.bzl", "expand_label")
Austin Schuh83cbb1e2023-06-23 12:59:02 -07002load("//tools/build_rules:select.bzl", "address_size_select")
Alex Perrycb7da4b2019-08-28 19:35:56 -07003
James Kuszmaulc0c08da2020-05-10 18:56:07 -07004AosConfigInfo = provider(fields = [
5 "transitive_flatbuffers",
6 "transitive_src",
7])
Alex Perrycb7da4b2019-08-28 19:35:56 -07008
Philipp Schraderdada1072020-11-24 11:34:46 -08009def aos_config(name, src, flatbuffers = [], deps = [], visibility = None, testonly = False, target_compatible_with = None):
Alex Perrycb7da4b2019-08-28 19:35:56 -070010 _aos_config(
11 name = name,
12 src = src,
Austin Schuh83cbb1e2023-06-23 12:59:02 -070013 flags = address_size_select({
14 "32": ["--max_queue_size_override=0xffff"],
15 "64": ["--max_queue_size_override=0xffffffff"],
16 }),
James Kuszmaul7aa19332022-09-07 20:39:18 -070017 config_json = name + ".json",
18 config_stripped = name + ".stripped.json",
19 config_binary = name + ".bfbs",
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 deps = deps,
21 flatbuffers = [expand_label(flatbuffer) + "_reflection_out" for flatbuffer in flatbuffers],
22 visibility = visibility,
Austin Schuh14d7d3d2020-09-10 18:14:36 -070023 testonly = testonly,
Philipp Schraderdada1072020-11-24 11:34:46 -080024 target_compatible_with = target_compatible_with,
Alex Perrycb7da4b2019-08-28 19:35:56 -070025 )
26
27def _aos_config_impl(ctx):
James Kuszmaul7aa19332022-09-07 20:39:18 -070028 config = ctx.outputs.config_json
29 stripped_config = ctx.outputs.config_stripped
30 binary_config = ctx.outputs.config_binary
Austin Schuh14d7d3d2020-09-10 18:14:36 -070031
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 flatbuffers_depset = depset(
33 ctx.files.flatbuffers,
34 transitive = [dep[AosConfigInfo].transitive_flatbuffers for dep in ctx.attr.deps],
35 )
36
37 src_depset = depset(
38 ctx.files.src,
39 transitive = [dep[AosConfigInfo].transitive_src for dep in ctx.attr.deps],
40 )
41
42 all_files = flatbuffers_depset.to_list() + src_depset.to_list()
43 ctx.actions.run(
Austin Schuh31b8d9a2023-07-28 11:23:49 -070044 outputs = [stripped_config, binary_config],
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 inputs = all_files,
Austin Schuh83cbb1e2023-06-23 12:59:02 -070046 arguments = ctx.attr.flags + [
Austin Schuh31b8d9a2023-07-28 11:23:49 -070047 "--stripped_output=" + stripped_config.path,
48 "--binary_output=" + binary_config.path,
Austin Schuh3f31eb52020-09-16 15:04:45 -070049 (ctx.label.workspace_root or ".") + "/" + ctx.files.src[0].short_path,
50 ctx.bin_dir.path,
51 ] + [f.path for f in flatbuffers_depset.to_list()],
Alex Perrycb7da4b2019-08-28 19:35:56 -070052 progress_message = "Flattening config",
53 executable = ctx.executable._config_flattener,
54 )
Austin Schuh31b8d9a2023-07-28 11:23:49 -070055 ctx.actions.run(
56 outputs = [config],
57 inputs = all_files,
58 arguments = ctx.attr.flags + [
59 "--full_output=" + config.path,
60 (ctx.label.workspace_root or ".") + "/" + ctx.files.src[0].short_path,
61 ctx.bin_dir.path,
62 ] + [f.path for f in flatbuffers_depset.to_list()],
63 progress_message = "Flattening config",
64 executable = ctx.executable._config_flattener,
65 )
66 runfiles = ctx.runfiles(files = [stripped_config, binary_config])
Austin Schuh14d7d3d2020-09-10 18:14:36 -070067 return [
68 DefaultInfo(
Austin Schuh31b8d9a2023-07-28 11:23:49 -070069 files = depset([stripped_config, binary_config]),
Austin Schuh14d7d3d2020-09-10 18:14:36 -070070 runfiles = runfiles,
71 ),
72 AosConfigInfo(
73 transitive_flatbuffers = flatbuffers_depset,
74 transitive_src = src_depset,
75 ),
76 ]
Alex Perrycb7da4b2019-08-28 19:35:56 -070077
78_aos_config = rule(
79 attrs = {
James Kuszmaul7aa19332022-09-07 20:39:18 -070080 "config_json": attr.output(mandatory = True),
81 "config_stripped": attr.output(mandatory = True),
82 "config_binary": attr.output(mandatory = True),
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 "_config_flattener": attr.label(
84 executable = True,
Adam Snaider13d48d92023-08-03 12:20:15 -070085 cfg = "exec",
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 default = Label("//aos:config_flattener"),
87 ),
88 "src": attr.label(
89 mandatory = True,
90 allow_files = True,
91 ),
Austin Schuh83cbb1e2023-06-23 12:59:02 -070092 "flags": attr.string_list(
93 doc = "Additional flags to pass to config_flattener.",
94 ),
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 "deps": attr.label_list(
96 providers = [AosConfigInfo],
97 ),
98 "flatbuffers": attr.label_list(
99 mandatory = False,
100 ),
101 },
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 implementation = _aos_config_impl,
103)