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",
50 nullptr,
51 flatbuffers::IDLOptions::kBinary,
52 "Generate wire format binaries for any data definitions",
53 flatbuffers::BinaryMakeRule },
54 { flatbuffers::GenerateTextFile, "-t", "--json", "text",
55 nullptr,
56 flatbuffers::IDLOptions::kJson,
57 "Generate text output for any data definitions",
58 flatbuffers::TextMakeRule },
59 { flatbuffers::GenerateCPP, "-c", "--cpp", "C++",
60 flatbuffers::GenerateCppGRPC,
61 flatbuffers::IDLOptions::kCpp,
62 "Generate C++ headers for tables/structs",
63 flatbuffers::CPPMakeRule },
64 { flatbuffers::GenerateGo, "-g", "--go", "Go",
65 flatbuffers::GenerateGoGRPC,
66 flatbuffers::IDLOptions::kGo,
67 "Generate Go files for tables/structs",
68 flatbuffers::GeneralMakeRule },
69 { flatbuffers::GenerateGeneral, "-j", "--java", "Java",
70 nullptr,
71 flatbuffers::IDLOptions::kJava,
72 "Generate Java classes for tables/structs",
73 flatbuffers::GeneralMakeRule },
74 { flatbuffers::GenerateJS, "-s", "--js", "JavaScript",
75 nullptr,
76 flatbuffers::IDLOptions::kJs,
77 "Generate JavaScript code for tables/structs",
78 flatbuffers::JSMakeRule },
79 { flatbuffers::GenerateGeneral, "-n", "--csharp", "C#",
80 nullptr,
81 flatbuffers::IDLOptions::kCSharp,
82 "Generate C# classes for tables/structs",
83 flatbuffers::GeneralMakeRule },
84 { flatbuffers::GeneratePython, "-p", "--python", "Python",
85 nullptr,
86 flatbuffers::IDLOptions::kPython,
87 "Generate Python files for tables/structs",
88 flatbuffers::GeneralMakeRule },
89 { flatbuffers::GeneratePhp, nullptr, "--php", "PHP",
90 nullptr,
91 flatbuffers::IDLOptions::kPhp,
92 "Generate PHP files for tables/structs",
93 flatbuffers::GeneralMakeRule },
94 };
95
96 flatbuffers::FlatCompiler::InitParams params;
97 params.generators = generators;
98 params.num_generators = sizeof(generators) / sizeof(generators[0]);
99 params.warn_fn = Warn;
100 params.error_fn = Error;
101
102 flatbuffers::FlatCompiler flatc(params);
103 return flatc.Compile(argc - 1, argv + 1);
104 }
105