1*890232f2SAndroid Build Coastguard Worker /*
2*890232f2SAndroid Build Coastguard Worker * Copyright 2014 Google Inc. All rights reserved.
3*890232f2SAndroid Build Coastguard Worker *
4*890232f2SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*890232f2SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*890232f2SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*890232f2SAndroid Build Coastguard Worker *
8*890232f2SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*890232f2SAndroid Build Coastguard Worker *
10*890232f2SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*890232f2SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*890232f2SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*890232f2SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*890232f2SAndroid Build Coastguard Worker * limitations under the License.
15*890232f2SAndroid Build Coastguard Worker */
16*890232f2SAndroid Build Coastguard Worker
17*890232f2SAndroid Build Coastguard Worker #include "flatbuffers/flatc.h"
18*890232f2SAndroid Build Coastguard Worker
19*890232f2SAndroid Build Coastguard Worker #include <list>
20*890232f2SAndroid Build Coastguard Worker #include <sstream>
21*890232f2SAndroid Build Coastguard Worker
22*890232f2SAndroid Build Coastguard Worker #include "annotated_binary_text_gen.h"
23*890232f2SAndroid Build Coastguard Worker #include "binary_annotator.h"
24*890232f2SAndroid Build Coastguard Worker #include "flatbuffers/util.h"
25*890232f2SAndroid Build Coastguard Worker
26*890232f2SAndroid Build Coastguard Worker namespace flatbuffers {
27*890232f2SAndroid Build Coastguard Worker
FLATC_VERSION()28*890232f2SAndroid Build Coastguard Worker static const char *FLATC_VERSION() { return FLATBUFFERS_VERSION(); }
29*890232f2SAndroid Build Coastguard Worker
ParseFile(flatbuffers::Parser & parser,const std::string & filename,const std::string & contents,std::vector<const char * > & include_directories) const30*890232f2SAndroid Build Coastguard Worker void FlatCompiler::ParseFile(
31*890232f2SAndroid Build Coastguard Worker flatbuffers::Parser &parser, const std::string &filename,
32*890232f2SAndroid Build Coastguard Worker const std::string &contents,
33*890232f2SAndroid Build Coastguard Worker std::vector<const char *> &include_directories) const {
34*890232f2SAndroid Build Coastguard Worker auto local_include_directory = flatbuffers::StripFileName(filename);
35*890232f2SAndroid Build Coastguard Worker include_directories.push_back(local_include_directory.c_str());
36*890232f2SAndroid Build Coastguard Worker include_directories.push_back(nullptr);
37*890232f2SAndroid Build Coastguard Worker if (!parser.Parse(contents.c_str(), &include_directories[0],
38*890232f2SAndroid Build Coastguard Worker filename.c_str())) {
39*890232f2SAndroid Build Coastguard Worker Error(parser.error_, false, false);
40*890232f2SAndroid Build Coastguard Worker }
41*890232f2SAndroid Build Coastguard Worker if (!parser.error_.empty()) { Warn(parser.error_, false); }
42*890232f2SAndroid Build Coastguard Worker include_directories.pop_back();
43*890232f2SAndroid Build Coastguard Worker include_directories.pop_back();
44*890232f2SAndroid Build Coastguard Worker }
45*890232f2SAndroid Build Coastguard Worker
LoadBinarySchema(flatbuffers::Parser & parser,const std::string & filename,const std::string & contents)46*890232f2SAndroid Build Coastguard Worker void FlatCompiler::LoadBinarySchema(flatbuffers::Parser &parser,
47*890232f2SAndroid Build Coastguard Worker const std::string &filename,
48*890232f2SAndroid Build Coastguard Worker const std::string &contents) {
49*890232f2SAndroid Build Coastguard Worker if (!parser.Deserialize(reinterpret_cast<const uint8_t *>(contents.c_str()),
50*890232f2SAndroid Build Coastguard Worker contents.size())) {
51*890232f2SAndroid Build Coastguard Worker Error("failed to load binary schema: " + filename, false, false);
52*890232f2SAndroid Build Coastguard Worker }
53*890232f2SAndroid Build Coastguard Worker }
54*890232f2SAndroid Build Coastguard Worker
Warn(const std::string & warn,bool show_exe_name) const55*890232f2SAndroid Build Coastguard Worker void FlatCompiler::Warn(const std::string &warn, bool show_exe_name) const {
56*890232f2SAndroid Build Coastguard Worker params_.warn_fn(this, warn, show_exe_name);
57*890232f2SAndroid Build Coastguard Worker }
58*890232f2SAndroid Build Coastguard Worker
Error(const std::string & err,bool usage,bool show_exe_name) const59*890232f2SAndroid Build Coastguard Worker void FlatCompiler::Error(const std::string &err, bool usage,
60*890232f2SAndroid Build Coastguard Worker bool show_exe_name) const {
61*890232f2SAndroid Build Coastguard Worker params_.error_fn(this, err, usage, show_exe_name);
62*890232f2SAndroid Build Coastguard Worker }
63*890232f2SAndroid Build Coastguard Worker
64*890232f2SAndroid Build Coastguard Worker const static FlatCOption options[] = {
65*890232f2SAndroid Build Coastguard Worker { "o", "", "PATH", "Prefix PATH to all generated files." },
66*890232f2SAndroid Build Coastguard Worker { "I", "", "PATH", "Search for includes in the specified path." },
67*890232f2SAndroid Build Coastguard Worker { "M", "", "", "Print make rules for generated files." },
68*890232f2SAndroid Build Coastguard Worker { "", "version", "", "Print the version number of flatc and exit." },
69*890232f2SAndroid Build Coastguard Worker { "h", "help", "", "Prints this help text and exit." },
70*890232f2SAndroid Build Coastguard Worker { "", "strict-json", "",
71*890232f2SAndroid Build Coastguard Worker "Strict JSON: field names must be / will be quoted, no trailing commas in "
72*890232f2SAndroid Build Coastguard Worker "tables/vectors." },
73*890232f2SAndroid Build Coastguard Worker { "", "allow-non-utf8", "",
74*890232f2SAndroid Build Coastguard Worker "Pass non-UTF-8 input through parser and emit nonstandard \\x escapes in "
75*890232f2SAndroid Build Coastguard Worker "JSON. (Default is to raise parse error on non-UTF-8 input.)" },
76*890232f2SAndroid Build Coastguard Worker { "", "natural-utf8", "",
77*890232f2SAndroid Build Coastguard Worker "Output strings with UTF-8 as human-readable strings. By default, UTF-8 "
78*890232f2SAndroid Build Coastguard Worker "characters are printed as \\uXXXX escapes." },
79*890232f2SAndroid Build Coastguard Worker { "", "defaults-json", "",
80*890232f2SAndroid Build Coastguard Worker "Output fields whose value is the default when writing JSON" },
81*890232f2SAndroid Build Coastguard Worker { "", "unknown-json", "",
82*890232f2SAndroid Build Coastguard Worker "Allow fields in JSON that are not defined in the schema. These fields "
83*890232f2SAndroid Build Coastguard Worker "will be discared when generating binaries." },
84*890232f2SAndroid Build Coastguard Worker { "", "no-prefix", "",
85*890232f2SAndroid Build Coastguard Worker "Don't prefix enum values with the enum type in C++." },
86*890232f2SAndroid Build Coastguard Worker { "", "scoped-enums", "",
87*890232f2SAndroid Build Coastguard Worker "Use C++11 style scoped and strongly typed enums. Also implies "
88*890232f2SAndroid Build Coastguard Worker "--no-prefix." },
89*890232f2SAndroid Build Coastguard Worker { "", "swift-implementation-only", "",
90*890232f2SAndroid Build Coastguard Worker "Adds a @_implementationOnly to swift imports" },
91*890232f2SAndroid Build Coastguard Worker { "", "gen-inclues", "",
92*890232f2SAndroid Build Coastguard Worker "(deprecated), this is the default behavior. If the original behavior is "
93*890232f2SAndroid Build Coastguard Worker "required (no include statements) use --no-includes." },
94*890232f2SAndroid Build Coastguard Worker { "", "no-includes", "",
95*890232f2SAndroid Build Coastguard Worker "Don't generate include statements for included schemas the generated "
96*890232f2SAndroid Build Coastguard Worker "file depends on (C++, Python, Proto-to-Fbs)." },
97*890232f2SAndroid Build Coastguard Worker { "", "gen-mutable", "",
98*890232f2SAndroid Build Coastguard Worker "Generate accessors that can mutate buffers in-place." },
99*890232f2SAndroid Build Coastguard Worker { "", "gen-onefile", "",
100*890232f2SAndroid Build Coastguard Worker "Generate a single output file for C#, Go, Java, Kotlin and Python. "
101*890232f2SAndroid Build Coastguard Worker "Implies --no-include." },
102*890232f2SAndroid Build Coastguard Worker { "", "gen-name-strings", "",
103*890232f2SAndroid Build Coastguard Worker "Generate type name functions for C++ and Rust." },
104*890232f2SAndroid Build Coastguard Worker { "", "gen-object-api", "", "Generate an additional object-based API." },
105*890232f2SAndroid Build Coastguard Worker { "", "gen-compare", "", "Generate operator== for object-based API types." },
106*890232f2SAndroid Build Coastguard Worker { "", "gen-nullable", "",
107*890232f2SAndroid Build Coastguard Worker "Add Clang _Nullable for C++ pointer. or @Nullable for Java" },
108*890232f2SAndroid Build Coastguard Worker { "", "java-checkerframe", "", "Add @Pure for Java." },
109*890232f2SAndroid Build Coastguard Worker { "", "gen-generated", "", "Add @Generated annotation for Java." },
110*890232f2SAndroid Build Coastguard Worker { "", "gen-jvmstatic", "",
111*890232f2SAndroid Build Coastguard Worker "Add @JvmStatic annotation for Kotlin methods in companion object for "
112*890232f2SAndroid Build Coastguard Worker "interop from Java to Kotlin." },
113*890232f2SAndroid Build Coastguard Worker { "", "gen-all", "",
114*890232f2SAndroid Build Coastguard Worker "Generate not just code for the current schema files, but for all files it "
115*890232f2SAndroid Build Coastguard Worker "includes as well. If the language uses a single file for output (by "
116*890232f2SAndroid Build Coastguard Worker "default the case for C++ and JS), all code will end up in this one "
117*890232f2SAndroid Build Coastguard Worker "file." },
118*890232f2SAndroid Build Coastguard Worker { "", "gen-json-emit", "",
119*890232f2SAndroid Build Coastguard Worker "Generates encoding code which emits Flatbuffers into JSON" },
120*890232f2SAndroid Build Coastguard Worker { "", "cpp-include", "", "Adds an #include in generated file." },
121*890232f2SAndroid Build Coastguard Worker { "", "cpp-ptr-type", "T",
122*890232f2SAndroid Build Coastguard Worker "Set object API pointer type (default std::unique_ptr)." },
123*890232f2SAndroid Build Coastguard Worker { "", "cpp-str-type", "T",
124*890232f2SAndroid Build Coastguard Worker "Set object API string type (default std::string). T::c_str(), T::length() "
125*890232f2SAndroid Build Coastguard Worker "and T::empty() must be supported. The custom type also needs to be "
126*890232f2SAndroid Build Coastguard Worker "constructible from std::string (see the --cpp-str-flex-ctor option to "
127*890232f2SAndroid Build Coastguard Worker "change this behavior)" },
128*890232f2SAndroid Build Coastguard Worker { "", "cpp-str-flex-ctor", "",
129*890232f2SAndroid Build Coastguard Worker "Don't construct custom string types by passing std::string from "
130*890232f2SAndroid Build Coastguard Worker "Flatbuffers, but (char* + length)." },
131*890232f2SAndroid Build Coastguard Worker { "", "cpp-field-case-style", "STYLE",
132*890232f2SAndroid Build Coastguard Worker "Generate C++ fields using selected case style. Supported STYLE values: * "
133*890232f2SAndroid Build Coastguard Worker "'unchanged' - leave unchanged (default) * 'upper' - schema snake_case "
134*890232f2SAndroid Build Coastguard Worker "emits UpperCamel; * 'lower' - schema snake_case emits lowerCamel." },
135*890232f2SAndroid Build Coastguard Worker { "", "cpp-std", "CPP_STD",
136*890232f2SAndroid Build Coastguard Worker "Generate a C++ code using features of selected C++ standard. Supported "
137*890232f2SAndroid Build Coastguard Worker "CPP_STD values: * 'c++0x' - generate code compatible with old compilers; "
138*890232f2SAndroid Build Coastguard Worker "'c++11' - use C++11 code generator (default); * 'c++17' - use C++17 "
139*890232f2SAndroid Build Coastguard Worker "features in generated code (experimental)." },
140*890232f2SAndroid Build Coastguard Worker { "", "cpp-static-reflection", "",
141*890232f2SAndroid Build Coastguard Worker "When using C++17, generate extra code to provide compile-time (static) "
142*890232f2SAndroid Build Coastguard Worker "reflection of Flatbuffers types. Requires --cpp-std to be \"c++17\" or "
143*890232f2SAndroid Build Coastguard Worker "higher." },
144*890232f2SAndroid Build Coastguard Worker { "", "object-prefix", "PREFIX",
145*890232f2SAndroid Build Coastguard Worker "Customize class prefix for C++ object-based API." },
146*890232f2SAndroid Build Coastguard Worker { "", "object-suffix", "SUFFIX",
147*890232f2SAndroid Build Coastguard Worker "Customize class suffix for C++ object-based API. Default Value is "
148*890232f2SAndroid Build Coastguard Worker "\"T\"." },
149*890232f2SAndroid Build Coastguard Worker { "", "go-namespace", "", "Generate the overriding namespace in Golang." },
150*890232f2SAndroid Build Coastguard Worker { "", "go-import", "IMPORT",
151*890232f2SAndroid Build Coastguard Worker "Generate the overriding import for flatbuffers in Golang (default is "
152*890232f2SAndroid Build Coastguard Worker "\"github.com/google/flatbuffers/go\")." },
153*890232f2SAndroid Build Coastguard Worker { "", "raw-binary", "",
154*890232f2SAndroid Build Coastguard Worker "Allow binaries without file_identifier to be read. This may crash flatc "
155*890232f2SAndroid Build Coastguard Worker "given a mismatched schema." },
156*890232f2SAndroid Build Coastguard Worker { "", "size-prefixed", "", "Input binaries are size prefixed buffers." },
157*890232f2SAndroid Build Coastguard Worker { "", "proto", "", "Input is a .proto, translate to .fbs." },
158*890232f2SAndroid Build Coastguard Worker { "", "proto-namespace-suffix", "SUFFIX",
159*890232f2SAndroid Build Coastguard Worker "Add this namespace to any flatbuffers generated from protobufs." },
160*890232f2SAndroid Build Coastguard Worker { "", "oneof-union", "", "Translate .proto oneofs to flatbuffer unions." },
161*890232f2SAndroid Build Coastguard Worker { "", "grpc", "", "Generate GRPC interfaces for the specified languages." },
162*890232f2SAndroid Build Coastguard Worker { "", "schema", "", "Serialize schemas instead of JSON (use with -b)." },
163*890232f2SAndroid Build Coastguard Worker { "", "bfbs-filenames", "PATH",
164*890232f2SAndroid Build Coastguard Worker "Sets the root path where reflection filenames in reflection.fbs are "
165*890232f2SAndroid Build Coastguard Worker "relative to. The 'root' is denoted with `//`. E.g. if PATH=/a/b/c "
166*890232f2SAndroid Build Coastguard Worker "then /a/d/e.fbs will be serialized as //../d/e.fbs. (PATH defaults to the "
167*890232f2SAndroid Build Coastguard Worker "directory of the first provided schema file." },
168*890232f2SAndroid Build Coastguard Worker { "", "bfbs-comments", "", "Add doc comments to the binary schema files." },
169*890232f2SAndroid Build Coastguard Worker { "", "bfbs-builtins", "",
170*890232f2SAndroid Build Coastguard Worker "Add builtin attributes to the binary schema files." },
171*890232f2SAndroid Build Coastguard Worker { "", "bfbs-gen-embed", "",
172*890232f2SAndroid Build Coastguard Worker "Generate code to embed the bfbs schema to the source." },
173*890232f2SAndroid Build Coastguard Worker { "", "conform", "FILE",
174*890232f2SAndroid Build Coastguard Worker "Specify a schema the following schemas should be an evolution of. Gives "
175*890232f2SAndroid Build Coastguard Worker "errors if not." },
176*890232f2SAndroid Build Coastguard Worker { "", "conform-includes", "PATH",
177*890232f2SAndroid Build Coastguard Worker "Include path for the schema given with --conform PATH" },
178*890232f2SAndroid Build Coastguard Worker { "", "filename-suffix", "SUFFIX",
179*890232f2SAndroid Build Coastguard Worker "The suffix appended to the generated file names (Default is "
180*890232f2SAndroid Build Coastguard Worker "'_generated')." },
181*890232f2SAndroid Build Coastguard Worker { "", "filename-ext", "EXT",
182*890232f2SAndroid Build Coastguard Worker "The extension appended to the generated file names. Default is "
183*890232f2SAndroid Build Coastguard Worker "language-specific (e.g., '.h' for C++)" },
184*890232f2SAndroid Build Coastguard Worker { "", "include-prefix", "PATH",
185*890232f2SAndroid Build Coastguard Worker "Prefix this PATH to any generated include statements." },
186*890232f2SAndroid Build Coastguard Worker { "", "keep-prefix", "",
187*890232f2SAndroid Build Coastguard Worker "Keep original prefix of schema include statement." },
188*890232f2SAndroid Build Coastguard Worker { "", "reflect-types", "",
189*890232f2SAndroid Build Coastguard Worker "Add minimal type reflection to code generation." },
190*890232f2SAndroid Build Coastguard Worker { "", "reflect-names", "", "Add minimal type/name reflection." },
191*890232f2SAndroid Build Coastguard Worker { "", "rust-serialize", "",
192*890232f2SAndroid Build Coastguard Worker "Implement serde::Serialize on generated Rust types." },
193*890232f2SAndroid Build Coastguard Worker { "", "rust-module-root-file", "",
194*890232f2SAndroid Build Coastguard Worker "Generate rust code in individual files with a module root file." },
195*890232f2SAndroid Build Coastguard Worker { "", "root-type", "T", "Select or override the default root_type." },
196*890232f2SAndroid Build Coastguard Worker { "", "require-explicit-ids", "",
197*890232f2SAndroid Build Coastguard Worker "When parsing schemas, require explicit ids (id: x)." },
198*890232f2SAndroid Build Coastguard Worker { "", "force-defaults", "",
199*890232f2SAndroid Build Coastguard Worker "Emit default values in binary output from JSON" },
200*890232f2SAndroid Build Coastguard Worker { "", "force-empty", "",
201*890232f2SAndroid Build Coastguard Worker "When serializing from object API representation, force strings and "
202*890232f2SAndroid Build Coastguard Worker "vectors to empty rather than null." },
203*890232f2SAndroid Build Coastguard Worker { "", "force-empty-vectors", "",
204*890232f2SAndroid Build Coastguard Worker "When serializing from object API representation, force vectors to empty "
205*890232f2SAndroid Build Coastguard Worker "rather than null." },
206*890232f2SAndroid Build Coastguard Worker { "", "flexbuffers", "",
207*890232f2SAndroid Build Coastguard Worker "Used with \"binary\" and \"json\" options, it generates data using "
208*890232f2SAndroid Build Coastguard Worker "schema-less FlexBuffers." },
209*890232f2SAndroid Build Coastguard Worker { "", "no-warnings", "", "Inhibit all warnings messages." },
210*890232f2SAndroid Build Coastguard Worker { "", "warnings-as-errors", "", "Treat all warnings as errors." },
211*890232f2SAndroid Build Coastguard Worker { "", "cs-global-alias", "",
212*890232f2SAndroid Build Coastguard Worker "Prepend \"global::\" to all user generated csharp classes and "
213*890232f2SAndroid Build Coastguard Worker "structs." },
214*890232f2SAndroid Build Coastguard Worker { "", "cs-gen-json-serializer", "",
215*890232f2SAndroid Build Coastguard Worker "Allows (de)serialization of JSON text in the Object API. (requires "
216*890232f2SAndroid Build Coastguard Worker "--gen-object-api)." },
217*890232f2SAndroid Build Coastguard Worker { "", "json-nested-bytes", "",
218*890232f2SAndroid Build Coastguard Worker "Allow a nested_flatbuffer field to be parsed as a vector of bytes"
219*890232f2SAndroid Build Coastguard Worker "in JSON, which is unsafe unless checked by a verifier afterwards." },
220*890232f2SAndroid Build Coastguard Worker { "", "ts-flat-files", "",
221*890232f2SAndroid Build Coastguard Worker "Only generated one typescript file per .fbs file." },
222*890232f2SAndroid Build Coastguard Worker { "", "annotate", "SCHEMA",
223*890232f2SAndroid Build Coastguard Worker "Annotate the provided BINARY_FILE with the specified SCHEMA file." },
224*890232f2SAndroid Build Coastguard Worker { "", "no-leak-private-annotation", "",
225*890232f2SAndroid Build Coastguard Worker "Prevents multiple type of annotations within a Fbs SCHEMA file."
226*890232f2SAndroid Build Coastguard Worker "Currently this is required to generate private types in Rust" },
227*890232f2SAndroid Build Coastguard Worker };
228*890232f2SAndroid Build Coastguard Worker
AppendTextWrappedString(std::stringstream & ss,std::string & text,size_t max_col,size_t start_col)229*890232f2SAndroid Build Coastguard Worker static void AppendTextWrappedString(std::stringstream &ss, std::string &text,
230*890232f2SAndroid Build Coastguard Worker size_t max_col, size_t start_col) {
231*890232f2SAndroid Build Coastguard Worker size_t max_line_length = max_col - start_col;
232*890232f2SAndroid Build Coastguard Worker
233*890232f2SAndroid Build Coastguard Worker if (text.length() > max_line_length) {
234*890232f2SAndroid Build Coastguard Worker size_t ideal_break_location = text.rfind(' ', max_line_length);
235*890232f2SAndroid Build Coastguard Worker size_t length = std::min(max_line_length, ideal_break_location);
236*890232f2SAndroid Build Coastguard Worker ss << text.substr(0, length) << "\n";
237*890232f2SAndroid Build Coastguard Worker ss << std::string(start_col, ' ');
238*890232f2SAndroid Build Coastguard Worker std::string rest_of_description = text.substr(
239*890232f2SAndroid Build Coastguard Worker ((ideal_break_location < max_line_length || text.at(length) == ' ')
240*890232f2SAndroid Build Coastguard Worker ? length + 1
241*890232f2SAndroid Build Coastguard Worker : length));
242*890232f2SAndroid Build Coastguard Worker AppendTextWrappedString(ss, rest_of_description, max_col, start_col);
243*890232f2SAndroid Build Coastguard Worker } else {
244*890232f2SAndroid Build Coastguard Worker ss << text;
245*890232f2SAndroid Build Coastguard Worker }
246*890232f2SAndroid Build Coastguard Worker }
247*890232f2SAndroid Build Coastguard Worker
AppendOption(std::stringstream & ss,const FlatCOption & option,size_t max_col,size_t min_col_for_description)248*890232f2SAndroid Build Coastguard Worker static void AppendOption(std::stringstream &ss, const FlatCOption &option,
249*890232f2SAndroid Build Coastguard Worker size_t max_col, size_t min_col_for_description) {
250*890232f2SAndroid Build Coastguard Worker size_t chars = 2;
251*890232f2SAndroid Build Coastguard Worker ss << " ";
252*890232f2SAndroid Build Coastguard Worker if (!option.short_opt.empty()) {
253*890232f2SAndroid Build Coastguard Worker chars += 2 + option.short_opt.length();
254*890232f2SAndroid Build Coastguard Worker ss << "-" << option.short_opt;
255*890232f2SAndroid Build Coastguard Worker if (!option.long_opt.empty()) {
256*890232f2SAndroid Build Coastguard Worker chars++;
257*890232f2SAndroid Build Coastguard Worker ss << ",";
258*890232f2SAndroid Build Coastguard Worker }
259*890232f2SAndroid Build Coastguard Worker ss << " ";
260*890232f2SAndroid Build Coastguard Worker }
261*890232f2SAndroid Build Coastguard Worker if (!option.long_opt.empty()) {
262*890232f2SAndroid Build Coastguard Worker chars += 3 + option.long_opt.length();
263*890232f2SAndroid Build Coastguard Worker ss << "--" << option.long_opt << " ";
264*890232f2SAndroid Build Coastguard Worker }
265*890232f2SAndroid Build Coastguard Worker if (!option.parameter.empty()) {
266*890232f2SAndroid Build Coastguard Worker chars += 1 + option.parameter.length();
267*890232f2SAndroid Build Coastguard Worker ss << option.parameter << " ";
268*890232f2SAndroid Build Coastguard Worker }
269*890232f2SAndroid Build Coastguard Worker size_t start_of_description = chars;
270*890232f2SAndroid Build Coastguard Worker if (start_of_description > min_col_for_description) {
271*890232f2SAndroid Build Coastguard Worker ss << "\n";
272*890232f2SAndroid Build Coastguard Worker start_of_description = min_col_for_description;
273*890232f2SAndroid Build Coastguard Worker ss << std::string(start_of_description, ' ');
274*890232f2SAndroid Build Coastguard Worker } else {
275*890232f2SAndroid Build Coastguard Worker while (start_of_description < min_col_for_description) {
276*890232f2SAndroid Build Coastguard Worker ss << " ";
277*890232f2SAndroid Build Coastguard Worker start_of_description++;
278*890232f2SAndroid Build Coastguard Worker }
279*890232f2SAndroid Build Coastguard Worker }
280*890232f2SAndroid Build Coastguard Worker if (!option.description.empty()) {
281*890232f2SAndroid Build Coastguard Worker std::string description = option.description;
282*890232f2SAndroid Build Coastguard Worker AppendTextWrappedString(ss, description, max_col, start_of_description);
283*890232f2SAndroid Build Coastguard Worker }
284*890232f2SAndroid Build Coastguard Worker ss << "\n";
285*890232f2SAndroid Build Coastguard Worker }
286*890232f2SAndroid Build Coastguard Worker
AppendShortOption(std::stringstream & ss,const FlatCOption & option)287*890232f2SAndroid Build Coastguard Worker static void AppendShortOption(std::stringstream &ss,
288*890232f2SAndroid Build Coastguard Worker const FlatCOption &option) {
289*890232f2SAndroid Build Coastguard Worker if (!option.short_opt.empty()) {
290*890232f2SAndroid Build Coastguard Worker ss << "-" << option.short_opt;
291*890232f2SAndroid Build Coastguard Worker if (!option.long_opt.empty()) { ss << "|"; }
292*890232f2SAndroid Build Coastguard Worker }
293*890232f2SAndroid Build Coastguard Worker if (!option.long_opt.empty()) { ss << "--" << option.long_opt; }
294*890232f2SAndroid Build Coastguard Worker }
295*890232f2SAndroid Build Coastguard Worker
GetShortUsageString(const char * program_name) const296*890232f2SAndroid Build Coastguard Worker std::string FlatCompiler::GetShortUsageString(const char *program_name) const {
297*890232f2SAndroid Build Coastguard Worker std::stringstream ss;
298*890232f2SAndroid Build Coastguard Worker ss << "Usage: " << program_name << " [";
299*890232f2SAndroid Build Coastguard Worker for (size_t i = 0; i < params_.num_generators; ++i) {
300*890232f2SAndroid Build Coastguard Worker const Generator &g = params_.generators[i];
301*890232f2SAndroid Build Coastguard Worker AppendShortOption(ss, g.option);
302*890232f2SAndroid Build Coastguard Worker ss << ", ";
303*890232f2SAndroid Build Coastguard Worker }
304*890232f2SAndroid Build Coastguard Worker for (const FlatCOption &option : options) {
305*890232f2SAndroid Build Coastguard Worker AppendShortOption(ss, option);
306*890232f2SAndroid Build Coastguard Worker ss << ", ";
307*890232f2SAndroid Build Coastguard Worker }
308*890232f2SAndroid Build Coastguard Worker ss.seekp(-2, ss.cur);
309*890232f2SAndroid Build Coastguard Worker ss << "]... FILE... [-- BINARY_FILE...]";
310*890232f2SAndroid Build Coastguard Worker std::string help = ss.str();
311*890232f2SAndroid Build Coastguard Worker std::stringstream ss_textwrap;
312*890232f2SAndroid Build Coastguard Worker AppendTextWrappedString(ss_textwrap, help, 80, 0);
313*890232f2SAndroid Build Coastguard Worker return ss_textwrap.str();
314*890232f2SAndroid Build Coastguard Worker }
315*890232f2SAndroid Build Coastguard Worker
GetUsageString(const char * program_name) const316*890232f2SAndroid Build Coastguard Worker std::string FlatCompiler::GetUsageString(const char *program_name) const {
317*890232f2SAndroid Build Coastguard Worker std::stringstream ss;
318*890232f2SAndroid Build Coastguard Worker ss << "Usage: " << program_name
319*890232f2SAndroid Build Coastguard Worker << " [OPTION]... FILE... [-- BINARY_FILE...]\n";
320*890232f2SAndroid Build Coastguard Worker for (size_t i = 0; i < params_.num_generators; ++i) {
321*890232f2SAndroid Build Coastguard Worker const Generator &g = params_.generators[i];
322*890232f2SAndroid Build Coastguard Worker AppendOption(ss, g.option, 80, 25);
323*890232f2SAndroid Build Coastguard Worker }
324*890232f2SAndroid Build Coastguard Worker
325*890232f2SAndroid Build Coastguard Worker ss << "\n";
326*890232f2SAndroid Build Coastguard Worker for (const FlatCOption &option : options) {
327*890232f2SAndroid Build Coastguard Worker AppendOption(ss, option, 80, 25);
328*890232f2SAndroid Build Coastguard Worker }
329*890232f2SAndroid Build Coastguard Worker ss << "\n";
330*890232f2SAndroid Build Coastguard Worker
331*890232f2SAndroid Build Coastguard Worker std::string files_description =
332*890232f2SAndroid Build Coastguard Worker "FILEs may be schemas (must end in .fbs), binary schemas (must end in "
333*890232f2SAndroid Build Coastguard Worker ".bfbs) or JSON files (conforming to preceding schema). BINARY_FILEs "
334*890232f2SAndroid Build Coastguard Worker "after the -- must be binary flatbuffer format files. Output files are "
335*890232f2SAndroid Build Coastguard Worker "named using the base file name of the input, and written to the current "
336*890232f2SAndroid Build Coastguard Worker "directory or the path given by -o. example: " +
337*890232f2SAndroid Build Coastguard Worker std::string(program_name) + " -c -b schema1.fbs schema2.fbs data.json";
338*890232f2SAndroid Build Coastguard Worker AppendTextWrappedString(ss, files_description, 80, 0);
339*890232f2SAndroid Build Coastguard Worker ss << "\n";
340*890232f2SAndroid Build Coastguard Worker return ss.str();
341*890232f2SAndroid Build Coastguard Worker }
342*890232f2SAndroid Build Coastguard Worker
AnnotateBinaries(const uint8_t * binary_schema,const uint64_t binary_schema_size,const std::string & schema_filename,const std::vector<std::string> & binary_files)343*890232f2SAndroid Build Coastguard Worker void FlatCompiler::AnnotateBinaries(
344*890232f2SAndroid Build Coastguard Worker const uint8_t *binary_schema, const uint64_t binary_schema_size,
345*890232f2SAndroid Build Coastguard Worker const std::string &schema_filename,
346*890232f2SAndroid Build Coastguard Worker const std::vector<std::string> &binary_files) {
347*890232f2SAndroid Build Coastguard Worker for (const std::string &filename : binary_files) {
348*890232f2SAndroid Build Coastguard Worker std::string binary_contents;
349*890232f2SAndroid Build Coastguard Worker if (!flatbuffers::LoadFile(filename.c_str(), true, &binary_contents)) {
350*890232f2SAndroid Build Coastguard Worker Warn("unable to load binary file: " + filename);
351*890232f2SAndroid Build Coastguard Worker continue;
352*890232f2SAndroid Build Coastguard Worker }
353*890232f2SAndroid Build Coastguard Worker
354*890232f2SAndroid Build Coastguard Worker const uint8_t *binary =
355*890232f2SAndroid Build Coastguard Worker reinterpret_cast<const uint8_t *>(binary_contents.c_str());
356*890232f2SAndroid Build Coastguard Worker const size_t binary_size = binary_contents.size();
357*890232f2SAndroid Build Coastguard Worker
358*890232f2SAndroid Build Coastguard Worker flatbuffers::BinaryAnnotator binary_annotator(
359*890232f2SAndroid Build Coastguard Worker binary_schema, binary_schema_size, binary, binary_size);
360*890232f2SAndroid Build Coastguard Worker
361*890232f2SAndroid Build Coastguard Worker auto annotations = binary_annotator.Annotate();
362*890232f2SAndroid Build Coastguard Worker
363*890232f2SAndroid Build Coastguard Worker // TODO(dbaileychess): Right now we just support a single text-based
364*890232f2SAndroid Build Coastguard Worker // output of the annotated binary schema, which we generate here. We
365*890232f2SAndroid Build Coastguard Worker // could output the raw annotations instead and have third-party tools
366*890232f2SAndroid Build Coastguard Worker // use them to generate their own output.
367*890232f2SAndroid Build Coastguard Worker flatbuffers::AnnotatedBinaryTextGenerator text_generator(
368*890232f2SAndroid Build Coastguard Worker flatbuffers::AnnotatedBinaryTextGenerator::Options{}, annotations,
369*890232f2SAndroid Build Coastguard Worker binary, binary_size);
370*890232f2SAndroid Build Coastguard Worker
371*890232f2SAndroid Build Coastguard Worker text_generator.Generate(filename, schema_filename);
372*890232f2SAndroid Build Coastguard Worker }
373*890232f2SAndroid Build Coastguard Worker }
374*890232f2SAndroid Build Coastguard Worker
Compile(int argc,const char ** argv)375*890232f2SAndroid Build Coastguard Worker int FlatCompiler::Compile(int argc, const char **argv) {
376*890232f2SAndroid Build Coastguard Worker if (params_.generators == nullptr || params_.num_generators == 0) {
377*890232f2SAndroid Build Coastguard Worker return 0;
378*890232f2SAndroid Build Coastguard Worker }
379*890232f2SAndroid Build Coastguard Worker
380*890232f2SAndroid Build Coastguard Worker if (argc <= 1) { Error("Need to provide at least one argument."); }
381*890232f2SAndroid Build Coastguard Worker
382*890232f2SAndroid Build Coastguard Worker flatbuffers::IDLOptions opts;
383*890232f2SAndroid Build Coastguard Worker std::string output_path;
384*890232f2SAndroid Build Coastguard Worker
385*890232f2SAndroid Build Coastguard Worker bool any_generator = false;
386*890232f2SAndroid Build Coastguard Worker bool print_make_rules = false;
387*890232f2SAndroid Build Coastguard Worker bool raw_binary = false;
388*890232f2SAndroid Build Coastguard Worker bool schema_binary = false;
389*890232f2SAndroid Build Coastguard Worker bool grpc_enabled = false;
390*890232f2SAndroid Build Coastguard Worker bool requires_bfbs = false;
391*890232f2SAndroid Build Coastguard Worker std::vector<std::string> filenames;
392*890232f2SAndroid Build Coastguard Worker std::list<std::string> include_directories_storage;
393*890232f2SAndroid Build Coastguard Worker std::vector<const char *> include_directories;
394*890232f2SAndroid Build Coastguard Worker std::vector<const char *> conform_include_directories;
395*890232f2SAndroid Build Coastguard Worker std::vector<bool> generator_enabled(params_.num_generators, false);
396*890232f2SAndroid Build Coastguard Worker size_t binary_files_from = std::numeric_limits<size_t>::max();
397*890232f2SAndroid Build Coastguard Worker std::string conform_to_schema;
398*890232f2SAndroid Build Coastguard Worker std::string annotate_schema;
399*890232f2SAndroid Build Coastguard Worker
400*890232f2SAndroid Build Coastguard Worker const char *program_name = argv[0];
401*890232f2SAndroid Build Coastguard Worker
402*890232f2SAndroid Build Coastguard Worker for (int argi = 1; argi < argc; argi++) {
403*890232f2SAndroid Build Coastguard Worker std::string arg = argv[argi];
404*890232f2SAndroid Build Coastguard Worker if (arg[0] == '-') {
405*890232f2SAndroid Build Coastguard Worker if (filenames.size() && arg[1] != '-')
406*890232f2SAndroid Build Coastguard Worker Error("invalid option location: " + arg, true);
407*890232f2SAndroid Build Coastguard Worker if (arg == "-o") {
408*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing path following: " + arg, true);
409*890232f2SAndroid Build Coastguard Worker output_path = flatbuffers::ConCatPathFileName(
410*890232f2SAndroid Build Coastguard Worker flatbuffers::PosixPath(argv[argi]), "");
411*890232f2SAndroid Build Coastguard Worker } else if (arg == "-I") {
412*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing path following: " + arg, true);
413*890232f2SAndroid Build Coastguard Worker include_directories_storage.push_back(
414*890232f2SAndroid Build Coastguard Worker flatbuffers::PosixPath(argv[argi]));
415*890232f2SAndroid Build Coastguard Worker include_directories.push_back(
416*890232f2SAndroid Build Coastguard Worker include_directories_storage.back().c_str());
417*890232f2SAndroid Build Coastguard Worker } else if (arg == "--bfbs-filenames") {
418*890232f2SAndroid Build Coastguard Worker if (++argi > argc) Error("missing path following: " + arg, true);
419*890232f2SAndroid Build Coastguard Worker opts.project_root = argv[argi];
420*890232f2SAndroid Build Coastguard Worker if (!DirExists(opts.project_root.c_str()))
421*890232f2SAndroid Build Coastguard Worker Error(arg + " is not a directory: " + opts.project_root);
422*890232f2SAndroid Build Coastguard Worker } else if (arg == "--conform") {
423*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing path following: " + arg, true);
424*890232f2SAndroid Build Coastguard Worker conform_to_schema = flatbuffers::PosixPath(argv[argi]);
425*890232f2SAndroid Build Coastguard Worker } else if (arg == "--conform-includes") {
426*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing path following: " + arg, true);
427*890232f2SAndroid Build Coastguard Worker include_directories_storage.push_back(
428*890232f2SAndroid Build Coastguard Worker flatbuffers::PosixPath(argv[argi]));
429*890232f2SAndroid Build Coastguard Worker conform_include_directories.push_back(
430*890232f2SAndroid Build Coastguard Worker include_directories_storage.back().c_str());
431*890232f2SAndroid Build Coastguard Worker } else if (arg == "--include-prefix") {
432*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing path following: " + arg, true);
433*890232f2SAndroid Build Coastguard Worker opts.include_prefix = flatbuffers::ConCatPathFileName(
434*890232f2SAndroid Build Coastguard Worker flatbuffers::PosixPath(argv[argi]), "");
435*890232f2SAndroid Build Coastguard Worker } else if (arg == "--keep-prefix") {
436*890232f2SAndroid Build Coastguard Worker opts.keep_prefix = true;
437*890232f2SAndroid Build Coastguard Worker } else if (arg == "--strict-json") {
438*890232f2SAndroid Build Coastguard Worker opts.strict_json = true;
439*890232f2SAndroid Build Coastguard Worker } else if (arg == "--allow-non-utf8") {
440*890232f2SAndroid Build Coastguard Worker opts.allow_non_utf8 = true;
441*890232f2SAndroid Build Coastguard Worker } else if (arg == "--natural-utf8") {
442*890232f2SAndroid Build Coastguard Worker opts.natural_utf8 = true;
443*890232f2SAndroid Build Coastguard Worker } else if (arg == "--go-namespace") {
444*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing golang namespace" + arg, true);
445*890232f2SAndroid Build Coastguard Worker opts.go_namespace = argv[argi];
446*890232f2SAndroid Build Coastguard Worker } else if (arg == "--go-import") {
447*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing golang import" + arg, true);
448*890232f2SAndroid Build Coastguard Worker opts.go_import = argv[argi];
449*890232f2SAndroid Build Coastguard Worker } else if (arg == "--defaults-json") {
450*890232f2SAndroid Build Coastguard Worker opts.output_default_scalars_in_json = true;
451*890232f2SAndroid Build Coastguard Worker } else if (arg == "--unknown-json") {
452*890232f2SAndroid Build Coastguard Worker opts.skip_unexpected_fields_in_json = true;
453*890232f2SAndroid Build Coastguard Worker } else if (arg == "--no-prefix") {
454*890232f2SAndroid Build Coastguard Worker opts.prefixed_enums = false;
455*890232f2SAndroid Build Coastguard Worker } else if (arg == "--scoped-enums") {
456*890232f2SAndroid Build Coastguard Worker opts.prefixed_enums = false;
457*890232f2SAndroid Build Coastguard Worker opts.scoped_enums = true;
458*890232f2SAndroid Build Coastguard Worker } else if (arg == "--no-union-value-namespacing") {
459*890232f2SAndroid Build Coastguard Worker opts.union_value_namespacing = false;
460*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-mutable") {
461*890232f2SAndroid Build Coastguard Worker opts.mutable_buffer = true;
462*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-name-strings") {
463*890232f2SAndroid Build Coastguard Worker opts.generate_name_strings = true;
464*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-object-api") {
465*890232f2SAndroid Build Coastguard Worker opts.generate_object_based_api = true;
466*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-compare") {
467*890232f2SAndroid Build Coastguard Worker opts.gen_compare = true;
468*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cpp-include") {
469*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing include following: " + arg, true);
470*890232f2SAndroid Build Coastguard Worker opts.cpp_includes.push_back(argv[argi]);
471*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cpp-ptr-type") {
472*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing type following: " + arg, true);
473*890232f2SAndroid Build Coastguard Worker opts.cpp_object_api_pointer_type = argv[argi];
474*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cpp-str-type") {
475*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing type following: " + arg, true);
476*890232f2SAndroid Build Coastguard Worker opts.cpp_object_api_string_type = argv[argi];
477*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cpp-str-flex-ctor") {
478*890232f2SAndroid Build Coastguard Worker opts.cpp_object_api_string_flexible_constructor = true;
479*890232f2SAndroid Build Coastguard Worker } else if (arg == "--no-cpp-direct-copy") {
480*890232f2SAndroid Build Coastguard Worker opts.cpp_direct_copy = false;
481*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cpp-field-case-style") {
482*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing case style following: " + arg, true);
483*890232f2SAndroid Build Coastguard Worker if (!strcmp(argv[argi], "unchanged"))
484*890232f2SAndroid Build Coastguard Worker opts.cpp_object_api_field_case_style =
485*890232f2SAndroid Build Coastguard Worker IDLOptions::CaseStyle_Unchanged;
486*890232f2SAndroid Build Coastguard Worker else if (!strcmp(argv[argi], "upper"))
487*890232f2SAndroid Build Coastguard Worker opts.cpp_object_api_field_case_style = IDLOptions::CaseStyle_Upper;
488*890232f2SAndroid Build Coastguard Worker else if (!strcmp(argv[argi], "lower"))
489*890232f2SAndroid Build Coastguard Worker opts.cpp_object_api_field_case_style = IDLOptions::CaseStyle_Lower;
490*890232f2SAndroid Build Coastguard Worker else
491*890232f2SAndroid Build Coastguard Worker Error("unknown case style: " + std::string(argv[argi]), true);
492*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-nullable") {
493*890232f2SAndroid Build Coastguard Worker opts.gen_nullable = true;
494*890232f2SAndroid Build Coastguard Worker } else if (arg == "--java-checkerframework") {
495*890232f2SAndroid Build Coastguard Worker opts.java_checkerframework = true;
496*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-generated") {
497*890232f2SAndroid Build Coastguard Worker opts.gen_generated = true;
498*890232f2SAndroid Build Coastguard Worker } else if (arg == "--swift-implementation-only") {
499*890232f2SAndroid Build Coastguard Worker opts.swift_implementation_only = true;
500*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-json-emit") {
501*890232f2SAndroid Build Coastguard Worker opts.gen_json_coders = true;
502*890232f2SAndroid Build Coastguard Worker } else if (arg == "--object-prefix") {
503*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing prefix following: " + arg, true);
504*890232f2SAndroid Build Coastguard Worker opts.object_prefix = argv[argi];
505*890232f2SAndroid Build Coastguard Worker } else if (arg == "--object-suffix") {
506*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing suffix following: " + arg, true);
507*890232f2SAndroid Build Coastguard Worker opts.object_suffix = argv[argi];
508*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-all") {
509*890232f2SAndroid Build Coastguard Worker opts.generate_all = true;
510*890232f2SAndroid Build Coastguard Worker opts.include_dependence_headers = false;
511*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-includes") {
512*890232f2SAndroid Build Coastguard Worker // Deprecated, remove this option some time in the future.
513*890232f2SAndroid Build Coastguard Worker Warn("warning: --gen-includes is deprecated (it is now default)\n");
514*890232f2SAndroid Build Coastguard Worker } else if (arg == "--no-includes") {
515*890232f2SAndroid Build Coastguard Worker opts.include_dependence_headers = false;
516*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-onefile") {
517*890232f2SAndroid Build Coastguard Worker opts.one_file = true;
518*890232f2SAndroid Build Coastguard Worker opts.include_dependence_headers = false;
519*890232f2SAndroid Build Coastguard Worker } else if (arg == "--raw-binary") {
520*890232f2SAndroid Build Coastguard Worker raw_binary = true;
521*890232f2SAndroid Build Coastguard Worker } else if (arg == "--size-prefixed") {
522*890232f2SAndroid Build Coastguard Worker opts.size_prefixed = true;
523*890232f2SAndroid Build Coastguard Worker } else if (arg == "--") { // Separator between text and binary inputs.
524*890232f2SAndroid Build Coastguard Worker binary_files_from = filenames.size();
525*890232f2SAndroid Build Coastguard Worker } else if (arg == "--proto") {
526*890232f2SAndroid Build Coastguard Worker opts.proto_mode = true;
527*890232f2SAndroid Build Coastguard Worker } else if (arg == "--proto-namespace-suffix") {
528*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing namespace suffix" + arg, true);
529*890232f2SAndroid Build Coastguard Worker opts.proto_namespace_suffix = argv[argi];
530*890232f2SAndroid Build Coastguard Worker } else if (arg == "--oneof-union") {
531*890232f2SAndroid Build Coastguard Worker opts.proto_oneof_union = true;
532*890232f2SAndroid Build Coastguard Worker } else if (arg == "--schema") {
533*890232f2SAndroid Build Coastguard Worker schema_binary = true;
534*890232f2SAndroid Build Coastguard Worker } else if (arg == "-M") {
535*890232f2SAndroid Build Coastguard Worker print_make_rules = true;
536*890232f2SAndroid Build Coastguard Worker } else if (arg == "--version") {
537*890232f2SAndroid Build Coastguard Worker printf("flatc version %s\n", FLATC_VERSION());
538*890232f2SAndroid Build Coastguard Worker exit(0);
539*890232f2SAndroid Build Coastguard Worker } else if (arg == "--help" || arg == "-h") {
540*890232f2SAndroid Build Coastguard Worker printf("%s\n", GetUsageString(program_name).c_str());
541*890232f2SAndroid Build Coastguard Worker exit(0);
542*890232f2SAndroid Build Coastguard Worker } else if (arg == "--grpc") {
543*890232f2SAndroid Build Coastguard Worker grpc_enabled = true;
544*890232f2SAndroid Build Coastguard Worker } else if (arg == "--bfbs-comments") {
545*890232f2SAndroid Build Coastguard Worker opts.binary_schema_comments = true;
546*890232f2SAndroid Build Coastguard Worker } else if (arg == "--bfbs-builtins") {
547*890232f2SAndroid Build Coastguard Worker opts.binary_schema_builtins = true;
548*890232f2SAndroid Build Coastguard Worker } else if (arg == "--bfbs-gen-embed") {
549*890232f2SAndroid Build Coastguard Worker opts.binary_schema_gen_embed = true;
550*890232f2SAndroid Build Coastguard Worker } else if (arg == "--reflect-types") {
551*890232f2SAndroid Build Coastguard Worker opts.mini_reflect = IDLOptions::kTypes;
552*890232f2SAndroid Build Coastguard Worker } else if (arg == "--reflect-names") {
553*890232f2SAndroid Build Coastguard Worker opts.mini_reflect = IDLOptions::kTypesAndNames;
554*890232f2SAndroid Build Coastguard Worker } else if (arg == "--rust-serialize") {
555*890232f2SAndroid Build Coastguard Worker opts.rust_serialize = true;
556*890232f2SAndroid Build Coastguard Worker } else if (arg == "--rust-module-root-file") {
557*890232f2SAndroid Build Coastguard Worker opts.rust_module_root_file = true;
558*890232f2SAndroid Build Coastguard Worker } else if (arg == "--require-explicit-ids") {
559*890232f2SAndroid Build Coastguard Worker opts.require_explicit_ids = true;
560*890232f2SAndroid Build Coastguard Worker } else if (arg == "--root-type") {
561*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing type following: " + arg, true);
562*890232f2SAndroid Build Coastguard Worker opts.root_type = argv[argi];
563*890232f2SAndroid Build Coastguard Worker } else if (arg == "--filename-suffix") {
564*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing filename suffix: " + arg, true);
565*890232f2SAndroid Build Coastguard Worker opts.filename_suffix = argv[argi];
566*890232f2SAndroid Build Coastguard Worker } else if (arg == "--filename-ext") {
567*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing filename extension: " + arg, true);
568*890232f2SAndroid Build Coastguard Worker opts.filename_extension = argv[argi];
569*890232f2SAndroid Build Coastguard Worker } else if (arg == "--force-defaults") {
570*890232f2SAndroid Build Coastguard Worker opts.force_defaults = true;
571*890232f2SAndroid Build Coastguard Worker } else if (arg == "--force-empty") {
572*890232f2SAndroid Build Coastguard Worker opts.set_empty_strings_to_null = false;
573*890232f2SAndroid Build Coastguard Worker opts.set_empty_vectors_to_null = false;
574*890232f2SAndroid Build Coastguard Worker } else if (arg == "--force-empty-vectors") {
575*890232f2SAndroid Build Coastguard Worker opts.set_empty_vectors_to_null = false;
576*890232f2SAndroid Build Coastguard Worker } else if (arg == "--java-primitive-has-method") {
577*890232f2SAndroid Build Coastguard Worker opts.java_primitive_has_method = true;
578*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cs-gen-json-serializer") {
579*890232f2SAndroid Build Coastguard Worker opts.cs_gen_json_serializer = true;
580*890232f2SAndroid Build Coastguard Worker } else if (arg == "--flexbuffers") {
581*890232f2SAndroid Build Coastguard Worker opts.use_flexbuffers = true;
582*890232f2SAndroid Build Coastguard Worker } else if (arg == "--gen-jvmstatic") {
583*890232f2SAndroid Build Coastguard Worker opts.gen_jvmstatic = true;
584*890232f2SAndroid Build Coastguard Worker } else if (arg == "--no-warnings") {
585*890232f2SAndroid Build Coastguard Worker opts.no_warnings = true;
586*890232f2SAndroid Build Coastguard Worker } else if (arg == "--warnings-as-errors") {
587*890232f2SAndroid Build Coastguard Worker opts.warnings_as_errors = true;
588*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cpp-std") {
589*890232f2SAndroid Build Coastguard Worker if (++argi >= argc)
590*890232f2SAndroid Build Coastguard Worker Error("missing C++ standard specification" + arg, true);
591*890232f2SAndroid Build Coastguard Worker opts.cpp_std = argv[argi];
592*890232f2SAndroid Build Coastguard Worker } else if (arg.rfind("--cpp-std=", 0) == 0) {
593*890232f2SAndroid Build Coastguard Worker opts.cpp_std = arg.substr(std::string("--cpp-std=").size());
594*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cpp-static-reflection") {
595*890232f2SAndroid Build Coastguard Worker opts.cpp_static_reflection = true;
596*890232f2SAndroid Build Coastguard Worker } else if (arg == "--cs-global-alias") {
597*890232f2SAndroid Build Coastguard Worker opts.cs_global_alias = true;
598*890232f2SAndroid Build Coastguard Worker } else if (arg == "--json-nested-bytes") {
599*890232f2SAndroid Build Coastguard Worker opts.json_nested_legacy_flatbuffers = true;
600*890232f2SAndroid Build Coastguard Worker } else if (arg == "--ts-flat-files") {
601*890232f2SAndroid Build Coastguard Worker opts.ts_flat_file = true;
602*890232f2SAndroid Build Coastguard Worker } else if (arg == "--no-leak-private-annotation") {
603*890232f2SAndroid Build Coastguard Worker opts.no_leak_private_annotations = true;
604*890232f2SAndroid Build Coastguard Worker } else if (arg == "--annotate") {
605*890232f2SAndroid Build Coastguard Worker if (++argi >= argc) Error("missing path following: " + arg, true);
606*890232f2SAndroid Build Coastguard Worker annotate_schema = flatbuffers::PosixPath(argv[argi]);
607*890232f2SAndroid Build Coastguard Worker } else {
608*890232f2SAndroid Build Coastguard Worker for (size_t i = 0; i < params_.num_generators; ++i) {
609*890232f2SAndroid Build Coastguard Worker if (arg == "--" + params_.generators[i].option.long_opt ||
610*890232f2SAndroid Build Coastguard Worker arg == "-" + params_.generators[i].option.short_opt) {
611*890232f2SAndroid Build Coastguard Worker generator_enabled[i] = true;
612*890232f2SAndroid Build Coastguard Worker any_generator = true;
613*890232f2SAndroid Build Coastguard Worker opts.lang_to_generate |= params_.generators[i].lang;
614*890232f2SAndroid Build Coastguard Worker if (params_.generators[i].bfbs_generator) {
615*890232f2SAndroid Build Coastguard Worker opts.binary_schema_comments = true;
616*890232f2SAndroid Build Coastguard Worker requires_bfbs = true;
617*890232f2SAndroid Build Coastguard Worker }
618*890232f2SAndroid Build Coastguard Worker goto found;
619*890232f2SAndroid Build Coastguard Worker }
620*890232f2SAndroid Build Coastguard Worker }
621*890232f2SAndroid Build Coastguard Worker Error("unknown commandline argument: " + arg, true);
622*890232f2SAndroid Build Coastguard Worker
623*890232f2SAndroid Build Coastguard Worker found:;
624*890232f2SAndroid Build Coastguard Worker }
625*890232f2SAndroid Build Coastguard Worker } else {
626*890232f2SAndroid Build Coastguard Worker filenames.push_back(flatbuffers::PosixPath(argv[argi]));
627*890232f2SAndroid Build Coastguard Worker }
628*890232f2SAndroid Build Coastguard Worker }
629*890232f2SAndroid Build Coastguard Worker
630*890232f2SAndroid Build Coastguard Worker if (!filenames.size()) Error("missing input files", false, true);
631*890232f2SAndroid Build Coastguard Worker
632*890232f2SAndroid Build Coastguard Worker if (opts.proto_mode) {
633*890232f2SAndroid Build Coastguard Worker if (any_generator)
634*890232f2SAndroid Build Coastguard Worker Error("cannot generate code directly from .proto files", true);
635*890232f2SAndroid Build Coastguard Worker } else if (!any_generator && conform_to_schema.empty() &&
636*890232f2SAndroid Build Coastguard Worker annotate_schema.empty()) {
637*890232f2SAndroid Build Coastguard Worker Error("no options: specify at least one generator.", true);
638*890232f2SAndroid Build Coastguard Worker }
639*890232f2SAndroid Build Coastguard Worker
640*890232f2SAndroid Build Coastguard Worker if (opts.cs_gen_json_serializer && !opts.generate_object_based_api) {
641*890232f2SAndroid Build Coastguard Worker Error(
642*890232f2SAndroid Build Coastguard Worker "--cs-gen-json-serializer requires --gen-object-api to be set as "
643*890232f2SAndroid Build Coastguard Worker "well.");
644*890232f2SAndroid Build Coastguard Worker }
645*890232f2SAndroid Build Coastguard Worker
646*890232f2SAndroid Build Coastguard Worker flatbuffers::Parser conform_parser;
647*890232f2SAndroid Build Coastguard Worker if (!conform_to_schema.empty()) {
648*890232f2SAndroid Build Coastguard Worker std::string contents;
649*890232f2SAndroid Build Coastguard Worker if (!flatbuffers::LoadFile(conform_to_schema.c_str(), true, &contents))
650*890232f2SAndroid Build Coastguard Worker Error("unable to load schema: " + conform_to_schema);
651*890232f2SAndroid Build Coastguard Worker
652*890232f2SAndroid Build Coastguard Worker if (flatbuffers::GetExtension(conform_to_schema) ==
653*890232f2SAndroid Build Coastguard Worker reflection::SchemaExtension()) {
654*890232f2SAndroid Build Coastguard Worker LoadBinarySchema(conform_parser, conform_to_schema, contents);
655*890232f2SAndroid Build Coastguard Worker } else {
656*890232f2SAndroid Build Coastguard Worker ParseFile(conform_parser, conform_to_schema, contents,
657*890232f2SAndroid Build Coastguard Worker conform_include_directories);
658*890232f2SAndroid Build Coastguard Worker }
659*890232f2SAndroid Build Coastguard Worker }
660*890232f2SAndroid Build Coastguard Worker
661*890232f2SAndroid Build Coastguard Worker if (!annotate_schema.empty()) {
662*890232f2SAndroid Build Coastguard Worker const std::string ext = flatbuffers::GetExtension(annotate_schema);
663*890232f2SAndroid Build Coastguard Worker if (!(ext == reflection::SchemaExtension() || ext == "fbs")) {
664*890232f2SAndroid Build Coastguard Worker Error("Expected a `.bfbs` or `.fbs` schema, got: " + annotate_schema);
665*890232f2SAndroid Build Coastguard Worker }
666*890232f2SAndroid Build Coastguard Worker
667*890232f2SAndroid Build Coastguard Worker const bool is_binary_schema = ext == reflection::SchemaExtension();
668*890232f2SAndroid Build Coastguard Worker
669*890232f2SAndroid Build Coastguard Worker std::string schema_contents;
670*890232f2SAndroid Build Coastguard Worker if (!flatbuffers::LoadFile(annotate_schema.c_str(),
671*890232f2SAndroid Build Coastguard Worker /*binary=*/is_binary_schema, &schema_contents)) {
672*890232f2SAndroid Build Coastguard Worker Error("unable to load schema: " + annotate_schema);
673*890232f2SAndroid Build Coastguard Worker }
674*890232f2SAndroid Build Coastguard Worker
675*890232f2SAndroid Build Coastguard Worker const uint8_t *binary_schema = nullptr;
676*890232f2SAndroid Build Coastguard Worker uint64_t binary_schema_size = 0;
677*890232f2SAndroid Build Coastguard Worker
678*890232f2SAndroid Build Coastguard Worker IDLOptions binary_opts;
679*890232f2SAndroid Build Coastguard Worker binary_opts.lang_to_generate |= flatbuffers::IDLOptions::kBinary;
680*890232f2SAndroid Build Coastguard Worker flatbuffers::Parser parser(binary_opts);
681*890232f2SAndroid Build Coastguard Worker
682*890232f2SAndroid Build Coastguard Worker if (is_binary_schema) {
683*890232f2SAndroid Build Coastguard Worker binary_schema =
684*890232f2SAndroid Build Coastguard Worker reinterpret_cast<const uint8_t *>(schema_contents.c_str());
685*890232f2SAndroid Build Coastguard Worker binary_schema_size = schema_contents.size();
686*890232f2SAndroid Build Coastguard Worker } else {
687*890232f2SAndroid Build Coastguard Worker // If we need to generate the .bfbs file from the provided schema file
688*890232f2SAndroid Build Coastguard Worker // (.fbs)
689*890232f2SAndroid Build Coastguard Worker ParseFile(parser, annotate_schema, schema_contents, include_directories);
690*890232f2SAndroid Build Coastguard Worker parser.Serialize();
691*890232f2SAndroid Build Coastguard Worker
692*890232f2SAndroid Build Coastguard Worker binary_schema = parser.builder_.GetBufferPointer();
693*890232f2SAndroid Build Coastguard Worker binary_schema_size = parser.builder_.GetSize();
694*890232f2SAndroid Build Coastguard Worker }
695*890232f2SAndroid Build Coastguard Worker
696*890232f2SAndroid Build Coastguard Worker if (binary_schema == nullptr || !binary_schema_size) {
697*890232f2SAndroid Build Coastguard Worker Error("could not parse a value binary schema from: " + annotate_schema);
698*890232f2SAndroid Build Coastguard Worker }
699*890232f2SAndroid Build Coastguard Worker
700*890232f2SAndroid Build Coastguard Worker // Annotate the provided files with the binary_schema.
701*890232f2SAndroid Build Coastguard Worker AnnotateBinaries(binary_schema, binary_schema_size, annotate_schema,
702*890232f2SAndroid Build Coastguard Worker filenames);
703*890232f2SAndroid Build Coastguard Worker
704*890232f2SAndroid Build Coastguard Worker // We don't support doing anything else after annotating a binary.
705*890232f2SAndroid Build Coastguard Worker return 0;
706*890232f2SAndroid Build Coastguard Worker }
707*890232f2SAndroid Build Coastguard Worker
708*890232f2SAndroid Build Coastguard Worker std::unique_ptr<flatbuffers::Parser> parser(new flatbuffers::Parser(opts));
709*890232f2SAndroid Build Coastguard Worker
710*890232f2SAndroid Build Coastguard Worker for (auto file_it = filenames.begin(); file_it != filenames.end();
711*890232f2SAndroid Build Coastguard Worker ++file_it) {
712*890232f2SAndroid Build Coastguard Worker auto &filename = *file_it;
713*890232f2SAndroid Build Coastguard Worker std::string contents;
714*890232f2SAndroid Build Coastguard Worker if (!flatbuffers::LoadFile(filename.c_str(), true, &contents))
715*890232f2SAndroid Build Coastguard Worker Error("unable to load file: " + filename);
716*890232f2SAndroid Build Coastguard Worker
717*890232f2SAndroid Build Coastguard Worker bool is_binary =
718*890232f2SAndroid Build Coastguard Worker static_cast<size_t>(file_it - filenames.begin()) >= binary_files_from;
719*890232f2SAndroid Build Coastguard Worker auto ext = flatbuffers::GetExtension(filename);
720*890232f2SAndroid Build Coastguard Worker const bool is_schema = ext == "fbs" || ext == "proto";
721*890232f2SAndroid Build Coastguard Worker if (is_schema && opts.project_root.empty()) {
722*890232f2SAndroid Build Coastguard Worker opts.project_root = StripFileName(filename);
723*890232f2SAndroid Build Coastguard Worker }
724*890232f2SAndroid Build Coastguard Worker const bool is_binary_schema = ext == reflection::SchemaExtension();
725*890232f2SAndroid Build Coastguard Worker if (is_binary) {
726*890232f2SAndroid Build Coastguard Worker parser->builder_.Clear();
727*890232f2SAndroid Build Coastguard Worker parser->builder_.PushFlatBuffer(
728*890232f2SAndroid Build Coastguard Worker reinterpret_cast<const uint8_t *>(contents.c_str()),
729*890232f2SAndroid Build Coastguard Worker contents.length());
730*890232f2SAndroid Build Coastguard Worker if (!raw_binary) {
731*890232f2SAndroid Build Coastguard Worker // Generally reading binaries that do not correspond to the schema
732*890232f2SAndroid Build Coastguard Worker // will crash, and sadly there's no way around that when the binary
733*890232f2SAndroid Build Coastguard Worker // does not contain a file identifier.
734*890232f2SAndroid Build Coastguard Worker // We'd expect that typically any binary used as a file would have
735*890232f2SAndroid Build Coastguard Worker // such an identifier, so by default we require them to match.
736*890232f2SAndroid Build Coastguard Worker if (!parser->file_identifier_.length()) {
737*890232f2SAndroid Build Coastguard Worker Error("current schema has no file_identifier: cannot test if \"" +
738*890232f2SAndroid Build Coastguard Worker filename +
739*890232f2SAndroid Build Coastguard Worker "\" matches the schema, use --raw-binary to read this file"
740*890232f2SAndroid Build Coastguard Worker " anyway.");
741*890232f2SAndroid Build Coastguard Worker } else if (!flatbuffers::BufferHasIdentifier(
742*890232f2SAndroid Build Coastguard Worker contents.c_str(), parser->file_identifier_.c_str(),
743*890232f2SAndroid Build Coastguard Worker opts.size_prefixed)) {
744*890232f2SAndroid Build Coastguard Worker Error("binary \"" + filename +
745*890232f2SAndroid Build Coastguard Worker "\" does not have expected file_identifier \"" +
746*890232f2SAndroid Build Coastguard Worker parser->file_identifier_ +
747*890232f2SAndroid Build Coastguard Worker "\", use --raw-binary to read this file anyway.");
748*890232f2SAndroid Build Coastguard Worker }
749*890232f2SAndroid Build Coastguard Worker }
750*890232f2SAndroid Build Coastguard Worker } else {
751*890232f2SAndroid Build Coastguard Worker // Check if file contains 0 bytes.
752*890232f2SAndroid Build Coastguard Worker if (!opts.use_flexbuffers && !is_binary_schema &&
753*890232f2SAndroid Build Coastguard Worker contents.length() != strlen(contents.c_str())) {
754*890232f2SAndroid Build Coastguard Worker Error("input file appears to be binary: " + filename, true);
755*890232f2SAndroid Build Coastguard Worker }
756*890232f2SAndroid Build Coastguard Worker if (is_schema || is_binary_schema) {
757*890232f2SAndroid Build Coastguard Worker // If we're processing multiple schemas, make sure to start each
758*890232f2SAndroid Build Coastguard Worker // one from scratch. If it depends on previous schemas it must do
759*890232f2SAndroid Build Coastguard Worker // so explicitly using an include.
760*890232f2SAndroid Build Coastguard Worker parser.reset(new flatbuffers::Parser(opts));
761*890232f2SAndroid Build Coastguard Worker }
762*890232f2SAndroid Build Coastguard Worker // Try to parse the file contents (binary schema/flexbuffer/textual
763*890232f2SAndroid Build Coastguard Worker // schema)
764*890232f2SAndroid Build Coastguard Worker if (is_binary_schema) {
765*890232f2SAndroid Build Coastguard Worker LoadBinarySchema(*parser.get(), filename, contents);
766*890232f2SAndroid Build Coastguard Worker } else if (opts.use_flexbuffers) {
767*890232f2SAndroid Build Coastguard Worker if (opts.lang_to_generate == IDLOptions::kJson) {
768*890232f2SAndroid Build Coastguard Worker auto data = reinterpret_cast<const uint8_t *>(contents.c_str());
769*890232f2SAndroid Build Coastguard Worker auto size = contents.size();
770*890232f2SAndroid Build Coastguard Worker std::vector<uint8_t> reuse_tracker;
771*890232f2SAndroid Build Coastguard Worker if (!flexbuffers::VerifyBuffer(data, size, &reuse_tracker))
772*890232f2SAndroid Build Coastguard Worker Error("flexbuffers file failed to verify: " + filename, false);
773*890232f2SAndroid Build Coastguard Worker parser->flex_root_ = flexbuffers::GetRoot(data, size);
774*890232f2SAndroid Build Coastguard Worker } else {
775*890232f2SAndroid Build Coastguard Worker parser->flex_builder_.Clear();
776*890232f2SAndroid Build Coastguard Worker ParseFile(*parser.get(), filename, contents, include_directories);
777*890232f2SAndroid Build Coastguard Worker }
778*890232f2SAndroid Build Coastguard Worker } else {
779*890232f2SAndroid Build Coastguard Worker ParseFile(*parser.get(), filename, contents, include_directories);
780*890232f2SAndroid Build Coastguard Worker if (!is_schema && !parser->builder_.GetSize()) {
781*890232f2SAndroid Build Coastguard Worker // If a file doesn't end in .fbs, it must be json/binary. Ensure we
782*890232f2SAndroid Build Coastguard Worker // didn't just parse a schema with a different extension.
783*890232f2SAndroid Build Coastguard Worker Error("input file is neither json nor a .fbs (schema) file: " +
784*890232f2SAndroid Build Coastguard Worker filename,
785*890232f2SAndroid Build Coastguard Worker true);
786*890232f2SAndroid Build Coastguard Worker }
787*890232f2SAndroid Build Coastguard Worker }
788*890232f2SAndroid Build Coastguard Worker if ((is_schema || is_binary_schema) && !conform_to_schema.empty()) {
789*890232f2SAndroid Build Coastguard Worker auto err = parser->ConformTo(conform_parser);
790*890232f2SAndroid Build Coastguard Worker if (!err.empty()) Error("schemas don\'t conform: " + err, false);
791*890232f2SAndroid Build Coastguard Worker }
792*890232f2SAndroid Build Coastguard Worker if (schema_binary || opts.binary_schema_gen_embed) {
793*890232f2SAndroid Build Coastguard Worker parser->Serialize();
794*890232f2SAndroid Build Coastguard Worker }
795*890232f2SAndroid Build Coastguard Worker if (schema_binary) {
796*890232f2SAndroid Build Coastguard Worker parser->file_extension_ = reflection::SchemaExtension();
797*890232f2SAndroid Build Coastguard Worker }
798*890232f2SAndroid Build Coastguard Worker }
799*890232f2SAndroid Build Coastguard Worker std::string filebase =
800*890232f2SAndroid Build Coastguard Worker flatbuffers::StripPath(flatbuffers::StripExtension(filename));
801*890232f2SAndroid Build Coastguard Worker
802*890232f2SAndroid Build Coastguard Worker // If one of the generators uses bfbs, serialize the parser and get
803*890232f2SAndroid Build Coastguard Worker // the serialized buffer and length.
804*890232f2SAndroid Build Coastguard Worker const uint8_t *bfbs_buffer = nullptr;
805*890232f2SAndroid Build Coastguard Worker int64_t bfbs_length = 0;
806*890232f2SAndroid Build Coastguard Worker if (requires_bfbs) {
807*890232f2SAndroid Build Coastguard Worker parser->Serialize();
808*890232f2SAndroid Build Coastguard Worker bfbs_buffer = parser->builder_.GetBufferPointer();
809*890232f2SAndroid Build Coastguard Worker bfbs_length = parser->builder_.GetSize();
810*890232f2SAndroid Build Coastguard Worker }
811*890232f2SAndroid Build Coastguard Worker
812*890232f2SAndroid Build Coastguard Worker for (size_t i = 0; i < params_.num_generators; ++i) {
813*890232f2SAndroid Build Coastguard Worker if (generator_enabled[i]) {
814*890232f2SAndroid Build Coastguard Worker if (!print_make_rules) {
815*890232f2SAndroid Build Coastguard Worker flatbuffers::EnsureDirExists(output_path);
816*890232f2SAndroid Build Coastguard Worker
817*890232f2SAndroid Build Coastguard Worker // Prefer bfbs generators if present.
818*890232f2SAndroid Build Coastguard Worker if (params_.generators[i].bfbs_generator) {
819*890232f2SAndroid Build Coastguard Worker const GeneratorStatus status =
820*890232f2SAndroid Build Coastguard Worker params_.generators[i].bfbs_generator->Generate(bfbs_buffer,
821*890232f2SAndroid Build Coastguard Worker bfbs_length);
822*890232f2SAndroid Build Coastguard Worker if (status != OK) {
823*890232f2SAndroid Build Coastguard Worker Error(std::string("Unable to generate ") +
824*890232f2SAndroid Build Coastguard Worker params_.generators[i].lang_name + " for " + filebase +
825*890232f2SAndroid Build Coastguard Worker " using bfbs generator.");
826*890232f2SAndroid Build Coastguard Worker }
827*890232f2SAndroid Build Coastguard Worker } else {
828*890232f2SAndroid Build Coastguard Worker if ((!params_.generators[i].schema_only ||
829*890232f2SAndroid Build Coastguard Worker (is_schema || is_binary_schema)) &&
830*890232f2SAndroid Build Coastguard Worker !params_.generators[i].generate(*parser.get(), output_path,
831*890232f2SAndroid Build Coastguard Worker filebase)) {
832*890232f2SAndroid Build Coastguard Worker Error(std::string("Unable to generate ") +
833*890232f2SAndroid Build Coastguard Worker params_.generators[i].lang_name + " for " + filebase);
834*890232f2SAndroid Build Coastguard Worker }
835*890232f2SAndroid Build Coastguard Worker }
836*890232f2SAndroid Build Coastguard Worker } else {
837*890232f2SAndroid Build Coastguard Worker if (params_.generators[i].make_rule == nullptr) {
838*890232f2SAndroid Build Coastguard Worker Error(std::string("Cannot generate make rule for ") +
839*890232f2SAndroid Build Coastguard Worker params_.generators[i].lang_name);
840*890232f2SAndroid Build Coastguard Worker } else {
841*890232f2SAndroid Build Coastguard Worker std::string make_rule = params_.generators[i].make_rule(
842*890232f2SAndroid Build Coastguard Worker *parser.get(), output_path, filename);
843*890232f2SAndroid Build Coastguard Worker if (!make_rule.empty())
844*890232f2SAndroid Build Coastguard Worker printf("%s\n",
845*890232f2SAndroid Build Coastguard Worker flatbuffers::WordWrap(make_rule, 80, " ", " \\").c_str());
846*890232f2SAndroid Build Coastguard Worker }
847*890232f2SAndroid Build Coastguard Worker }
848*890232f2SAndroid Build Coastguard Worker if (grpc_enabled) {
849*890232f2SAndroid Build Coastguard Worker if (params_.generators[i].generateGRPC != nullptr) {
850*890232f2SAndroid Build Coastguard Worker if (!params_.generators[i].generateGRPC(*parser.get(), output_path,
851*890232f2SAndroid Build Coastguard Worker filebase)) {
852*890232f2SAndroid Build Coastguard Worker Error(std::string("Unable to generate GRPC interface for ") +
853*890232f2SAndroid Build Coastguard Worker params_.generators[i].lang_name);
854*890232f2SAndroid Build Coastguard Worker }
855*890232f2SAndroid Build Coastguard Worker } else {
856*890232f2SAndroid Build Coastguard Worker Warn(std::string("GRPC interface generator not implemented for ") +
857*890232f2SAndroid Build Coastguard Worker params_.generators[i].lang_name);
858*890232f2SAndroid Build Coastguard Worker }
859*890232f2SAndroid Build Coastguard Worker }
860*890232f2SAndroid Build Coastguard Worker }
861*890232f2SAndroid Build Coastguard Worker }
862*890232f2SAndroid Build Coastguard Worker
863*890232f2SAndroid Build Coastguard Worker if (!opts.root_type.empty()) {
864*890232f2SAndroid Build Coastguard Worker if (!parser->SetRootType(opts.root_type.c_str()))
865*890232f2SAndroid Build Coastguard Worker Error("unknown root type: " + opts.root_type);
866*890232f2SAndroid Build Coastguard Worker else if (parser->root_struct_def_->fixed)
867*890232f2SAndroid Build Coastguard Worker Error("root type must be a table");
868*890232f2SAndroid Build Coastguard Worker }
869*890232f2SAndroid Build Coastguard Worker
870*890232f2SAndroid Build Coastguard Worker if (opts.proto_mode) GenerateFBS(*parser.get(), output_path, filebase);
871*890232f2SAndroid Build Coastguard Worker
872*890232f2SAndroid Build Coastguard Worker // We do not want to generate code for the definitions in this file
873*890232f2SAndroid Build Coastguard Worker // in any files coming up next.
874*890232f2SAndroid Build Coastguard Worker parser->MarkGenerated();
875*890232f2SAndroid Build Coastguard Worker }
876*890232f2SAndroid Build Coastguard Worker
877*890232f2SAndroid Build Coastguard Worker // Once all the files have been parsed, run any generators Parsing Completed
878*890232f2SAndroid Build Coastguard Worker // function for final generation.
879*890232f2SAndroid Build Coastguard Worker for (size_t i = 0; i < params_.num_generators; ++i) {
880*890232f2SAndroid Build Coastguard Worker if (generator_enabled[i] &&
881*890232f2SAndroid Build Coastguard Worker params_.generators[i].parsing_completed != nullptr) {
882*890232f2SAndroid Build Coastguard Worker if (!params_.generators[i].parsing_completed(*parser, output_path)) {
883*890232f2SAndroid Build Coastguard Worker Error("failed running parsing completed for " +
884*890232f2SAndroid Build Coastguard Worker std::string(params_.generators[i].lang_name));
885*890232f2SAndroid Build Coastguard Worker }
886*890232f2SAndroid Build Coastguard Worker }
887*890232f2SAndroid Build Coastguard Worker }
888*890232f2SAndroid Build Coastguard Worker
889*890232f2SAndroid Build Coastguard Worker return 0;
890*890232f2SAndroid Build Coastguard Worker }
891*890232f2SAndroid Build Coastguard Worker
892*890232f2SAndroid Build Coastguard Worker } // namespace flatbuffers
893