Moved the MiniTable generator into a separate directory.

This is the first in a series of changes intended to improve the code structure of the generator.

PiperOrigin-RevId: 666093310
pull/17870/head
Joshua Haberman 6 months ago committed by Copybara-Service
parent 582201a95e
commit bc394d2c6d
  1. 2
      bazel/upb_minitable_proto_library.bzl
  2. 2
      pkg/BUILD.bazel
  3. 1
      upb/reflection/BUILD
  4. 75
      upb_generator/BUILD
  5. 76
      upb_generator/bootstrap_compiler.bzl
  6. 91
      upb_generator/minitable/BUILD
  7. 2
      upb_generator/minitable/generator.cc
  8. 0
      upb_generator/minitable/generator.h
  9. 2
      upb_generator/minitable/main.cc

@ -45,7 +45,7 @@ upb_minitable_proto_library_aspect = aspect(
default = "//upb:upb_proto_library_copts__for_generated_code_only_do_not_use", default = "//upb:upb_proto_library_copts__for_generated_code_only_do_not_use",
), ),
"_upb_minitable_toolchain": attr.label( "_upb_minitable_toolchain": attr.label(
default = Label("//upb_generator:protoc-gen-upb_minitable_toolchain"), default = Label("//upb_generator/minitable:toolchain"),
), ),
"_cc_toolchain": attr.label( "_cc_toolchain": attr.label(
default = "@bazel_tools//tools/cpp:current_cc_toolchain", default = "@bazel_tools//tools/cpp:current_cc_toolchain",

@ -256,7 +256,7 @@ cc_dist_library(
], ],
tags = ["manual"], tags = ["manual"],
deps = [ deps = [
"//upb_generator:protoc-gen-upb_minitable_lib_lib", "//upb_generator/minitable:generator_with_main",
], ],
) )

@ -36,7 +36,6 @@ bootstrap_upb_proto_library(
oss_src_rules = ["//:descriptor_proto_srcs"], oss_src_rules = ["//:descriptor_proto_srcs"],
oss_strip_prefix = "third_party/protobuf/github/bootstrap/src", oss_strip_prefix = "third_party/protobuf/github/bootstrap/src",
proto_lib_deps = ["//:descriptor_proto"], proto_lib_deps = ["//:descriptor_proto"],
visibility = ["//visibility:public"],
) )
upb_proto_reflection_library( upb_proto_reflection_library(

@ -76,7 +76,6 @@ bootstrap_upb_proto_library(
], ],
oss_strip_prefix = "third_party/protobuf/github/bootstrap/src", oss_strip_prefix = "third_party/protobuf/github/bootstrap/src",
proto_lib_deps = ["//:compiler_plugin_proto"], proto_lib_deps = ["//:compiler_plugin_proto"],
visibility = ["//upb:friends"],
deps = ["//upb/reflection:descriptor_upb_proto"], deps = ["//upb/reflection:descriptor_upb_proto"],
) )
@ -299,80 +298,6 @@ proto_lang_toolchain(
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )
bootstrap_cc_library(
name = "protoc-gen-upb_minitable_lib",
srcs = ["protoc-gen-upb_minitable.cc"],
hdrs = ["protoc-gen-upb_minitable.h"],
bootstrap_deps = [
":common",
":file_layout",
":names",
":plugin",
":plugin_upb_proto",
"//upb/reflection:descriptor_upb_proto",
"//upb/reflection:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//upb:friends"],
deps = [
"//src/google/protobuf/compiler:code_generator",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
"//upb:port",
"//upb:wire_reader",
"//upb/mini_table:internal",
"@com_google_absl//absl/container:flat_hash_map",
"@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/strings",
],
)
bootstrap_cc_binary(
name = "protoc-gen-upb_minitable",
bootstrap_deps = [
":protoc-gen-upb_minitable_lib_lib",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
)
# TODO: This wrapper lib is a hack that we need because of how CcInfo works in Bazel 6.
# In Bazel 7, our cmake dependency scraping works fine with cc_binary.
bootstrap_cc_library(
name = "protoc-gen-upb_minitable_lib_lib",
srcs = ["protoc-gen-upb_minitable-main.cc"],
bootstrap_deps = [
":file_layout",
":common",
":plugin",
":protoc-gen-upb_minitable_lib",
"//upb/reflection:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//pkg:__pkg__"],
deps = [
"//upb:base",
"//upb:port",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings",
],
)
proto_lang_toolchain(
name = "protoc-gen-upb_minitable_toolchain",
command_line = "--upb_minitable_out=$(OUT)",
output_files = "multiple",
plugin = ":protoc-gen-upb_minitable_stage1",
plugin_format_flag = "--plugin=protoc-gen-upb_minitable=%s",
progress_message = "Generating upb minitables",
runtime = "//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
visibility = ["//visibility:public"],
)
cc_binary( cc_binary(
name = "protoc-gen-upbdefs", name = "protoc-gen-upbdefs",
linkopts = ["-lm"], linkopts = ["-lm"],

@ -18,7 +18,6 @@ load(
_stages = ["_stage0", "_stage1", ""] _stages = ["_stage0", "_stage1", ""]
_protoc = "//:protoc" _protoc = "//:protoc"
_upbc_base = "//upb_generator:protoc-gen-"
# begin:google_only # begin:google_only
# _is_google3 = True # _is_google3 = True
@ -30,24 +29,70 @@ _is_google3 = False
_extra_proto_path = "-I$$(dirname $(location @com_google_protobuf//:descriptor_proto_srcs))/../.. " _extra_proto_path = "-I$$(dirname $(location @com_google_protobuf//:descriptor_proto_srcs))/../.. "
# end:github_only # end:github_only
# This visibility is used automatically for anything used by the bootstrapping process.
_bootstrap_visibility = [
"//upb_generator:__subpackages__",
"//upb/reflection:__pkg__",
# begin:github_only
"//upb:__pkg__", # For the amalgamations.
"//python/dist:__pkg__", # For the Python source package.
# end:github_only
]
def _stage_visibility(stage, visibility):
return visibility if stage == "" else _bootstrap_visibility
def _upbc(generator, stage): def _upbc(generator, stage):
return _upbc_base + generator + _stages[stage] if generator == "upb":
return "//upb_generator:protoc-gen-upb" + _stages[stage]
else:
return "//upb_generator/minitable:protoc-gen-upb_minitable" + _stages[stage]
def bootstrap_cc_library(name, visibility = [], deps = [], bootstrap_deps = [], **kwargs):
"""A version of cc_library() that is augmented to allow for bootstrapping the compiler.
def bootstrap_cc_library(name, visibility, deps = [], bootstrap_deps = [], **kwargs): In addition to the normal cc_library() target, this rule will also generate _stage0 and _stage1
targets that are used internally for bootstrapping, and will automatically have bootstrap
visibility. However the final target will use the normal visibility, and will behave like a
normal cc_library() target.
Args:
name: Name of this rule. This name will resolve to a upb_proto_library().
deps: Normal cc_library() deps.
bootstrap_deps: Special bootstrap_upb_proto_library() or bootstrap_cc_library() deps.
visibility: Visibility of the final target.
**kwargs: Other arguments that will be passed through to cc_library().
upb_proto_library().
"""
for stage in _stages: for stage in _stages:
stage_visibility = visibility if stage == "" else ["//upb_generator:__pkg__"]
native.cc_library( native.cc_library(
name = name + stage, name = name + stage,
deps = deps + [dep + stage for dep in bootstrap_deps], deps = deps + [dep + stage for dep in bootstrap_deps],
visibility = stage_visibility, visibility = _stage_visibility(stage, visibility),
**kwargs **kwargs
) )
def bootstrap_cc_binary(name, deps = [], bootstrap_deps = [], **kwargs): def bootstrap_cc_binary(name, visibility = [], deps = [], bootstrap_deps = [], **kwargs):
"""A version of cc_binary() that is augmented to allow for bootstrapping the compiler.
In addition to the normal cc_binary() target, this rule will also generate _stage0 and _stage1
targets that are used internally for bootstrapping, and will automatically have bootstrap
visibility. However the final target will use the normal visibility, and will behave like a
normal cc_binary() target.
Args:
name: Name of this rule. This name will resolve to a upb_proto_library().
deps: Normal cc_library() deps.
bootstrap_deps: Special bootstrap_upb_proto_library() or bootstrap_cc_library() deps.
visibility: Visibility of the final target.
**kwargs: Other arguments that will be passed through to cc_binary().
upb_proto_library().
"""
for stage in _stages: for stage in _stages:
native.cc_binary( native.cc_binary(
name = name + stage, name = name + stage,
deps = deps + [dep + stage for dep in bootstrap_deps], deps = deps + [dep + stage for dep in bootstrap_deps],
visibility = _stage_visibility(stage, visibility),
**kwargs **kwargs
) )
@ -100,7 +145,7 @@ def _generate_stage1_proto(name, src_files, src_rules, generator, kwargs):
"=$(location " + _upbc(generator, 0) + ") " + _extra_proto_path + "=$(location " + _upbc(generator, 0) + ") " + _extra_proto_path +
"--" + generator + "_out=bootstrap_stage=1:$(RULEDIR)/stage1 " + "--" + generator + "_out=bootstrap_stage=1:$(RULEDIR)/stage1 " +
" ".join(src_files), " ".join(src_files),
visibility = ["//upb_generator:__pkg__"], visibility = _bootstrap_visibility,
tools = [ tools = [
_protoc, _protoc,
_upbc(generator, 0), _upbc(generator, 0),
@ -155,13 +200,18 @@ def bootstrap_upb_proto_library(
oss_src_rules, oss_src_rules,
oss_strip_prefix, oss_strip_prefix,
proto_lib_deps, proto_lib_deps,
visibility,
deps = [], deps = [],
**kwargs): **kwargs):
"""A version of upb_proto_library() that is augmented to allow for bootstrapping the compiler. """A version of upb_proto_library() that is augmented to allow for bootstrapping the compiler.
Note that this rule is only intended to be used by bootstrap_cc_library() targets. End users
should use the normal upb_proto_library() targets. As a result, we don't have a visibility
parameter: all targets will automatically have bootstrap visibility.
Args: Args:
name: Name of this rule. This name will resolve to a upb_proto_library(). name: Name of this rule. This name will resolve to a upb_proto_library().
bootstrap_hdr: The forwarding header that exposes the generated code, taking into account
the current stage.
google3_src_files: Google3 filenames of .proto files that should be built by this rule. google3_src_files: Google3 filenames of .proto files that should be built by this rule.
The names should be relative to the depot base. The names should be relative to the depot base.
google3_src_rules: Target names of the Blaze rules that will provide these filenames. google3_src_rules: Target names of the Blaze rules that will provide these filenames.
@ -170,8 +220,6 @@ def bootstrap_upb_proto_library(
oss_strip_prefix: Prefix that should be stripped from OSS file names. oss_strip_prefix: Prefix that should be stripped from OSS file names.
proto_lib_deps: proto_library() rules that we will use to build the protos when we are proto_lib_deps: proto_library() rules that we will use to build the protos when we are
not bootstrapping. not bootstrapping.
visibility: Visibility list for the final upb_proto_library() rule. Bootstrapping rules
will always be hidden, and will not honor the visibility parameter passed here.
deps: other bootstrap_upb_proto_library() rules that this one depends on. deps: other bootstrap_upb_proto_library() rules that this one depends on.
**kwargs: Other arguments that will be passed through to cc_library(), genrule(), and **kwargs: Other arguments that will be passed through to cc_library(), genrule(), and
upb_proto_library(). upb_proto_library().
@ -183,7 +231,7 @@ def bootstrap_upb_proto_library(
name = name + "_stage0", name = name + "_stage0",
srcs = _generated_hdrs_and_srcs(oss_src_files, "stage0", "upb"), srcs = _generated_hdrs_and_srcs(oss_src_files, "stage0", "upb"),
hdrs = [bootstrap_hdr], hdrs = [bootstrap_hdr],
visibility = ["//upb_generator:__pkg__"], visibility = _bootstrap_visibility,
defines = ["UPB_BOOTSTRAP_STAGE=0"], defines = ["UPB_BOOTSTRAP_STAGE=0"],
deps = [ deps = [
"//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", "//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
@ -203,7 +251,7 @@ def bootstrap_upb_proto_library(
name = name + "_minitable_stage1", name = name + "_minitable_stage1",
srcs = _generated_files(src_files, "stage1", "upb_minitable", "c"), srcs = _generated_files(src_files, "stage1", "upb_minitable", "c"),
hdrs = _generated_files(src_files, "stage1", "upb_minitable", "h"), hdrs = _generated_files(src_files, "stage1", "upb_minitable", "h"),
visibility = ["//upb_generator:__pkg__"], visibility = _bootstrap_visibility,
defines = ["UPB_BOOTSTRAP_STAGE=1"], defines = ["UPB_BOOTSTRAP_STAGE=1"],
deps = [ deps = [
"//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", "//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
@ -214,7 +262,7 @@ def bootstrap_upb_proto_library(
name = name + "_stage1", name = name + "_stage1",
srcs = _generated_files(src_files, "stage1", "upb", "h"), srcs = _generated_files(src_files, "stage1", "upb", "h"),
hdrs = [bootstrap_hdr], hdrs = [bootstrap_hdr],
visibility = ["//upb_generator:__pkg__"], visibility = _bootstrap_visibility,
defines = ["UPB_BOOTSTRAP_STAGE=1"], defines = ["UPB_BOOTSTRAP_STAGE=1"],
deps = [ deps = [
"//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me", "//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
@ -233,7 +281,7 @@ def bootstrap_upb_proto_library(
name = name, name = name,
hdrs = [bootstrap_hdr], hdrs = [bootstrap_hdr],
deps = [name + "_upb_proto"], deps = [name + "_upb_proto"],
visibility = visibility, visibility = _bootstrap_visibility,
**kwargs **kwargs
) )

@ -0,0 +1,91 @@
# Copyright (c) 2009-2024, Google LLC
# All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
load("//upb/bazel:build_defs.bzl", "UPB_DEFAULT_CPPOPTS")
load(
"//upb_generator:bootstrap_compiler.bzl",
"bootstrap_cc_binary",
"bootstrap_cc_library",
)
bootstrap_cc_library(
name = "generator",
srcs = ["generator.cc"],
hdrs = ["generator.h"],
bootstrap_deps = [
"//upb_generator:common",
"//upb_generator:file_layout",
"//upb_generator:names",
"//upb_generator:plugin",
"//upb_generator:plugin_upb_proto",
"//upb/reflection:descriptor_upb_proto",
"//upb/reflection:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
"//src/google/protobuf/compiler:code_generator",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
"//upb:port",
"//upb:wire_reader",
"//upb/mini_table:internal",
"@com_google_absl//absl/container:flat_hash_map",
"@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/strings",
],
)
bootstrap_cc_binary(
name = "protoc-gen-upb_minitable",
bootstrap_deps = [
":generator_with_main",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = [
"//editions/codegen_tests:__pkg__",
"//net/proto2/contrib/protoc_explorer:__pkg__",
"//third_party/prototiller/transformer:__pkg__",
],
)
# TODO: This wrapper lib is a hack that we need because of how CcInfo works in Bazel 6.
# In Bazel 7, our cmake dependency scraping works fine with cc_binary.
bootstrap_cc_library(
name = "generator_with_main",
srcs = ["main.cc"],
bootstrap_deps = [
"//upb_generator:file_layout",
"//upb_generator:common",
"//upb_generator:plugin",
":generator",
"//upb/reflection:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//pkg:__pkg__"],
deps = [
"//upb:base",
"//upb:port",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings",
],
)
proto_lang_toolchain(
name = "toolchain",
command_line = "--upb_minitable_out=$(OUT)",
output_files = "multiple",
plugin = ":protoc-gen-upb_minitable_stage1",
plugin_format_flag = "--plugin=protoc-gen-upb_minitable=%s",
progress_message = "Generating upb minitables",
runtime = "//upb:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
# TODO: Restrict to "//bazel:__pkg__" once we are on Bazel >=6.5.
visibility = ["//visibility:public"],
)

@ -5,7 +5,7 @@
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
#include "upb_generator/protoc-gen-upb_minitable.h" #include "upb_generator/minitable/generator.h"
#include <string.h> #include <string.h>

@ -15,8 +15,8 @@
#include "upb/reflection/def.hpp" #include "upb/reflection/def.hpp"
#include "upb_generator/common.h" #include "upb_generator/common.h"
#include "upb_generator/file_layout.h" #include "upb_generator/file_layout.h"
#include "upb_generator/minitable/generator.h"
#include "upb_generator/plugin.h" #include "upb_generator/plugin.h"
#include "upb_generator/protoc-gen-upb_minitable.h"
// Must be last. // Must be last.
#include "upb/port/def.inc" #include "upb/port/def.inc"
Loading…
Cancel
Save