Skip to content

Commit fea6d97

Browse files
acozzettecopybara-github
authored andcommitted
Move the upb minitable generator back out of protoc
PiperOrigin-RevId: 761649284
1 parent 0be8c0b commit fea6d97

File tree

15 files changed

+149
-177
lines changed

15 files changed

+149
-177
lines changed

cmake/install.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ if (protobuf_BUILD_PROTOC_BINARIES)
6161
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc
6262
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)
6363
if (protobuf_BUILD_LIBUPB)
64-
foreach (generator upb upbdefs)
64+
foreach (generator upb upbdefs upb_minitable)
6565
list(APPEND _protobuf_binaries protoc-gen-${generator})
6666
install(TARGETS protoc-gen-${generator} EXPORT protobuf-targets
6767
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT upb-generators

cmake/installed_bin_golden.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
protoc
22
protoc-gen-upb
3+
protoc-gen-upb_minitable
34
protoc-gen-upbdefs

cmake/tests.cmake

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ add_test(NAME lazily-build-dependencies-test
198198
if (protobuf_BUILD_LIBUPB)
199199
set(upb_test_proto_genfiles)
200200
foreach(proto_file ${upb_test_protos_files} ${descriptor_proto_proto_srcs})
201-
foreach(generator upb upbdefs)
201+
foreach(generator upb upbdefs upb_minitable)
202202
protobuf_generate(
203203
PROTOS ${proto_file}
204204
LANGUAGE ${generator}
@@ -211,18 +211,6 @@ if (protobuf_BUILD_LIBUPB)
211211
)
212212
set(upb_test_proto_genfiles ${upb_test_proto_genfiles} ${pb_generated_files})
213213
endforeach()
214-
215-
# The minitable generator has a slightly different setup from the other upb
216-
# generators, since it is built into protoc.
217-
protobuf_generate(
218-
PROTOS ${proto_file}
219-
LANGUAGE upb_minitable
220-
GENERATE_EXTENSIONS .upb_minitable.h .upb_minitable.c
221-
OUT_VAR pb_generated_files
222-
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
223-
IMPORT_DIRS ${protobuf_SOURCE_DIR}
224-
)
225-
set(upb_test_proto_genfiles ${upb_test_proto_genfiles} ${pb_generated_files})
226214
endforeach(proto_file)
227215

228216
add_executable(upb-test

cmake/upb_generators.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set(bootstrap_sources
88
${bootstrap_cmake_dir}/google/protobuf/compiler/plugin.upb_minitable.c
99
)
1010

11-
foreach(generator upb upbdefs)
11+
foreach(generator upb upbdefs upb_minitable)
1212
add_executable(protoc-gen-${generator}
1313
${protoc-gen-${generator}_srcs}
1414
${protoc-gen-${generator}_hdrs}

pkg/BUILD.bazel

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ gen_file_lists(
115115
":upb": "libupb",
116116
":protoc-gen-upb": "protoc-gen-upb",
117117
":protoc-gen-upbdefs": "protoc-gen-upbdefs",
118+
":protoc-gen-upb_minitable": "protoc-gen-upb_minitable",
118119
# Protos:
119120
"//src/google/protobuf:well_known_type_protos": "wkt_protos",
120121
"//src/google/protobuf:cpp_features_proto": "cpp_features_proto",
@@ -211,7 +212,6 @@ cc_dist_library(
211212
"//src/google/protobuf/compiler/python",
212213
"//src/google/protobuf/compiler/ruby",
213214
"//src/google/protobuf/compiler/rust",
214-
"//upb_generator/minitable:generator",
215215
],
216216
)
217217

@@ -282,6 +282,17 @@ cc_dist_library(
282282
],
283283
)
284284

285+
cc_dist_library(
286+
name = "protoc-gen-upb_minitable",
287+
dist_deps = [
288+
":upb",
289+
],
290+
tags = ["manual"],
291+
deps = [
292+
"//upb_generator/minitable:protoc-gen-upb_minitable",
293+
],
294+
)
295+
285296
cc_dist_library(
286297
name = "conformance_cpp",
287298
testonly = 1,

rust/release_crates/protobuf_codegen/Cargo-template.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@ description = "Code generator for Rust Protocol Buffers bindings"
77
license = "BSD-3-Clause"
88
rust-version = "1.79"
99

10-
11-
[dependencies]
12-
cc = "1.1.6"
13-
1410
[dev-dependencies]
1511
googletest = "0.13.0"

rust/release_crates/protobuf_codegen/src/lib.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,6 @@ impl CodeGen {
118118
.collect()
119119
}
120120

121-
fn expected_generated_c_files(&self) -> Vec<PathBuf> {
122-
self.inputs
123-
.iter()
124-
.map(|input| {
125-
let mut input = input.clone();
126-
assert!(input.set_extension("upb_minitable.c"));
127-
self.output_dir.join(input)
128-
})
129-
.collect()
130-
}
131-
132121
fn generate_crate_mapping_file(&self) -> PathBuf {
133122
let crate_mapping_path = self.output_dir.join("crate_mapping.txt");
134123
let mut file = File::create(crate_mapping_path.clone()).unwrap();
@@ -186,8 +175,7 @@ impl CodeGen {
186175
let crate_mapping_path = self.generate_crate_mapping_file();
187176

188177
cmd.arg(format!("--rust_out={}", self.output_dir.display()))
189-
.arg("--rust_opt=experimental-codegen=enabled,kernel=upb")
190-
.arg(format!("--upb_minitable_out={}", self.output_dir.display()));
178+
.arg("--rust_opt=experimental-codegen=enabled,kernel=upb");
191179
for include in &self.includes {
192180
cmd.arg(format!("--proto_path={}", include.display()));
193181
}
@@ -201,40 +189,14 @@ impl CodeGen {
201189
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
202190
eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap());
203191
assert!(output.status.success());
204-
self.compile_only()
205-
}
206-
207-
/// Builds and links the C code.
208-
pub fn compile_only(&self) -> Result<(), String> {
209-
let mut cc_build = cc::Build::new();
210-
cc_build
211-
.include(
212-
std::env::var_os("DEP_UPB_INCLUDE")
213-
.expect("DEP_UPB_INCLUDE should have been set, make sure that the Protobuf crate is a dependency"),
214-
)
215-
.include(self.output_dir.clone())
216-
.flag("-std=c99");
217-
218-
for dep in &self.dependencies {
219-
for path in &dep.c_include_paths {
220-
cc_build.include(path);
221-
}
222-
}
223192

224193
for path in &self.expected_generated_rs_files() {
225194
if !path.exists() {
226195
return Err(format!("expected generated file {} does not exist", path.display()));
227196
}
228197
println!("cargo:rerun-if-changed={}", path.display());
229198
}
230-
for path in &self.expected_generated_c_files() {
231-
if !path.exists() {
232-
return Err(format!("expected generated file {} does not exist", path.display()));
233-
}
234-
println!("cargo:rerun-if-changed={}", path.display());
235-
cc_build.file(path);
236-
}
237-
cc_build.compile(&format!("{}_upb_gen_code", std::env::var("CARGO_PKG_NAME").unwrap()));
199+
238200
Ok(())
239201
}
240202
}

src/google/protobuf/compiler/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ bootstrap_cc_library(
224224
],
225225
bootstrap_deps = [
226226
"//src/google/protobuf/compiler/rust:rust",
227-
"//upb_generator/minitable:generator",
228227
],
229228
copts = COPTS,
230229
visibility = [

src/google/protobuf/compiler/main.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "google/protobuf/compiler/python/pyi_generator.h"
1818
#include "google/protobuf/compiler/ruby/ruby_generator.h"
1919
#include "google/protobuf/compiler/rust/generator.h"
20-
#include "upb_generator/minitable/generator.h"
2120

2221
#ifdef DISABLE_PROTOC_CONFIG
2322
#include "google/protobuf/compiler/allowlists/allowlist.h"
@@ -107,11 +106,6 @@ int ProtobufMain(int argc, char* argv[]) {
107106
rust::RustGenerator rust_generator;
108107
cli.RegisterGenerator("--rust_out", "--rust_opt", &rust_generator,
109108
"Generate Rust sources.");
110-
111-
// upb minitables
112-
upb::generator::MiniTableGenerator minitable_generator;
113-
cli.RegisterGenerator("--upb_minitable_out", "--upb_minitable_opt",
114-
&minitable_generator, "Generate upb minitables");
115109
#ifdef DISABLE_PROTOC_CONFIG
116110
auto cleanup = internal::DisableAllowlistInternalOnly();
117111
#endif // DISABLE_PROTOC_CONFIG
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2025, Google LLC
2+
# All rights reserved.
3+
#
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file or at
6+
# https://developers.google.com/open-source/licenses/bsd
7+
8+
package(
9+
default_visibility = ["//:__subpackages__"],
10+
)
11+
12+
alias(
13+
name = "protoc_minimal",
14+
actual = "//src/google/protobuf/compiler:protoc_minimal",
15+
)

upb_generator/bootstrap_compiler.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ load(
1414
)
1515

1616
_stages = ["_stage0", "_stage1", ""]
17-
18-
_protoc = "//:protoc_stage0"
17+
_protoc = "//src/google/protobuf/compiler/release:protoc_minimal"
1918

2019
_extra_proto_path = "-I$$(dirname $(location //:descriptor_proto_srcs))/../.. "
2120

upb_generator/minitable/BUILD

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,13 @@ bootstrap_cc_library(
2626
"//upb_generator:file_layout",
2727
"//upb/reflection:descriptor_upb_proto",
2828
"//upb/reflection:reflection",
29-
"//upb_generator:plugin",
3029
],
3130
copts = UPB_DEFAULT_CPPOPTS,
32-
visibility = [
33-
"//pkg:__pkg__",
34-
"//src/google/protobuf/compiler:__pkg__",
35-
],
3631
deps = [
3732
":names",
3833
":names_internal",
39-
"//src/google/protobuf:port",
4034
"//src/google/protobuf/compiler:code_generator",
4135
"//src/google/protobuf/compiler:code_generator_lite",
42-
"//src/google/protobuf/compiler:plugin",
4336
"//upb:base",
4437
"//upb:mem",
4538
"//upb:mini_table",
@@ -103,6 +96,7 @@ bootstrap_cc_binary(
10396
visibility = [
10497
"//editions/codegen_tests:__pkg__",
10598
"//net/proto2/contrib/protoc_explorer:__pkg__",
99+
"//pkg:__pkg__",
106100
"//rust:__subpackages__",
107101
"//third_party/prototiller/transformer:__pkg__",
108102
],

upb_generator/minitable/generator.cc

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include "absl/strings/string_view.h"
2323
#include "absl/strings/substitute.h"
2424
#include "google/protobuf/compiler/code_generator.h"
25-
#include "google/protobuf/compiler/code_generator_lite.h"
26-
#include "upb/mem/arena.hpp"
2725
#include "upb/mini_table/enum.h"
2826
#include "upb/mini_table/field.h"
2927
#include "upb/mini_table/internal/field.h"
@@ -35,7 +33,6 @@
3533
#include "upb_generator/file_layout.h"
3634
#include "upb_generator/minitable/names.h"
3735
#include "upb_generator/minitable/names_internal.h"
38-
#include "upb_generator/plugin.h"
3936

4037
// Must be last.
4138
#include "upb/port/def.inc"
@@ -468,72 +465,5 @@ void WriteMiniTableMultipleSources(
468465
}
469466
}
470467

471-
std::string SourceFilename(upb::FileDefPtr file) {
472-
return StripExtension(file.name()) + ".upb_minitable.c";
473-
}
474-
475-
void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file,
476-
const MiniTableOptions& options,
477-
google::protobuf::compiler::GeneratorContext* context) {
478-
Output h_output;
479-
WriteMiniTableHeader(pools, file, options, h_output);
480-
{
481-
auto stream = absl::WrapUnique(
482-
context->Open(MiniTableHeaderFilename(file.name(), false)));
483-
ABSL_CHECK(stream->WriteCord(absl::Cord(h_output.output())));
484-
}
485-
486-
Output c_output;
487-
WriteMiniTableSource(pools, file, options, c_output);
488-
{
489-
auto stream = absl::WrapUnique(context->Open(SourceFilename(file)));
490-
ABSL_CHECK(stream->WriteCord(absl::Cord(c_output.output())));
491-
}
492-
493-
if (options.one_output_per_message) {
494-
WriteMiniTableMultipleSources(pools, file, options, context);
495-
}
496-
}
497-
498-
bool ParseOptions(MiniTableOptions* options, absl::string_view parameter,
499-
std::string* error) {
500-
for (const auto& pair : ParseGeneratorParameter(parameter)) {
501-
if (pair.first == "bootstrap_stage") {
502-
options->bootstrap = true;
503-
} else if (pair.first == "experimental_strip_nonfunctional_codegen") {
504-
options->strip_nonfunctional_codegen = true;
505-
} else if (pair.first == "one_output_per_message") {
506-
options->one_output_per_message = true;
507-
} else {
508-
*error = absl::Substitute("Unknown parameter: $0", pair.first);
509-
return false;
510-
}
511-
}
512-
513-
return true;
514-
}
515-
516-
bool MiniTableGenerator::GenerateAll(
517-
const std::vector<const google::protobuf::FileDescriptor*>& files,
518-
const std::string& parameter,
519-
google::protobuf::compiler::GeneratorContext* generator_context,
520-
std::string* error) const {
521-
MiniTableOptions options;
522-
if (!ParseOptions(&options, parameter, error)) {
523-
return false;
524-
}
525-
526-
upb::Arena arena;
527-
DefPoolPair pools;
528-
absl::flat_hash_set<std::string> files_seen;
529-
for (const auto* file : files) {
530-
PopulateDefPool(file, &arena, &pools, &files_seen);
531-
upb::FileDefPtr upb_file = pools.GetFile(file->name());
532-
GenerateFile(pools, upb_file, options, generator_context);
533-
}
534-
535-
return true;
536-
}
537-
538468
} // namespace generator
539469
} // namespace upb

upb_generator/minitable/generator.h

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include "upb_generator/common.h"
1111
#include "upb_generator/file_layout.h"
1212

13-
// Must be included last.
14-
#include "google/protobuf/port_def.inc"
15-
1613
namespace upb {
1714
namespace generator {
1815

@@ -31,33 +28,5 @@ void WriteMiniTableMultipleSources(const DefPoolPair& pools,
3128
void WriteMiniTableHeader(const DefPoolPair& pools, upb::FileDefPtr file,
3229
const MiniTableOptions& options, Output& output);
3330

34-
class PROTOC_EXPORT MiniTableGenerator
35-
: public google::protobuf::compiler::CodeGenerator {
36-
bool Generate(const google::protobuf::FileDescriptor* file,
37-
const std::string& parameter,
38-
google::protobuf::compiler::GeneratorContext* generator_context,
39-
std::string* error) const override {
40-
std::vector<const google::protobuf::FileDescriptor*> files{file};
41-
return GenerateAll(files, parameter, generator_context, error);
42-
}
43-
44-
bool GenerateAll(const std::vector<const google::protobuf::FileDescriptor*>& files,
45-
const std::string& parameter,
46-
google::protobuf::compiler::GeneratorContext* generator_context,
47-
std::string* error) const override;
48-
49-
uint64_t GetSupportedFeatures() const override {
50-
return FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS;
51-
}
52-
google::protobuf::Edition GetMinimumEdition() const override {
53-
return google::protobuf::Edition::EDITION_PROTO2;
54-
}
55-
google::protobuf::Edition GetMaximumEdition() const override {
56-
return google::protobuf::Edition::EDITION_2023;
57-
}
58-
};
59-
6031
} // namespace generator
6132
} // namespace upb
62-
63-
#include "google/protobuf/port_undef.inc"

0 commit comments

Comments
 (0)