blob: d6dcf1efa4b9d6e4c2b09e8cf37d3e75a532907c [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001load("//tools/build_rules:label.bzl", "expand_label")
2
James Kuszmaulc0c08da2020-05-10 18:56:07 -07003AosConfigInfo = provider(fields = [
4 "transitive_flatbuffers",
5 "transitive_src",
6])
Alex Perrycb7da4b2019-08-28 19:35:56 -07007
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008def aos_config(name, src, flatbuffers = [], deps = [], visibility = None):
Alex Perrycb7da4b2019-08-28 19:35:56 -07009 _aos_config(
10 name = name,
11 src = src,
12 deps = deps,
13 flatbuffers = [expand_label(flatbuffer) + "_reflection_out" for flatbuffer in flatbuffers],
14 visibility = visibility,
15 )
16
17def _aos_config_impl(ctx):
18 flatbuffers_depset = depset(
19 ctx.files.flatbuffers,
20 transitive = [dep[AosConfigInfo].transitive_flatbuffers for dep in ctx.attr.deps],
21 )
22
23 src_depset = depset(
24 ctx.files.src,
25 transitive = [dep[AosConfigInfo].transitive_src for dep in ctx.attr.deps],
26 )
27
28 all_files = flatbuffers_depset.to_list() + src_depset.to_list()
29 ctx.actions.run(
Austin Schuh83544222020-04-20 16:16:29 -070030 outputs = [ctx.outputs.config, ctx.outputs.stripped_config],
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 inputs = all_files,
James Kuszmaulc0c08da2020-05-10 18:56:07 -070032 arguments = [ctx.outputs.config.path, ctx.outputs.stripped_config.path, ctx.files.src[0].short_path, ctx.bin_dir.path] + [f.path for f in flatbuffers_depset.to_list()],
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 progress_message = "Flattening config",
34 executable = ctx.executable._config_flattener,
35 )
36 return AosConfigInfo(
37 transitive_flatbuffers = flatbuffers_depset,
38 transitive_src = src_depset,
39 )
40
41_aos_config = rule(
42 attrs = {
43 "_config_flattener": attr.label(
44 executable = True,
45 cfg = "host",
46 default = Label("//aos:config_flattener"),
47 ),
48 "src": attr.label(
49 mandatory = True,
50 allow_files = True,
51 ),
52 "deps": attr.label_list(
53 providers = [AosConfigInfo],
54 ),
55 "flatbuffers": attr.label_list(
56 mandatory = False,
57 ),
58 },
59 outputs = {
60 "config": "%{name}.json",
Austin Schuh83544222020-04-20 16:16:29 -070061 "stripped_config": "%{name}.stripped.json",
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 },
63 implementation = _aos_config_impl,
64)