mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 06:30:54 +00:00
[gRPC] Add new options to control the gRPC code generation. (#8298)
The new options are: - `--grpc-filename-suffix` controls the suffix of the generated files; - `--grpc-use-system-headers` controls the type of C++ includes generated; - `--grpc-search-path` controls the directory that contains gRPC runtime; - `--grpc-additional-header` allows to provide additional dependencies for the generated code.
This commit is contained in:
@@ -258,6 +258,13 @@ const static FlatCOption flatc_options[] = {
|
||||
"Omit emission of namespace entrypoint file" },
|
||||
{ "", "file-names-only", "",
|
||||
"Print out generated file names without writing to the files" },
|
||||
{ "", "grpc-filename-suffix", "SUFFIX",
|
||||
"The suffix for the generated file names (Default is '.fb')." },
|
||||
{ "", "grpc-additional-header", "",
|
||||
"Additional headers to prepend to the generated files." },
|
||||
{ "", "grpc-use-system-headers", "",
|
||||
"Use <> for headers included from the generated code." },
|
||||
{ "", "grpc-search-path", "PATH", "Prefix to any gRPC includes." },
|
||||
};
|
||||
|
||||
auto cmp = [](FlatCOption a, FlatCOption b) { return a.long_opt < b.long_opt; };
|
||||
@@ -674,6 +681,30 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
|
||||
} else if (arg == "--file-names-only") {
|
||||
// TODO (khhn): Provide 2 implementation
|
||||
options.file_names_only = true;
|
||||
} else if (arg == "--grpc-filename-suffix") {
|
||||
if (++argi >= argc) Error("missing gRPC filename suffix: " + arg, true);
|
||||
opts.grpc_filename_suffix = argv[argi];
|
||||
} else if (arg.rfind("--grpc-filename-suffix=", 0) == 0) {
|
||||
opts.grpc_filename_suffix =
|
||||
arg.substr(std::string("--grpc-filename-suffix=").size());
|
||||
} else if (arg == "--grpc-additional-header") {
|
||||
if (++argi >= argc) Error("missing include following: " + arg, true);
|
||||
opts.grpc_additional_headers.push_back(argv[argi]);
|
||||
} else if (arg.rfind("--grpc-additional-header=", 0) == 0) {
|
||||
opts.grpc_additional_headers.push_back(
|
||||
arg.substr(std::string("--grpc-additional-header=").size()));
|
||||
} else if (arg == "--grpc-search-path") {
|
||||
if (++argi >= argc) Error("missing gRPC search path: " + arg, true);
|
||||
opts.grpc_search_path = argv[argi];
|
||||
} else if (arg.rfind("--grpc-search-path=", 0) == 0) {
|
||||
opts.grpc_search_path =
|
||||
arg.substr(std::string("--grpc-search-path=").size());
|
||||
} else if (arg == "--grpc-use-system-headers" ||
|
||||
arg == "--grpc-use-system-headers=true") {
|
||||
opts.grpc_use_system_headers = true;
|
||||
} else if (arg == "--no-grpc-use-system-headers" ||
|
||||
arg == "--grpc-use-system-headers=false") {
|
||||
opts.grpc_use_system_headers = false;
|
||||
} else {
|
||||
if (arg == "--proto") { opts.proto_mode = true; }
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
// independent from idl_parser, since this code is not needed for most clients
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "flatbuffers/code_generators.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
@@ -253,6 +255,15 @@ class FlatBufFile : public grpc_generator::File {
|
||||
std::string additional_headers() const {
|
||||
switch (language_) {
|
||||
case kLanguageCpp: {
|
||||
if (!parser_.opts.grpc_additional_headers.empty()) {
|
||||
std::string result = "";
|
||||
for (const std::string &header :
|
||||
parser_.opts.grpc_additional_headers) {
|
||||
if (!result.empty()) result += "\n";
|
||||
result += "#include \"" + header + "\"";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return "#include \"flatbuffers/grpc.h\"\n";
|
||||
}
|
||||
case kLanguageGo: {
|
||||
@@ -356,10 +367,16 @@ bool GenerateCppGRPC(const Parser &parser, const std::string &path,
|
||||
|
||||
grpc_cpp_generator::Parameters generator_parameters;
|
||||
// TODO(wvo): make the other parameters in this struct configurable.
|
||||
generator_parameters.use_system_headers = true;
|
||||
generator_parameters.use_system_headers = opts.grpc_use_system_headers;
|
||||
generator_parameters.message_header_extension = suffix;
|
||||
|
||||
FlatBufFile fbfile(parser, file_name, FlatBufFile::kLanguageCpp);
|
||||
generator_parameters.service_header_extension =
|
||||
".grpc" + opts.grpc_filename_suffix + ".h";
|
||||
generator_parameters.grpc_search_path = opts.grpc_search_path;
|
||||
std::string filename = flatbuffers::StripExtension(parser.file_being_parsed_);
|
||||
if (!opts.keep_prefix) {
|
||||
filename = flatbuffers::StripPath(filename);
|
||||
}
|
||||
FlatBufFile fbfile(parser, filename, FlatBufFile::kLanguageCpp);
|
||||
|
||||
std::string header_code =
|
||||
grpc_cpp_generator::GetHeaderPrologue(&fbfile, generator_parameters) +
|
||||
@@ -373,10 +390,14 @@ bool GenerateCppGRPC(const Parser &parser, const std::string &path,
|
||||
grpc_cpp_generator::GetSourceServices(&fbfile, generator_parameters) +
|
||||
grpc_cpp_generator::GetSourceEpilogue(&fbfile, generator_parameters);
|
||||
|
||||
return flatbuffers::SaveFile((path + file_name + ".grpc.fb.h").c_str(),
|
||||
header_code, false) &&
|
||||
flatbuffers::SaveFile((path + file_name + ".grpc.fb.cc").c_str(),
|
||||
source_code, false);
|
||||
return flatbuffers::SaveFile(
|
||||
(path + file_name + ".grpc" + opts.grpc_filename_suffix + ".h")
|
||||
.c_str(),
|
||||
header_code, false) &&
|
||||
flatbuffers::SaveFile(
|
||||
(path + file_name + ".grpc" + opts.grpc_filename_suffix + ".cc")
|
||||
.c_str(),
|
||||
source_code, false);
|
||||
}
|
||||
|
||||
class JavaGRPCGenerator : public flatbuffers::BaseGenerator {
|
||||
|
||||
Reference in New Issue
Block a user