blob: b089d24912f45b47d5a05126940e3d72b14d2dfb [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include <string>
2#include <vector>
3
Philipp Schrader790cb542023-07-05 21:06:52 -07004#include "gflags/gflags.h"
5#include "glog/logging.h"
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/configuration.h"
8#include "aos/init.h"
9#include "aos/json_to_flatbuffer.h"
10#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011
12namespace aos {
13
14int Main(int argc, char **argv) {
Austin Schuh15182322020-10-10 15:25:21 -070015 CHECK_GE(argc, 6) << ": Too few arguments";
Alex Perrycb7da4b2019-08-28 19:35:56 -070016
Austin Schuh83544222020-04-20 16:16:29 -070017 const char *full_output = argv[1];
18 const char *stripped_output = argv[2];
Austin Schuh15182322020-10-10 15:25:21 -070019 const char *binary_output = argv[3];
20 const char *config_path = argv[4];
James Kuszmaulc0c08da2020-05-10 18:56:07 -070021 // In order to support not only importing things by absolute path, but also
22 // importing the outputs of genrules (rather than just manually written
23 // files), we need to tell ReadConfig where the generated files directory is.
Austin Schuh15182322020-10-10 15:25:21 -070024 const char *bazel_outs_directory = argv[5];
Austin Schuh83544222020-04-20 16:16:29 -070025
26 VLOG(1) << "Reading " << config_path;
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 FlatbufferDetachedBuffer<Configuration> config =
James Kuszmaulc0c08da2020-05-10 18:56:07 -070028 configuration::ReadConfig(config_path, {bazel_outs_directory});
Alex Perrycb7da4b2019-08-28 19:35:56 -070029
Austin Schuhd54780b2020-10-03 16:26:02 -070030 for (const Channel *channel : *config.message().channels()) {
31 if (channel->max_size() % alignof(flatbuffers::largest_scalar_t) != 0) {
32 LOG(FATAL) << "max_size() (" << channel->max_size()
33 << ") is not a multiple of alignment ("
34 << alignof(flatbuffers::largest_scalar_t) << ") for channel "
35 << configuration::CleanedChannelToString(channel) << ".";
36 }
37 }
38
Austin Schuh0de30f32020-12-06 12:44:28 -080039 std::vector<aos::FlatbufferVector<reflection::Schema>> schemas;
Alex Perrycb7da4b2019-08-28 19:35:56 -070040
Austin Schuh15182322020-10-10 15:25:21 -070041 for (int i = 6; i < argc; ++i) {
Austin Schuh0de30f32020-12-06 12:44:28 -080042 schemas.emplace_back(FileToFlatbuffer<reflection::Schema>(argv[i]));
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 }
44
Austin Schuh15182322020-10-10 15:25:21 -070045 aos::FlatbufferDetachedBuffer<Configuration> merged_config =
46 configuration::MergeConfiguration(config, schemas);
Austin Schuha4fc60f2020-11-01 23:06:47 -080047 // In case we've done something overly weird to the flatbuffer...
Mithun Bharadwaj77c21052023-05-10 10:48:22 -070048 CHECK(merged_config.Verify())
49 << ": Failed to verify flatbuffer. NOTE: Very large flatbuffers could be "
50 "exceeding max_tables in flatbuffers::Verifier.";
Austin Schuh15182322020-10-10 15:25:21 -070051
52 const std::string merged_config_json =
53 FlatbufferToJson(&merged_config.message(), {.multi_line = true});
Alex Perrycb7da4b2019-08-28 19:35:56 -070054
55 // TODO(austin): Figure out how to squash the schemas onto 1 line so it is
Brian Silverman8533bac2020-01-31 17:44:27 -080056 // easier to read?
Austin Schuh15182322020-10-10 15:25:21 -070057 VLOG(1) << "Flattened config is " << merged_config_json;
58 util::WriteStringToFileOrDie(full_output, merged_config_json);
Ravago Jonescf453ab2020-05-06 21:14:53 -070059 util::WriteStringToFileOrDie(
60 stripped_output,
61 FlatbufferToJson(&config.message(), {.multi_line = true}));
Austin Schuh15182322020-10-10 15:25:21 -070062 aos::WriteFlatbufferToFile(binary_output, merged_config);
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 return 0;
64}
65
66} // namespace aos
67
68int main(int argc, char **argv) {
69 aos::InitGoogle(&argc, &argv);
70 return aos::Main(argc, argv);
71}