xref: /aosp_15_r20/external/flatbuffers/src/flatc_main.cpp (revision 890232f25432b36107d06881e0a25aaa6b473652)
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 <cstdio>
18 #include <memory>
19 
20 #include "bfbs_gen_lua.h"
21 #include "flatbuffers/base.h"
22 #include "flatbuffers/flatc.h"
23 #include "flatbuffers/util.h"
24 
25 static const char *g_program_name = nullptr;
26 
Warn(const flatbuffers::FlatCompiler * flatc,const std::string & warn,bool show_exe_name)27 static void Warn(const flatbuffers::FlatCompiler *flatc,
28                  const std::string &warn, bool show_exe_name) {
29   (void)flatc;
30   if (show_exe_name) { printf("%s: ", g_program_name); }
31   fprintf(stderr, "\nwarning:\n  %s\n\n", warn.c_str());
32 }
33 
Error(const flatbuffers::FlatCompiler * flatc,const std::string & err,bool usage,bool show_exe_name)34 static void Error(const flatbuffers::FlatCompiler *flatc,
35                   const std::string &err, bool usage, bool show_exe_name) {
36   if (show_exe_name) { printf("%s: ", g_program_name); }
37   if (usage && flatc) {
38     fprintf(stderr, "%s\n", flatc->GetShortUsageString(g_program_name).c_str());
39   }
40   fprintf(stderr, "\nerror:\n  %s\n\n", err.c_str());
41   exit(1);
42 }
43 
44 namespace flatbuffers {
LogCompilerWarn(const std::string & warn)45 void LogCompilerWarn(const std::string &warn) {
46   Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true);
47 }
LogCompilerError(const std::string & err)48 void LogCompilerError(const std::string &err) {
49   Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false,
50         true);
51 }
52 }  // namespace flatbuffers
53 
main(int argc,const char * argv[])54 int main(int argc, const char *argv[]) {
55   // Prevent Appveyor-CI hangs.
56   flatbuffers::SetupDefaultCRTReportMode();
57 
58   const std::string flatbuffers_version(flatbuffers::FLATBUFFERS_VERSION());
59 
60   std::unique_ptr<flatbuffers::BfbsGenerator> bfbs_gen_lua =
61       flatbuffers::NewLuaBfbsGenerator(flatbuffers_version);
62 
63   g_program_name = argv[0];
64 
65   const flatbuffers::FlatCompiler::Generator generators[] = {
66     { flatbuffers::GenerateBinary, "binary", false, nullptr,
67       flatbuffers::IDLOptions::kBinary,
68       flatbuffers::FlatCOption{
69           "b", "binary", "",
70           "Generate wire format binaries for any data definitions" },
71       flatbuffers::BinaryMakeRule, nullptr, nullptr },
72     { flatbuffers::GenerateTextFile, "text", false, nullptr,
73       flatbuffers::IDLOptions::kJson,
74       flatbuffers::FlatCOption{
75           "t", "json", "", "Generate text output for any data definitions" },
76 
77       flatbuffers::TextMakeRule, nullptr, nullptr },
78     { flatbuffers::GenerateCPP, "C++", true, flatbuffers::GenerateCppGRPC,
79       flatbuffers::IDLOptions::kCpp,
80       flatbuffers::FlatCOption{ "c", "cpp", "",
81                                 "Generate C++ headers for tables/structs" },
82       flatbuffers::CPPMakeRule, nullptr, nullptr },
83     { flatbuffers::GenerateGo, "Go", true, flatbuffers::GenerateGoGRPC,
84       flatbuffers::IDLOptions::kGo,
85       flatbuffers::FlatCOption{ "g", "go", "",
86                                 "Generate Go files for tables/structs" },
87       nullptr, nullptr, nullptr },
88     { flatbuffers::GenerateJava, "Java", true, flatbuffers::GenerateJavaGRPC,
89       flatbuffers::IDLOptions::kJava,
90       flatbuffers::FlatCOption{ "j", "java", "",
91                                 "Generate Java classes for tables/structs" },
92       flatbuffers::JavaMakeRule, nullptr, nullptr },
93     { flatbuffers::GenerateDart, "Dart", true, nullptr,
94       flatbuffers::IDLOptions::kDart,
95       flatbuffers::FlatCOption{ "d", "dart", "",
96                                 "Generate Dart classes for tables/structs" },
97       flatbuffers::DartMakeRule, nullptr, nullptr },
98     { flatbuffers::GenerateTS, "TypeScript", true, flatbuffers::GenerateTSGRPC,
99       flatbuffers::IDLOptions::kTs,
100       flatbuffers::FlatCOption{ "T", "ts", "",
101                                 "Generate TypeScript code for tables/structs" },
102       flatbuffers::TSMakeRule, nullptr, nullptr },
103     { flatbuffers::GenerateCSharp, "C#", true, nullptr,
104       flatbuffers::IDLOptions::kCSharp,
105       flatbuffers::FlatCOption{ "n", "csharp", "",
106                                 "Generate C# classes for tables/structs" },
107       flatbuffers::CSharpMakeRule, nullptr, nullptr },
108     { flatbuffers::GeneratePython, "Python", true,
109       flatbuffers::GeneratePythonGRPC, flatbuffers::IDLOptions::kPython,
110       flatbuffers::FlatCOption{ "p", "python", "",
111                                 "Generate Python files for tables/structs" },
112       nullptr, nullptr, nullptr },
113     { flatbuffers::GenerateLobster, "Lobster", true, nullptr,
114       flatbuffers::IDLOptions::kLobster,
115       flatbuffers::FlatCOption{ "", "lobster", "",
116                                 "Generate Lobster files for tables/structs" },
117       nullptr, nullptr, nullptr },
118     { flatbuffers::GenerateLua, "Lua", true, nullptr,
119       flatbuffers::IDLOptions::kLua,
120       flatbuffers::FlatCOption{ "l", "lua", "",
121                                 "Generate Lua files for tables/structs" },
122       nullptr, bfbs_gen_lua.get(), nullptr },
123     { flatbuffers::GenerateRust, "Rust", true, nullptr,
124       flatbuffers::IDLOptions::kRust,
125       flatbuffers::FlatCOption{ "r", "rust", "",
126                                 "Generate Rust files for tables/structs" },
127       flatbuffers::RustMakeRule, nullptr,
128       flatbuffers::GenerateRustModuleRootFile },
129     { flatbuffers::GeneratePhp, "PHP", true, nullptr,
130       flatbuffers::IDLOptions::kPhp,
131       flatbuffers::FlatCOption{ "", "php", "",
132                                 "Generate PHP files for tables/structs" },
133       nullptr, nullptr, nullptr },
134     { flatbuffers::GenerateKotlin, "Kotlin", true, nullptr,
135       flatbuffers::IDLOptions::kKotlin,
136       flatbuffers::FlatCOption{ "", "kotlin", "",
137                                 "Generate Kotlin classes for tables/structs" },
138       nullptr, nullptr, nullptr },
139     { flatbuffers::GenerateJsonSchema, "JsonSchema", true, nullptr,
140       flatbuffers::IDLOptions::kJsonSchema,
141       flatbuffers::FlatCOption{ "", "jsonschema", "", "Generate Json schema" },
142       nullptr, nullptr, nullptr },
143     { flatbuffers::GenerateSwift, "swift", true, flatbuffers::GenerateSwiftGRPC,
144       flatbuffers::IDLOptions::kSwift,
145       flatbuffers::FlatCOption{ "", "swift", "",
146                                 "Generate Swift files for tables/structs" },
147       nullptr, nullptr, nullptr },
148   };
149 
150   flatbuffers::FlatCompiler::InitParams params;
151   params.generators = generators;
152   params.num_generators = sizeof(generators) / sizeof(generators[0]);
153   params.warn_fn = Warn;
154   params.error_fn = Error;
155 
156   flatbuffers::FlatCompiler flatc(params);
157   return flatc.Compile(argc, argv);
158 }
159