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