blob: 4aa6b4afddab94aa723d4df5246ad62c807b366d [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
Philipp Schraderdada1072020-11-24 11:34:46 -08008def aos_config(name, src, flatbuffers = [], deps = [], visibility = None, testonly = False, target_compatible_with = None):
Alex Perrycb7da4b2019-08-28 19:35:56 -07009 _aos_config(
10 name = name,
11 src = src,
James Kuszmaul7aa19332022-09-07 20:39:18 -070012 config_json = name + ".json",
13 config_stripped = name + ".stripped.json",
14 config_binary = name + ".bfbs",
Alex Perrycb7da4b2019-08-28 19:35:56 -070015 deps = deps,
16 flatbuffers = [expand_label(flatbuffer) + "_reflection_out" for flatbuffer in flatbuffers],
17 visibility = visibility,
Austin Schuh14d7d3d2020-09-10 18:14:36 -070018 testonly = testonly,
Philipp Schraderdada1072020-11-24 11:34:46 -080019 target_compatible_with = target_compatible_with,
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 )
21
22def _aos_config_impl(ctx):
James Kuszmaul7aa19332022-09-07 20:39:18 -070023 config = ctx.outputs.config_json
24 stripped_config = ctx.outputs.config_stripped
25 binary_config = ctx.outputs.config_binary
Austin Schuh14d7d3d2020-09-10 18:14:36 -070026
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 flatbuffers_depset = depset(
28 ctx.files.flatbuffers,
29 transitive = [dep[AosConfigInfo].transitive_flatbuffers for dep in ctx.attr.deps],
30 )
31
32 src_depset = depset(
33 ctx.files.src,
34 transitive = [dep[AosConfigInfo].transitive_src for dep in ctx.attr.deps],
35 )
36
37 all_files = flatbuffers_depset.to_list() + src_depset.to_list()
38 ctx.actions.run(
Austin Schuh15182322020-10-10 15:25:21 -070039 outputs = [config, stripped_config, binary_config],
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 inputs = all_files,
Austin Schuh3f31eb52020-09-16 15:04:45 -070041 arguments = [
42 config.path,
43 stripped_config.path,
Austin Schuh15182322020-10-10 15:25:21 -070044 binary_config.path,
Austin Schuh3f31eb52020-09-16 15:04:45 -070045 (ctx.label.workspace_root or ".") + "/" + ctx.files.src[0].short_path,
46 ctx.bin_dir.path,
47 ] + [f.path for f in flatbuffers_depset.to_list()],
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 progress_message = "Flattening config",
49 executable = ctx.executable._config_flattener,
50 )
Austin Schuh15182322020-10-10 15:25:21 -070051 runfiles = ctx.runfiles(files = [config, stripped_config, binary_config])
Austin Schuh14d7d3d2020-09-10 18:14:36 -070052 return [
53 DefaultInfo(
Austin Schuh15182322020-10-10 15:25:21 -070054 files = depset([config, stripped_config, binary_config]),
Austin Schuh14d7d3d2020-09-10 18:14:36 -070055 runfiles = runfiles,
56 ),
57 AosConfigInfo(
58 transitive_flatbuffers = flatbuffers_depset,
59 transitive_src = src_depset,
60 ),
61 ]
Alex Perrycb7da4b2019-08-28 19:35:56 -070062
63_aos_config = rule(
64 attrs = {
James Kuszmaul7aa19332022-09-07 20:39:18 -070065 "config_json": attr.output(mandatory = True),
66 "config_stripped": attr.output(mandatory = True),
67 "config_binary": attr.output(mandatory = True),
Alex Perrycb7da4b2019-08-28 19:35:56 -070068 "_config_flattener": attr.label(
69 executable = True,
70 cfg = "host",
71 default = Label("//aos:config_flattener"),
72 ),
73 "src": attr.label(
74 mandatory = True,
75 allow_files = True,
76 ),
77 "deps": attr.label_list(
78 providers = [AosConfigInfo],
79 ),
80 "flatbuffers": attr.label_list(
81 mandatory = False,
82 ),
83 },
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 implementation = _aos_config_impl,
85)