blob: a11efd5f1d11f6a8cd6302d367537414f8b6e7b5 [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")
Austin Schuh15182322020-10-10 15:25:21 -070021 binary_config = ctx.actions.declare_file(ctx.label.name + ".bfbs")
Austin Schuh14d7d3d2020-09-10 18:14:36 -070022
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 flatbuffers_depset = depset(
24 ctx.files.flatbuffers,
25 transitive = [dep[AosConfigInfo].transitive_flatbuffers for dep in ctx.attr.deps],
26 )
27
28 src_depset = depset(
29 ctx.files.src,
30 transitive = [dep[AosConfigInfo].transitive_src for dep in ctx.attr.deps],
31 )
32
33 all_files = flatbuffers_depset.to_list() + src_depset.to_list()
34 ctx.actions.run(
Austin Schuh15182322020-10-10 15:25:21 -070035 outputs = [config, stripped_config, binary_config],
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 inputs = all_files,
Austin Schuh3f31eb52020-09-16 15:04:45 -070037 arguments = [
38 config.path,
39 stripped_config.path,
Austin Schuh15182322020-10-10 15:25:21 -070040 binary_config.path,
Austin Schuh3f31eb52020-09-16 15:04:45 -070041 (ctx.label.workspace_root or ".") + "/" + ctx.files.src[0].short_path,
42 ctx.bin_dir.path,
43 ] + [f.path for f in flatbuffers_depset.to_list()],
Alex Perrycb7da4b2019-08-28 19:35:56 -070044 progress_message = "Flattening config",
45 executable = ctx.executable._config_flattener,
46 )
Austin Schuh15182322020-10-10 15:25:21 -070047 runfiles = ctx.runfiles(files = [config, stripped_config, binary_config])
Austin Schuh14d7d3d2020-09-10 18:14:36 -070048 return [
49 DefaultInfo(
Austin Schuh15182322020-10-10 15:25:21 -070050 files = depset([config, stripped_config, binary_config]),
Austin Schuh14d7d3d2020-09-10 18:14:36 -070051 runfiles = runfiles,
52 ),
53 AosConfigInfo(
54 transitive_flatbuffers = flatbuffers_depset,
55 transitive_src = src_depset,
56 ),
57 ]
Alex Perrycb7da4b2019-08-28 19:35:56 -070058
59_aos_config = rule(
60 attrs = {
61 "_config_flattener": attr.label(
62 executable = True,
63 cfg = "host",
64 default = Label("//aos:config_flattener"),
65 ),
66 "src": attr.label(
67 mandatory = True,
68 allow_files = True,
69 ),
70 "deps": attr.label_list(
71 providers = [AosConfigInfo],
72 ),
73 "flatbuffers": attr.label_list(
74 mandatory = False,
75 ),
76 },
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 implementation = _aos_config_impl,
78)