blob: d39cd50d4d9dd7e881e680049d7b72aa96fd0f39 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#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
11namespace aos {
12
13int Main(int argc, char **argv) {
James Kuszmaulc0c08da2020-05-10 18:56:07 -070014 CHECK_GE(argc, 5) << ": Too few arguments";
Alex Perrycb7da4b2019-08-28 19:35:56 -070015
Austin Schuh83544222020-04-20 16:16:29 -070016 const char *full_output = argv[1];
17 const char *stripped_output = argv[2];
18 const char *config_path = argv[3];
James Kuszmaulc0c08da2020-05-10 18:56:07 -070019 // 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 Schuh83544222020-04-20 16:16:29 -070023
24 VLOG(1) << "Reading " << config_path;
Alex Perrycb7da4b2019-08-28 19:35:56 -070025 FlatbufferDetachedBuffer<Configuration> config =
James Kuszmaulc0c08da2020-05-10 18:56:07 -070026 configuration::ReadConfig(config_path, {bazel_outs_directory});
Alex Perrycb7da4b2019-08-28 19:35:56 -070027
28 std::vector<aos::FlatbufferString<reflection::Schema>> schemas;
29
James Kuszmaulc0c08da2020-05-10 18:56:07 -070030 for (int i = 5; i < argc; ++i) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 schemas.emplace_back(util::ReadFileToStringOrDie(argv[i]));
32 }
33
34 const std::string merged_config = FlatbufferToJson(
Ravago Jonescf453ab2020-05-06 21:14:53 -070035 &configuration::MergeConfiguration(config, schemas).message(),
36 {.multi_line = true});
Alex Perrycb7da4b2019-08-28 19:35:56 -070037
38 // TODO(austin): Figure out how to squash the schemas onto 1 line so it is
Brian Silverman8533bac2020-01-31 17:44:27 -080039 // easier to read?
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 VLOG(1) << "Flattened config is " << merged_config;
Austin Schuh83544222020-04-20 16:16:29 -070041 util::WriteStringToFileOrDie(full_output, merged_config);
Ravago Jonescf453ab2020-05-06 21:14:53 -070042 util::WriteStringToFileOrDie(
43 stripped_output,
44 FlatbufferToJson(&config.message(), {.multi_line = true}));
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 return 0;
46}
47
48} // namespace aos
49
50int main(int argc, char **argv) {
51 aos::InitGoogle(&argc, &argv);
52 return aos::Main(argc, argv);
53}