blob: c942bdacb2ca730fe58c0f561fdffc05662b038a [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
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
20static const char *g_program_name = nullptr;
21
22static 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
29static 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());
Austin Schuh272c6132020-11-14 16:37:52 -080033 if (usage && flatc) {
34 printf("%s", flatc->GetUsageString(g_program_name).c_str());
35 }
Austin Schuhe89fa2d2019-08-14 20:24:23 -070036 exit(1);
37}
38
Austin Schuh272c6132020-11-14 16:37:52 -080039namespace flatbuffers {
40void LogCompilerWarn(const std::string &warn) {
41 Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true);
42}
43void LogCompilerError(const std::string &err) {
44 Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false,
45 true);
46}
47} // namespace flatbuffers
48
Austin Schuhe89fa2d2019-08-14 20:24:23 -070049int 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,
Austin Schuh272c6132020-11-14 16:37:52 -080069 "Generate Go files for tables/structs", nullptr },
70 { flatbuffers::GenerateJava, "-j", "--java", "Java", true,
Austin Schuhe89fa2d2019-08-14 20:24:23 -070071 flatbuffers::GenerateJavaGRPC, flatbuffers::IDLOptions::kJava,
72 "Generate Java classes for tables/structs",
Austin Schuh272c6132020-11-14 16:37:52 -080073 flatbuffers::JavaCSharpMakeRule },
Austin Schuhe89fa2d2019-08-14 20:24:23 -070074 { flatbuffers::GenerateJSTS, "-s", "--js", "JavaScript", true, nullptr,
75 flatbuffers::IDLOptions::kJs,
Austin Schuh272c6132020-11-14 16:37:52 -080076 "Generate JavaScript code for tables/structs",
77 flatbuffers::JSTSMakeRule },
Austin Schuhe89fa2d2019-08-14 20:24:23 -070078 { flatbuffers::GenerateDart, "-d", "--dart", "Dart", true, nullptr,
79 flatbuffers::IDLOptions::kDart,
80 "Generate Dart classes for tables/structs", flatbuffers::DartMakeRule },
Austin Schuh272c6132020-11-14 16:37:52 -080081 { flatbuffers::GenerateJSTS, "-T", "--ts", "TypeScript", true,
82 flatbuffers::GenerateTSGRPC, flatbuffers::IDLOptions::kTs,
83 "Generate TypeScript code for tables/structs",
84 flatbuffers::JSTSMakeRule },
85 { flatbuffers::GenerateCSharp, "-n", "--csharp", "C#", true, nullptr,
Austin Schuhe89fa2d2019-08-14 20:24:23 -070086 flatbuffers::IDLOptions::kCSharp,
Austin Schuh272c6132020-11-14 16:37:52 -080087 "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 },
Austin Schuhe89fa2d2019-08-14 20:24:23 -070095 { flatbuffers::GenerateLua, "-l", "--lua", "Lua", true, nullptr,
Austin Schuh272c6132020-11-14 16:37:52 -080096 flatbuffers::IDLOptions::kLua, "Generate Lua files for tables/structs",
97 nullptr },
Austin Schuhe89fa2d2019-08-14 20:24:23 -070098 { flatbuffers::GenerateRust, "-r", "--rust", "Rust", true, nullptr,
Austin Schuh272c6132020-11-14 16:37:52 -080099 flatbuffers::IDLOptions::kRust, "Generate Rust files for tables/structs",
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700100 flatbuffers::RustMakeRule },
101 { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr,
102 flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs",
Austin Schuh272c6132020-11-14 16:37:52 -0800103 nullptr },
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700104 { flatbuffers::GenerateKotlin, nullptr, "--kotlin", "Kotlin", true, nullptr,
Austin Schuh272c6132020-11-14 16:37:52 -0800105 flatbuffers::IDLOptions::kKotlin,
106 "Generate Kotlin classes for tables/structs", nullptr },
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700107 { flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema",
108 true, nullptr, flatbuffers::IDLOptions::kJsonSchema,
Austin Schuh272c6132020-11-14 16:37:52 -0800109 "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 },
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700113 };
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}