Added --gen-all to generate code for a schema and all its includes.

Also refactored the way options are stored.

Change-Id: I709ac908cd2aba396c9c282725cf1d42ccce0882
Tested: on Linux.
This commit is contained in:
Wouter van Oortmerssen
2015-11-30 17:42:19 -08:00
parent 47478117d8
commit 45bda6e08d
15 changed files with 240 additions and 268 deletions

View File

@@ -26,55 +26,53 @@ static void Error(const std::string &err, bool usage = false,
struct Generator {
bool (*generate)(const flatbuffers::Parser &parser,
const std::string &path,
const std::string &file_name,
const flatbuffers::GeneratorOptions &opts);
const std::string &file_name);
const char *generator_opt_short;
const char *generator_opt_long;
const char *lang_name;
flatbuffers::GeneratorOptions::Language lang;
flatbuffers::IDLOptions::Language lang;
const char *generator_help;
std::string (*make_rule)(const flatbuffers::Parser &parser,
const std::string &path,
const std::string &file_name,
const flatbuffers::GeneratorOptions &opts);
const std::string &file_name);
};
const Generator generators[] = {
{ flatbuffers::GenerateBinary, "-b", "--binary", "binary",
flatbuffers::GeneratorOptions::kMAX,
flatbuffers::IDLOptions::kMAX,
"Generate wire format binaries for any data definitions",
flatbuffers::BinaryMakeRule },
{ flatbuffers::GenerateTextFile, "-t", "--json", "text",
flatbuffers::GeneratorOptions::kMAX,
flatbuffers::IDLOptions::kMAX,
"Generate text output for any data definitions",
flatbuffers::TextMakeRule },
{ flatbuffers::GenerateCPP, "-c", "--cpp", "C++",
flatbuffers::GeneratorOptions::kMAX,
flatbuffers::IDLOptions::kMAX,
"Generate C++ headers for tables/structs",
flatbuffers::CPPMakeRule },
{ flatbuffers::GenerateGo, "-g", "--go", "Go",
flatbuffers::GeneratorOptions::kGo,
flatbuffers::IDLOptions::kGo,
"Generate Go files for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateGeneral, "-j", "--java", "Java",
flatbuffers::GeneratorOptions::kJava,
flatbuffers::IDLOptions::kJava,
"Generate Java classes for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateJS, "-s", "--js", "JavaScript",
flatbuffers::GeneratorOptions::kMAX,
flatbuffers::IDLOptions::kMAX,
"Generate JavaScript code for tables/structs",
flatbuffers::JSMakeRule },
{ flatbuffers::GenerateGeneral, "-n", "--csharp", "C#",
flatbuffers::GeneratorOptions::kCSharp,
flatbuffers::IDLOptions::kCSharp,
"Generate C# classes for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePython, "-p", "--python", "Python",
flatbuffers::GeneratorOptions::kMAX,
flatbuffers::IDLOptions::kMAX,
"Generate Python files for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePhp, nullptr, "--php", "PHP",
flatbuffers::GeneratorOptions::kMAX,
flatbuffers::IDLOptions::kMAX,
"Generate PHP files for tables/structs",
flatbuffers::GeneralMakeRule },
};
@@ -129,13 +127,12 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) {
int main(int argc, const char *argv[]) {
program_name = argv[0];
flatbuffers::GeneratorOptions opts;
flatbuffers::IDLOptions opts;
std::string output_path;
const size_t num_generators = sizeof(generators) / sizeof(generators[0]);
bool generator_enabled[num_generators] = { false };
bool any_generator = false;
bool print_make_rules = false;
bool proto_mode = false;
bool raw_binary = false;
bool schema_binary = false;
std::vector<std::string> filenames;
@@ -165,6 +162,9 @@ int main(int argc, const char *argv[]) {
opts.scoped_enums = true;
} else if(arg == "--gen-mutable") {
opts.mutable_buffer = true;
} else if(arg == "--gen-all") {
opts.generate_all = true;
opts.include_dependence_headers = false;
} else if(arg == "--gen-includes") {
// Deprecated, remove this option some time in the future.
printf("warning: --gen-includes is deprecated (it is now default)\n");
@@ -177,7 +177,7 @@ int main(int argc, const char *argv[]) {
} else if(arg == "--") { // Separator between text and binary inputs.
binary_files_from = filenames.size();
} else if(arg == "--proto") {
proto_mode = true;
opts.proto_mode = true;
any_generator = true;
} else if(arg == "--schema") {
schema_binary = true;
@@ -204,10 +204,10 @@ int main(int argc, const char *argv[]) {
if (!filenames.size()) Error("missing input files", false, true);
if (!any_generator)
Error("no options: specify one of -c -g -j -t -b etc.", true);
Error("no options: specify at least one generator.", true);
// Now process the files:
parser = new flatbuffers::Parser(opts.strict_json, proto_mode);
parser = new flatbuffers::Parser(opts);
for (auto file_it = filenames.begin();
file_it != filenames.end();
++file_it) {
@@ -248,7 +248,7 @@ int main(int argc, const char *argv[]) {
// one from scratch. If it depends on previous schemas it must do
// so explicitly using an include.
delete parser;
parser = new flatbuffers::Parser(opts.strict_json, proto_mode);
parser = new flatbuffers::Parser(opts);
}
auto local_include_directory = flatbuffers::StripFileName(*file_it);
include_directories.push_back(local_include_directory.c_str());
@@ -272,7 +272,7 @@ int main(int argc, const char *argv[]) {
if (generator_enabled[i]) {
if (!print_make_rules) {
flatbuffers::EnsureDirExists(output_path);
if (!generators[i].generate(*parser, output_path, filebase, opts)) {
if (!generators[i].generate(*parser, output_path, filebase)) {
Error(std::string("Unable to generate ") +
generators[i].lang_name +
" for " +
@@ -280,7 +280,7 @@ int main(int argc, const char *argv[]) {
}
} else {
std::string make_rule = generators[i].make_rule(
*parser, output_path, *file_it, opts);
*parser, output_path, *file_it);
if (!make_rule.empty())
printf("%s\n", flatbuffers::WordWrap(
make_rule, 80, " ", " \\").c_str());
@@ -288,7 +288,11 @@ int main(int argc, const char *argv[]) {
}
}
if (proto_mode) GenerateFBS(*parser, output_path, filebase, opts);
if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase);
// We do not want to generate code for the definitions in this file
// in any files coming up next.
parser->MarkGenerated();
}
delete parser;