diff --git a/cmake/upb_generators.cmake b/cmake/upb_generators.cmake index b2e66b130e..c120e450d4 100644 --- a/cmake/upb_generators.cmake +++ b/cmake/upb_generators.cmake @@ -17,6 +17,7 @@ foreach(generator upb upbdefs upb_minitable) target_include_directories(protoc-gen-${generator} PRIVATE ${bootstrap_cmake_dir}) target_link_libraries(protoc-gen-${generator} ${protobuf_LIB_PROTOBUF} + ${protobuf_LIB_PROTOC} ${protobuf_LIB_UPB} ${protobuf_ABSL_USED_TARGETS} ) diff --git a/hpb_generator/protoc-gen-upb-protos.cc b/hpb_generator/protoc-gen-upb-protos.cc index d946fb260a..b431e0de0e 100644 --- a/hpb_generator/protoc-gen-upb-protos.cc +++ b/hpb_generator/protoc-gen-upb-protos.cc @@ -31,8 +31,9 @@ using FileDescriptor = ::google::protobuf::FileDescriptor; using google::protobuf::Edition; void WriteSource(const protobuf::FileDescriptor* file, Output& output, - bool fasttable_enabled); -void WriteHeader(const protobuf::FileDescriptor* file, Output& output); + bool fasttable_enabled, bool strip_feature_includes); +void WriteHeader(const protobuf::FileDescriptor* file, Output& output, + bool strip_feature_includes); void WriteForwardingHeader(const protobuf::FileDescriptor* file, Output& output); void WriteMessageImplementations(const protobuf::FileDescriptor* file, @@ -42,7 +43,8 @@ void WriteTypedefForwardingHeader( const std::vector& file_messages, Output& output); void WriteHeaderMessageForwardDecls(const protobuf::FileDescriptor* file, - Output& output); + Output& output, + bool strip_feature_includes); class Generator : public protoc::CodeGenerator { public: @@ -63,6 +65,7 @@ bool Generator::Generate(const protobuf::FileDescriptor* file, protoc::GeneratorContext* context, std::string* error) const { bool fasttable_enabled = false; + bool strip_nonfunctional_codegen = false; std::vector> params; google::protobuf::compiler::ParseGeneratorParameter(parameter, ¶ms); @@ -70,7 +73,7 @@ bool Generator::Generate(const protobuf::FileDescriptor* file, if (pair.first == "fasttable") { fasttable_enabled = true; } else if (pair.first == "experimental_strip_nonfunctional_codegen") { - continue; + strip_nonfunctional_codegen = true; } else { *error = "Unknown parameter: " + pair.first; return false; @@ -87,13 +90,13 @@ bool Generator::Generate(const protobuf::FileDescriptor* file, std::unique_ptr header_output_stream( context->Open(CppHeaderFilename(file))); Output header_output(header_output_stream.get()); - WriteHeader(file, header_output); + WriteHeader(file, header_output, strip_nonfunctional_codegen); // Write model.upb.proto.cc std::unique_ptr cc_output_stream( context->Open(CppSourceFilename(file))); Output cc_output(cc_output_stream.get()); - WriteSource(file, cc_output, fasttable_enabled); + WriteSource(file, cc_output, fasttable_enabled, strip_nonfunctional_codegen); return true; } @@ -123,7 +126,8 @@ void WriteForwardingHeader(const protobuf::FileDescriptor* file, output("#endif /* $0_UPB_FWD_H_ */\n", ToPreproc(file->name())); } -void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { +void WriteHeader(const protobuf::FileDescriptor* file, Output& output, + bool strip_feature_includes) { EmitFileWarning(file, output); output( R"cc( @@ -161,7 +165,7 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { output("\n"); } - WriteHeaderMessageForwardDecls(file, output); + WriteHeaderMessageForwardDecls(file, output, strip_feature_includes); WriteStartNamespace(file, output); std::vector this_file_enums = @@ -190,7 +194,7 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { // Writes a .upb.cc source file. void WriteSource(const protobuf::FileDescriptor* file, Output& output, - bool fasttable_enabled) { + bool fasttable_enabled, bool strip_feature_includes) { EmitFileWarning(file, output); output( @@ -203,6 +207,11 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output, CppHeaderFilename(file)); for (int i = 0; i < file->dependency_count(); i++) { + if (strip_feature_includes && + compiler::IsKnownFeatureProto(file->dependency(i)->name())) { + // Strip feature imports for editions codegen tests. + continue; + } output("#include \"$0\"\n", CppHeaderFilename(file->dependency(i))); } output("#include \"upb/port/def.inc\"\n"); @@ -253,12 +262,18 @@ void WriteTypedefForwardingHeader( /// Writes includes for upb C minitables and fwd.h for transitive typedefs. void WriteHeaderMessageForwardDecls(const protobuf::FileDescriptor* file, - Output& output) { + Output& output, + bool strip_feature_includes) { // Import forward-declaration of types defined in this file. output("#include \"$0\"\n", UpbCFilename(file)); output("#include \"$0\"\n", ForwardingHeaderFilename(file)); // Import forward-declaration of types in dependencies. for (int i = 0; i < file->dependency_count(); ++i) { + if (strip_feature_includes && + compiler::IsKnownFeatureProto(file->dependency(i)->name())) { + // Strip feature imports for editions codegen tests. + continue; + } output("#include \"$0\"\n", ForwardingHeaderFilename(file->dependency(i))); } output("\n"); diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 34d65ceb41..c7c3f9dd24 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -225,6 +225,7 @@ cc_dist_library( name = "protoc-gen-upb", dist_deps = [ ":protobuf", + ":protoc", ":upb", ], tags = ["manual"], @@ -237,6 +238,7 @@ cc_dist_library( name = "protoc-gen-upbdefs", dist_deps = [ ":protobuf", + ":protoc", ":upb", ], tags = ["manual"], @@ -249,6 +251,7 @@ cc_dist_library( name = "protoc-gen-upb_minitable", dist_deps = [ ":protobuf", + ":protoc", ":upb", ], tags = ["manual"], diff --git a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc index 6aefb500ef..1e8d3f2825 100644 --- a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc +++ b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc @@ -9,8 +9,8 @@ #include -#include "google/protobuf/compiler/code_generator.h" #include "absl/strings/str_join.h" +#include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/csharp/csharp_enum.h" #include "google/protobuf/compiler/csharp/csharp_field_base.h" #include "google/protobuf/compiler/csharp/csharp_helpers.h" @@ -170,10 +170,14 @@ void ReflectionClassGenerator::WriteDescriptor(io::Printer* printer) { "descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,\n"); printer->Print(" new pbr::FileDescriptor[] { "); for (int i = 0; i < file_->dependency_count(); i++) { - printer->Print( - "$full_reflection_class_name$.Descriptor, ", - "full_reflection_class_name", - GetReflectionClassName(file_->dependency(i))); + if (options()->strip_nonfunctional_codegen && + IsKnownFeatureProto(file_->dependency(i)->name())) { + // Strip feature imports for editions codegen tests. + continue; + } + printer->Print("$full_reflection_class_name$.Descriptor, ", + "full_reflection_class_name", + GetReflectionClassName(file_->dependency(i))); } printer->Print("},\n" " new pbr::GeneratedClrTypeInfo("); diff --git a/src/google/protobuf/compiler/rust/generator.cc b/src/google/protobuf/compiler/rust/generator.cc index 52a120f409..0f44c88a8c 100644 --- a/src/google/protobuf/compiler/rust/generator.cc +++ b/src/google/protobuf/compiler/rust/generator.cc @@ -215,6 +215,11 @@ bool RustGenerator::Generate(const FileDescriptor* file, {"proto_deps_h", [&] { for (int i = 0; i < file->dependency_count(); i++) { + if (opts->strip_nonfunctional_codegen && + IsKnownFeatureProto(file->dependency(i)->name())) { + // Strip feature imports for editions codegen tests. + continue; + } thunks_printer->Emit( {{"proto_dep_h", GetHeaderFile(ctx, *file->dependency(i))}}, R"cc( diff --git a/upb_generator/BUILD b/upb_generator/BUILD index c9ce196467..43be436b74 100644 --- a/upb_generator/BUILD +++ b/upb_generator/BUILD @@ -103,6 +103,7 @@ bootstrap_cc_library( visibility = ["//upb:friend_generators"], deps = [ ":mangle", + "//src/google/protobuf:port", "//upb:mini_table", "//upb:port", "@com_google_absl//absl/strings", @@ -198,6 +199,7 @@ cc_library( "//upb:friends", ], deps = [ + "//src/google/protobuf:port", "@com_google_absl//absl/strings", ], ) @@ -273,6 +275,7 @@ bootstrap_cc_library( copts = UPB_DEFAULT_CPPOPTS, visibility = ["//pkg:__pkg__"], deps = [ + "//src/google/protobuf/compiler:code_generator", "//upb:base", "//upb:mem", "//upb:mini_table", @@ -313,6 +316,7 @@ bootstrap_cc_library( copts = UPB_DEFAULT_CPPOPTS, visibility = ["//upb:friends"], deps = [ + "//src/google/protobuf/compiler:code_generator", "//upb:base", "//upb:mem", "//upb:mini_table", diff --git a/upb_generator/common.h b/upb_generator/common.h index ce92f4f63f..2afa005d9f 100644 --- a/upb_generator/common.h +++ b/upb_generator/common.h @@ -15,6 +15,9 @@ #include "absl/strings/substitute.h" #include "upb/reflection/def.hpp" +// Must be last. +#include "google/protobuf/port_def.inc" + namespace upb { namespace generator { @@ -59,7 +62,7 @@ std::string StripExtension(absl::string_view fname); std::string ToCIdent(absl::string_view str); std::string ToPreproc(absl::string_view str); void EmitFileWarning(absl::string_view name, Output& output); -std::string MessageInit(absl::string_view full_name); +PROTOC_EXPORT std::string MessageInit(absl::string_view full_name); std::string MessageInitName(upb::MessageDefPtr descriptor); std::string MessageName(upb::MessageDefPtr descriptor); std::string FileLayoutName(upb::FileDefPtr file); @@ -81,4 +84,6 @@ std::string GetFieldRep(const upb_MiniTableField* field32, } // namespace generator } // namespace upb +#include "google/protobuf/port_undef.inc" + #endif // UPB_GENERATOR_COMMON_H diff --git a/upb_generator/mangle.h b/upb_generator/mangle.h index de8c804de2..04b7c9864c 100644 --- a/upb_generator/mangle.h +++ b/upb_generator/mangle.h @@ -5,12 +5,17 @@ #include "absl/strings/string_view.h" +// Must be last. +#include "google/protobuf/port_def.inc" + namespace upb { namespace generator { -std::string MessageInit(absl::string_view full_name); +PROTOC_EXPORT std::string MessageInit(absl::string_view full_name); } // namespace generator } // namespace upb +#include "google/protobuf/port_undef.inc" + #endif // THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_ diff --git a/upb_generator/protoc-gen-upb.cc b/upb_generator/protoc-gen-upb.cc index f1ef7e062c..10b51f0dfa 100644 --- a/upb_generator/protoc-gen-upb.cc +++ b/upb_generator/protoc-gen-upb.cc @@ -25,6 +25,7 @@ #include "absl/strings/str_replace.h" #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" +#include "google/protobuf/compiler/code_generator.h" #include "upb/base/descriptor_constants.h" #include "upb/base/status.hpp" #include "upb/base/string_view.h" @@ -45,6 +46,7 @@ namespace { struct Options { bool bootstrap = false; + bool strip_nonfunctional_codegen = false; }; std::string SourceFilename(upb::FileDefPtr file) { @@ -894,11 +896,14 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, if (!options.bootstrap) { output("#include \"$0\"\n\n", MiniTableHeaderFilename(file)); for (int i = 0; i < file.dependency_count(); i++) { + if (options.strip_nonfunctional_codegen && + google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) { + // Strip feature imports for editions codegen tests. + continue; + } output("#include \"$0\"\n", MiniTableHeaderFilename(file.dependency(i))); } - if (file.dependency_count() > 0) { - output("\n"); - } + output("\n"); } output( @@ -1107,6 +1112,10 @@ void WriteMiniDescriptorSource(const DefPoolPair& pools, upb::FileDefPtr file, CApiHeaderFilename(file)); for (int i = 0; i < file.dependency_count(); i++) { + if (options.strip_nonfunctional_codegen && + google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) { + continue; + } output("#include \"$0\"\n", CApiHeaderFilename(file.dependency(i))); } @@ -1155,7 +1164,7 @@ bool ParseOptions(Plugin* plugin, Options* options) { if (pair.first == "bootstrap_upb") { options->bootstrap = true; } else if (pair.first == "experimental_strip_nonfunctional_codegen") { - continue; + options->strip_nonfunctional_codegen = true; } else { plugin->SetError(absl::Substitute("Unknown parameter: $0", pair.first)); return false; diff --git a/upb_generator/protoc-gen-upb_minitable-main.cc b/upb_generator/protoc-gen-upb_minitable-main.cc index e58d3edea1..2073f077ff 100644 --- a/upb_generator/protoc-gen-upb_minitable-main.cc +++ b/upb_generator/protoc-gen-upb_minitable-main.cc @@ -50,7 +50,7 @@ void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file, bool ParseOptions(MiniTableOptions* options, Plugin* plugin) { for (const auto& pair : ParseGeneratorParameter(plugin->parameter())) { if (pair.first == "experimental_strip_nonfunctional_codegen") { - continue; + options->strip_nonfunctional_codegen = true; } else if (pair.first == "one_output_per_message") { options->one_output_per_message = true; } else { diff --git a/upb_generator/protoc-gen-upb_minitable.cc b/upb_generator/protoc-gen-upb_minitable.cc index c5fffaed5c..333ab2ebd1 100644 --- a/upb_generator/protoc-gen-upb_minitable.cc +++ b/upb_generator/protoc-gen-upb_minitable.cc @@ -24,6 +24,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" +#include "google/protobuf/compiler/code_generator.h" #include "upb/base/descriptor_constants.h" #include "upb/mini_table/enum.h" #include "upb/mini_table/field.h" @@ -550,7 +551,9 @@ void WriteMiniTableHeader(const DefPoolPair& pools, upb::FileDefPtr file, ToPreproc(file.name())); } -void WriteMiniTableSourceIncludes(upb::FileDefPtr file, Output& output) { +void WriteMiniTableSourceIncludes(upb::FileDefPtr file, + const MiniTableOptions& options, + Output& output) { EmitFileWarning(file.name(), output); output( @@ -560,6 +563,11 @@ void WriteMiniTableSourceIncludes(upb::FileDefPtr file, Output& output) { MiniTableHeaderFilename(file)); for (int i = 0; i < file.dependency_count(); i++) { + if (options.strip_nonfunctional_codegen && + google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) { + // Strip feature imports for editions codegen tests. + continue; + } output("#include \"$0\"\n", MiniTableHeaderFilename(file.dependency(i))); } @@ -576,7 +584,7 @@ void WriteMiniTableSourceIncludes(upb::FileDefPtr file, Output& output) { void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file, const MiniTableOptions& options, Output& output) { - WriteMiniTableSourceIncludes(file, output); + WriteMiniTableSourceIncludes(file, options, output); std::vector messages = SortedMessages(file); std::vector extensions = SortedExtensions(file); @@ -673,21 +681,21 @@ void WriteMiniTableMultipleSources(const DefPoolPair& pools, for (auto message : messages) { Output output; - WriteMiniTableSourceIncludes(file, output); + WriteMiniTableSourceIncludes(file, options, output); WriteMessage(message, pools, options, output); plugin->AddOutputFile(MultipleSourceFilename(file, message.full_name(), &i), output.output()); } for (const auto e : enums) { Output output; - WriteMiniTableSourceIncludes(file, output); + WriteMiniTableSourceIncludes(file, options, output); WriteEnum(e, output); plugin->AddOutputFile(MultipleSourceFilename(file, e.full_name(), &i), output.output()); } for (const auto ext : extensions) { Output output; - WriteMiniTableSourceIncludes(file, output); + WriteMiniTableSourceIncludes(file, options, output); WriteExtension(pools, ext, output); plugin->AddOutputFile(MultipleSourceFilename(file, ext.full_name(), &i), output.output()); diff --git a/upb_generator/protoc-gen-upb_minitable.h b/upb_generator/protoc-gen-upb_minitable.h index cbc3e8de19..5023771ea0 100644 --- a/upb_generator/protoc-gen-upb_minitable.h +++ b/upb_generator/protoc-gen-upb_minitable.h @@ -26,6 +26,7 @@ namespace generator { struct MiniTableOptions { bool one_output_per_message = false; + bool strip_nonfunctional_codegen = false; }; void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file,