blob: b87fff23dd3882218a1e867c3cd0ac341b573fc9 [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 Schuh3f31eb52020-09-16 15:04:45 -070036 arguments = [
37 config.path,
38 stripped_config.path,
39 (ctx.label.workspace_root or ".") + "/" + ctx.files.src[0].short_path,
40 ctx.bin_dir.path,
41 ] + [f.path for f in flatbuffers_depset.to_list()],
Alex Perrycb7da4b2019-08-28 19:35:56 -070042 progress_message = "Flattening config",
43 executable = ctx.executable._config_flattener,
44 )
Austin Schuh14d7d3d2020-09-10 18:14:36 -070045 runfiles = ctx.runfiles(files = [config, stripped_config])
46 return [
47 DefaultInfo(
48 files = depset([config, stripped_config]),
49 runfiles = runfiles,
50 ),
51 AosConfigInfo(
52 transitive_flatbuffers = flatbuffers_depset,
53 transitive_src = src_depset,
54 ),
55 ]
Alex Perrycb7da4b2019-08-28 19:35:56 -070056
57_aos_config = rule(
58 attrs = {
59 "_config_flattener": attr.label(
60 executable = True,
61 cfg = "host",
62 default = Label("//aos:config_flattener"),
63 ),
64 "src": attr.label(
65 mandatory = True,
66 allow_files = True,
67 ),
68 "deps": attr.label_list(
69 providers = [AosConfigInfo],
70 ),
71 "flatbuffers": attr.label_list(
72 mandatory = False,
73 ),
74 },
Alex Perrycb7da4b2019-08-28 19:35:56 -070075 implementation = _aos_config_impl,
76)