Skip to content

Commit

Permalink
Migrate all upb plugins over to the C++ protoc plugin framework
Browse files Browse the repository at this point in the history
We need the minitable plugin to use the C++ plugin framework because it's soon
going to live in the same binary as the Rust code generator. The other plugins
we might as well move just for consistency, and this allows us to delete the
`upb::Plugin` class as well.

PiperOrigin-RevId: 716234662
  • Loading branch information
acozzette authored and copybara-github committed Jan 16, 2025
1 parent cafeaa4 commit db86acf
Show file tree
Hide file tree
Showing 21 changed files with 328 additions and 234 deletions.
2 changes: 1 addition & 1 deletion benchmarks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ cc_test(
":benchmark_descriptor_sv_cc_proto",
":benchmark_descriptor_upb_proto",
":benchmark_descriptor_upb_proto_reflection",
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf/json",
"//upb:base",
"//upb:json",
Expand Down
1 change: 1 addition & 0 deletions cmake/upb_generators.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ foreach(generator upb upbdefs upb_minitable)
)
target_include_directories(protoc-gen-${generator} PRIVATE ${bootstrap_cmake_dir})
target_link_libraries(protoc-gen-${generator}
utf8_validity
${protobuf_LIB_UPB}
${protobuf_ABSL_USED_TARGETS}
)
Expand Down
3 changes: 1 addition & 2 deletions editions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ cc_test(
],
deps = [
":defaults_test_embedded",
"//:protobuf",
"//java/core:java_features_cc_proto",
"//src/google/protobuf",
"//src/google/protobuf:cpp_features_cc_proto",
Expand Down Expand Up @@ -310,7 +309,7 @@ cc_test(
":test_editions_default_features_cc_proto",
":test_messages_proto2_editions_cc_proto",
":test_messages_proto3_editions_cc_proto",
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf:test_textproto",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
Expand Down
9 changes: 9 additions & 0 deletions src/google/protobuf/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ cc_library(
],
)

cc_library(
name = "plugin",
hdrs = ["plugin.h"],
copts = COPTS,
strip_include_prefix = "/src",
visibility = ["//visibility:public"],
deps = [":code_generator"],
)

cc_library(
name = "code_generator_lite",
srcs = ["code_generator_lite.cc"],
Expand Down
6 changes: 3 additions & 3 deletions upb/message/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ cc_test(
srcs = ["accessors_test.cc"],
deps = [
":message",
"//:protobuf",
"//src/google/protobuf",
"//upb:base",
"//upb:mem",
"//upb:mini_descriptor",
Expand Down Expand Up @@ -312,7 +312,7 @@ cc_test(
":copy",
":internal",
":message",
"//:protobuf",
"//src/google/protobuf",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
Expand Down Expand Up @@ -347,7 +347,7 @@ cc_test(
":internal",
":message",
":promote",
"//:protobuf",
"//src/google/protobuf",
"//upb:base",
"//upb:mem",
"//upb:mini_descriptor",
Expand Down
2 changes: 1 addition & 1 deletion upb/mini_descriptor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cc_test(
deps = [
":internal",
":mini_descriptor",
"//:protobuf",
"//src/google/protobuf",
"//upb:base",
"//upb:mem",
"//upb:message",
Expand Down
2 changes: 1 addition & 1 deletion upb/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cc_library(
hdrs = ["parse_text_proto.h"],
visibility = ["//upb:__subpackages__"],
deps = [
"//:protobuf",
"//src/google/protobuf",
"@com_google_googletest//:gtest",
],
)
Expand Down
4 changes: 2 additions & 2 deletions upb/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ cc_library(
hdrs = ["def_to_proto_test.h"],
deps = [
":def_to_proto",
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf:descriptor_upb_c_proto",
"//src/google/protobuf/util:differencer",
"//upb:base",
Expand All @@ -70,7 +70,7 @@ cc_test(
":def_to_proto_test_lib",
":def_to_proto_test_upb_proto",
":def_to_proto_test_upb_proto_reflection",
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf:descriptor_upb_c_proto",
"//src/google/protobuf:descriptor_upb_reflection_proto",
"//src/google/protobuf/util:differencer",
Expand Down
5 changes: 5 additions & 0 deletions upb_generator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,22 @@ bootstrap_cc_library(

bootstrap_cc_library(
name = "plugin",
srcs = [
"plugin.cc",
],
hdrs = [
"plugin.h",
],
bootstrap_deps = [
":plugin_upb_proto",
"//upb/reflection:descriptor_upb_proto",
"//upb/reflection:reflection",
"//upb_generator:file_layout",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//upb:friend_generators"],
deps = [
"//src/google/protobuf",
"//src/google/protobuf/compiler:code_generator_lite",
"//upb:base",
"//upb:mem",
Expand Down
5 changes: 4 additions & 1 deletion upb_generator/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ bootstrap_cc_library(
"//upb_generator:common",
"//upb_generator:file_layout",
"//upb_generator:plugin",
"//upb_generator:plugin_upb_proto",
"//upb/reflection:descriptor_upb_proto",
"//upb/reflection:reflection",
],
Expand All @@ -71,6 +70,8 @@ bootstrap_cc_library(
deps = [
":names",
":names_internal",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:plugin",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
Expand All @@ -84,7 +85,9 @@ bootstrap_cc_library(
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
],
)

Expand Down
93 changes: 64 additions & 29 deletions upb_generator/c/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@
#include <vector>

#include "absl/base/macros.h"
#include "absl/container/flat_hash_set.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/memory/memory.h"
#include "absl/strings/cord.h"
#include "absl/strings/escaping.h"
#include "absl/strings/match.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#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 "google/protobuf/compiler/plugin.h"
#include "upb/base/descriptor_constants.h"
#include "upb/base/status.hpp"
#include "upb/base/string_view.h"
#include "upb/mem/arena.hpp"
#include "upb/mini_table/field.h"
#include "upb/reflection/def.hpp"
#include "upb_generator/c/names.h"
Expand Down Expand Up @@ -1148,67 +1154,96 @@ void WriteMiniDescriptorSource(const DefPoolPair& pools, upb::FileDefPtr file,
}

void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file,
const Options& options, Plugin* plugin) {
const Options& options,
google::protobuf::compiler::GeneratorContext* context) {
Output h_output;
WriteHeader(pools, file, options, h_output);
plugin->AddOutputFile(CApiHeaderFilename(file.name(), false),
h_output.output());
{
auto stream =
absl::WrapUnique(context->Open(CApiHeaderFilename(file.name(), false)));
ABSL_CHECK(stream->WriteCord(absl::Cord(h_output.output())));
}

if (options.bootstrap_stage == 0) {
Output c_output;
WriteMiniDescriptorSource(pools, file, options, c_output);
plugin->AddOutputFile(SourceFilename(file), c_output.output());
auto stream = absl::WrapUnique(context->Open(SourceFilename(file)));
ABSL_CHECK(stream->WriteCord(absl::Cord(c_output.output())));
} else {
// TODO: remove once we can figure out how to make both Blaze
// and Bazel happy with header-only libraries.

plugin->AddOutputFile(SourceFilename(file), "\n");
auto stream = absl::WrapUnique(context->Open(SourceFilename(file)));
ABSL_CHECK(stream->WriteCord(absl::Cord("\n")));
}
}

bool ParseOptions(Plugin* plugin, Options* options) {
for (const auto& pair : ParseGeneratorParameter(plugin->parameter())) {
bool ParseOptions(absl::string_view parameter, Options* options,
std::string* error) {
for (const auto& pair : ParseGeneratorParameter(parameter)) {
if (pair.first == "bootstrap_stage") {
if (!absl::SimpleAtoi(pair.second, &options->bootstrap_stage)) {
plugin->SetError(absl::Substitute("Bad stage: $0", pair.second));
*error = absl::Substitute("Bad stage: $0", pair.second);
return false;
}
} else if (pair.first == "experimental_strip_nonfunctional_codegen") {
options->strip_nonfunctional_codegen = true;
} else {
plugin->SetError(absl::Substitute("Unknown parameter: $0", pair.first));
*error = absl::Substitute("Unknown parameter: $0", pair.first);
return false;
}
}

return true;
}

absl::string_view ToStringView(upb_StringView str) {
return absl::string_view(str.data, str.size);
}
class CGenerator : public google::protobuf::compiler::CodeGenerator {
bool Generate(const google::protobuf::FileDescriptor* file,
const std::string& parameter,
google::protobuf::compiler::GeneratorContext* generator_context,
std::string* error) const override {
std::vector<const google::protobuf::FileDescriptor*> files{file};
return GenerateAll(files, parameter, generator_context, error);
}

bool GenerateAll(const std::vector<const google::protobuf::FileDescriptor*>& files,
const std::string& parameter,
google::protobuf::compiler::GeneratorContext* generator_context,
std::string* error) const override {
Options options;
if (!ParseOptions(parameter, &options, error)) {
return false;
}

upb::Arena arena;
DefPoolPair pools;
absl::flat_hash_set<std::string> files_seen;
for (const auto* file : files) {
PopulateDefPool(file, &arena, &pools, &files_seen);
upb::FileDefPtr upb_file = pools.GetFile(file->name());
GenerateFile(pools, upb_file, options, generator_context);
}

return true;
}

uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS;
}
google::protobuf::Edition GetMinimumEdition() const override {
return google::protobuf::Edition::EDITION_PROTO2;
}
google::protobuf::Edition GetMaximumEdition() const override {
return google::protobuf::Edition::EDITION_2023;
}
};

} // namespace

} // namespace generator
} // namespace upb

int main(int argc, char** argv) {
upb::generator::DefPoolPair pools;
upb::generator::Plugin plugin;
upb::generator::Options options;
if (!ParseOptions(&plugin, &options)) return 0;
plugin.GenerateFilesRaw(
[&](const UPB_DESC(FileDescriptorProto) * file_proto, bool generate) {
upb::Status status;
upb::FileDefPtr file = pools.AddFile(file_proto, &status);
if (!file) {
absl::string_view name = upb::generator::ToStringView(
UPB_DESC(FileDescriptorProto_name)(file_proto));
ABSL_LOG(FATAL) << "Couldn't add file " << name
<< " to DefPool: " << status.error_message();
}
if (generate) GenerateFile(pools, file, options, &plugin);
});
return 0;
upb::generator::CGenerator generator;
return google::protobuf::compiler::PluginMain(argc, argv, &generator);
}
1 change: 0 additions & 1 deletion upb_generator/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ cc_library(
"//third_party/kotlin/protobuf/generator/native:__subpackages__",
],
deps = [
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf:descriptor_upb_c_proto",
"//upb:base",
Expand Down
4 changes: 4 additions & 0 deletions upb_generator/file_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class DefPoolPair {
return file64;
}

upb::FileDefPtr GetFile(absl::string_view name) const {
return pool64_.FindFileByName(name.data());
}

const upb_MiniTable* GetMiniTable32(upb::MessageDefPtr m) const {
return pool32_.FindMessageByName(m.full_name()).mini_table();
}
Expand Down
11 changes: 9 additions & 2 deletions upb_generator/minitable/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ bootstrap_cc_library(
bootstrap_deps = [
"//upb_generator:common",
"//upb_generator:file_layout",
"//upb_generator:plugin",
"//upb_generator:plugin_upb_proto",
"//upb/reflection:descriptor_upb_proto",
"//upb/reflection:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":names",
":names_internal",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:code_generator_lite",
"//upb:base",
"//upb:mem",
Expand All @@ -49,7 +48,9 @@ bootstrap_cc_library(
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
],
)

Expand Down Expand Up @@ -115,13 +116,19 @@ bootstrap_cc_library(
deps = [
":names",
":names_internal",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:code_generator_lite",
"//src/google/protobuf/compiler:plugin",
"//upb:base",
"//upb:mem",
"//upb:port",
"//upb_generator/common:names",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
],
)

Expand Down
Loading

0 comments on commit db86acf

Please sign in to comment.