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()); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 33 | if (usage && flatc) { |
| 34 | printf("%s", flatc->GetUsageString(g_program_name).c_str()); |
| 35 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 36 | exit(1); |
| 37 | } |
| 38 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 39 | namespace flatbuffers { |
| 40 | void LogCompilerWarn(const std::string &warn) { |
| 41 | Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true); |
| 42 | } |
| 43 | void LogCompilerError(const std::string &err) { |
| 44 | Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false, |
| 45 | true); |
| 46 | } |
| 47 | } // namespace flatbuffers |
| 48 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 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, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 69 | "Generate Go files for tables/structs", nullptr }, |
| 70 | { flatbuffers::GenerateJava, "-j", "--java", "Java", true, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 71 | flatbuffers::GenerateJavaGRPC, flatbuffers::IDLOptions::kJava, |
| 72 | "Generate Java classes for tables/structs", |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 73 | flatbuffers::JavaCSharpMakeRule }, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 74 | { flatbuffers::GenerateJSTS, "-s", "--js", "JavaScript", true, nullptr, |
| 75 | flatbuffers::IDLOptions::kJs, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 76 | "Generate JavaScript code for tables/structs", |
| 77 | flatbuffers::JSTSMakeRule }, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 78 | { flatbuffers::GenerateDart, "-d", "--dart", "Dart", true, nullptr, |
| 79 | flatbuffers::IDLOptions::kDart, |
| 80 | "Generate Dart classes for tables/structs", flatbuffers::DartMakeRule }, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 81 | { 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 86 | flatbuffers::IDLOptions::kCSharp, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 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 }, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 95 | { flatbuffers::GenerateLua, "-l", "--lua", "Lua", true, nullptr, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 96 | flatbuffers::IDLOptions::kLua, "Generate Lua files for tables/structs", |
| 97 | nullptr }, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 98 | { flatbuffers::GenerateRust, "-r", "--rust", "Rust", true, nullptr, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 99 | flatbuffers::IDLOptions::kRust, "Generate Rust files for tables/structs", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 100 | flatbuffers::RustMakeRule }, |
| 101 | { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr, |
| 102 | flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs", |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 103 | nullptr }, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 104 | { flatbuffers::GenerateKotlin, nullptr, "--kotlin", "Kotlin", true, nullptr, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 105 | flatbuffers::IDLOptions::kKotlin, |
| 106 | "Generate Kotlin classes for tables/structs", nullptr }, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 107 | { flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema", |
| 108 | true, nullptr, flatbuffers::IDLOptions::kJsonSchema, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 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 }, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 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 | } |