blob: 83959a1ef62e2fa0ef2c8264c9ff5efb050b30d4 [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
Austin Schuhd54780b2020-10-03 16:26:02 -070028 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 Perrycb7da4b2019-08-28 19:35:56 -070037 std::vector<aos::FlatbufferString<reflection::Schema>> schemas;
38
James Kuszmaulc0c08da2020-05-10 18:56:07 -070039 for (int i = 5; i < argc; ++i) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 schemas.emplace_back(util::ReadFileToStringOrDie(argv[i]));
41 }
42
43 const std::string merged_config = FlatbufferToJson(
Ravago Jonescf453ab2020-05-06 21:14:53 -070044 &configuration::MergeConfiguration(config, schemas).message(),
45 {.multi_line = true});
Alex Perrycb7da4b2019-08-28 19:35:56 -070046
47 // TODO(austin): Figure out how to squash the schemas onto 1 line so it is
Brian Silverman8533bac2020-01-31 17:44:27 -080048 // easier to read?
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 VLOG(1) << "Flattened config is " << merged_config;
Austin Schuh83544222020-04-20 16:16:29 -070050 util::WriteStringToFileOrDie(full_output, merged_config);
Ravago Jonescf453ab2020-05-06 21:14:53 -070051 util::WriteStringToFileOrDie(
52 stripped_output,
53 FlatbufferToJson(&config.message(), {.multi_line = true}));
Alex Perrycb7da4b2019-08-28 19:35:56 -070054 return 0;
55}
56
57} // namespace aos
58
59int main(int argc, char **argv) {
60 aos::InitGoogle(&argc, &argv);
61 return aos::Main(argc, argv);
62}