Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | load("//tools/build_rules:label.bzl", "expand_label") |
| 2 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 3 | AosConfigInfo = provider(fields = [ |
| 4 | "transitive_flatbuffers", |
| 5 | "transitive_src", |
| 6 | ]) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | |
Austin Schuh | 14d7d3d | 2020-09-10 18:14:36 -0700 | [diff] [blame] | 8 | def aos_config(name, src, flatbuffers = [], deps = [], visibility = None, testonly = False): |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | _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 Schuh | 14d7d3d | 2020-09-10 18:14:36 -0700 | [diff] [blame] | 15 | testonly = testonly, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | def _aos_config_impl(ctx): |
Austin Schuh | 14d7d3d | 2020-09-10 18:14:36 -0700 | [diff] [blame] | 19 | config = ctx.actions.declare_file(ctx.label.name + ".json") |
| 20 | stripped_config = ctx.actions.declare_file(ctx.label.name + ".stripped.json") |
| 21 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 22 | 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 Schuh | 14d7d3d | 2020-09-10 18:14:36 -0700 | [diff] [blame] | 34 | outputs = [config, stripped_config], |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | inputs = all_files, |
Austin Schuh | 3f31eb5 | 2020-09-16 15:04:45 -0700 | [diff] [blame^] | 36 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 42 | progress_message = "Flattening config", |
| 43 | executable = ctx.executable._config_flattener, |
| 44 | ) |
Austin Schuh | 14d7d3d | 2020-09-10 18:14:36 -0700 | [diff] [blame] | 45 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 56 | |
| 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | implementation = _aos_config_impl, |
| 76 | ) |