blob: 8f040d6ab0851cceb8996b8591b609b83a09be3f [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 Schuh14d7d3d2020-09-10 18:14:36 -07008def aos_config(name, src, flatbuffers = [], deps = [], visibility = None, testonly = False):
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,
Austin Schuh14d7d3d2020-09-10 18:14:36 -070015 testonly = testonly,
Alex Perrycb7da4b2019-08-28 19:35:56 -070016 )
17
18def _aos_config_impl(ctx):
Austin Schuh14d7d3d2020-09-10 18:14:36 -070019 config = ctx.actions.declare_file(ctx.label.name + ".json")
20 stripped_config = ctx.actions.declare_file(ctx.label.name + ".stripped.json")
21
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 flatbuffers_depset = depset(
23 ctx.files.flatbuffers,
24 transitive = [dep[AosConfigInfo].transitive_flatbuffers for dep in ctx.attr.deps],
25 )
26
27 src_depset = depset(
28 ctx.files.src,
29 transitive = [dep[AosConfigInfo].transitive_src for dep in ctx.attr.deps],
30 )
31
32 all_files = flatbuffers_depset.to_list() + src_depset.to_list()
33 ctx.actions.run(
Austin Schuh14d7d3d2020-09-10 18:14:36 -070034 outputs = [config, stripped_config],
Alex Perrycb7da4b2019-08-28 19:35:56 -070035 inputs = all_files,
Austin Schuh14d7d3d2020-09-10 18:14:36 -070036 arguments = [config.path, 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 -070037 progress_message = "Flattening config",
38 executable = ctx.executable._config_flattener,
39 )
Austin Schuh14d7d3d2020-09-10 18:14:36 -070040 runfiles = ctx.runfiles(files = [config, stripped_config])
41 return [
42 DefaultInfo(
43 files = depset([config, stripped_config]),
44 runfiles = runfiles,
45 ),
46 AosConfigInfo(
47 transitive_flatbuffers = flatbuffers_depset,
48 transitive_src = src_depset,
49 ),
50 ]
Alex Perrycb7da4b2019-08-28 19:35:56 -070051
52_aos_config = rule(
53 attrs = {
54 "_config_flattener": attr.label(
55 executable = True,
56 cfg = "host",
57 default = Label("//aos:config_flattener"),
58 ),
59 "src": attr.label(
60 mandatory = True,
61 allow_files = True,
62 ),
63 "deps": attr.label_list(
64 providers = [AosConfigInfo],
65 ),
66 "flatbuffers": attr.label_list(
67 mandatory = False,
68 ),
69 },
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 implementation = _aos_config_impl,
71)