| 930 | } |
| 931 | |
| 932 | int FlatCompiler::Compile(const FlatCOptions &options) { |
| 933 | // TODO(derekbailey): change to std::optional<Parser> |
| 934 | Parser conform_parser = GetConformParser(options); |
| 935 | |
| 936 | // TODO(derekbailey): split to own method. |
| 937 | if (!options.annotate_schema.empty()) { |
| 938 | const std::string ext = flatbuffers::GetExtension(options.annotate_schema); |
| 939 | if (!(ext == reflection::SchemaExtension() || ext == "fbs")) { |
| 940 | Error("Expected a `.bfbs` or `.fbs` schema, got: " + |
| 941 | options.annotate_schema); |
| 942 | } |
| 943 | |
| 944 | const bool is_binary_schema = ext == reflection::SchemaExtension(); |
| 945 | |
| 946 | std::string schema_contents; |
| 947 | if (!flatbuffers::LoadFile(options.annotate_schema.c_str(), |
| 948 | /*binary=*/is_binary_schema, &schema_contents)) { |
| 949 | Error("unable to load schema: " + options.annotate_schema); |
| 950 | } |
| 951 | |
| 952 | const uint8_t *binary_schema = nullptr; |
| 953 | uint64_t binary_schema_size = 0; |
| 954 | |
| 955 | IDLOptions binary_opts; |
| 956 | binary_opts.lang_to_generate |= flatbuffers::IDLOptions::kBinary; |
| 957 | Parser parser(binary_opts); |
| 958 | |
| 959 | if (is_binary_schema) { |
| 960 | binary_schema = |
| 961 | reinterpret_cast<const uint8_t *>(schema_contents.c_str()); |
| 962 | binary_schema_size = schema_contents.size(); |
| 963 | } else { |
| 964 | // If we need to generate the .bfbs file from the provided schema file |
| 965 | // (.fbs) |
| 966 | ParseFile(parser, options.annotate_schema, schema_contents, |
| 967 | options.include_directories); |
| 968 | parser.Serialize(); |
| 969 | |
| 970 | binary_schema = parser.builder_.GetBufferPointer(); |
| 971 | binary_schema_size = parser.builder_.GetSize(); |
| 972 | } |
| 973 | |
| 974 | if (binary_schema == nullptr || !binary_schema_size) { |
| 975 | Error("could not parse a value binary schema from: " + |
| 976 | options.annotate_schema); |
| 977 | } |
| 978 | |
| 979 | // Annotate the provided files with the binary_schema. |
| 980 | AnnotateBinaries(binary_schema, binary_schema_size, options); |
| 981 | |
| 982 | // We don't support doing anything else after annotating a binary. |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | if (options.generators.empty() && options.conform_to_schema.empty()) { |
| 987 | Error("No generator registered"); |
| 988 | return -1; |
| 989 | } |
no test coverage detected