Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/flatbuffers/flatc.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class FlatCompiler {

explicit FlatCompiler(const InitParams &params) : params_(params) {}

bool RegisterCodeGenerator(const std::string& flag,
bool RegisterCodeGenerator(const FlatCOption &option,
std::shared_ptr<CodeGenerator> code_generator);

int Compile(const FlatCOptions &options);
Expand Down
42 changes: 38 additions & 4 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ const static FlatCOption flatc_options[] = {
"Currently this is required to generate private types in Rust" },
};

auto cmp = [](FlatCOption a, FlatCOption b) { return a.long_opt < b.long_opt; };
static std::set<FlatCOption, decltype(cmp)> language_options(cmp);

static void AppendTextWrappedString(std::stringstream &ss, std::string &text,
size_t max_col, size_t start_col) {
size_t max_line_length = max_col - start_col;
Expand Down Expand Up @@ -312,12 +315,19 @@ std::string FlatCompiler::GetShortUsageString(
const std::string &program_name) const {
std::stringstream ss;
ss << "Usage: " << program_name << " [";

for (const FlatCOption &option : language_options) {
AppendShortOption(ss, option);
ss << ", ";
}

// TODO(derekbailey): These should be generated from this.generators
for (size_t i = 0; i < params_.num_generators; ++i) {
const Generator &g = params_.generators[i];
AppendShortOption(ss, g.option);
ss << ", ";
}

for (const FlatCOption &option : flatc_options) {
AppendShortOption(ss, option);
ss << ", ";
Expand All @@ -335,6 +345,11 @@ std::string FlatCompiler::GetUsageString(
std::stringstream ss;
ss << "Usage: " << program_name
<< " [OPTION]... FILE... [-- BINARY_FILE...]\n";

for (const FlatCOption &option : language_options) {
AppendOption(ss, option, 80, 25);
}

// TODO(derekbailey): These should be generated from this.generators
for (size_t i = 0; i < params_.num_generators; ++i) {
const Generator &g = params_.generators[i];
Expand Down Expand Up @@ -1018,12 +1033,31 @@ int FlatCompiler::Compile(const FlatCOptions &options) {
}

bool FlatCompiler::RegisterCodeGenerator(
const std::string &flag, std::shared_ptr<CodeGenerator> code_generator) {
if (code_generators_.find(flag) != code_generators_.end()) {
Error("multiple generators registered under: " + flag, false, false);
const FlatCOption &option, std::shared_ptr<CodeGenerator> code_generator) {
if (!option.short_opt.empty() &&
code_generators_.find("-" + option.short_opt) != code_generators_.end()) {
Error("multiple generators registered under: -" + option.short_opt, false,
false);
return false;
}
code_generators_[flag] = std::move(code_generator);

if (!option.short_opt.empty()) {
code_generators_["-" + option.short_opt] = code_generator;
}

if (!option.long_opt.empty() &&
code_generators_.find("--" + option.long_opt) != code_generators_.end()) {
Error("multiple generators registered under: --" + option.long_opt, false,
false);
return false;
}

if (!option.long_opt.empty()) {
code_generators_["--" + option.long_opt] = code_generator;
}

language_options.insert(option);

return true;
}

Expand Down
192 changes: 56 additions & 136 deletions src/flatc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,88 +78,17 @@ int main(int argc, const char *argv[]) {
g_program_name = argv[0];

const flatbuffers::FlatCompiler::Generator generators[] = {
{ flatbuffers::GenerateBinary, "binary", false, nullptr,
flatbuffers::IDLOptions::kBinary,
flatbuffers::FlatCOption{
"b", "binary", "",
"Generate wire format binaries for any data definitions" },
flatbuffers::BinaryMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateTextFile, "text", false, nullptr,
flatbuffers::IDLOptions::kJson,
flatbuffers::FlatCOption{
"t", "json", "", "Generate text output for any data definitions" },

flatbuffers::TextMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateCPP, "C++", true, flatbuffers::GenerateCppGRPC,
flatbuffers::IDLOptions::kCpp,
flatbuffers::FlatCOption{ "c", "cpp", "",
"Generate C++ headers for tables/structs" },
flatbuffers::CPPMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateGo, "Go", true, flatbuffers::GenerateGoGRPC,
flatbuffers::IDLOptions::kGo,
flatbuffers::FlatCOption{ "g", "go", "",
"Generate Go files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateJava, "Java", true, flatbuffers::GenerateJavaGRPC,
flatbuffers::IDLOptions::kJava,
flatbuffers::FlatCOption{ "j", "java", "",
"Generate Java classes for tables/structs" },
flatbuffers::JavaMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateDart, "Dart", true, nullptr,
flatbuffers::IDLOptions::kDart,
flatbuffers::FlatCOption{ "d", "dart", "",
"Generate Dart classes for tables/structs" },
flatbuffers::DartMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateTS, "TypeScript", true, flatbuffers::GenerateTSGRPC,
flatbuffers::IDLOptions::kTs,
flatbuffers::FlatCOption{ "T", "ts", "",
"Generate TypeScript code for tables/structs" },
flatbuffers::TSMakeRule, nullptr, nullptr },
{ flatbuffers::GenerateCSharp, "C#", true, nullptr,
flatbuffers::IDLOptions::kCSharp,
flatbuffers::FlatCOption{ "n", "csharp", "",
"Generate C# classes for tables/structs" },
flatbuffers::CSharpMakeRule, nullptr, nullptr },
{ flatbuffers::GeneratePython, "Python", true,
flatbuffers::GeneratePythonGRPC, flatbuffers::IDLOptions::kPython,
flatbuffers::FlatCOption{ "p", "python", "",
"Generate Python files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateLobster, "Lobster", true, nullptr,
flatbuffers::IDLOptions::kLobster,
flatbuffers::FlatCOption{ "", "lobster", "",
"Generate Lobster files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateLua, "Lua", true, nullptr,
flatbuffers::IDLOptions::kLua,
flatbuffers::FlatCOption{ "l", "lua", "",
"Generate Lua files for tables/structs" },
nullptr, bfbs_gen_lua.get(), nullptr },
{ flatbuffers::GenerateRust, "Rust", true, nullptr,
flatbuffers::IDLOptions::kRust,
flatbuffers::FlatCOption{ "r", "rust", "",
"Generate Rust files for tables/structs" },
flatbuffers::RustMakeRule, nullptr,
flatbuffers::GenerateRustModuleRootFile },
{ flatbuffers::GeneratePhp, "PHP", true, nullptr,
flatbuffers::IDLOptions::kPhp,
flatbuffers::FlatCOption{ "", "php", "",
"Generate PHP files for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateKotlin, "Kotlin", true, nullptr,
flatbuffers::IDLOptions::kKotlin,
flatbuffers::FlatCOption{ "", "kotlin", "",
"Generate Kotlin classes for tables/structs" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateJsonSchema, "JsonSchema", true, nullptr,
flatbuffers::IDLOptions::kJsonSchema,
flatbuffers::FlatCOption{ "", "jsonschema", "", "Generate Json schema" },
nullptr, nullptr, nullptr },
{ flatbuffers::GenerateSwift, "swift", true, flatbuffers::GenerateSwiftGRPC,
flatbuffers::IDLOptions::kSwift,
flatbuffers::FlatCOption{ "", "swift", "",
"Generate Swift files for tables/structs" },
nullptr, nullptr, nullptr },
{ nullptr, "Nim", true, nullptr, flatbuffers::IDLOptions::kNim,
flatbuffers::FlatCOption{ "", "nim", "",
"Generate Nim files for tables/structs" },
Expand All @@ -174,84 +103,75 @@ int main(int argc, const char *argv[]) {

flatbuffers::FlatCompiler flatc(params);

std::shared_ptr<flatbuffers::CodeGenerator> binary_generator =
flatbuffers::NewBinaryCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> cpp_generator =
flatbuffers::NewCppCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> csharp_generator =
flatbuffers::NewCSharpCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> dart_generator =
flatbuffers::NewDartCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> go_generator =
flatbuffers::NewGoCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> java_generator =
flatbuffers::NewJavaCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> json_schema_generator =
flatbuffers::NewJsonSchemaCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> kotlin_generator =
flatbuffers::NewKotlinCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> lobster_generator =
flatbuffers::NewLobsterCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> php_generator =
flatbuffers::NewPhpCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> python_generator =
flatbuffers::NewPythonCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> rust_generator =
flatbuffers::NewRustCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> swift_generator =
flatbuffers::NewSwiftCodeGenerator();

std::shared_ptr<flatbuffers::CodeGenerator> ts_generator =
flatbuffers::NewTsCodeGenerator();

flatc.RegisterCodeGenerator("--binary", binary_generator);
flatc.RegisterCodeGenerator("-b", binary_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{
"b", "binary", "",
"Generate wire format binaries for any data definitions" },
flatbuffers::NewBinaryCodeGenerator());

flatc.RegisterCodeGenerator("--cpp", cpp_generator);
flatc.RegisterCodeGenerator("-c", cpp_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "c", "cpp", "",
"Generate C++ headers for tables/structs" },
flatbuffers::NewCppCodeGenerator());

flatc.RegisterCodeGenerator("--csharp", csharp_generator);
flatc.RegisterCodeGenerator("-n", csharp_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "n", "csharp", "",
"Generate C# classes for tables/structs" },
flatbuffers::NewCSharpCodeGenerator());

flatc.RegisterCodeGenerator("--dart", dart_generator);
flatc.RegisterCodeGenerator("-d", dart_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "d", "dart", "",
"Generate Dart classes for tables/structs" },
flatbuffers::NewDartCodeGenerator());

flatc.RegisterCodeGenerator("--go", go_generator);
flatc.RegisterCodeGenerator("-g", go_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "g", "go", "",
"Generate Go files for tables/structs" },
flatbuffers::NewGoCodeGenerator());

flatc.RegisterCodeGenerator("--java", java_generator);
flatc.RegisterCodeGenerator("-j", java_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "j", "java", "",
"Generate Java classes for tables/structs" },
flatbuffers::NewJavaCodeGenerator());

flatc.RegisterCodeGenerator("--jsonschema", json_schema_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "jsonschema", "", "Generate Json schema" },
flatbuffers::NewJsonSchemaCodeGenerator());

flatc.RegisterCodeGenerator("--kotlin", kotlin_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "kotlin", "",
"Generate Kotlin classes for tables/structs" },
flatbuffers::NewKotlinCodeGenerator());

flatc.RegisterCodeGenerator("--lobster", lobster_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "lobster", "",
"Generate Lobster files for tables/structs" },
flatbuffers::NewLobsterCodeGenerator());

flatc.RegisterCodeGenerator("--php", php_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "php", "",
"Generate PHP files for tables/structs" },
flatbuffers::NewPhpCodeGenerator());

flatc.RegisterCodeGenerator("--python", python_generator);
flatc.RegisterCodeGenerator("-p", python_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "p", "python", "",
"Generate Python files for tables/structs" },
flatbuffers::NewPythonCodeGenerator());

flatc.RegisterCodeGenerator("--rust", rust_generator);
flatc.RegisterCodeGenerator("-r", rust_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "r", "rust", "",
"Generate Rust files for tables/structs" },
flatbuffers::NewRustCodeGenerator());

flatc.RegisterCodeGenerator("--swift", swift_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "", "swift", "",
"Generate Swift files for tables/structs" },
flatbuffers::NewSwiftCodeGenerator());

flatc.RegisterCodeGenerator("--ts", ts_generator);
flatc.RegisterCodeGenerator("-T", ts_generator);
flatc.RegisterCodeGenerator(
flatbuffers::FlatCOption{ "T", "ts", "",
"Generate TypeScript code for tables/structs" },
flatbuffers::NewTsCodeGenerator());

// Create the FlatC options by parsing the command line arguments.
const flatbuffers::FlatCOptions &options =
Expand Down