1
0

flatc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #include "flatbuffers/flatbuffers.h"
  17. #include "flatbuffers/idl.h"
  18. #include "flatbuffers/util.h"
  19. #include <limits>
  20. #define FLATC_VERSION "1.5.0 (" __DATE__ ")"
  21. static void Error(const std::string &err, bool usage = false,
  22. bool show_exe_name = true);
  23. // This struct allows us to create a table of all possible output generators
  24. // for the various programming languages and formats we support.
  25. struct Generator {
  26. bool (*generate)(const flatbuffers::Parser &parser,
  27. const std::string &path,
  28. const std::string &file_name);
  29. const char *generator_opt_short;
  30. const char *generator_opt_long;
  31. const char *lang_name;
  32. bool (*generateGRPC)(const flatbuffers::Parser &parser,
  33. const std::string &path,
  34. const std::string &file_name);
  35. flatbuffers::IDLOptions::Language lang;
  36. const char *generator_help;
  37. std::string (*make_rule)(const flatbuffers::Parser &parser,
  38. const std::string &path,
  39. const std::string &file_name);
  40. };
  41. const Generator generators[] = {
  42. { flatbuffers::GenerateBinary, "-b", "--binary", "binary",
  43. nullptr,
  44. flatbuffers::IDLOptions::kMAX,
  45. "Generate wire format binaries for any data definitions",
  46. flatbuffers::BinaryMakeRule },
  47. { flatbuffers::GenerateTextFile, "-t", "--json", "text",
  48. nullptr,
  49. flatbuffers::IDLOptions::kMAX,
  50. "Generate text output for any data definitions",
  51. flatbuffers::TextMakeRule },
  52. { flatbuffers::GenerateCPP, "-c", "--cpp", "C++",
  53. flatbuffers::GenerateCppGRPC,
  54. flatbuffers::IDLOptions::kMAX,
  55. "Generate C++ headers for tables/structs",
  56. flatbuffers::CPPMakeRule },
  57. { flatbuffers::GenerateGo, "-g", "--go", "Go",
  58. flatbuffers::GenerateGoGRPC,
  59. flatbuffers::IDLOptions::kGo,
  60. "Generate Go files for tables/structs",
  61. flatbuffers::GeneralMakeRule },
  62. { flatbuffers::GenerateGeneral, "-j", "--java", "Java",
  63. nullptr,
  64. flatbuffers::IDLOptions::kJava,
  65. "Generate Java classes for tables/structs",
  66. flatbuffers::GeneralMakeRule },
  67. { flatbuffers::GenerateJS, "-s", "--js", "JavaScript",
  68. nullptr,
  69. flatbuffers::IDLOptions::kMAX,
  70. "Generate JavaScript code for tables/structs",
  71. flatbuffers::JSMakeRule },
  72. { flatbuffers::GenerateGeneral, "-n", "--csharp", "C#",
  73. nullptr,
  74. flatbuffers::IDLOptions::kCSharp,
  75. "Generate C# classes for tables/structs",
  76. flatbuffers::GeneralMakeRule },
  77. { flatbuffers::GeneratePython, "-p", "--python", "Python",
  78. nullptr,
  79. flatbuffers::IDLOptions::kMAX,
  80. "Generate Python files for tables/structs",
  81. flatbuffers::GeneralMakeRule },
  82. { flatbuffers::GeneratePhp, nullptr, "--php", "PHP",
  83. nullptr,
  84. flatbuffers::IDLOptions::kMAX,
  85. "Generate PHP files for tables/structs",
  86. flatbuffers::GeneralMakeRule },
  87. };
  88. const char *g_program_name = nullptr;
  89. flatbuffers::Parser *g_parser = nullptr;
  90. static void Warn(const std::string &warn, bool show_exe_name = true) {
  91. if (show_exe_name) printf("%s: ", g_program_name);
  92. printf("warning: %s\n", warn.c_str());
  93. }
  94. static void Error(const std::string &err, bool usage, bool show_exe_name) {
  95. if (show_exe_name) printf("%s: ", g_program_name);
  96. printf("error: %s\n", err.c_str());
  97. if (usage) {
  98. printf("usage: %s [OPTION]... FILE... [-- FILE...]\n", g_program_name);
  99. for (size_t i = 0; i < sizeof(generators) / sizeof(generators[0]); ++i)
  100. printf(" %-12s %s %s.\n",
  101. generators[i].generator_opt_long,
  102. generators[i].generator_opt_short
  103. ? generators[i].generator_opt_short
  104. : " ",
  105. generators[i].generator_help);
  106. printf(
  107. " -o PATH Prefix PATH to all generated files.\n"
  108. " -I PATH Search for includes in the specified path.\n"
  109. " -M Print make rules for generated files.\n"
  110. " --version Print the version number of flatc and exit.\n"
  111. " --strict-json Strict JSON: field names must be / will be quoted,\n"
  112. " no trailing commas in tables/vectors.\n"
  113. " --allow-non-utf8 Pass non-UTF-8 input through parser and emit nonstandard\n"
  114. " \\x escapes in JSON. (Default is to raise parse error on\n"
  115. " non-UTF-8 input.)\n"
  116. " --defaults-json Output fields whose value is the default when\n"
  117. " writing JSON\n"
  118. " --unknown-json Allow fields in JSON that are not defined in the\n"
  119. " schema. These fields will be discared when generating\n"
  120. " binaries.\n"
  121. " --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
  122. " --scoped-enums Use C++11 style scoped and strongly typed enums.\n"
  123. " also implies --no-prefix.\n"
  124. " --gen-includes (deprecated), this is the default behavior.\n"
  125. " If the original behavior is required (no include\n"
  126. " statements) use --no-includes.\n"
  127. " --no-includes Don\'t generate include statements for included\n"
  128. " schemas the generated file depends on (C++).\n"
  129. " --gen-mutable Generate accessors that can mutate buffers in-place.\n"
  130. " --gen-onefile Generate single output file for C#.\n"
  131. " --gen-name-strings Generate type name functions for C++.\n"
  132. " --escape-proto-ids Disable appending '_' in namespaces names.\n"
  133. " --gen-object-api Generate an additional object-based API.\n"
  134. " --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
  135. " --raw-binary Allow binaries without file_indentifier to be read.\n"
  136. " This may crash flatc given a mismatched schema.\n"
  137. " --proto Input is a .proto, translate to .fbs.\n"
  138. " --grpc Generate GRPC interfaces for the specified languages\n"
  139. " --schema Serialize schemas instead of JSON (use with -b)\n"
  140. " --conform FILE Specify a schema the following schemas should be\n"
  141. " an evolution of. Gives errors if not.\n"
  142. " --conform-includes Include path for the schema given with --conform\n"
  143. " PATH \n"
  144. "FILEs may be schemas, or JSON files (conforming to preceding schema)\n"
  145. "FILEs after the -- must be binary flatbuffer format files.\n"
  146. "Output files are named using the base file name of the input,\n"
  147. "and written to the current directory or the path given by -o.\n"
  148. "example: %s -c -b schema1.fbs schema2.fbs data.json\n",
  149. g_program_name);
  150. }
  151. if (g_parser) delete g_parser;
  152. exit(1);
  153. }
  154. static void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
  155. const std::string &contents,
  156. std::vector<const char *> &include_directories) {
  157. auto local_include_directory = flatbuffers::StripFileName(filename);
  158. include_directories.push_back(local_include_directory.c_str());
  159. include_directories.push_back(nullptr);
  160. if (!parser.Parse(contents.c_str(), &include_directories[0],
  161. filename.c_str()))
  162. Error(parser.error_, false, false);
  163. include_directories.pop_back();
  164. include_directories.pop_back();
  165. }
  166. int main(int argc, const char *argv[]) {
  167. g_program_name = argv[0];
  168. flatbuffers::IDLOptions opts;
  169. std::string output_path;
  170. const size_t num_generators = sizeof(generators) / sizeof(generators[0]);
  171. bool generator_enabled[num_generators] = { false };
  172. bool any_generator = false;
  173. bool print_make_rules = false;
  174. bool raw_binary = false;
  175. bool schema_binary = false;
  176. bool grpc_enabled = false;
  177. std::vector<std::string> filenames;
  178. std::vector<const char *> include_directories;
  179. std::vector<const char *> conform_include_directories;
  180. size_t binary_files_from = std::numeric_limits<size_t>::max();
  181. std::string conform_to_schema;
  182. for (int argi = 1; 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(argv[argi], "");
  190. } else if(arg == "-I") {
  191. if (++argi >= argc) Error("missing path following" + arg, true);
  192. include_directories.push_back(argv[argi]);
  193. } else if(arg == "--conform") {
  194. if (++argi >= argc) Error("missing path following" + arg, true);
  195. conform_to_schema = argv[argi];
  196. } else if (arg == "--conform-includes") {
  197. if (++argi >= argc) Error("missing path following" + arg, true);
  198. conform_include_directories.push_back(argv[argi]);
  199. } else if(arg == "--strict-json") {
  200. opts.strict_json = true;
  201. } else if(arg == "--allow-non-utf8") {
  202. opts.allow_non_utf8 = true;
  203. } else if(arg == "--no-js-exports") {
  204. opts.skip_js_exports = true;
  205. } else if(arg == "--defaults-json") {
  206. opts.output_default_scalars_in_json = true;
  207. } else if (arg == "--unknown-json") {
  208. opts.skip_unexpected_fields_in_json = true;
  209. } else if(arg == "--no-prefix") {
  210. opts.prefixed_enums = false;
  211. } else if(arg == "--scoped-enums") {
  212. opts.prefixed_enums = false;
  213. opts.scoped_enums = true;
  214. } else if (arg == "--no-union-value-namespacing") {
  215. opts.union_value_namespacing = false;
  216. } else if(arg == "--gen-mutable") {
  217. opts.mutable_buffer = true;
  218. } else if(arg == "--gen-name-strings") {
  219. opts.generate_name_strings = true;
  220. } else if(arg == "--gen-object-api") {
  221. opts.generate_object_based_api = true;
  222. } else if (arg == "--cpp-ptr-type") {
  223. if (++argi >= argc) Error("missing type following" + arg, true);
  224. opts.cpp_object_api_pointer_type = argv[argi];
  225. } else if(arg == "--gen-all") {
  226. opts.generate_all = true;
  227. opts.include_dependence_headers = false;
  228. } else if(arg == "--gen-includes") {
  229. // Deprecated, remove this option some time in the future.
  230. printf("warning: --gen-includes is deprecated (it is now default)\n");
  231. } else if(arg == "--no-includes") {
  232. opts.include_dependence_headers = false;
  233. } else if (arg == "--gen-onefile") {
  234. opts.one_file = true;
  235. } else if (arg == "--raw-binary") {
  236. raw_binary = true;
  237. } else if(arg == "--") { // Separator between text and binary inputs.
  238. binary_files_from = filenames.size();
  239. } else if(arg == "--proto") {
  240. opts.proto_mode = true;
  241. } else if(arg == "--escape-proto-ids") {
  242. opts.escape_proto_identifiers = true;
  243. } else if(arg == "--schema") {
  244. schema_binary = true;
  245. } else if(arg == "-M") {
  246. print_make_rules = true;
  247. } else if(arg == "--version") {
  248. printf("flatc version %s\n", FLATC_VERSION);
  249. exit(0);
  250. } else if(arg == "--grpc") {
  251. grpc_enabled = true;
  252. } else {
  253. for (size_t i = 0; i < num_generators; ++i) {
  254. if (arg == generators[i].generator_opt_long ||
  255. (generators[i].generator_opt_short &&
  256. arg == generators[i].generator_opt_short)) {
  257. generator_enabled[i] = true;
  258. any_generator = true;
  259. goto found;
  260. }
  261. }
  262. Error("unknown commandline argument: " + arg, true);
  263. found:;
  264. }
  265. } else {
  266. filenames.push_back(argv[argi]);
  267. }
  268. }
  269. if (!filenames.size()) Error("missing input files", false, true);
  270. if (opts.proto_mode) {
  271. if (any_generator)
  272. Error("cannot generate code directly from .proto files", true);
  273. } else if (!any_generator && conform_to_schema.empty()) {
  274. Error("no options: specify at least one generator.", true);
  275. }
  276. flatbuffers::Parser conform_parser;
  277. if (!conform_to_schema.empty()) {
  278. std::string contents;
  279. if (!flatbuffers::LoadFile(conform_to_schema.c_str(), true, &contents))
  280. Error("unable to load schema: " + conform_to_schema);
  281. ParseFile(conform_parser, conform_to_schema, contents,
  282. conform_include_directories);
  283. }
  284. // Now process the files:
  285. g_parser = new flatbuffers::Parser(opts);
  286. for (auto file_it = filenames.begin();
  287. file_it != filenames.end();
  288. ++file_it) {
  289. std::string contents;
  290. if (!flatbuffers::LoadFile(file_it->c_str(), true, &contents))
  291. Error("unable to load file: " + *file_it);
  292. bool is_binary = static_cast<size_t>(file_it - filenames.begin()) >=
  293. binary_files_from;
  294. if (is_binary) {
  295. g_parser->builder_.Clear();
  296. g_parser->builder_.PushFlatBuffer(
  297. reinterpret_cast<const uint8_t *>(contents.c_str()),
  298. contents.length());
  299. if (!raw_binary) {
  300. // Generally reading binaries that do not correspond to the schema
  301. // will crash, and sadly there's no way around that when the binary
  302. // does not contain a file identifier.
  303. // We'd expect that typically any binary used as a file would have
  304. // such an identifier, so by default we require them to match.
  305. if (!g_parser->file_identifier_.length()) {
  306. Error("current schema has no file_identifier: cannot test if \"" +
  307. *file_it +
  308. "\" matches the schema, use --raw-binary to read this file"
  309. " anyway.");
  310. } else if (!flatbuffers::BufferHasIdentifier(contents.c_str(),
  311. g_parser->file_identifier_.c_str())) {
  312. Error("binary \"" +
  313. *file_it +
  314. "\" does not have expected file_identifier \"" +
  315. g_parser->file_identifier_ +
  316. "\", use --raw-binary to read this file anyway.");
  317. }
  318. }
  319. } else {
  320. // Check if file contains 0 bytes.
  321. if (contents.length() != strlen(contents.c_str())) {
  322. Error("input file appears to be binary: " + *file_it, true);
  323. }
  324. auto is_schema = flatbuffers::GetExtension(*file_it) == "fbs";
  325. if (is_schema) {
  326. // If we're processing multiple schemas, make sure to start each
  327. // one from scratch. If it depends on previous schemas it must do
  328. // so explicitly using an include.
  329. delete g_parser;
  330. g_parser = new flatbuffers::Parser(opts);
  331. }
  332. ParseFile(*g_parser, *file_it, contents, include_directories);
  333. if (is_schema && !conform_to_schema.empty()) {
  334. auto err = g_parser->ConformTo(conform_parser);
  335. if (!err.empty()) Error("schemas don\'t conform: " + err);
  336. }
  337. if (schema_binary) {
  338. g_parser->Serialize();
  339. g_parser->file_extension_ = reflection::SchemaExtension();
  340. }
  341. }
  342. std::string filebase = flatbuffers::StripPath(
  343. flatbuffers::StripExtension(*file_it));
  344. for (size_t i = 0; i < num_generators; ++i) {
  345. g_parser->opts.lang = generators[i].lang;
  346. if (generator_enabled[i]) {
  347. if (!print_make_rules) {
  348. flatbuffers::EnsureDirExists(output_path);
  349. if (!generators[i].generate(*g_parser, output_path, filebase)) {
  350. Error(std::string("Unable to generate ") +
  351. generators[i].lang_name +
  352. " for " +
  353. filebase);
  354. }
  355. } else {
  356. std::string make_rule = generators[i].make_rule(
  357. *g_parser, output_path, *file_it);
  358. if (!make_rule.empty())
  359. printf("%s\n", flatbuffers::WordWrap(
  360. make_rule, 80, " ", " \\").c_str());
  361. }
  362. if (grpc_enabled) {
  363. if (generators[i].generateGRPC != nullptr) {
  364. if (!generators[i].generateGRPC(*g_parser, output_path,
  365. filebase)) {
  366. Error(std::string("Unable to generate GRPC interface for") +
  367. generators[i].lang_name);
  368. }
  369. } else {
  370. Warn(std::string("GRPC interface generator not implemented for ")
  371. + generators[i].lang_name);
  372. }
  373. }
  374. }
  375. }
  376. if (opts.proto_mode) GenerateFBS(*g_parser, output_path, filebase);
  377. // We do not want to generate code for the definitions in this file
  378. // in any files coming up next.
  379. g_parser->MarkGenerated();
  380. }
  381. delete g_parser;
  382. return 0;
  383. }