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