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