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 
21 #define FLATC_VERSION "1.11.0"
22 
23 namespace flatbuffers {
24 
ParseFile(flatbuffers::Parser & parser,const std::string & filename,const std::string & contents,std::vector<const char * > & include_directories) const25 void FlatCompiler::ParseFile(
26     flatbuffers::Parser &parser, const std::string &filename,
27     const std::string &contents,
28     std::vector<const char *> &include_directories) const {
29   auto local_include_directory = flatbuffers::StripFileName(filename);
30   include_directories.push_back(local_include_directory.c_str());
31   include_directories.push_back(nullptr);
32   if (!parser.Parse(contents.c_str(), &include_directories[0],
33                     filename.c_str())) {
34     Error(parser.error_, false, false);
35   }
36   if (!parser.error_.empty()) { Warn(parser.error_, false); }
37   include_directories.pop_back();
38   include_directories.pop_back();
39 }
40 
LoadBinarySchema(flatbuffers::Parser & parser,const std::string & filename,const std::string & contents)41 void FlatCompiler::LoadBinarySchema(flatbuffers::Parser &parser,
42                                     const std::string &filename,
43                                     const std::string &contents) {
44   if (!parser.Deserialize(reinterpret_cast<const uint8_t *>(contents.c_str()),
45       contents.size())) {
46     Error("failed to load binary schema: " + filename, false, false);
47   }
48 }
49 
Warn(const std::string & warn,bool show_exe_name) const50 void FlatCompiler::Warn(const std::string &warn, bool show_exe_name) const {
51   params_.warn_fn(this, warn, show_exe_name);
52 }
53 
Error(const std::string & err,bool usage,bool show_exe_name) const54 void FlatCompiler::Error(const std::string &err, bool usage,
55                          bool show_exe_name) const {
56   params_.error_fn(this, err, usage, show_exe_name);
57 }
58 
GetUsageString(const char * program_name) const59 std::string FlatCompiler::GetUsageString(const char *program_name) const {
60   std::stringstream ss;
61   ss << "Usage: " << program_name << " [OPTION]... FILE... [-- FILE...]\n";
62   for (size_t i = 0; i < params_.num_generators; ++i) {
63     const Generator &g = params_.generators[i];
64 
65     std::stringstream full_name;
66     full_name << std::setw(12) << std::left << g.generator_opt_long;
67     const char *name = g.generator_opt_short ? g.generator_opt_short : "  ";
68     const char *help = g.generator_help;
69 
70     ss << "  " << full_name.str() << " " << name << "    " << help << ".\n";
71   }
72   // clang-format off
73   ss <<
74     "  -o PATH            Prefix PATH to all generated files.\n"
75     "  -I PATH            Search for includes in the specified path.\n"
76     "  -M                 Print make rules for generated files.\n"
77     "  --version          Print the version number of flatc and exit.\n"
78     "  --strict-json      Strict JSON: field names must be / will be quoted,\n"
79     "                     no trailing commas in tables/vectors.\n"
80     "  --allow-non-utf8   Pass non-UTF-8 input through parser and emit nonstandard\n"
81     "                     \\x escapes in JSON. (Default is to raise parse error on\n"
82     "                     non-UTF-8 input.)\n"
83     "  --natural-utf8     Output strings with UTF-8 as human-readable strings.\n"
84     "                     By default, UTF-8 characters are printed as \\uXXXX escapes.\n"
85     "  --defaults-json    Output fields whose value is the default when\n"
86     "                     writing JSON\n"
87     "  --unknown-json     Allow fields in JSON that are not defined in the\n"
88     "                     schema. These fields will be discared when generating\n"
89     "                     binaries.\n"
90     "  --no-prefix        Don\'t prefix enum values with the enum type in C++.\n"
91     "  --scoped-enums     Use C++11 style scoped and strongly typed enums.\n"
92     "                     also implies --no-prefix.\n"
93     "  --gen-includes     (deprecated), this is the default behavior.\n"
94     "                     If the original behavior is required (no include\n"
95     "                     statements) use --no-includes.\n"
96     "  --no-includes      Don\'t generate include statements for included\n"
97     "                     schemas the generated file depends on (C++).\n"
98     "  --gen-mutable      Generate accessors that can mutate buffers in-place.\n"
99     "  --gen-onefile      Generate single output file for C# and Go.\n"
100     "  --gen-name-strings Generate type name functions for C++.\n"
101     "  --gen-object-api   Generate an additional object-based API.\n"
102     "  --gen-compare      Generate operator== for object-based API types.\n"
103     "  --gen-nullable     Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
104     "  --gen-generated    Add @Generated annotation for Java\n"
105     "  --gen-all          Generate not just code for the current schema files,\n"
106     "                     but for all files it includes as well.\n"
107     "                     If the language uses a single file for output (by default\n"
108     "                     the case for C++ and JS), all code will end up in this one\n"
109     "                     file.\n"
110     "  --cpp-ptr-type T   Set object API pointer type (default std::unique_ptr).\n"
111     "  --cpp-str-type T   Set object API string type (default std::string).\n"
112     "                     T::c_str(), T::length() and T::empty() must be supported.\n"
113     "                     The custom type also needs to be constructible from std::string\n"
114     "                     (see the --cpp-str-flex-ctor option to change this behavior).\n"
115     "  --cpp-str-flex-ctor Don't construct custom string types by passing std::string\n"
116     "                     from Flatbuffers, but (char* + length).\n"
117     "  --object-prefix    Customise class prefix for C++ object-based API.\n"
118     "  --object-suffix    Customise class suffix for C++ object-based API.\n"
119     "                     Default value is \"T\".\n"
120     "  --no-js-exports    Removes Node.js style export lines in JS.\n"
121     "  --goog-js-export   Uses goog.exports* for closure compiler exporting in JS.\n"
122     "  --es6-js-export    Uses ECMAScript 6 export style lines in JS.\n"
123     "  --go-namespace     Generate the overrided namespace in Golang.\n"
124     "  --go-import        Generate the overrided import for flatbuffers in Golang\n"
125     "                     (default is \"github.com/google/flatbuffers/go\").\n"
126     "  --raw-binary       Allow binaries without file_indentifier to be read.\n"
127     "                     This may crash flatc given a mismatched schema.\n"
128     "  --size-prefixed    Input binaries are size prefixed buffers.\n"
129     "  --proto            Input is a .proto, translate to .fbs.\n"
130     "  --oneof-union      Translate .proto oneofs to flatbuffer unions.\n"
131     "  --grpc             Generate GRPC interfaces for the specified languages.\n"
132     "  --schema           Serialize schemas instead of JSON (use with -b).\n"
133     "  --bfbs-comments    Add doc comments to the binary schema files.\n"
134     "  --bfbs-builtins    Add builtin attributes to the binary schema files.\n"
135     "  --conform FILE     Specify a schema the following schemas should be\n"
136     "                     an evolution of. Gives errors if not.\n"
137     "  --conform-includes Include path for the schema given with --conform PATH\n"
138     "  --include-prefix   Prefix this path to any generated include statements.\n"
139     "    PATH\n"
140     "  --keep-prefix      Keep original prefix of schema include statement.\n"
141     "  --no-fb-import     Don't include flatbuffers import statement for TypeScript.\n"
142     "  --no-ts-reexport   Don't re-export imported dependencies for TypeScript.\n"
143     "  --short-names      Use short function names for JS and TypeScript.\n"
144     "  --reflect-types    Add minimal type reflection to code generation.\n"
145     "  --reflect-names    Add minimal type/name reflection.\n"
146     "  --root-type T      Select or override the default root_type\n"
147     "  --force-defaults   Emit default values in binary output from JSON\n"
148     "  --force-empty      When serializing from object API representation,\n"
149     "                     force strings and vectors to empty rather than null.\n"
150     "FILEs may be schemas (must end in .fbs), binary schemas (must end in .bfbs),\n"
151     "or JSON files (conforming to preceding schema). FILEs after the -- must be\n"
152     "binary flatbuffer format files.\n"
153     "Output files are named using the base file name of the input,\n"
154     "and written to the current directory or the path given by -o.\n"
155     "example: " << program_name << " -c -b schema1.fbs schema2.fbs data.json\n";
156   // clang-format on
157   return ss.str();
158 }
159 
Compile(int argc,const char ** argv)160 int FlatCompiler::Compile(int argc, const char **argv) {
161   if (params_.generators == nullptr || params_.num_generators == 0) {
162     return 0;
163   }
164 
165   flatbuffers::IDLOptions opts;
166   std::string output_path;
167 
168   bool any_generator = false;
169   bool print_make_rules = false;
170   bool raw_binary = false;
171   bool schema_binary = false;
172   bool grpc_enabled = false;
173   std::vector<std::string> filenames;
174   std::list<std::string> include_directories_storage;
175   std::vector<const char *> include_directories;
176   std::vector<const char *> conform_include_directories;
177   std::vector<bool> generator_enabled(params_.num_generators, false);
178   size_t binary_files_from = std::numeric_limits<size_t>::max();
179   std::string conform_to_schema;
180 
181   for (int argi = 0; argi < argc; argi++) {
182     std::string arg = argv[argi];
183     if (arg[0] == '-') {
184       if (filenames.size() && arg[1] != '-')
185         Error("invalid option location: " + arg, true);
186       if (arg == "-o") {
187         if (++argi >= argc) Error("missing path following: " + arg, true);
188         output_path = flatbuffers::ConCatPathFileName(
189             flatbuffers::PosixPath(argv[argi]), "");
190       } else if (arg == "-I") {
191         if (++argi >= argc) Error("missing path following" + arg, true);
192         include_directories_storage.push_back(
193             flatbuffers::PosixPath(argv[argi]));
194         include_directories.push_back(
195             include_directories_storage.back().c_str());
196       } else if (arg == "--conform") {
197         if (++argi >= argc) Error("missing path following" + arg, true);
198         conform_to_schema = flatbuffers::PosixPath(argv[argi]);
199       } else if (arg == "--conform-includes") {
200         if (++argi >= argc) Error("missing path following" + arg, true);
201         include_directories_storage.push_back(
202             flatbuffers::PosixPath(argv[argi]));
203         conform_include_directories.push_back(
204             include_directories_storage.back().c_str());
205       } else if (arg == "--include-prefix") {
206         if (++argi >= argc) Error("missing path following" + arg, true);
207         opts.include_prefix = flatbuffers::ConCatPathFileName(
208             flatbuffers::PosixPath(argv[argi]), "");
209       } else if (arg == "--keep-prefix") {
210         opts.keep_include_path = true;
211       } else if (arg == "--strict-json") {
212         opts.strict_json = true;
213       } else if (arg == "--allow-non-utf8") {
214         opts.allow_non_utf8 = true;
215       } else if (arg == "--natural-utf8") {
216         opts.natural_utf8 = true;
217       } else if (arg == "--no-js-exports") {
218         opts.skip_js_exports = true;
219       } else if (arg == "--goog-js-export") {
220         opts.use_goog_js_export_format = true;
221         opts.use_ES6_js_export_format = false;
222       } else if (arg == "--es6-js-export") {
223         opts.use_goog_js_export_format = false;
224         opts.use_ES6_js_export_format = true;
225       } else if (arg == "--go-namespace") {
226         if (++argi >= argc) Error("missing golang namespace" + arg, true);
227         opts.go_namespace = argv[argi];
228       } else if (arg == "--go-import") {
229         if (++argi >= argc) Error("missing golang import" + arg, true);
230         opts.go_import = argv[argi];
231       } else if (arg == "--defaults-json") {
232         opts.output_default_scalars_in_json = true;
233       } else if (arg == "--unknown-json") {
234         opts.skip_unexpected_fields_in_json = true;
235       } else if (arg == "--no-prefix") {
236         opts.prefixed_enums = false;
237       } else if (arg == "--scoped-enums") {
238         opts.prefixed_enums = false;
239         opts.scoped_enums = true;
240       } else if (arg == "--no-union-value-namespacing") {
241         opts.union_value_namespacing = false;
242       } else if (arg == "--gen-mutable") {
243         opts.mutable_buffer = true;
244       } else if (arg == "--gen-name-strings") {
245         opts.generate_name_strings = true;
246       } else if (arg == "--gen-object-api") {
247         opts.generate_object_based_api = true;
248       } else if (arg == "--gen-compare") {
249         opts.gen_compare = true;
250       } else if (arg == "--cpp-ptr-type") {
251         if (++argi >= argc) Error("missing type following" + arg, true);
252         opts.cpp_object_api_pointer_type = argv[argi];
253       } else if (arg == "--cpp-str-type") {
254         if (++argi >= argc) Error("missing type following" + arg, true);
255         opts.cpp_object_api_string_type = argv[argi];
256       } else if (arg == "--cpp-str-flex-ctor") {
257         opts.cpp_object_api_string_flexible_constructor = true;
258       } else if (arg == "--gen-nullable") {
259         opts.gen_nullable = true;
260       } else if (arg == "--gen-generated") {
261         opts.gen_generated = true;
262       } else if (arg == "--object-prefix") {
263         if (++argi >= argc) Error("missing prefix following" + arg, true);
264         opts.object_prefix = argv[argi];
265       } else if (arg == "--object-suffix") {
266         if (++argi >= argc) Error("missing suffix following" + arg, true);
267         opts.object_suffix = argv[argi];
268       } else if (arg == "--gen-all") {
269         opts.generate_all = true;
270         opts.include_dependence_headers = false;
271       } else if (arg == "--gen-includes") {
272         // Deprecated, remove this option some time in the future.
273         printf("warning: --gen-includes is deprecated (it is now default)\n");
274       } else if (arg == "--no-includes") {
275         opts.include_dependence_headers = false;
276       } else if (arg == "--gen-onefile") {
277         opts.one_file = true;
278       } else if (arg == "--raw-binary") {
279         raw_binary = true;
280       } else if (arg == "--size-prefixed") {
281         opts.size_prefixed = true;
282       } else if (arg == "--") {  // Separator between text and binary inputs.
283         binary_files_from = filenames.size();
284       } else if (arg == "--proto") {
285         opts.proto_mode = true;
286       } else if (arg == "--oneof-union") {
287         opts.proto_oneof_union = true;
288       } else if (arg == "--schema") {
289         schema_binary = true;
290       } else if (arg == "-M") {
291         print_make_rules = true;
292       } else if (arg == "--version") {
293         printf("flatc version %s\n", FLATC_VERSION);
294         exit(0);
295       } else if (arg == "--grpc") {
296         grpc_enabled = true;
297       } else if (arg == "--bfbs-comments") {
298         opts.binary_schema_comments = true;
299       } else if (arg == "--bfbs-builtins") {
300         opts.binary_schema_builtins = true;
301       } else if (arg == "--no-fb-import") {
302         opts.skip_flatbuffers_import = true;
303       } else if (arg == "--no-ts-reexport") {
304         opts.reexport_ts_modules = false;
305       } else if (arg == "--short-names") {
306         opts.js_ts_short_names = true;
307       } else if (arg == "--reflect-types") {
308         opts.mini_reflect = IDLOptions::kTypes;
309       } else if (arg == "--reflect-names") {
310         opts.mini_reflect = IDLOptions::kTypesAndNames;
311       } else if (arg == "--root-type") {
312         if (++argi >= argc) Error("missing type following" + arg, true);
313         opts.root_type = argv[argi];
314       } else if (arg == "--force-defaults") {
315         opts.force_defaults = true;
316       } else if (arg == "--force-empty") {
317         opts.set_empty_to_null = false;
318       } else {
319         for (size_t i = 0; i < params_.num_generators; ++i) {
320           if (arg == params_.generators[i].generator_opt_long ||
321               (params_.generators[i].generator_opt_short &&
322                arg == params_.generators[i].generator_opt_short)) {
323             generator_enabled[i] = true;
324             any_generator = true;
325             opts.lang_to_generate |= params_.generators[i].lang;
326             goto found;
327           }
328         }
329         Error("unknown commandline argument: " + arg, true);
330       found:;
331       }
332     } else {
333       filenames.push_back(flatbuffers::PosixPath(argv[argi]));
334     }
335   }
336 
337   if (!filenames.size()) Error("missing input files", false, true);
338 
339   if (opts.proto_mode) {
340     if (any_generator)
341       Error("cannot generate code directly from .proto files", true);
342   } else if (!any_generator && conform_to_schema.empty()) {
343     Error("no options: specify at least one generator.", true);
344   }
345 
346   flatbuffers::Parser conform_parser;
347   if (!conform_to_schema.empty()) {
348     std::string contents;
349     if (!flatbuffers::LoadFile(conform_to_schema.c_str(), true, &contents))
350       Error("unable to load schema: " + conform_to_schema);
351 
352     if (flatbuffers::GetExtension(conform_to_schema) ==
353         reflection::SchemaExtension()) {
354       LoadBinarySchema(conform_parser, conform_to_schema, contents);
355     } else {
356       ParseFile(conform_parser, conform_to_schema, contents,
357                 conform_include_directories);
358     }
359   }
360 
361   std::unique_ptr<flatbuffers::Parser> parser(new flatbuffers::Parser(opts));
362 
363   for (auto file_it = filenames.begin(); file_it != filenames.end();
364        ++file_it) {
365     auto &filename = *file_it;
366     std::string contents;
367     if (!flatbuffers::LoadFile(filename.c_str(), true, &contents))
368       Error("unable to load file: " + filename);
369 
370     bool is_binary =
371         static_cast<size_t>(file_it - filenames.begin()) >= binary_files_from;
372     auto ext = flatbuffers::GetExtension(filename);
373     auto is_schema = ext == "fbs" || ext == "proto";
374     auto is_binary_schema = ext == reflection::SchemaExtension();
375     if (is_binary) {
376       parser->builder_.Clear();
377       parser->builder_.PushFlatBuffer(
378           reinterpret_cast<const uint8_t *>(contents.c_str()),
379           contents.length());
380       if (!raw_binary) {
381         // Generally reading binaries that do not correspond to the schema
382         // will crash, and sadly there's no way around that when the binary
383         // does not contain a file identifier.
384         // We'd expect that typically any binary used as a file would have
385         // such an identifier, so by default we require them to match.
386         if (!parser->file_identifier_.length()) {
387           Error("current schema has no file_identifier: cannot test if \"" +
388                 filename +
389                 "\" matches the schema, use --raw-binary to read this file"
390                 " anyway.");
391         } else if (!flatbuffers::BufferHasIdentifier(
392                        contents.c_str(), parser->file_identifier_.c_str(), opts.size_prefixed)) {
393           Error("binary \"" + filename +
394                 "\" does not have expected file_identifier \"" +
395                 parser->file_identifier_ +
396                 "\", use --raw-binary to read this file anyway.");
397         }
398       }
399     } else {
400       // Check if file contains 0 bytes.
401       if (!is_binary_schema && contents.length() != strlen(contents.c_str())) {
402         Error("input file appears to be binary: " + filename, true);
403       }
404       if (is_schema) {
405         // If we're processing multiple schemas, make sure to start each
406         // one from scratch. If it depends on previous schemas it must do
407         // so explicitly using an include.
408         parser.reset(new flatbuffers::Parser(opts));
409       }
410       if (is_binary_schema) {
411         LoadBinarySchema(*parser.get(), filename, contents);
412       } else {
413         ParseFile(*parser.get(), filename, contents, include_directories);
414         if (!is_schema && !parser->builder_.GetSize()) {
415           // If a file doesn't end in .fbs, it must be json/binary. Ensure we
416           // didn't just parse a schema with a different extension.
417           Error("input file is neither json nor a .fbs (schema) file: " +
418                     filename,
419                 true);
420         }
421       }
422       if ((is_schema || is_binary_schema) && !conform_to_schema.empty()) {
423         auto err = parser->ConformTo(conform_parser);
424         if (!err.empty()) Error("schemas don\'t conform: " + err);
425       }
426       if (schema_binary) {
427         parser->Serialize();
428         parser->file_extension_ = reflection::SchemaExtension();
429       }
430     }
431 
432     std::string filebase =
433         flatbuffers::StripPath(flatbuffers::StripExtension(filename));
434 
435     for (size_t i = 0; i < params_.num_generators; ++i) {
436       parser->opts.lang = params_.generators[i].lang;
437       if (generator_enabled[i]) {
438         if (!print_make_rules) {
439           flatbuffers::EnsureDirExists(output_path);
440           if ((!params_.generators[i].schema_only ||
441                (is_schema || is_binary_schema)) &&
442               !params_.generators[i].generate(*parser.get(), output_path,
443                                               filebase)) {
444             Error(std::string("Unable to generate ") +
445                   params_.generators[i].lang_name + " for " + filebase);
446           }
447         } else {
448           std::string make_rule = params_.generators[i].make_rule(
449               *parser.get(), output_path, filename);
450           if (!make_rule.empty())
451             printf("%s\n",
452                    flatbuffers::WordWrap(make_rule, 80, " ", " \\").c_str());
453         }
454         if (grpc_enabled) {
455           if (params_.generators[i].generateGRPC != nullptr) {
456             if (!params_.generators[i].generateGRPC(*parser.get(), output_path,
457                                                     filebase)) {
458               Error(std::string("Unable to generate GRPC interface for") +
459                     params_.generators[i].lang_name);
460             }
461           } else {
462             Warn(std::string("GRPC interface generator not implemented for ") +
463                  params_.generators[i].lang_name);
464           }
465         }
466       }
467     }
468 
469     if (!opts.root_type.empty()) {
470       if (!parser->SetRootType(opts.root_type.c_str()))
471         Error("unknown root type: " + opts.root_type);
472       else if (parser->root_struct_def_->fixed)
473         Error("root type must be a table");
474     }
475 
476     if (opts.proto_mode) GenerateFBS(*parser.get(), output_path, filebase);
477 
478     // We do not want to generate code for the definitions in this file
479     // in any files coming up next.
480     parser->MarkGenerated();
481   }
482   return 0;
483 }
484 
485 }  // namespace flatbuffers
486