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