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