blob: a9500e3b5013ec3db5a81a0bc45dfc4cd5c25849 [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) {
Austin Schuh15182322020-10-10 15:25:21 -070014 CHECK_GE(argc, 6) << ": 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];
Austin Schuh15182322020-10-10 15:25:21 -070018 const char *binary_output = argv[3];
19 const char *config_path = argv[4];
James Kuszmaulc0c08da2020-05-10 18:56:07 -070020 // 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 Schuh15182322020-10-10 15:25:21 -070023 const char *bazel_outs_directory = argv[5];
Austin Schuh83544222020-04-20 16:16:29 -070024
25 VLOG(1) << "Reading " << config_path;
Alex Perrycb7da4b2019-08-28 19:35:56 -070026 FlatbufferDetachedBuffer<Configuration> config =
James Kuszmaulc0c08da2020-05-10 18:56:07 -070027 configuration::ReadConfig(config_path, {bazel_outs_directory});
Alex Perrycb7da4b2019-08-28 19:35:56 -070028
Austin Schuhd54780b2020-10-03 16:26:02 -070029 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 Perrycb7da4b2019-08-28 19:35:56 -070038 std::vector<aos::FlatbufferString<reflection::Schema>> schemas;
39
Austin Schuh15182322020-10-10 15:25:21 -070040 for (int i = 6; i < argc; ++i) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 schemas.emplace_back(util::ReadFileToStringOrDie(argv[i]));
42 }
43
Austin Schuh15182322020-10-10 15:25:21 -070044 aos::FlatbufferDetachedBuffer<Configuration> merged_config =
45 configuration::MergeConfiguration(config, schemas);
Austin Schuha4fc60f2020-11-01 23:06:47 -080046 // In case we've done something overly weird to the flatbuffer...
47 CHECK(merged_config.Verify());
Austin Schuh15182322020-10-10 15:25:21 -070048
49 const std::string merged_config_json =
50 FlatbufferToJson(&merged_config.message(), {.multi_line = true});
Alex Perrycb7da4b2019-08-28 19:35:56 -070051
52 // TODO(austin): Figure out how to squash the schemas onto 1 line so it is
Brian Silverman8533bac2020-01-31 17:44:27 -080053 // easier to read?
Austin Schuh15182322020-10-10 15:25:21 -070054 VLOG(1) << "Flattened config is " << merged_config_json;
55 util::WriteStringToFileOrDie(full_output, merged_config_json);
Ravago Jonescf453ab2020-05-06 21:14:53 -070056 util::WriteStringToFileOrDie(
57 stripped_output,
58 FlatbufferToJson(&config.message(), {.multi_line = true}));
Austin Schuh15182322020-10-10 15:25:21 -070059 aos::WriteFlatbufferToFile(binary_output, merged_config);
Alex Perrycb7da4b2019-08-28 19:35:56 -070060 return 0;
61}
62
63} // namespace aos
64
65int main(int argc, char **argv) {
66 aos::InitGoogle(&argc, &argv);
67 return aos::Main(argc, argv);
68}