Made codegen always output a file, even on an empty schema.

Previously, we had a check to simply skip such files, but this
tends to make build systems unhappy.

This only affects C++ and JS, since other language output per-class
files.

Change-Id: I54224642725bbafb9f6e1654ed3693e62ca9f7d7
Tested: on Linux.
parent 6a7ec85e
...@@ -95,8 +95,6 @@ class BaseGenerator { ...@@ -95,8 +95,6 @@ class BaseGenerator {
static const char *FlatBuffersGeneratedWarning(); static const char *FlatBuffersGeneratedWarning();
bool IsEverythingGenerated() const;
static std::string FullNamespace(const char *separator, const Namespace &ns); static std::string FullNamespace(const char *separator, const Namespace &ns);
static std::string LastNamespacePart(const Namespace &ns); static std::string LastNamespacePart(const Namespace &ns);
......
...@@ -42,6 +42,7 @@ class FlatCompiler { ...@@ -42,6 +42,7 @@ class FlatCompiler {
const char *generator_opt_short; const char *generator_opt_short;
const char *generator_opt_long; const char *generator_opt_long;
const char *lang_name; const char *lang_name;
bool schema_only;
GenerateFn generateGRPC; GenerateFn generateGRPC;
flatbuffers::IDLOptions::Language lang; flatbuffers::IDLOptions::Language lang;
const char *generator_help; const char *generator_help;
......
...@@ -89,18 +89,6 @@ std::string BaseGenerator::NamespaceDir(const Namespace &ns) const { ...@@ -89,18 +89,6 @@ std::string BaseGenerator::NamespaceDir(const Namespace &ns) const {
return BaseGenerator::NamespaceDir(parser_, path_, ns); return BaseGenerator::NamespaceDir(parser_, path_, ns);
} }
bool BaseGenerator::IsEverythingGenerated() const {
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
if (!(*it)->generated) return false;
}
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
if (!(*it)->generated) return false;
}
return true;
}
std::string BaseGenerator::FullNamespace(const char *separator, std::string BaseGenerator::FullNamespace(const char *separator,
const Namespace &ns) { const Namespace &ns) {
std::string namespace_name; std::string namespace_name;
......
...@@ -267,101 +267,103 @@ int FlatCompiler::Compile(int argc, const char** argv) { ...@@ -267,101 +267,103 @@ int FlatCompiler::Compile(int argc, const char** argv) {
for (auto file_it = filenames.begin(); for (auto file_it = filenames.begin();
file_it != filenames.end(); file_it != filenames.end();
++file_it) { ++file_it) {
std::string contents; auto &filename = *file_it;
if (!flatbuffers::LoadFile(file_it->c_str(), true, &contents)) std::string contents;
Error("unable to load file: " + *file_it); if (!flatbuffers::LoadFile(filename.c_str(), true, &contents))
Error("unable to load file: " + filename);
bool is_binary = static_cast<size_t>(file_it - filenames.begin()) >= bool is_binary = static_cast<size_t>(file_it - filenames.begin()) >=
binary_files_from; binary_files_from;
if (is_binary) { auto is_schema = flatbuffers::GetExtension(filename) == "fbs";
parser->builder_.Clear(); if (is_binary) {
parser->builder_.PushFlatBuffer( parser->builder_.Clear();
reinterpret_cast<const uint8_t *>(contents.c_str()), parser->builder_.PushFlatBuffer(
contents.length()); reinterpret_cast<const uint8_t *>(contents.c_str()),
if (!raw_binary) { contents.length());
// Generally reading binaries that do not correspond to the schema if (!raw_binary) {
// will crash, and sadly there's no way around that when the binary // Generally reading binaries that do not correspond to the schema
// does not contain a file identifier. // will crash, and sadly there's no way around that when the binary
// We'd expect that typically any binary used as a file would have // does not contain a file identifier.
// such an identifier, so by default we require them to match. // We'd expect that typically any binary used as a file would have
if (!parser->file_identifier_.length()) { // such an identifier, so by default we require them to match.
Error("current schema has no file_identifier: cannot test if \"" + if (!parser->file_identifier_.length()) {
*file_it + Error("current schema has no file_identifier: cannot test if \"" +
"\" matches the schema, use --raw-binary to read this file" filename +
" anyway."); "\" matches the schema, use --raw-binary to read this file"
} else if (!flatbuffers::BufferHasIdentifier(contents.c_str(), " anyway.");
parser->file_identifier_.c_str())) { } else if (!flatbuffers::BufferHasIdentifier(contents.c_str(),
Error("binary \"" + parser->file_identifier_.c_str())) {
*file_it + Error("binary \"" +
"\" does not have expected file_identifier \"" + filename +
parser->file_identifier_ + "\" does not have expected file_identifier \"" +
"\", use --raw-binary to read this file anyway."); parser->file_identifier_ +
} "\", use --raw-binary to read this file anyway.");
}
} else {
// Check if file contains 0 bytes.
if (contents.length() != strlen(contents.c_str())) {
Error("input file appears to be binary: " + *file_it, true);
}
auto is_schema = flatbuffers::GetExtension(*file_it) == "fbs";
if (is_schema) {
// If we're processing multiple schemas, make sure to start each
// one from scratch. If it depends on previous schemas it must do
// so explicitly using an include.
parser.reset(new flatbuffers::Parser(opts));
}
ParseFile(*parser.get(), *file_it, contents, include_directories);
if (is_schema && !conform_to_schema.empty()) {
auto err = parser->ConformTo(conform_parser);
if (!err.empty()) Error("schemas don\'t conform: " + err);
}
if (schema_binary) {
parser->Serialize();
parser->file_extension_ = reflection::SchemaExtension();
} }
} }
} else {
// Check if file contains 0 bytes.
if (contents.length() != strlen(contents.c_str())) {
Error("input file appears to be binary: " + filename, true);
}
if (is_schema) {
// If we're processing multiple schemas, make sure to start each
// one from scratch. If it depends on previous schemas it must do
// so explicitly using an include.
parser.reset(new flatbuffers::Parser(opts));
}
ParseFile(*parser.get(), filename, contents, include_directories);
if (is_schema && !conform_to_schema.empty()) {
auto err = parser->ConformTo(conform_parser);
if (!err.empty()) Error("schemas don\'t conform: " + err);
}
if (schema_binary) {
parser->Serialize();
parser->file_extension_ = reflection::SchemaExtension();
}
}
std::string filebase = flatbuffers::StripPath( std::string filebase = flatbuffers::StripPath(
flatbuffers::StripExtension(*file_it)); flatbuffers::StripExtension(filename));
for (size_t i = 0; i < params_.num_generators; ++i) { for (size_t i = 0; i < params_.num_generators; ++i) {
parser->opts.lang = params_.generators[i].lang; parser->opts.lang = params_.generators[i].lang;
if (generator_enabled[i]) { if (generator_enabled[i]) {
if (!print_make_rules) { if (!print_make_rules) {
flatbuffers::EnsureDirExists(output_path); flatbuffers::EnsureDirExists(output_path);
if (!params_.generators[i].generate(*parser.get(), output_path, filebase)) { if ((!params_.generators[i].schema_only || is_schema) &&
Error(std::string("Unable to generate ") + !params_.generators[i].generate(*parser.get(), output_path, filebase)) {
params_.generators[i].lang_name + Error(std::string("Unable to generate ") +
" for " + params_.generators[i].lang_name +
filebase); " for " +
} filebase);
} else {
std::string make_rule = params_.generators[i].make_rule(
*parser.get(), output_path, *file_it);
if (!make_rule.empty())
printf("%s\n", flatbuffers::WordWrap(
make_rule, 80, " ", " \\").c_str());
} }
if (grpc_enabled) { } else {
if (params_.generators[i].generateGRPC != nullptr) { std::string make_rule = params_.generators[i].make_rule(
if (!params_.generators[i].generateGRPC(*parser.get(), output_path, *parser.get(), output_path, filename);
filebase)) { if (!make_rule.empty())
Error(std::string("Unable to generate GRPC interface for") + printf("%s\n", flatbuffers::WordWrap(
params_.generators[i].lang_name); make_rule, 80, " ", " \\").c_str());
} }
} else { if (grpc_enabled) {
Warn(std::string("GRPC interface generator not implemented for ") if (params_.generators[i].generateGRPC != nullptr) {
+ params_.generators[i].lang_name); if (!params_.generators[i].generateGRPC(*parser.get(), output_path,
filebase)) {
Error(std::string("Unable to generate GRPC interface for") +
params_.generators[i].lang_name);
} }
} else {
Warn(std::string("GRPC interface generator not implemented for ")
+ params_.generators[i].lang_name);
} }
} }
} }
}
if (opts.proto_mode) GenerateFBS(*parser.get(), output_path, filebase); if (opts.proto_mode) GenerateFBS(*parser.get(), output_path, filebase);
// We do not want to generate code for the definitions in this file // We do not want to generate code for the definitions in this file
// in any files coming up next. // in any files coming up next.
parser->MarkGenerated(); parser->MarkGenerated();
} }
return 0; return 0;
} }
......
...@@ -46,52 +46,52 @@ int main(int argc, const char *argv[]) { ...@@ -46,52 +46,52 @@ int main(int argc, const char *argv[]) {
g_program_name = argv[0]; g_program_name = argv[0];
const flatbuffers::FlatCompiler::Generator generators[] = { const flatbuffers::FlatCompiler::Generator generators[] = {
{ flatbuffers::GenerateBinary, "-b", "--binary", "binary", { flatbuffers::GenerateBinary, "-b", "--binary", "binary", false,
nullptr, nullptr,
flatbuffers::IDLOptions::kBinary, flatbuffers::IDLOptions::kBinary,
"Generate wire format binaries for any data definitions", "Generate wire format binaries for any data definitions",
flatbuffers::BinaryMakeRule }, flatbuffers::BinaryMakeRule },
{ flatbuffers::GenerateTextFile, "-t", "--json", "text", { flatbuffers::GenerateTextFile, "-t", "--json", "text", false,
nullptr, nullptr,
flatbuffers::IDLOptions::kJson, flatbuffers::IDLOptions::kJson,
"Generate text output for any data definitions", "Generate text output for any data definitions",
flatbuffers::TextMakeRule }, flatbuffers::TextMakeRule },
{ flatbuffers::GenerateCPP, "-c", "--cpp", "C++", { flatbuffers::GenerateCPP, "-c", "--cpp", "C++", true,
flatbuffers::GenerateCppGRPC, flatbuffers::GenerateCppGRPC,
flatbuffers::IDLOptions::kCpp, flatbuffers::IDLOptions::kCpp,
"Generate C++ headers for tables/structs", "Generate C++ headers for tables/structs",
flatbuffers::CPPMakeRule }, flatbuffers::CPPMakeRule },
{ flatbuffers::GenerateGo, "-g", "--go", "Go", { flatbuffers::GenerateGo, "-g", "--go", "Go", true,
flatbuffers::GenerateGoGRPC, flatbuffers::GenerateGoGRPC,
flatbuffers::IDLOptions::kGo, flatbuffers::IDLOptions::kGo,
"Generate Go files for tables/structs", "Generate Go files for tables/structs",
flatbuffers::GeneralMakeRule }, flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateGeneral, "-j", "--java", "Java", { flatbuffers::GenerateGeneral, "-j", "--java", "Java", true,
nullptr, nullptr,
flatbuffers::IDLOptions::kJava, flatbuffers::IDLOptions::kJava,
"Generate Java classes for tables/structs", "Generate Java classes for tables/structs",
flatbuffers::GeneralMakeRule }, flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateJS, "-s", "--js", "JavaScript", { flatbuffers::GenerateJS, "-s", "--js", "JavaScript", true,
nullptr, nullptr,
flatbuffers::IDLOptions::kJs, flatbuffers::IDLOptions::kJs,
"Generate JavaScript code for tables/structs", "Generate JavaScript code for tables/structs",
flatbuffers::JSMakeRule }, flatbuffers::JSMakeRule },
{ flatbuffers::GenerateJS, "-T", "--ts", "TypeScript", { flatbuffers::GenerateJS, "-T", "--ts", "TypeScript", true,
nullptr, nullptr,
flatbuffers::IDLOptions::kTs, flatbuffers::IDLOptions::kTs,
"Generate TypeScript code for tables/structs", "Generate TypeScript code for tables/structs",
flatbuffers::JSMakeRule }, flatbuffers::JSMakeRule },
{ flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", { flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", true,
nullptr, nullptr,
flatbuffers::IDLOptions::kCSharp, flatbuffers::IDLOptions::kCSharp,
"Generate C# classes for tables/structs", "Generate C# classes for tables/structs",
flatbuffers::GeneralMakeRule }, flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePython, "-p", "--python", "Python", { flatbuffers::GeneratePython, "-p", "--python", "Python", true,
nullptr, nullptr,
flatbuffers::IDLOptions::kPython, flatbuffers::IDLOptions::kPython,
"Generate Python files for tables/structs", "Generate Python files for tables/structs",
flatbuffers::GeneralMakeRule }, flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePhp, nullptr, "--php", "PHP", { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true,
nullptr, nullptr,
flatbuffers::IDLOptions::kPhp, flatbuffers::IDLOptions::kPhp,
"Generate PHP files for tables/structs", "Generate PHP files for tables/structs",
......
...@@ -87,8 +87,6 @@ class CppGenerator : public BaseGenerator { ...@@ -87,8 +87,6 @@ class CppGenerator : public BaseGenerator {
// Iterate through all definitions we haven't generate code for (enums, // Iterate through all definitions we haven't generate code for (enums,
// structs, and tables) and output them to a single file. // structs, and tables) and output them to a single file.
bool generate() { bool generate() {
if (IsEverythingGenerated()) return true;
code_.Clear(); code_.Clear();
code_ += "// " + std::string(FlatBuffersGeneratedWarning()); code_ += "// " + std::string(FlatBuffersGeneratedWarning());
...@@ -261,8 +259,7 @@ class CppGenerator : public BaseGenerator { ...@@ -261,8 +259,7 @@ class CppGenerator : public BaseGenerator {
} }
} }
assert(cur_name_space_); if (cur_name_space_) SetNameSpace(nullptr);
SetNameSpace(nullptr);
// Close the include guard. // Close the include guard.
code_ += "#endif // " + include_guard; code_ += "#endif // " + include_guard;
......
...@@ -83,8 +83,6 @@ class JsGenerator : public BaseGenerator { ...@@ -83,8 +83,6 @@ class JsGenerator : public BaseGenerator {
// Iterate through all definitions we haven't generate code for (enums, // Iterate through all definitions we haven't generate code for (enums,
// structs, and tables) and output them to a single file. // structs, and tables) and output them to a single file.
bool generate() { bool generate() {
if (IsEverythingGenerated()) return true;
imported_fileset imported_files; imported_fileset imported_files;
reexport_map reexports; reexport_map reexports;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment