• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bfbs_gen_nim.h"
22 #include "flatbuffers/base.h"
23 #include "flatbuffers/code_generator.h"
24 #include "flatbuffers/flatc.h"
25 #include "flatbuffers/util.h"
26 #include "idl_gen_binary.h"
27 #include "idl_gen_cpp.h"
28 #include "idl_gen_csharp.h"
29 #include "idl_gen_dart.h"
30 #include "idl_gen_fbs.h"
31 #include "idl_gen_go.h"
32 #include "idl_gen_java.h"
33 #include "idl_gen_json_schema.h"
34 #include "idl_gen_kotlin.h"
35 #include "idl_gen_lobster.h"
36 #include "idl_gen_php.h"
37 #include "idl_gen_python.h"
38 #include "idl_gen_rust.h"
39 #include "idl_gen_swift.h"
40 #include "idl_gen_text.h"
41 #include "idl_gen_ts.h"
42 
43 static const char *g_program_name = nullptr;
44 
Warn(const flatbuffers::FlatCompiler * flatc,const std::string & warn,bool show_exe_name)45 static void Warn(const flatbuffers::FlatCompiler *flatc,
46                  const std::string &warn, bool show_exe_name) {
47   (void)flatc;
48   if (show_exe_name) { printf("%s: ", g_program_name); }
49   fprintf(stderr, "\nwarning:\n  %s\n\n", warn.c_str());
50 }
51 
Error(const flatbuffers::FlatCompiler * flatc,const std::string & err,bool usage,bool show_exe_name)52 static void Error(const flatbuffers::FlatCompiler *flatc,
53                   const std::string &err, bool usage, bool show_exe_name) {
54   if (show_exe_name) { printf("%s: ", g_program_name); }
55   if (usage && flatc) {
56     fprintf(stderr, "%s\n", flatc->GetShortUsageString(g_program_name).c_str());
57   }
58   fprintf(stderr, "\nerror:\n  %s\n\n", err.c_str());
59   exit(1);
60 }
61 
62 namespace flatbuffers {
LogCompilerWarn(const std::string & warn)63 void LogCompilerWarn(const std::string &warn) {
64   Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true);
65 }
LogCompilerError(const std::string & err)66 void LogCompilerError(const std::string &err) {
67   Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false,
68         true);
69 }
70 }  // namespace flatbuffers
71 
main(int argc,const char * argv[])72 int main(int argc, const char *argv[]) {
73   const std::string flatbuffers_version(flatbuffers::FLATBUFFERS_VERSION());
74 
75   g_program_name = argv[0];
76 
77   flatbuffers::FlatCompiler::InitParams params;
78   params.warn_fn = Warn;
79   params.error_fn = Error;
80 
81   flatbuffers::FlatCompiler flatc(params);
82 
83   flatc.RegisterCodeGenerator(
84       flatbuffers::FlatCOption{
85           "b", "binary", "",
86           "Generate wire format binaries for any data definitions" },
87       flatbuffers::NewBinaryCodeGenerator());
88 
89   flatc.RegisterCodeGenerator(
90       flatbuffers::FlatCOption{ "c", "cpp", "",
91                                 "Generate C++ headers for tables/structs" },
92       flatbuffers::NewCppCodeGenerator());
93 
94   flatc.RegisterCodeGenerator(
95       flatbuffers::FlatCOption{ "n", "csharp", "",
96                                 "Generate C# classes for tables/structs" },
97       flatbuffers::NewCSharpCodeGenerator());
98 
99   flatc.RegisterCodeGenerator(
100       flatbuffers::FlatCOption{ "d", "dart", "",
101                                 "Generate Dart classes for tables/structs" },
102       flatbuffers::NewDartCodeGenerator());
103 
104   flatc.RegisterCodeGenerator(
105       flatbuffers::FlatCOption{ "", "proto", "",
106                                 "Input is a .proto, translate to .fbs" },
107       flatbuffers::NewFBSCodeGenerator());
108 
109   flatc.RegisterCodeGenerator(
110       flatbuffers::FlatCOption{ "g", "go", "",
111                                 "Generate Go files for tables/structs" },
112       flatbuffers::NewGoCodeGenerator());
113 
114   flatc.RegisterCodeGenerator(
115       flatbuffers::FlatCOption{ "j", "java", "",
116                                 "Generate Java classes for tables/structs" },
117       flatbuffers::NewJavaCodeGenerator());
118 
119   flatc.RegisterCodeGenerator(
120       flatbuffers::FlatCOption{ "", "jsonschema", "", "Generate Json schema" },
121       flatbuffers::NewJsonSchemaCodeGenerator());
122 
123   flatc.RegisterCodeGenerator(
124       flatbuffers::FlatCOption{ "", "kotlin", "",
125                                 "Generate Kotlin classes for tables/structs" },
126       flatbuffers::NewKotlinCodeGenerator());
127 
128   flatc.RegisterCodeGenerator(
129       flatbuffers::FlatCOption{ "", "kotlin-kmp", "",
130                                 "Generate Kotlin multiplatform classes for tables/structs" },
131       flatbuffers::NewKotlinKMPCodeGenerator());
132 
133   flatc.RegisterCodeGenerator(
134       flatbuffers::FlatCOption{ "", "lobster", "",
135                                 "Generate Lobster files for tables/structs" },
136       flatbuffers::NewLobsterCodeGenerator());
137 
138   flatc.RegisterCodeGenerator(
139       flatbuffers::FlatCOption{ "l", "lua", "",
140                                 "Generate Lua files for tables/structs" },
141       flatbuffers::NewLuaBfbsGenerator(flatbuffers_version));
142 
143   flatc.RegisterCodeGenerator(
144       flatbuffers::FlatCOption{ "", "nim", "",
145                                 "Generate Nim files for tables/structs" },
146       flatbuffers::NewNimBfbsGenerator(flatbuffers_version));
147 
148   flatc.RegisterCodeGenerator(
149       flatbuffers::FlatCOption{ "p", "python", "",
150                                 "Generate Python files for tables/structs" },
151       flatbuffers::NewPythonCodeGenerator());
152 
153   flatc.RegisterCodeGenerator(
154       flatbuffers::FlatCOption{ "", "php", "",
155                                 "Generate PHP files for tables/structs" },
156       flatbuffers::NewPhpCodeGenerator());
157 
158   flatc.RegisterCodeGenerator(
159       flatbuffers::FlatCOption{ "r", "rust", "",
160                                 "Generate Rust files for tables/structs" },
161       flatbuffers::NewRustCodeGenerator());
162 
163   flatc.RegisterCodeGenerator(
164       flatbuffers::FlatCOption{
165           "t", "json", "", "Generate text output for any data definitions" },
166       flatbuffers::NewTextCodeGenerator());
167 
168   flatc.RegisterCodeGenerator(
169       flatbuffers::FlatCOption{ "", "swift", "",
170                                 "Generate Swift files for tables/structs" },
171       flatbuffers::NewSwiftCodeGenerator());
172 
173   flatc.RegisterCodeGenerator(
174       flatbuffers::FlatCOption{ "T", "ts", "",
175                                 "Generate TypeScript code for tables/structs" },
176       flatbuffers::NewTsCodeGenerator());
177 
178   // Create the FlatC options by parsing the command line arguments.
179   const flatbuffers::FlatCOptions &options =
180       flatc.ParseFromCommandLineArguments(argc, argv);
181 
182   // Compile with the extracted FlatC options.
183   return flatc.Compile(options);
184 }
185