blob: b4c4251c4cb5163af493f71aabd88c592672a248 [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
James Kuszmaul8e62b022022-03-22 09:33:25 -070017#include <cstdio>
18#include <memory>
19
20#include "bfbs_gen_lua.h"
21#include "flatbuffers/base.h"
Austin Schuhe89fa2d2019-08-14 20:24:23 -070022#include "flatbuffers/flatc.h"
23#include "flatbuffers/util.h"
24
25static const char *g_program_name = nullptr;
26
27static 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); }
James Kuszmaul8e62b022022-03-22 09:33:25 -070031 fprintf(stderr, "\nwarning:\n %s\n\n", warn.c_str());
Austin Schuhe89fa2d2019-08-14 20:24:23 -070032}
33
34static 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); }
Austin Schuh272c6132020-11-14 16:37:52 -080037 if (usage && flatc) {
James Kuszmaul8e62b022022-03-22 09:33:25 -070038 fprintf(stderr, "%s\n", flatc->GetShortUsageString(g_program_name).c_str());
Austin Schuh272c6132020-11-14 16:37:52 -080039 }
James Kuszmaul8e62b022022-03-22 09:33:25 -070040 fprintf(stderr, "\nerror:\n %s\n\n", err.c_str());
Austin Schuhe89fa2d2019-08-14 20:24:23 -070041 exit(1);
42}
43
Austin Schuh272c6132020-11-14 16:37:52 -080044namespace flatbuffers {
45void LogCompilerWarn(const std::string &warn) {
46 Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true);
47}
48void LogCompilerError(const std::string &err) {
49 Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false,
50 true);
51}
52} // namespace flatbuffers
53
Austin Schuhe89fa2d2019-08-14 20:24:23 -070054int main(int argc, const char *argv[]) {
James Kuszmaul8e62b022022-03-22 09:33:25 -070055 const std::string flatbuffers_version(flatbuffers::FLATBUFFERS_VERSION());
56
57 std::unique_ptr<flatbuffers::BfbsGenerator> bfbs_gen_lua =
58 flatbuffers::NewLuaBfbsGenerator(flatbuffers_version);
59
Austin Schuhe89fa2d2019-08-14 20:24:23 -070060 g_program_name = argv[0];
61
62 const flatbuffers::FlatCompiler::Generator generators[] = {
James Kuszmaul8e62b022022-03-22 09:33:25 -070063 { flatbuffers::GenerateBinary, "binary", false, nullptr,
Austin Schuhe89fa2d2019-08-14 20:24:23 -070064 flatbuffers::IDLOptions::kBinary,
James Kuszmaul8e62b022022-03-22 09:33:25 -070065 flatbuffers::FlatCOption{
66 "b", "binary", "",
67 "Generate wire format binaries for any data definitions" },
68 flatbuffers::BinaryMakeRule, nullptr, nullptr },
69 { flatbuffers::GenerateTextFile, "text", false, nullptr,
Austin Schuhe89fa2d2019-08-14 20:24:23 -070070 flatbuffers::IDLOptions::kJson,
James Kuszmaul8e62b022022-03-22 09:33:25 -070071 flatbuffers::FlatCOption{
72 "t", "json", "", "Generate text output for any data definitions" },
73
74 flatbuffers::TextMakeRule, nullptr, nullptr },
75 { flatbuffers::GenerateCPP, "C++", true, flatbuffers::GenerateCppGRPC,
76 flatbuffers::IDLOptions::kCpp,
77 flatbuffers::FlatCOption{ "c", "cpp", "",
78 "Generate C++ headers for tables/structs" },
79 flatbuffers::CPPMakeRule, nullptr, nullptr },
80 { flatbuffers::GenerateGo, "Go", true, flatbuffers::GenerateGoGRPC,
81 flatbuffers::IDLOptions::kGo,
82 flatbuffers::FlatCOption{ "g", "go", "",
83 "Generate Go files for tables/structs" },
84 nullptr, nullptr, nullptr },
85 { flatbuffers::GenerateJava, "Java", true, flatbuffers::GenerateJavaGRPC,
86 flatbuffers::IDLOptions::kJava,
87 flatbuffers::FlatCOption{ "j", "java", "",
88 "Generate Java classes for tables/structs" },
89 flatbuffers::JavaMakeRule, nullptr, nullptr },
90 { flatbuffers::GenerateDart, "Dart", true, nullptr,
Austin Schuhe89fa2d2019-08-14 20:24:23 -070091 flatbuffers::IDLOptions::kDart,
James Kuszmaul8e62b022022-03-22 09:33:25 -070092 flatbuffers::FlatCOption{ "d", "dart", "",
93 "Generate Dart classes for tables/structs" },
94 flatbuffers::DartMakeRule, nullptr, nullptr },
95 { flatbuffers::GenerateTS, "TypeScript", true, flatbuffers::GenerateTSGRPC,
96 flatbuffers::IDLOptions::kTs,
97 flatbuffers::FlatCOption{ "T", "ts", "",
98 "Generate TypeScript code for tables/structs" },
99 flatbuffers::TSMakeRule, nullptr, nullptr },
100 { flatbuffers::GenerateCSharp, "C#", true, nullptr,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700101 flatbuffers::IDLOptions::kCSharp,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700102 flatbuffers::FlatCOption{ "n", "csharp", "",
103 "Generate C# classes for tables/structs" },
104 flatbuffers::CSharpMakeRule, nullptr, nullptr },
105 { flatbuffers::GeneratePython, "Python", true,
Austin Schuh272c6132020-11-14 16:37:52 -0800106 flatbuffers::GeneratePythonGRPC, flatbuffers::IDLOptions::kPython,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700107 flatbuffers::FlatCOption{ "p", "python", "",
108 "Generate Python files for tables/structs" },
109 nullptr, nullptr, nullptr },
110 { flatbuffers::GenerateLobster, "Lobster", true, nullptr,
111 flatbuffers::IDLOptions::kLobster,
112 flatbuffers::FlatCOption{ "", "lobster", "",
113 "Generate Lobster files for tables/structs" },
114 nullptr, nullptr, nullptr },
115 { flatbuffers::GenerateLua, "Lua", true, nullptr,
116 flatbuffers::IDLOptions::kLua,
117 flatbuffers::FlatCOption{ "l", "lua", "",
118 "Generate Lua files for tables/structs" },
119 nullptr, bfbs_gen_lua.get(), nullptr },
120 { flatbuffers::GenerateRust, "Rust", true, nullptr,
121 flatbuffers::IDLOptions::kRust,
122 flatbuffers::FlatCOption{ "r", "rust", "",
123 "Generate Rust files for tables/structs" },
124 flatbuffers::RustMakeRule, nullptr,
125 flatbuffers::GenerateRustModuleRootFile },
126 { flatbuffers::GeneratePhp, "PHP", true, nullptr,
127 flatbuffers::IDLOptions::kPhp,
128 flatbuffers::FlatCOption{ "", "php", "",
129 "Generate PHP files for tables/structs" },
130 nullptr, nullptr, nullptr },
131 { flatbuffers::GenerateKotlin, "Kotlin", true, nullptr,
Austin Schuh272c6132020-11-14 16:37:52 -0800132 flatbuffers::IDLOptions::kKotlin,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700133 flatbuffers::FlatCOption{ "", "kotlin", "",
134 "Generate Kotlin classes for tables/structs" },
135 nullptr, nullptr, nullptr },
136 { flatbuffers::GenerateJsonSchema, "JsonSchema", true, nullptr,
137 flatbuffers::IDLOptions::kJsonSchema,
138 flatbuffers::FlatCOption{ "", "jsonschema", "", "Generate Json schema" },
139 nullptr, nullptr, nullptr },
140 { flatbuffers::GenerateSwift, "swift", true, flatbuffers::GenerateSwiftGRPC,
141 flatbuffers::IDLOptions::kSwift,
142 flatbuffers::FlatCOption{ "", "swift", "",
143 "Generate Swift files for tables/structs" },
144 nullptr, nullptr, nullptr },
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700145 };
146
147 flatbuffers::FlatCompiler::InitParams params;
148 params.generators = generators;
149 params.num_generators = sizeof(generators) / sizeof(generators[0]);
150 params.warn_fn = Warn;
151 params.error_fn = Error;
152
153 flatbuffers::FlatCompiler flatc(params);
James Kuszmaul8e62b022022-03-22 09:33:25 -0700154 return flatc.Compile(argc, argv);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700155}