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::GenerateJSTS, "-s", "--js", "JavaScript", true, nullptr,
75       flatbuffers::IDLOptions::kJs,
76       "Generate JavaScript code for tables/structs",
77       flatbuffers::JSTSMakeRule },
78     { flatbuffers::GenerateDart, "-d", "--dart", "Dart", true, nullptr,
79       flatbuffers::IDLOptions::kDart,
80       "Generate Dart classes for tables/structs", flatbuffers::DartMakeRule },
81     { flatbuffers::GenerateJSTS, "-T", "--ts", "TypeScript", true, nullptr,
82       flatbuffers::IDLOptions::kTs,
83       "Generate TypeScript code for tables/structs",
84       flatbuffers::JSTSMakeRule },
85     { flatbuffers::GenerateCSharp, "-n", "--csharp", "C#", true, nullptr,
86       flatbuffers::IDLOptions::kCSharp,
87       "Generate C# classes for tables/structs",
88       flatbuffers::JavaCSharpMakeRule },
89     { flatbuffers::GeneratePython, "-p", "--python", "Python", true,
90       flatbuffers::GeneratePythonGRPC, flatbuffers::IDLOptions::kPython,
91       "Generate Python files for tables/structs", nullptr },
92     { flatbuffers::GenerateLobster, nullptr, "--lobster", "Lobster", true,
93       nullptr, flatbuffers::IDLOptions::kLobster,
94       "Generate Lobster files for tables/structs", nullptr },
95     { flatbuffers::GenerateLua, "-l", "--lua", "Lua", true, nullptr,
96       flatbuffers::IDLOptions::kLua, "Generate Lua files for tables/structs",
97       nullptr },
98     { flatbuffers::GenerateRust, "-r", "--rust", "Rust", true, nullptr,
99       flatbuffers::IDLOptions::kRust, "Generate Rust files for tables/structs",
100       flatbuffers::RustMakeRule },
101     { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr,
102       flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs",
103       nullptr },
104     { flatbuffers::GenerateKotlin, nullptr, "--kotlin", "Kotlin", true, nullptr,
105       flatbuffers::IDLOptions::kKotlin,
106       "Generate Kotlin classes for tables/structs", nullptr },
107     { flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema",
108       true, nullptr, flatbuffers::IDLOptions::kJsonSchema,
109       "Generate Json schema", nullptr },
110     { flatbuffers::GenerateSwift, nullptr, "--swift", "swift", true,
111       flatbuffers::GenerateSwiftGRPC, flatbuffers::IDLOptions::kSwift,
112       "Generate Swift files for tables/structs", nullptr },
113   };
114 
115   flatbuffers::FlatCompiler::InitParams params;
116   params.generators = generators;
117   params.num_generators = sizeof(generators) / sizeof(generators[0]);
118   params.warn_fn = Warn;
119   params.error_fn = Error;
120 
121   flatbuffers::FlatCompiler flatc(params);
122   return flatc.Compile(argc - 1, argv + 1);
123 }
124