blob: 26850760f8539ce09fe829e02c7f7b2d28328079 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include <string>
2#include <vector>
3
Austin Schuh99f7c6a2024-06-25 22:07:44 -07004#include "absl/flags/flag.h"
5#include "absl/log/check.h"
6#include "absl/log/log.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "aos/configuration.h"
9#include "aos/init.h"
10#include "aos/json_to_flatbuffer.h"
11#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012
Austin Schuh99f7c6a2024-06-25 22:07:44 -070013ABSL_FLAG(std::string, full_output, "",
14 "If provided, the path to write the full json configuration to.");
15ABSL_FLAG(std::string, stripped_output, "",
16 "If provided, the path to write the stripped json configuration to.");
17ABSL_FLAG(
18 std::string, binary_output, "",
Austin Schuh31b8d9a2023-07-28 11:23:49 -070019 "If provided, the path to write the binary flatbuffer configuration to.");
20
Alex Perrycb7da4b2019-08-28 19:35:56 -070021namespace aos {
22
23int Main(int argc, char **argv) {
Austin Schuh31b8d9a2023-07-28 11:23:49 -070024 CHECK_GE(argc, 3) << ": Too few arguments";
Alex Perrycb7da4b2019-08-28 19:35:56 -070025
Austin Schuh31b8d9a2023-07-28 11:23:49 -070026 const char *config_path = argv[1];
James Kuszmaulc0c08da2020-05-10 18:56:07 -070027 // In order to support not only importing things by absolute path, but also
28 // importing the outputs of genrules (rather than just manually written
29 // files), we need to tell ReadConfig where the generated files directory is.
Austin Schuh31b8d9a2023-07-28 11:23:49 -070030 const char *bazel_outs_directory = argv[2];
Austin Schuh83544222020-04-20 16:16:29 -070031
32 VLOG(1) << "Reading " << config_path;
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 FlatbufferDetachedBuffer<Configuration> config =
James Kuszmaulc0c08da2020-05-10 18:56:07 -070034 configuration::ReadConfig(config_path, {bazel_outs_directory});
Alex Perrycb7da4b2019-08-28 19:35:56 -070035
Austin Schuhd54780b2020-10-03 16:26:02 -070036 for (const Channel *channel : *config.message().channels()) {
37 if (channel->max_size() % alignof(flatbuffers::largest_scalar_t) != 0) {
38 LOG(FATAL) << "max_size() (" << channel->max_size()
39 << ") is not a multiple of alignment ("
40 << alignof(flatbuffers::largest_scalar_t) << ") for channel "
41 << configuration::CleanedChannelToString(channel) << ".";
42 }
43 }
44
Austin Schuh0de30f32020-12-06 12:44:28 -080045 std::vector<aos::FlatbufferVector<reflection::Schema>> schemas;
Alex Perrycb7da4b2019-08-28 19:35:56 -070046
Austin Schuh31b8d9a2023-07-28 11:23:49 -070047 for (int i = 3; i < argc; ++i) {
Austin Schuh605d5ff2024-05-10 15:59:54 -070048 auto schema = FileToFlatbuffer<reflection::Schema>(argv[i]);
49 if (!schema.message().has_root_table()) {
50 LOG(ERROR) << "Schema in " << argv[i]
51 << " does not have a root table, aborting";
52 return 1;
53 }
54 schemas.emplace_back(std::move(schema));
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 }
56
Austin Schuh15182322020-10-10 15:25:21 -070057 aos::FlatbufferDetachedBuffer<Configuration> merged_config =
58 configuration::MergeConfiguration(config, schemas);
Austin Schuha4fc60f2020-11-01 23:06:47 -080059 // In case we've done something overly weird to the flatbuffer...
Mithun Bharadwaj77c21052023-05-10 10:48:22 -070060 CHECK(merged_config.Verify())
61 << ": Failed to verify flatbuffer. NOTE: Very large flatbuffers could be "
62 "exceeding max_tables in flatbuffers::Verifier.";
Austin Schuh15182322020-10-10 15:25:21 -070063
64 const std::string merged_config_json =
65 FlatbufferToJson(&merged_config.message(), {.multi_line = true});
Alex Perrycb7da4b2019-08-28 19:35:56 -070066
67 // TODO(austin): Figure out how to squash the schemas onto 1 line so it is
Brian Silverman8533bac2020-01-31 17:44:27 -080068 // easier to read?
Austin Schuh15182322020-10-10 15:25:21 -070069 VLOG(1) << "Flattened config is " << merged_config_json;
Austin Schuh99f7c6a2024-06-25 22:07:44 -070070 if (!absl::GetFlag(FLAGS_full_output).empty()) {
71 util::WriteStringToFileOrDie(absl::GetFlag(FLAGS_full_output),
72 merged_config_json);
Austin Schuh31b8d9a2023-07-28 11:23:49 -070073 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -070074 if (!absl::GetFlag(FLAGS_stripped_output).empty()) {
Austin Schuh31b8d9a2023-07-28 11:23:49 -070075 util::WriteStringToFileOrDie(
Austin Schuh99f7c6a2024-06-25 22:07:44 -070076 absl::GetFlag(FLAGS_stripped_output),
Austin Schuh31b8d9a2023-07-28 11:23:49 -070077 FlatbufferToJson(&config.message(), {.multi_line = true}));
78 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -070079 if (!absl::GetFlag(FLAGS_binary_output).empty()) {
80 aos::WriteFlatbufferToFile(absl::GetFlag(FLAGS_binary_output),
81 merged_config);
Austin Schuh31b8d9a2023-07-28 11:23:49 -070082 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 return 0;
84}
85
86} // namespace aos
87
88int main(int argc, char **argv) {
89 aos::InitGoogle(&argc, &argv);
90 return aos::Main(argc, argv);
91}