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 | |
| 28 | std::vector<aos::FlatbufferString<reflection::Schema>> schemas; |
| 29 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame^] | 30 | for (int i = 5; i < argc; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | schemas.emplace_back(util::ReadFileToStringOrDie(argv[i])); |
| 32 | } |
| 33 | |
| 34 | const std::string merged_config = FlatbufferToJson( |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 35 | &configuration::MergeConfiguration(config, schemas).message(), |
| 36 | {.multi_line = true}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 37 | |
| 38 | // 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] | 39 | // easier to read? |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 40 | VLOG(1) << "Flattened config is " << merged_config; |
Austin Schuh | 8354422 | 2020-04-20 16:16:29 -0700 | [diff] [blame] | 41 | util::WriteStringToFileOrDie(full_output, merged_config); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 42 | util::WriteStringToFileOrDie( |
| 43 | stripped_output, |
| 44 | FlatbufferToJson(&config.message(), {.multi_line = true})); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | } // namespace aos |
| 49 | |
| 50 | int main(int argc, char **argv) { |
| 51 | aos::InitGoogle(&argc, &argv); |
| 52 | return aos::Main(argc, argv); |
| 53 | } |