protoc-c: add no_generate option

Allows suppression of .pb-c.{c,h} file generation on a .proto file
basis.

protobuf-c.proto: Use the new option in itself, because the options are
never used by protobuf-c runtime, only by the protoc-gen-c compiler.

t/test-full.proto: import protobuf-c.proto to test that
protobuf-c.pb-c.h dependency does not get included in test-full.pb-c.h.
This commit is contained in:
Ilya Lipnitskiy 2021-03-20 14:07:23 -07:00
parent 0e060260f0
commit 883b1acc1b
No known key found for this signature in database
GPG Key ID: 435C02AAE7CF2014
5 changed files with 20 additions and 4 deletions

View File

@ -164,7 +164,8 @@ noinst_PROGRAMS += \
t_generated_code2_cxx_generate_packed_data_SOURCES = \ t_generated_code2_cxx_generate_packed_data_SOURCES = \
t/generated-code2/cxx-generate-packed-data.cc \ t/generated-code2/cxx-generate-packed-data.cc \
t/test-full.pb.cc t/test-full.pb.cc \
protobuf-c/protobuf-c.pb.cc
$(t_generated_code2_cxx_generate_packed_data_OBJECTS): t/test-full.pb.h $(t_generated_code2_cxx_generate_packed_data_OBJECTS): t/test-full.pb.h
t_generated_code2_cxx_generate_packed_data_CXXFLAGS = \ t_generated_code2_cxx_generate_packed_data_CXXFLAGS = \
$(AM_CXXFLAGS) \ $(AM_CXXFLAGS) \

View File

@ -30,7 +30,13 @@
syntax = "proto2"; syntax = "proto2";
import "google/protobuf/descriptor.proto"; import "google/protobuf/descriptor.proto";
// We never need to generate protobuf-c.pb-c.{c,h}, the options are used
// only by protoc-gen-c, never by the protobuf-c runtime itself
option (pb_c_file).no_generate = true;
message ProtobufCFileOptions { message ProtobufCFileOptions {
// Suppresses pb-c.{c,h} file output completely.
optional bool no_generate = 1 [default = false];
} }
extend google.protobuf.FileOptions { extend google.protobuf.FileOptions {

View File

@ -157,10 +157,14 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
"protoc_version", SimpleItoa(PROTOBUF_C_VERSION_NUMBER)); "protoc_version", SimpleItoa(PROTOBUF_C_VERSION_NUMBER));
for (int i = 0; i < file_->dependency_count(); i++) { for (int i = 0; i < file_->dependency_count(); i++) {
const ProtobufCFileOptions opt =
file_->dependency(i)->options().GetExtension(pb_c_file);
if (!opt.no_generate()) {
printer->Print( printer->Print(
"#include \"$dependency$.pb-c.h\"\n", "#include \"$dependency$.pb-c.h\"\n",
"dependency", StripProto(file_->dependency(i)->name())); "dependency", StripProto(file_->dependency(i)->name()));
} }
}
printer->Print("\n"); printer->Print("\n");

View File

@ -106,6 +106,9 @@ bool CGenerator::Generate(const FileDescriptor* file,
const std::string& parameter, const std::string& parameter,
OutputDirectory* output_directory, OutputDirectory* output_directory,
std::string* error) const { std::string* error) const {
if (file->options().GetExtension(pb_c_file).no_generate())
return true;
std::vector<std::pair<std::string, std::string> > options; std::vector<std::pair<std::string, std::string> > options;
ParseOptions(parameter, &options); ParseOptions(parameter, &options);

View File

@ -1,5 +1,7 @@
package foo; package foo;
import "protobuf-c/protobuf-c.proto";
message SubMess { message SubMess {
required int32 test = 4; required int32 test = 4;