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 #include "flatbuffers/util.h"
19
20 static const char *g_program_name = nullptr;
21
Warn(const flatbuffers::FlatCompiler * flatc,const std::string & warn,bool show_exe_name)22 static void Warn(const flatbuffers::FlatCompiler *flatc,
23 const std::string &warn, bool show_exe_name) {
24 (void)flatc;
25 if (show_exe_name) { printf("%s: ", g_program_name); }
26 printf("warning: %s\n", warn.c_str());
27 }
28
Error(const flatbuffers::FlatCompiler * flatc,const std::string & err,bool usage,bool show_exe_name)29 static void Error(const flatbuffers::FlatCompiler *flatc,
30 const std::string &err, bool usage, bool show_exe_name) {
31 if (show_exe_name) { printf("%s: ", g_program_name); }
32 printf("error: %s\n", err.c_str());
33 if (usage && flatc) {
34 printf("%s", flatc->GetUsageString(g_program_name).c_str());
35 }
36 exit(1);
37 }
38
39 namespace flatbuffers {
LogCompilerWarn(const std::string & warn)40 void LogCompilerWarn(const std::string &warn) {
41 Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true);
42 }
LogCompilerError(const std::string & err)43 void LogCompilerError(const std::string &err) {
44 Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false,
45 true);
46 }
47 } // namespace flatbuffers
48
main(int argc,const char * argv[])49 int main(int argc, const char *argv[]) {
50 // Prevent Appveyor-CI hangs.
51 flatbuffers::SetupDefaultCRTReportMode();
52
53 g_program_name = argv[0];
54
55 const flatbuffers::FlatCompiler::Generator generators[] = {
56 { flatbuffers::GenerateBinary, "-b", "--binary", "binary", false, nullptr,
57 flatbuffers::IDLOptions::kBinary,
58 "Generate wire format binaries for any data definitions",
59 flatbuffers::BinaryMakeRule },
60 { flatbuffers::GenerateTextFile, "-t", "--json", "text", false, nullptr,
61 flatbuffers::IDLOptions::kJson,
62 "Generate text output for any data definitions",
63 flatbuffers::TextMakeRule },
64 { flatbuffers::GenerateCPP, "-c", "--cpp", "C++", true,
65 flatbuffers::GenerateCppGRPC, flatbuffers::IDLOptions::kCpp,
66 "Generate C++ headers for tables/structs", flatbuffers::CPPMakeRule },
67 { flatbuffers::GenerateGo, "-g", "--go", "Go", true,
68 flatbuffers::GenerateGoGRPC, flatbuffers::IDLOptions::kGo,
69 "Generate Go files for tables/structs", nullptr },
70 { flatbuffers::GenerateJava, "-j", "--java", "Java", true,
71 flatbuffers::GenerateJavaGRPC, flatbuffers::IDLOptions::kJava,
72 "Generate Java classes for tables/structs",
73 flatbuffers::JavaCSharpMakeRule },
74 { flatbuffers::GenerateDart, "-d", "--dart", "Dart", true, nullptr,
75 flatbuffers::IDLOptions::kDart,
76 "Generate Dart classes for tables/structs", flatbuffers::DartMakeRule },
77 { flatbuffers::GenerateTS, "-T", "--ts", "TypeScript", true,
78 flatbuffers::GenerateTSGRPC, flatbuffers::IDLOptions::kTs,
79 "Generate TypeScript code for tables/structs", flatbuffers::TSMakeRule },
80 { flatbuffers::GenerateCSharp, "-n", "--csharp", "C#", true, nullptr,
81 flatbuffers::IDLOptions::kCSharp,
82 "Generate C# classes for tables/structs",
83 flatbuffers::JavaCSharpMakeRule },
84 { flatbuffers::GeneratePython, "-p", "--python", "Python", true,
85 flatbuffers::GeneratePythonGRPC, flatbuffers::IDLOptions::kPython,
86 "Generate Python files for tables/structs", nullptr },
87 { flatbuffers::GenerateLobster, nullptr, "--lobster", "Lobster", true,
88 nullptr, flatbuffers::IDLOptions::kLobster,
89 "Generate Lobster files for tables/structs", nullptr },
90 { flatbuffers::GenerateLua, "-l", "--lua", "Lua", true, nullptr,
91 flatbuffers::IDLOptions::kLua, "Generate Lua files for tables/structs",
92 nullptr },
93 { flatbuffers::GenerateRust, "-r", "--rust", "Rust", true, nullptr,
94 flatbuffers::IDLOptions::kRust, "Generate Rust files for tables/structs",
95 flatbuffers::RustMakeRule },
96 { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr,
97 flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs",
98 nullptr },
99 { flatbuffers::GenerateKotlin, nullptr, "--kotlin", "Kotlin", true, nullptr,
100 flatbuffers::IDLOptions::kKotlin,
101 "Generate Kotlin classes for tables/structs", nullptr },
102 { flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema",
103 true, nullptr, flatbuffers::IDLOptions::kJsonSchema,
104 "Generate Json schema", nullptr },
105 { flatbuffers::GenerateSwift, nullptr, "--swift", "swift", true,
106 flatbuffers::GenerateSwiftGRPC, flatbuffers::IDLOptions::kSwift,
107 "Generate Swift files for tables/structs", nullptr },
108 };
109
110 flatbuffers::FlatCompiler::InitParams params;
111 params.generators = generators;
112 params.num_generators = sizeof(generators) / sizeof(generators[0]);
113 params.warn_fn = Warn;
114 params.error_fn = Error;
115
116 flatbuffers::FlatCompiler flatc(params);
117 return flatc.Compile(argc - 1, argv + 1);
118 }
119