Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include <string> |
| 2 | #include <vector> |
| 3 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 4 | #include "absl/flags/flag.h" |
| 5 | #include "absl/log/check.h" |
| 6 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/configuration.h" |
| 9 | #include "aos/init.h" |
| 10 | #include "aos/json_to_flatbuffer.h" |
| 11 | #include "aos/util/file.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 13 | ABSL_FLAG(std::string, full_output, "", |
| 14 | "If provided, the path to write the full json configuration to."); |
| 15 | ABSL_FLAG(std::string, stripped_output, "", |
| 16 | "If provided, the path to write the stripped json configuration to."); |
| 17 | ABSL_FLAG( |
| 18 | std::string, binary_output, "", |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 19 | "If provided, the path to write the binary flatbuffer configuration to."); |
| 20 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | namespace aos { |
| 22 | |
| 23 | int Main(int argc, char **argv) { |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 24 | CHECK_GE(argc, 3) << ": Too few arguments"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 26 | const char *config_path = argv[1]; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 27 | // 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 Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 30 | const char *bazel_outs_directory = argv[2]; |
Austin Schuh | 8354422 | 2020-04-20 16:16:29 -0700 | [diff] [blame] | 31 | |
| 32 | VLOG(1) << "Reading " << config_path; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | FlatbufferDetachedBuffer<Configuration> config = |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 34 | configuration::ReadConfig(config_path, {bazel_outs_directory}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 36 | 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 Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 45 | std::vector<aos::FlatbufferVector<reflection::Schema>> schemas; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 47 | for (int i = 3; i < argc; ++i) { |
Austin Schuh | 605d5ff | 2024-05-10 15:59:54 -0700 | [diff] [blame] | 48 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 57 | aos::FlatbufferDetachedBuffer<Configuration> merged_config = |
| 58 | configuration::MergeConfiguration(config, schemas); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 59 | // In case we've done something overly weird to the flatbuffer... |
Mithun Bharadwaj | 77c2105 | 2023-05-10 10:48:22 -0700 | [diff] [blame] | 60 | CHECK(merged_config.Verify()) |
| 61 | << ": Failed to verify flatbuffer. NOTE: Very large flatbuffers could be " |
| 62 | "exceeding max_tables in flatbuffers::Verifier."; |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 63 | |
| 64 | const std::string merged_config_json = |
| 65 | FlatbufferToJson(&merged_config.message(), {.multi_line = true}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | |
| 67 | // TODO(austin): Figure out how to squash the schemas onto 1 line so it is |
Brian Silverman | 8533bac | 2020-01-31 17:44:27 -0800 | [diff] [blame] | 68 | // easier to read? |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 69 | VLOG(1) << "Flattened config is " << merged_config_json; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 70 | if (!absl::GetFlag(FLAGS_full_output).empty()) { |
| 71 | util::WriteStringToFileOrDie(absl::GetFlag(FLAGS_full_output), |
| 72 | merged_config_json); |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 73 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 74 | if (!absl::GetFlag(FLAGS_stripped_output).empty()) { |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 75 | util::WriteStringToFileOrDie( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 76 | absl::GetFlag(FLAGS_stripped_output), |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 77 | FlatbufferToJson(&config.message(), {.multi_line = true})); |
| 78 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 79 | if (!absl::GetFlag(FLAGS_binary_output).empty()) { |
| 80 | aos::WriteFlatbufferToFile(absl::GetFlag(FLAGS_binary_output), |
| 81 | merged_config); |
Austin Schuh | 31b8d9a | 2023-07-28 11:23:49 -0700 | [diff] [blame] | 82 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | } // namespace aos |
| 87 | |
| 88 | int main(int argc, char **argv) { |
| 89 | aos::InitGoogle(&argc, &argv); |
| 90 | return aos::Main(argc, argv); |
| 91 | } |