From 883b1acc1b630844df1d746d575f96a0dd6b3ccd Mon Sep 17 00:00:00 2001 From: Ilya Lipnitskiy Date: Sat, 20 Mar 2021 14:07:23 -0700 Subject: [PATCH] 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. --- Makefile.am | 3 ++- protobuf-c/protobuf-c.proto | 6 ++++++ protoc-c/c_file.cc | 10 +++++++--- protoc-c/c_generator.cc | 3 +++ t/test-full.proto | 2 ++ 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Makefile.am b/Makefile.am index d8feb0d..1e1e1df 100644 --- a/Makefile.am +++ b/Makefile.am @@ -164,7 +164,8 @@ noinst_PROGRAMS += \ t_generated_code2_cxx_generate_packed_data_SOURCES = \ 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_CXXFLAGS = \ $(AM_CXXFLAGS) \ diff --git a/protobuf-c/protobuf-c.proto b/protobuf-c/protobuf-c.proto index 1c54cf8..66de032 100644 --- a/protobuf-c/protobuf-c.proto +++ b/protobuf-c/protobuf-c.proto @@ -30,7 +30,13 @@ syntax = "proto2"; 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 { + // Suppresses pb-c.{c,h} file output completely. + optional bool no_generate = 1 [default = false]; } extend google.protobuf.FileOptions { diff --git a/protoc-c/c_file.cc b/protoc-c/c_file.cc index f291679..9ea788b 100644 --- a/protoc-c/c_file.cc +++ b/protoc-c/c_file.cc @@ -157,9 +157,13 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { "protoc_version", SimpleItoa(PROTOBUF_C_VERSION_NUMBER)); for (int i = 0; i < file_->dependency_count(); i++) { - printer->Print( - "#include \"$dependency$.pb-c.h\"\n", - "dependency", StripProto(file_->dependency(i)->name())); + const ProtobufCFileOptions opt = + file_->dependency(i)->options().GetExtension(pb_c_file); + if (!opt.no_generate()) { + printer->Print( + "#include \"$dependency$.pb-c.h\"\n", + "dependency", StripProto(file_->dependency(i)->name())); + } } printer->Print("\n"); diff --git a/protoc-c/c_generator.cc b/protoc-c/c_generator.cc index e30cf7b..e2fccdf 100644 --- a/protoc-c/c_generator.cc +++ b/protoc-c/c_generator.cc @@ -106,6 +106,9 @@ bool CGenerator::Generate(const FileDescriptor* file, const std::string& parameter, OutputDirectory* output_directory, std::string* error) const { + if (file->options().GetExtension(pb_c_file).no_generate()) + return true; + std::vector > options; ParseOptions(parameter, &options); diff --git a/t/test-full.proto b/t/test-full.proto index c04fce7..2ebd690 100644 --- a/t/test-full.proto +++ b/t/test-full.proto @@ -1,5 +1,7 @@ package foo; +import "protobuf-c/protobuf-c.proto"; + message SubMess { required int32 test = 4;