blob: 57d6af3320ee17c6c4f1641ef9a4345f1a2f487a [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,
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,
Philipp Schraderdada1072020-11-24 11:34:46 -080016 target_compatible_with = target_compatible_with,
Alex Perrycb7da4b2019-08-28 19:35:56 -070017 )
18
19def _aos_config_impl(ctx):
Austin Schuh14d7d3d2020-09-10 18:14:36 -070020 config = ctx.actions.declare_file(ctx.label.name + ".json")
21 stripped_config = ctx.actions.declare_file(ctx.label.name + ".stripped.json")
Austin Schuh15182322020-10-10 15:25:21 -070022 binary_config = ctx.actions.declare_file(ctx.label.name + ".bfbs")
Austin Schuh14d7d3d2020-09-10 18:14:36 -070023
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 flatbuffers_depset = depset(
25 ctx.files.flatbuffers,
26 transitive = [dep[AosConfigInfo].transitive_flatbuffers for dep in ctx.attr.deps],
27 )
28
29 src_depset = depset(
30 ctx.files.src,
31 transitive = [dep[AosConfigInfo].transitive_src for dep in ctx.attr.deps],
32 )
33
34 all_files = flatbuffers_depset.to_list() + src_depset.to_list()
35 ctx.actions.run(
Austin Schuh15182322020-10-10 15:25:21 -070036 outputs = [config, stripped_config, binary_config],
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 inputs = all_files,
Austin Schuh3f31eb52020-09-16 15:04:45 -070038 arguments = [
39 config.path,
40 stripped_config.path,
Austin Schuh15182322020-10-10 15:25:21 -070041 binary_config.path,
Austin Schuh3f31eb52020-09-16 15:04:45 -070042 (ctx.label.workspace_root or ".") + "/" + ctx.files.src[0].short_path,
43 ctx.bin_dir.path,
44 ] + [f.path for f in flatbuffers_depset.to_list()],
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 progress_message = "Flattening config",
46 executable = ctx.executable._config_flattener,
47 )
Austin Schuh15182322020-10-10 15:25:21 -070048 runfiles = ctx.runfiles(files = [config, stripped_config, binary_config])
Austin Schuh14d7d3d2020-09-10 18:14:36 -070049 return [
50 DefaultInfo(
Austin Schuh15182322020-10-10 15:25:21 -070051 files = depset([config, stripped_config, binary_config]),
Austin Schuh14d7d3d2020-09-10 18:14:36 -070052 runfiles = runfiles,
53 ),
54 AosConfigInfo(
55 transitive_flatbuffers = flatbuffers_depset,
56 transitive_src = src_depset,
57 ),
58 ]
Alex Perrycb7da4b2019-08-28 19:35:56 -070059
60_aos_config = rule(
61 attrs = {
62 "_config_flattener": attr.label(
63 executable = True,
64 cfg = "host",
65 default = Label("//aos:config_flattener"),
66 ),
67 "src": attr.label(
68 mandatory = True,
69 allow_files = True,
70 ),
71 "deps": attr.label_list(
72 providers = [AosConfigInfo],
73 ),
74 "flatbuffers": attr.label_list(
75 mandatory = False,
76 ),
77 },
Alex Perrycb7da4b2019-08-28 19:35:56 -070078 implementation = _aos_config_impl,
79)