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

source code of flutter_engine/third_party/flatbuffers/src/flatc.cpp