mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-15 16:57:29 +00:00
Added option to flatc to generate dependent header statements.
Bug: 17322776 Change-Id: I3a4d3cb4ccd40bc3200a87653aa0ab8ecb90ce60 Tested: on Linux.
This commit is contained in:
@@ -101,6 +101,8 @@ static void Error(const char *err, const char *obj, bool usage) {
|
||||
" -I PATH Search for includes in the specified path.\n"
|
||||
" -S Strict JSON: add quotes to field names.\n"
|
||||
" -P Don\'t prefix enum values with the enum name in C++.\n"
|
||||
" -H Generate include statements for included schemas the\n"
|
||||
" generated file depends on (C++).\n"
|
||||
"FILEs may depend on declarations in earlier files.\n"
|
||||
"FILEs after the -- must be binary flatbuffer format files.\n"
|
||||
"Output files are named using the base file name of the input,"
|
||||
@@ -144,6 +146,9 @@ int main(int argc, const char *argv[]) {
|
||||
case 'P':
|
||||
opts.prefixed_enums = false;
|
||||
break;
|
||||
case 'H':
|
||||
opts.include_dependence_headers = true;
|
||||
break;
|
||||
case '-': // Separator between text and binary input files.
|
||||
binary_files_from = filenames.size();
|
||||
break;
|
||||
@@ -189,7 +194,8 @@ int main(int argc, const char *argv[]) {
|
||||
auto local_include_directory = flatbuffers::StripFileName(*file_it);
|
||||
include_directories.push_back(local_include_directory.c_str());
|
||||
include_directories.push_back(nullptr);
|
||||
if (!parser.Parse(contents.c_str(), &include_directories[0]))
|
||||
if (!parser.Parse(contents.c_str(), &include_directories[0],
|
||||
file_it->c_str()))
|
||||
Error((*file_it + ": " + parser.error_).c_str());
|
||||
include_directories.pop_back();
|
||||
include_directories.pop_back();
|
||||
|
||||
@@ -547,6 +547,20 @@ std::string GenerateCPP(const Parser &parser,
|
||||
|
||||
code += "#include \"flatbuffers/flatbuffers.h\"\n\n";
|
||||
|
||||
if (opts.include_dependence_headers) {
|
||||
int num_includes = 0;
|
||||
for (auto it = parser.included_files_.begin();
|
||||
it != parser.included_files_.end(); ++it) {
|
||||
auto basename = flatbuffers::StripPath(
|
||||
flatbuffers::StripExtension(it->first));
|
||||
if (basename != file_name) {
|
||||
code += "#include \"" + basename + "_generated.h\"\n";
|
||||
num_includes++;
|
||||
}
|
||||
}
|
||||
if (num_includes) code += "\n";
|
||||
}
|
||||
|
||||
code += forward_decl_code_other_namespace;
|
||||
code += "\n";
|
||||
|
||||
|
||||
@@ -866,7 +866,9 @@ void Parser::MarkGenerated() {
|
||||
}
|
||||
}
|
||||
|
||||
bool Parser::Parse(const char *source, const char **include_paths) {
|
||||
bool Parser::Parse(const char *source, const char **include_paths,
|
||||
const char *source_filename) {
|
||||
if (source_filename) included_files_[source_filename] = true;
|
||||
source_ = cursor_ = source;
|
||||
line_ = 1;
|
||||
error_.clear();
|
||||
|
||||
Reference in New Issue
Block a user