Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include <string> |
| 2 | #include <vector> |
| 3 | |
| 4 | #include "aos/configuration.h" |
| 5 | #include "aos/init.h" |
| 6 | #include "aos/json_to_flatbuffer.h" |
| 7 | #include "aos/util/file.h" |
| 8 | #include "gflags/gflags.h" |
| 9 | #include "glog/logging.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | int Main(int argc, char **argv) { |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame^] | 14 | CHECK_GE(argc, 6) << ": Too few arguments"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | |
Austin Schuh | 8354422 | 2020-04-20 16:16:29 -0700 | [diff] [blame] | 16 | const char *full_output = argv[1]; |
| 17 | const char *stripped_output = argv[2]; |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame^] | 18 | const char *binary_output = argv[3]; |
| 19 | const char *config_path = argv[4]; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 20 | // In order to support not only importing things by absolute path, but also |
| 21 | // importing the outputs of genrules (rather than just manually written |
| 22 | // files), we need to tell ReadConfig where the generated files directory is. |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame^] | 23 | const char *bazel_outs_directory = argv[5]; |
Austin Schuh | 8354422 | 2020-04-20 16:16:29 -0700 | [diff] [blame] | 24 | |
| 25 | VLOG(1) << "Reading " << config_path; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | FlatbufferDetachedBuffer<Configuration> config = |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 27 | configuration::ReadConfig(config_path, {bazel_outs_directory}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 28 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 29 | for (const Channel *channel : *config.message().channels()) { |
| 30 | if (channel->max_size() % alignof(flatbuffers::largest_scalar_t) != 0) { |
| 31 | LOG(FATAL) << "max_size() (" << channel->max_size() |
| 32 | << ") is not a multiple of alignment (" |
| 33 | << alignof(flatbuffers::largest_scalar_t) << ") for channel " |
| 34 | << configuration::CleanedChannelToString(channel) << "."; |
| 35 | } |
| 36 | } |
| 37 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | std::vector<aos::FlatbufferString<reflection::Schema>> schemas; |
| 39 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame^] | 40 | for (int i = 6; i < argc; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | schemas.emplace_back(util::ReadFileToStringOrDie(argv[i])); |
| 42 | } |
| 43 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame^] | 44 | aos::FlatbufferDetachedBuffer<Configuration> merged_config = |
| 45 | configuration::MergeConfiguration(config, schemas); |
| 46 | |
| 47 | const std::string merged_config_json = |
| 48 | FlatbufferToJson(&merged_config.message(), {.multi_line = true}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | |
| 50 | // 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] | 51 | // easier to read? |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame^] | 52 | VLOG(1) << "Flattened config is " << merged_config_json; |
| 53 | util::WriteStringToFileOrDie(full_output, merged_config_json); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 54 | util::WriteStringToFileOrDie( |
| 55 | stripped_output, |
| 56 | FlatbufferToJson(&config.message(), {.multi_line = true})); |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame^] | 57 | aos::WriteFlatbufferToFile(binary_output, merged_config); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | } // namespace aos |
| 62 | |
| 63 | int main(int argc, char **argv) { |
| 64 | aos::InitGoogle(&argc, &argv); |
| 65 | return aos::Main(argc, argv); |
| 66 | } |