1 /*
2 * Copyright 2017 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 static const char *g_program_name = nullptr;
20
Warn(const flatbuffers::FlatCompiler * flatc,const std::string & warn,bool show_exe_name)21 static void Warn(const flatbuffers::FlatCompiler *flatc,
22 const std::string &warn,
23 bool show_exe_name) {
24 (void)flatc;
25 if (show_exe_name) {
26 printf("%s: ", g_program_name);
27 }
28 printf("warning: %s\n", warn.c_str());
29 }
30
Error(const flatbuffers::FlatCompiler * flatc,const std::string & err,bool usage,bool show_exe_name)31 static void Error(const flatbuffers::FlatCompiler *flatc,
32 const std::string &err,
33 bool usage,
34 bool show_exe_name) {
35 if (show_exe_name) {
36 printf("%s: ", g_program_name);
37 }
38 printf("error: %s\n", err.c_str());
39 if (usage) {
40 printf("%s", flatc->GetUsageString(g_program_name).c_str());
41 }
42 exit(1);
43 }
44
main(int argc,const char * argv[])45 int main(int argc, const char *argv[]) {
46 g_program_name = argv[0];
47
48 const flatbuffers::FlatCompiler::Generator generators[] = {
49 { flatbuffers::GenerateBinary, "-b", "--binary", "binary", false,
50 nullptr,
51 flatbuffers::IDLOptions::kBinary,
52 "Generate wire format binaries for any data definitions",
53 flatbuffers::BinaryMakeRule },
54 { flatbuffers::GenerateTextFile, "-t", "--json", "text", false,
55 nullptr,
56 flatbuffers::IDLOptions::kJson,
57 "Generate text output for any data definitions",
58 flatbuffers::TextMakeRule },
59 { flatbuffers::GenerateCPP, "-c", "--cpp", "C++", true,
60 flatbuffers::GenerateCppGRPC,
61 flatbuffers::IDLOptions::kCpp,
62 "Generate C++ headers for tables/structs",
63 flatbuffers::CPPMakeRule },
64 { flatbuffers::GenerateGo, "-g", "--go", "Go", true,
65 flatbuffers::GenerateGoGRPC,
66 flatbuffers::IDLOptions::kGo,
67 "Generate Go files for tables/structs",
68 flatbuffers::GeneralMakeRule },
69 { flatbuffers::GenerateGeneral, "-j", "--java", "Java", true,
70 nullptr,
71 flatbuffers::IDLOptions::kJava,
72 "Generate Java classes for tables/structs",
73 flatbuffers::GeneralMakeRule },
74 { flatbuffers::GenerateJS, "-s", "--js", "JavaScript", true,
75 nullptr,
76 flatbuffers::IDLOptions::kJs,
77 "Generate JavaScript code for tables/structs",
78 flatbuffers::JSMakeRule },
79 { flatbuffers::GenerateJS, "-T", "--ts", "TypeScript", true,
80 nullptr,
81 flatbuffers::IDLOptions::kTs,
82 "Generate TypeScript code for tables/structs",
83 flatbuffers::JSMakeRule },
84 { flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", true,
85 nullptr,
86 flatbuffers::IDLOptions::kCSharp,
87 "Generate C# classes for tables/structs",
88 flatbuffers::GeneralMakeRule },
89 { flatbuffers::GeneratePython, "-p", "--python", "Python", true,
90 nullptr,
91 flatbuffers::IDLOptions::kPython,
92 "Generate Python files for tables/structs",
93 flatbuffers::GeneralMakeRule },
94 { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true,
95 nullptr,
96 flatbuffers::IDLOptions::kPhp,
97 "Generate PHP files for tables/structs",
98 flatbuffers::GeneralMakeRule },
99 { flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema", true,
100 nullptr,
101 flatbuffers::IDLOptions::kJsonSchema,
102 "Generate Json schema",
103 flatbuffers::GeneralMakeRule },
104 };
105
106 flatbuffers::FlatCompiler::InitParams params;
107 params.generators = generators;
108 params.num_generators = sizeof(generators) / sizeof(generators[0]);
109 params.warn_fn = Warn;
110 params.error_fn = Error;
111
112 flatbuffers::FlatCompiler flatc(params);
113 return flatc.Compile(argc - 1, argv + 1);
114 }
115