diff options
-rw-r--r-- | BUILD | 146 | ||||
-rw-r--r-- | CMakeLists.txt | 12 | ||||
-rw-r--r-- | WORKSPACE | 15 | ||||
-rw-r--r-- | build_defs.bzl | 319 | ||||
-rw-r--r-- | generated_for_cmake/google/protobuf/descriptor.upb.h | 33 | ||||
-rw-r--r-- | repository_defs.bzl | 5 | ||||
-rw-r--r-- | tests/conformance_upb.c | 28 | ||||
-rw-r--r-- | tests/test_cpp.cc | 5 | ||||
-rw-r--r-- | upb/handlers-inl.h | 4 | ||||
-rw-r--r-- | upb/handlers.h | 4 | ||||
-rw-r--r-- | upb_proto_library.bzl | 291 | ||||
-rw-r--r-- | upbc/generator.cc | 39 | ||||
-rw-r--r-- | upbc/message_layout.h | 10 |
13 files changed, 483 insertions, 428 deletions
@@ -7,11 +7,13 @@ load( "lua_library", "lua_test", "make_shell_script", - "map_dep", "upb_amalgamation", +) + +load( + ":upb_proto_library.bzl", "upb_proto_library", "upb_proto_reflection_library", - "upb_proto_srcs", ) licenses(["notice"]) # BSD (Google-authored w/ possible external contributions) @@ -49,8 +51,6 @@ cc_library( "upb/decode.c", "upb/encode.c", "upb/msg.c", - "upb/port_def.inc", - "upb/port_undef.inc", "upb/table.c", "upb/table.int.h", "upb/upb.c", @@ -63,12 +63,18 @@ cc_library( "upb/upb.h", ], copts = COPTS, + # Internal-only, but we have to make them public for generated code. + textual_hdrs = [ + "upb/port_def.inc", + "upb/port_undef.inc", + ], visibility = ["//visibility:public"], ) upb_proto_library( name = "descriptor_upbproto", deps = ["@com_google_protobuf//:descriptor_proto"], + visibility = ["//visibility:public"], ) cc_library( @@ -85,7 +91,8 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":descriptor_upbproto", - ":upb" + ":table", + ":upb", ], ) @@ -105,8 +112,11 @@ cc_library( "upb/legacy_msg_reflection.c", ], hdrs = ["upb/legacy_msg_reflection.h"], - deps = [":upb"], copts = COPTS, + deps = [ + ":table", + ":upb", + ], ) cc_library( @@ -123,6 +133,7 @@ cc_library( copts = COPTS, deps = [ ":reflection", + ":table", ":upb", ], ) @@ -145,12 +156,15 @@ cc_library( ], copts = COPTS, deps = [ + ":descriptor_upbproto", ":handlers", + ":reflection", ":table", ":upb", ], ) +# copybara:strip_for_google3_begin cc_library( name = "upb_json", srcs = [ @@ -167,13 +181,18 @@ cc_library( ":upb_pb", ], ) +# copybara:strip_end cc_library( name = "upb_cc_bindings", hdrs = [ "upb/bindings/stdc++/string.h", ], - deps = [":upb"], + deps = [ + ":descriptor_upbproto", + ":handlers", + ":upb", + ], ) # upb compiler ################################################################# @@ -186,31 +205,30 @@ cc_library( "upbc/message_layout.h", ], hdrs = ["upbc/generator.h"], + copts = CPPOPTS, deps = [ - map_dep("@absl//absl/base:core_headers"), - map_dep("@absl//absl/strings"), - map_dep("@com_google_protobuf//:protobuf"), - map_dep("@com_google_protobuf//:protoc_lib"), + "@absl//absl/base:core_headers", + "@absl//absl/container:flat_hash_map", + "@absl//absl/strings", + "@com_google_protobuf//:protobuf", + "@com_google_protobuf//:protoc_lib", ], - copts = CPPOPTS, ) cc_binary( name = "protoc-gen-upb", srcs = ["upbc/main.cc"], + copts = CPPOPTS, + visibility = ["//visibility:public"], deps = [ ":upbc_generator", - map_dep("@com_google_protobuf//:protoc_lib"), + "@com_google_protobuf//:protoc_lib", ], - copts = CPPOPTS, - visibility = ["//visibility:public"], ) # We strip the tests and remaining rules from google3 until the upb_proto_library() # and upb_proto_reflection_library() rules are fixed. -# copybara:strip_for_google3_begin - # C/C++ tests ################################################################## cc_library( @@ -224,16 +242,24 @@ cc_library( "tests/upb_test.h", ], copts = CPPOPTS, + deps = [ + ":handlers", + ":upb", + ], ) cc_test( name = "test_varint", - srcs = ["tests/pb/test_varint.c"], + srcs = [ + "tests/pb/test_varint.c", + "upb/pb/varint.int.h", + ], + copts = COPTS, deps = [ + ":upb", ":upb_pb", ":upb_test", ], - copts = COPTS, ) proto_library( @@ -245,35 +271,23 @@ proto_library( upb_proto_reflection_library( name = "test_decoder_upbproto", - deps = ["test_decoder_proto"], + deps = [":test_decoder_proto"], ) cc_test( name = "test_decoder", - srcs = ["tests/pb/test_decoder.cc"], - deps = [ - ":test_decoder_upbproto", - ":upb_pb", - ":upb_test", + srcs = [ + "tests/pb/test_decoder.cc", + "upb/pb/varint.int.h", ], copts = CPPOPTS, -) - -upb_proto_reflection_library( - name = "descriptor_upbreflection", - deps = ["@com_google_protobuf//:descriptor_proto"], -) - -cc_test( - name = "test_encoder", - srcs = ["tests/pb/test_encoder.cc"], deps = [ - "descriptor_upbreflection", - ":upb_cc_bindings", + ":handlers", + ":test_decoder_upbproto", + ":upb", ":upb_pb", ":upb_test", ], - copts = CPPOPTS, ) proto_library( @@ -291,23 +305,46 @@ upb_proto_reflection_library( cc_test( name = "test_cpp", srcs = ["tests/test_cpp.cc"], + copts = CPPOPTS, deps = [ + ":handlers", + ":reflection", ":test_cpp_upbproto", ":upb", ":upb_pb", ":upb_test", ], - copts = CPPOPTS, ) cc_test( name = "test_table", srcs = ["tests/test_table.cc"], + copts = CPPOPTS, deps = [ + ":table", ":upb", ":upb_test", ], +) + +# copybara:strip_for_google3_begin +upb_proto_reflection_library( + name = "descriptor_upbreflection", + deps = ["@com_google_protobuf//:descriptor_proto"], +) + +cc_test( + name = "test_encoder", + srcs = ["tests/pb/test_encoder.cc"], copts = CPPOPTS, + deps = [ + ":descriptor_upbproto", + ":descriptor_upbreflection", + ":upb", + ":upb_cc_bindings", + ":upb_pb", + ":upb_test", + ], ) proto_library( @@ -341,27 +378,31 @@ cc_test( srcs = [ "tests/json/test_json.cc", ], + copts = CPPOPTS, deps = [ - ":test_json_upbprotoreflection", ":test_json_upbproto", + ":test_json_upbprotoreflection", ":upb_json", ":upb_test", ], - copts = CPPOPTS, ) +# copybara:strip_end upb_proto_library( name = "conformance_proto_upb", + testonly = 1, deps = ["@com_google_protobuf//:conformance_proto"], ) upb_proto_library( name = "test_messages_proto3_proto_upb", + testonly = 1, deps = ["@com_google_protobuf//:test_messages_proto3_proto"], ) cc_binary( name = "conformance_upb", + testonly = 1, srcs = [ "tests/conformance_upb.c", ], @@ -376,7 +417,7 @@ cc_binary( make_shell_script( name = "gen_test_conformance_upb", out = "test_conformance_upb.sh", - contents = "$(rlocation com_google_protobuf/conformance_test_runner) $(rlocation upb/conformance_upb)", + contents = "external/com_google_protobuf/conformance_test_runner ./conformance_upb", ) sh_test( @@ -385,11 +426,12 @@ sh_test( data = [ "tests/conformance_upb_failures.txt", ":conformance_upb", - "@bazel_tools//tools/bash/runfiles", "@com_google_protobuf//:conformance_test_runner", ], ) +# copybara:strip_for_google3_begin + # Amalgamation ################################################################# py_binary( @@ -397,11 +439,6 @@ py_binary( srcs = ["tools/amalgamate.py"], ) -upb_proto_srcs( - name = "descriptor_upbproto_srcs", - deps = ["@com_google_protobuf//:descriptor_proto"], -) - upb_amalgamation( name = "gen_amalgamation", outs = [ @@ -411,7 +448,7 @@ upb_amalgamation( amalgamator = ":amalgamate", libs = [ ":upb", - ":descriptor_upbproto_srcs", + ":descriptor_upbproto", ":reflection", ":handlers", ":upb_pb", @@ -494,7 +531,7 @@ filegroup( "upbc/**/*", "upb/**/*", "tests/**/*", - ]) + ]), ) make_shell_script( @@ -506,10 +543,7 @@ make_shell_script( sh_test( name = "cmake_build", srcs = ["run_cmake_build.sh"], - data = [ - ":cmake_files", - "@bazel_tools//tools/bash/runfiles", - ], + data = [":cmake_files"], ) # Generated files ############################################################## @@ -556,7 +590,7 @@ genrule( genrule( name = "copy_protos", - srcs = [":descriptor_upbproto_srcs"], + srcs = [":descriptor_upbproto"], outs = [ "generated-in/generated_for_cmake/google/protobuf/descriptor.upb.c", "generated-in/generated_for_cmake/google/protobuf/descriptor.upb.h", @@ -568,9 +602,9 @@ generated_file_staleness_test( name = "test_generated_files", outs = [ "CMakeLists.txt", - "generated_for_cmake/upb/json/parser.c", "generated_for_cmake/google/protobuf/descriptor.upb.c", "generated_for_cmake/google/protobuf/descriptor.upb.h", + "generated_for_cmake/upb/json/parser.c", ], generated_pattern = "generated-in/%s", ) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6bc5b4..2573173 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,8 +64,6 @@ add_library(upb upb/decode.c upb/encode.c upb/msg.c - upb/port_def.inc - upb/port_undef.inc upb/table.c upb/table.int.h upb/upb.c @@ -81,6 +79,7 @@ add_library(reflection upb/msgfactory.h) target_link_libraries(reflection descriptor_upbproto + table upb) add_library(table INTERFACE) target_link_libraries(table INTERFACE @@ -89,6 +88,7 @@ add_library(legacy_msg_reflection upb/legacy_msg_reflection.c upb/legacy_msg_reflection.h) target_link_libraries(legacy_msg_reflection + table upb) add_library(handlers upb/handlers.c @@ -98,6 +98,7 @@ add_library(handlers upb/sink.h) target_link_libraries(handlers reflection + table upb) add_library(upb_pb upb/pb/compile_decoder.c @@ -111,7 +112,9 @@ add_library(upb_pb upb/pb/encoder.h upb/pb/textprinter.h) target_link_libraries(upb_pb + descriptor_upbproto handlers + reflection table upb) add_library(upb_json @@ -124,10 +127,15 @@ target_link_libraries(upb_json upb_pb) add_library(upb_cc_bindings INTERFACE) target_link_libraries(upb_cc_bindings INTERFACE + descriptor_upbproto + handlers upb) add_library(upb_test tests/testmain.cc tests/test_util.h tests/upb_test.h) +target_link_libraries(upb_test + handlers + upb) @@ -1,4 +1,3 @@ - workspace(name = "upb") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") @@ -6,7 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") load(":repository_defs.bzl", "bazel_version_repository") bazel_version_repository( - name = "bazel_version" + name = "bazel_version", ) http_archive( @@ -23,7 +22,7 @@ http_archive( git_repository( name = "com_google_protobuf", remote = "https://github.com/protocolbuffers/protobuf.git", - commit = "78ca77ac8799f67fda7b9a01cc691cd9fe526f25", + commit = "d41002663fd04325ead28439dfd5ce2822b0d6fb", ) http_archive( @@ -38,19 +37,19 @@ git_repository( name = "absl", commit = "070f6e47b33a2909d039e620c873204f78809492", remote = "https://github.com/abseil/abseil-cpp.git", - shallow_since = "1541627663 -0500" + shallow_since = "1541627663 -0500", ) http_archive( name = "ragel", - sha256 = "5f156edb65d20b856d638dd9ee2dfb43285914d9aa2b6ec779dac0270cd56c3f", build_file = "//:ragel.BUILD", + sha256 = "5f156edb65d20b856d638dd9ee2dfb43285914d9aa2b6ec779dac0270cd56c3f", strip_prefix = "ragel-6.10", urls = ["http://www.colm.net/files/ragel/ragel-6.10.tar.gz"], ) http_archive( - name = "bazel_skylib", - strip_prefix = "bazel-skylib-master", - urls = ["https://github.com/bazelbuild/bazel-skylib/archive/master.tar.gz"], + name = "bazel_skylib", + strip_prefix = "bazel-skylib-master", + urls = ["https://github.com/bazelbuild/bazel-skylib/archive/master.tar.gz"], ) diff --git a/build_defs.bzl b/build_defs.bzl index 38df359..44ef0a3 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -1,37 +1,31 @@ +"""Internal rules for building upb.""" -load("@bazel_skylib//lib:paths.bzl", "paths") -load("@bazel_skylib//lib:versions.bzl", "versions") -load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") -load("@bazel_version//:bazel_version.bzl", "bazel_version") - -_shell_find_runfiles = """ - # --- begin runfiles.bash initialization --- - # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). - set -euo pipefail - if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then - if [[ -f "$0.runfiles_manifest" ]]; then - export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" - elif [[ -f "$0.runfiles/MANIFEST" ]]; then - export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" - elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then - export RUNFILES_DIR="$0.runfiles" - fi - fi - if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then - source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" - elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then - source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ - "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" - else - echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" - exit 1 - fi - # --- end runfiles.bash initialization --- -""" +load(":upb_proto_library.bzl", "GeneratedSrcs") def _librule(name): return name + "_lib" +def _get_real_short_path(file): + # For some reason, files from other archives have short paths that look like: + # ../com_google_protobuf/google/protobuf/descriptor.proto + short_path = file.short_path + if short_path.startswith("../"): + second_slash = short_path.index("/", 3) + short_path = short_path[second_slash + 1:] + return short_path + +def _get_real_root(file): + real_short_path = _get_real_short_path(file) + return file.path[:-len(real_short_path) - 1] + +def _get_real_roots(files): + roots = {} + for file in files: + real_root = _get_real_root(file) + if real_root: + roots[real_root] = True + return roots.keys() + def lua_cclibrary(name, srcs, hdrs = [], deps = [], luadeps = []): lib_rule = name + "_lib" so_rule = "lib" + name + ".so" @@ -93,11 +87,11 @@ def lua_library(name, srcs, strip_prefix, luadeps = []): ) def make_shell_script(name, contents, out): - script_contents = (_shell_find_runfiles + contents).replace("$", "$$") + contents = contents.replace("$", "$$") native.genrule( name = "gen_" + name, outs = [out], - cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % script_contents, + cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents, ) def _lua_binary_or_test(name, luamain, luadeps, rule): @@ -117,7 +111,7 @@ $(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@" rule( name = name, srcs = [script], - data = ["@lua//:lua", "@bazel_tools//tools/bash/runfiles", luamain] + luadeps, + data = ["@lua//:lua", luamain] + luadeps, ) def lua_binary(name, luamain, luadeps = []): @@ -180,8 +174,9 @@ SrcList = provider( ) def _file_list_aspect_impl(target, ctx): - if SrcList in target: - return [] + if GeneratedSrcs in target: + srcs = target[GeneratedSrcs] + return [SrcList(srcs = srcs.srcs, hdrs = srcs.hdrs)] srcs = [] hdrs = [] @@ -189,6 +184,8 @@ def _file_list_aspect_impl(target, ctx): srcs += src.files.to_list() for hdr in ctx.rule.attr.hdrs: hdrs += hdr.files.to_list() + for hdr in ctx.rule.attr.textual_hdrs: + hdrs += hdr.files.to_list() return [SrcList(srcs = srcs, hdrs = hdrs)] _file_list_aspect = aspect( @@ -209,6 +206,7 @@ def _upb_amalgamation(ctx): progress_message = "Making amalgamation", executable = ctx.executable.amalgamator, ) + return [] upb_amalgamation = rule( attrs = { @@ -222,259 +220,6 @@ upb_amalgamation = rule( implementation = _upb_amalgamation, ) -is_bazel = not hasattr(native, "genmpm") - -google3_dep_map = { - "@absl//absl/base:core_headers": "//third_party/absl/base:core_headers", - "@absl//absl/strings": "//third_party/absl/strings", - "@com_google_protobuf//:protoc": "//third_party/protobuf:protoc", - "@com_google_protobuf//:protobuf": "//third_party/protobuf:protobuf", - "@com_google_protobuf//:protoc_lib": "//third_party/protobuf:libprotoc", -} - -def map_dep(dep): - if is_bazel: - return dep - else: - return google3_dep_map[dep] - -def _get_real_short_path(file): - # For some reason, files from other archives have short paths that look like: - # ../com_google_protobuf/google/protobuf/descriptor.proto - short_path = file.short_path - if short_path.startswith("../"): - second_slash = short_path.index("/", 3) - short_path = short_path[second_slash + 1:] - return short_path - -def _get_real_root(file): - real_short_path = _get_real_short_path(file) - return file.path[:-len(real_short_path) - 1] - -def _get_real_roots(files): - roots = {} - for file in files: - real_root = _get_real_root(file) - if real_root: - roots[real_root] = True - return roots.keys() - -def _generate_output_file(ctx, src, extension): - real_short_path = _get_real_short_path(src) - output_filename = paths.replace_extension(real_short_path, extension) - ret = ctx.new_file(ctx.genfiles_dir, output_filename) - return ret - -def filter_none(elems): - out = [] - for elem in elems: - if elem: - out.append(elem) - return out - -# upb_proto_library() rule - -def cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos): - compilation_contexts = [info.compilation_context for info in dep_ccinfos] - linking_contexts = [info.linking_context for info in dep_ccinfos] - toolchain = find_cpp_toolchain(ctx) - feature_configuration = cc_common.configure_features( - cc_toolchain = toolchain, - requested_features = ctx.features, - unsupported_features = ctx.disabled_features, - ) - - if is_bazel: - if bazel_version == "0.24.1": - # Compatibility code until gRPC is on 0.25.2 or later. - compilation_info = cc_common.compile( - ctx = ctx, - feature_configuration = feature_configuration, - cc_toolchain = toolchain, - srcs = srcs, - hdrs = hdrs, - compilation_contexts = compilation_contexts, - ) - linking_info = cc_common.link( - ctx = ctx, - feature_configuration = feature_configuration, - cc_toolchain = toolchain, - cc_compilation_outputs = compilation_info.cc_compilation_outputs, - linking_contexts = linking_contexts, - ) - return CcInfo( - compilation_context = compilation_info.compilation_context, - linking_context = linking_info.linking_context, - ) - - if not versions.is_at_least("0.25.2", bazel_version): - fail("upb requires Bazel >=0.25.2 or 0.24.1") - - (compilation_context, compilation_outputs) = cc_common.compile( - actions = ctx.actions, - feature_configuration = feature_configuration, - cc_toolchain = toolchain, - name = name, - srcs = srcs, - public_hdrs = hdrs, - compilation_contexts = compilation_contexts, - ) - (linking_context, linking_outputs) = cc_common.create_linking_context_from_compilation_outputs( - actions = ctx.actions, - name = name, - feature_configuration = feature_configuration, - cc_toolchain = toolchain, - compilation_outputs = compilation_outputs, - linking_contexts = linking_contexts, - ) - - return CcInfo( - compilation_context = compilation_context, - linking_context = linking_context, - ) - -def _compile_upb_protos(ctx, proto_info, proto_sources, ext): - srcs = [_generate_output_file(ctx, name, ext + ".c") for name in proto_sources] - hdrs = [_generate_output_file(ctx, name, ext + ".h") for name in proto_sources] - transitive_sets = list(proto_info.transitive_descriptor_sets) - ctx.actions.run( - inputs = depset( - direct = [ctx.executable._upbc, proto_info.direct_descriptor_set], - transitive = [proto_info.transitive_descriptor_sets], - ), - outputs = srcs + hdrs, - executable = ctx.executable._protoc, - arguments = [ - "--upb_out=" + _get_real_root(srcs[0]), - "--plugin=protoc-gen-upb=" + ctx.executable._upbc.path, - "--descriptor_set_in=" + ":".join([f.path for f in transitive_sets]), - ] + - [_get_real_short_path(file) for file in proto_sources], - progress_message = "Generating upb protos for :" + ctx.label.name, - ) - return SrcList(srcs = srcs, hdrs = hdrs) - -# upb_proto_library() shared code ############################################# - -# Can share these with upb_proto_library() once cc_common.link() supports name -# param. - -def _upb_proto_rule_impl(ctx): - if len(ctx.attr.deps) != 1: - fail("only one deps dependency allowed.") - dep = ctx.attr.deps[0] - if CcInfo not in dep: - fail("proto_library rule must generate CcInfo (aspect should have handled this).") - lib = dep[CcInfo].linking_context.libraries_to_link[0] - files = filter_none([ - lib.static_library, - lib.pic_static_library, - lib.dynamic_library, - ]) - return [ - DefaultInfo(files = depset(files)), - dep[CcInfo], - ] - -def _upb_proto_aspect_impl(target, ctx): - proto_info = target[ProtoInfo] - files = _compile_upb_protos(ctx, proto_info, proto_info.direct_sources, ctx.attr._ext) - deps = ctx.rule.attr.deps + [ctx.attr._upb] - dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep] - cc_info = cc_library_func( - ctx = ctx, - name = ctx.rule.attr.name, - hdrs = files.hdrs, - srcs = files.srcs, - dep_ccinfos = dep_ccinfos, - ) - return [cc_info] - -# upb_proto_library() ########################################################## - -_upb_proto_library_aspect = aspect( - attrs = { - "_upbc": attr.label( - executable = True, - cfg = "host", - default = ":protoc-gen-upb", - ), - "_protoc": attr.label( - executable = True, - cfg = "host", - default = map_dep("@com_google_protobuf//:protoc"), - ), - "_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"), - "_upb": attr.label(default = ":upb"), - "_ext": attr.string(default = ".upb"), - }, - implementation = _upb_proto_aspect_impl, - attr_aspects = ["deps"], - fragments = ["cpp"], -) - -upb_proto_library = rule( - output_to_genfiles = True, - implementation = _upb_proto_rule_impl, - attrs = { - "deps": attr.label_list( - aspects = [_upb_proto_library_aspect], - allow_rules = ["proto_library"], - providers = [ProtoInfo], - ), - }, -) - -# upb_proto_srcs() ############################################################# - -def _upb_proto_srcs_impl(ctx): - srcs = [] - hdrs = [] - for dep in ctx.attr.deps: - if hasattr(dep, "proto"): - proto_info = dep[ProtoInfo] - files = _compile_upb_protos(ctx, proto_info, proto_info.transitive_sources, ctx.attr.ext) - srcs += files.srcs - hdrs += files.hdrs - return [ - SrcList(srcs = srcs, hdrs = hdrs), - DefaultInfo(files = depset(srcs + hdrs)), - ] - -upb_proto_srcs = rule( - attrs = { - "_upbc": attr.label( - executable = True, - cfg = "host", - default = ":protoc-gen-upb", - ), - "_protoc": attr.label( - executable = True, - cfg = "host", - default = map_dep("@com_google_protobuf//:protoc"), - ), - "deps": attr.label_list(), - "ext": attr.string(default = ".upb") - }, - implementation = _upb_proto_srcs_impl, -) - -# upb_proto_reflection_library() ############################################### - -def upb_proto_reflection_library(name, deps): - srcs_rule = name + "_defsrcs.cc" - upb_proto_srcs( - name = srcs_rule, - deps = deps, - ext = ".upbdefs", - ) - native.cc_library( - name = name, - srcs = [":" + srcs_rule], - deps = [":upb", ":reflection"], - copts = ["-Ibazel-out/k8-fastbuild/bin"], - ) - def licenses(*args): # No-op (for Google-internal usage). pass diff --git a/generated_for_cmake/google/protobuf/descriptor.upb.h b/generated_for_cmake/google/protobuf/descriptor.upb.h index 7f164fb..3016c91 100644 --- a/generated_for_cmake/google/protobuf/descriptor.upb.h +++ b/generated_for_cmake/google/protobuf/descriptor.upb.h @@ -10,12 +10,12 @@ #define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ #include "upb/generated_util.h" - #include "upb/msg.h" - #include "upb/decode.h" #include "upb/encode.h" + #include "upb/port_def.inc" + #ifdef __cplusplus extern "C" { #endif @@ -102,8 +102,6 @@ extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit; extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit; extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit; -/* Enums */ - typedef enum { google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1, google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2, @@ -186,7 +184,6 @@ UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_FileDescr return sub; } - /* google.protobuf.FileDescriptorProto */ UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) { @@ -340,7 +337,6 @@ UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_F UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value; } - /* google.protobuf.DescriptorProto */ UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) { @@ -487,7 +483,6 @@ UPB_INLINE bool google_protobuf_DescriptorProto_add_reserved_name(google_protobu msg, UPB_SIZE(44, 88), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); } - /* google.protobuf.DescriptorProto.ExtensionRange */ UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) { @@ -531,7 +526,6 @@ UPB_INLINE struct google_protobuf_ExtensionRangeOptions* google_protobuf_Descrip return sub; } - /* google.protobuf.DescriptorProto.ReservedRange */ UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_arena *arena) { @@ -560,7 +554,6 @@ UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_pro UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } - /* google.protobuf.ExtensionRangeOptions */ UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) { @@ -591,7 +584,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_Extension return sub; } - /* google.protobuf.FieldDescriptorProto */ UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) { @@ -677,7 +669,6 @@ UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protob UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)) = value; } - /* google.protobuf.OneofDescriptorProto */ UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) { @@ -715,7 +706,6 @@ UPB_INLINE struct google_protobuf_OneofOptions* google_protobuf_OneofDescriptorP return sub; } - /* google.protobuf.EnumDescriptorProto */ UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_arena *arena) { @@ -792,7 +782,6 @@ UPB_INLINE bool google_protobuf_EnumDescriptorProto_add_reserved_name(google_pro msg, UPB_SIZE(24, 48), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); } - /* google.protobuf.EnumDescriptorProto.EnumReservedRange */ UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) { @@ -821,7 +810,6 @@ UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(go UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } - /* google.protobuf.EnumValueDescriptorProto */ UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) { @@ -865,7 +853,6 @@ UPB_INLINE struct google_protobuf_EnumValueOptions* google_protobuf_EnumValueDes return sub; } - /* google.protobuf.ServiceDescriptorProto */ UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_arena *arena) { @@ -917,7 +904,6 @@ UPB_INLINE struct google_protobuf_ServiceOptions* google_protobuf_ServiceDescrip return sub; } - /* google.protobuf.MethodDescriptorProto */ UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_arena *arena) { @@ -979,7 +965,6 @@ UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(googl UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; } - /* google.protobuf.FileOptions */ UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) { @@ -1130,7 +1115,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FileOptio return sub; } - /* google.protobuf.MessageOptions */ UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) { @@ -1185,7 +1169,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MessageOp return sub; } - /* google.protobuf.FieldOptions */ UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) { @@ -1252,7 +1235,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FieldOpti return sub; } - /* google.protobuf.OneofOptions */ UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) { @@ -1283,7 +1265,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_OneofOpti return sub; } - /* google.protobuf.EnumOptions */ UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) { @@ -1326,7 +1307,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumOptio return sub; } - /* google.protobuf.EnumValueOptions */ UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) { @@ -1363,7 +1343,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumValue return sub; } - /* google.protobuf.ServiceOptions */ UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) { @@ -1400,7 +1379,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ServiceOp return sub; } - /* google.protobuf.MethodOptions */ UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) { @@ -1443,7 +1421,6 @@ UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MethodOpt return sub; } - /* google.protobuf.UninterpretedOption */ UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) { @@ -1510,7 +1487,6 @@ UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_p UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value; } - /* google.protobuf.UninterpretedOption.NamePart */ UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) { @@ -1539,7 +1515,6 @@ UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(go UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; } - /* google.protobuf.SourceCodeInfo */ UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) { @@ -1570,7 +1545,6 @@ UPB_INLINE struct google_protobuf_SourceCodeInfo_Location* google_protobuf_Sourc return sub; } - /* google.protobuf.SourceCodeInfo.Location */ UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) { @@ -1632,7 +1606,6 @@ UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_leading_detached_com msg, UPB_SIZE(28, 56), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena); } - /* google.protobuf.GeneratedCodeInfo */ UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) { @@ -1663,7 +1636,6 @@ UPB_INLINE struct google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_ return sub; } - /* google.protobuf.GeneratedCodeInfo.Annotation */ UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_arena *arena) { @@ -1709,7 +1681,6 @@ UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_prot UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; } - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/repository_defs.bzl b/repository_defs.bzl index 5bbc9cb..7b6e78e 100644 --- a/repository_defs.bzl +++ b/repository_defs.bzl @@ -1,4 +1,3 @@ - # A hacky way to work around the fact that native.bazel_version is only # available from WORKSPACE macros, not BUILD macros or rules. # @@ -11,6 +10,6 @@ def _impl(repository_ctx): repository_ctx.file("BUILD", "") bazel_version_repository = repository_rule( - implementation=_impl, - local=True, + implementation = _impl, + local = True, ) diff --git a/tests/conformance_upb.c b/tests/conformance_upb.c index 782346f..36e550e 100644 --- a/tests/conformance_upb.c +++ b/tests/conformance_upb.c @@ -75,21 +75,16 @@ void DoTest( break; } - case conformance_ConformanceRequest_payload_json_payload: { - static const char msg[] = "JSON support not yet implemented."; - conformance_ConformanceResponse_set_skipped( - response, upb_strview_make(msg, sizeof(msg))); - return; - } - case conformance_ConformanceRequest_payload_NOT_SET: fprintf(stderr, "conformance_upb: Request didn't have payload.\n"); return; - default: - fprintf(stderr, "conformance_upb: Unexpected case: %d\n", - conformance_ConformanceRequest_payload_case(request)); - exit(1); + default: { + static const char msg[] = "Unsupported input format."; + conformance_ConformanceResponse_set_skipped( + response, upb_strview_make(msg, sizeof(msg))); + return; + } } switch (conformance_ConformanceRequest_requested_output_format(request)) { @@ -113,17 +108,12 @@ void DoTest( break; } - case conformance_JSON: { - static const char msg[] = "JSON support not yet implemented."; + default: { + static const char msg[] = "Unsupported output format."; conformance_ConformanceResponse_set_skipped( response, upb_strview_make(msg, sizeof(msg))); - break; + return; } - - default: - fprintf(stderr, "conformance_upb: Unknown output format: %d\n", - conformance_ConformanceRequest_requested_output_format(request)); - exit(1); } return; diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 8feb6e2..abbafda 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -12,14 +12,13 @@ #include <sstream> #include "tests/test_cpp.upbdefs.h" +#include "tests/upb_test.h" #include "upb/def.h" #include "upb/handlers.h" #include "upb/pb/decoder.h" #include "upb/pb/textprinter.h" -#include "upb/upb.h" -#include "upb_test.h" - #include "upb/port_def.inc" +#include "upb/upb.h" template <class T> void AssertInsert(T* const container, const typename T::value_type& val) { diff --git a/upb/handlers-inl.h b/upb/handlers-inl.h index 40a0047..8f8634b 100644 --- a/upb/handlers-inl.h +++ b/upb/handlers-inl.h @@ -10,6 +10,8 @@ #include <stddef.h> #include "upb/handlers.h" +#include "upb/port_def.inc" + #ifdef __cplusplus /* Type detection and typedefs for integer types. @@ -916,4 +918,6 @@ inline void Handler<T>::AddCleanup(upb_handlers* h) const { #undef UPB_INT64ALT_T #undef UPB_UINT64ALT_T +#include "upb/port_undef.inc" + #endif /* UPB_HANDLERS_INL_H_ */ diff --git a/upb/handlers.h b/upb/handlers.h index 856be31..2d2380b 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -725,8 +725,8 @@ bool upb_msg_getscalarhandlerdata(const upb_handlers *h, } /* extern "C" */ #endif -#include "upb/handlers-inl.h" - #include "upb/port_undef.inc" +#include "upb/handlers-inl.h" + #endif /* UPB_HANDLERS_H */ diff --git a/upb_proto_library.bzl b/upb_proto_library.bzl new file mode 100644 index 0000000..962facd --- /dev/null +++ b/upb_proto_library.bzl @@ -0,0 +1,291 @@ +"""Public rules for using upb protos: + - upb_proto_library() + - upb_proto_reflection_library() +""" + +load("@bazel_skylib//lib:paths.bzl", "paths") +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") + +# copybara:strip_for_google3_begin +load("@bazel_skylib//lib:versions.bzl", "versions") +load("@bazel_version//:bazel_version.bzl", "bazel_version") +# copybara:strip_end + +# Generic support code ######################################################### + +_is_bazel = not hasattr(native, "genmpm") + +def _get_real_short_path(file): + # For some reason, files from other archives have short paths that look like: + # ../com_google_protobuf/google/protobuf/descriptor.proto + short_path = file.short_path + if short_path.startswith("../"): + second_slash = short_path.index("/", 3) + short_path = short_path[second_slash + 1:] + return short_path + +def _get_real_root(file): + real_short_path = _get_real_short_path(file) + return file.path[:-len(real_short_path) - 1] + +def _get_real_roots(files): + roots = {} + for file in files: + real_root = _get_real_root(file) + if real_root: + roots[real_root] = True + return roots.keys() + +def _generate_output_file(ctx, src, extension): + if _is_bazel: + real_short_path = _get_real_short_path(src) + else: + real_short_path = paths.relativize(src.short_path, ctx.label.package) + output_filename = paths.replace_extension(real_short_path, extension) + ret = ctx.new_file(ctx.genfiles_dir, output_filename) + return ret + +def _filter_none(elems): + out = [] + for elem in elems: + if elem: + out.append(elem) + return out + +def _cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos): + """Like cc_library(), but callable from rules. + + Args: + ctx: Rule context. + name: Unique name used to generate output files. + hdrs: Public headers that can be #included from other rules. + srcs: C/C++ source files. + dep_ccinfos: CcInfo providers of dependencies we should build/link against. + + Returns: + CcInfo provider for this compilation. + """ + + compilation_contexts = [info.compilation_context for info in dep_ccinfos] + linking_contexts = [info.linking_context for info in dep_ccinfos] + toolchain = find_cpp_toolchain(ctx) + feature_configuration = cc_common.configure_features( + cc_toolchain = toolchain, + requested_features = ctx.features, + unsupported_features = ctx.disabled_features, + ) + + # copybara:strip_for_google3_begin + if bazel_version == "0.24.1": + # Compatibility code until gRPC is on 0.25.2 or later. + compilation_info = cc_common.compile( + ctx = ctx, + feature_configuration = feature_configuration, + cc_toolchain = toolchain, + srcs = srcs, + hdrs = hdrs, + compilation_contexts = compilation_contexts, + ) + linking_info = cc_common.link( + ctx = ctx, + feature_configuration = feature_configuration, + cc_toolchain = toolchain, + cc_compilation_outputs = compilation_info.cc_compilation_outputs, + linking_contexts = linking_contexts, + ) + return CcInfo( + compilation_context = compilation_info.compilation_context, + linking_context = linking_info.linking_context, + ) + + if not versions.is_at_least("0.25.2", bazel_version): + fail("upb requires Bazel >=0.25.2 or 0.24.1") + + # copybara:strip_end + + blaze_only_args = {} + + if not _is_bazel: + blaze_only_args["grep_includes"] = ctx.file._grep_includes + + (compilation_context, compilation_outputs) = cc_common.compile( + actions = ctx.actions, + feature_configuration = feature_configuration, + cc_toolchain = toolchain, + name = name, + srcs = srcs, + public_hdrs = hdrs, + compilation_contexts = compilation_contexts, + **blaze_only_args + ) + (linking_context, linking_outputs) = cc_common.create_linking_context_from_compilation_outputs( + actions = ctx.actions, + name = name, + feature_configuration = feature_configuration, + cc_toolchain = toolchain, + compilation_outputs = compilation_outputs, + linking_contexts = linking_contexts, + **blaze_only_args + ) + + return CcInfo( + compilation_context = compilation_context, + linking_context = linking_context, + ) + +# upb_proto_library / upb_proto_reflection_library shared code ################# + +GeneratedSrcs = provider( + fields = { + "srcs": "list of srcs", + "hdrs": "list of hdrs", + }, +) + +_WrappedCcInfo = provider(fields = ["cc_info"]) +_WrappedGeneratedSrcs = provider(fields = ["srcs"]) + +def _compile_upb_protos(ctx, proto_info, proto_sources, ext): + srcs = [_generate_output_file(ctx, name, ext + ".c") for name in proto_sources] + hdrs = [_generate_output_file(ctx, name, ext + ".h") for name in proto_sources] + transitive_sets = list(proto_info.transitive_descriptor_sets) + ctx.actions.run( + inputs = depset( + direct = [ctx.executable._upbc, proto_info.direct_descriptor_set], + transitive = [proto_info.transitive_descriptor_sets], + ), + outputs = srcs + hdrs, + executable = ctx.executable._protoc, + arguments = [ + "--upb_out=" + _get_real_root(srcs[0]), + "--plugin=protoc-gen-upb=" + ctx.executable._upbc.path, + "--descriptor_set_in=" + ":".join([f.path for f in transitive_sets]), + ] + + [_get_real_short_path(file) for file in proto_sources], + progress_message = "Generating upb protos for :" + ctx.label.name, + ) + return GeneratedSrcs(srcs = srcs, hdrs = hdrs) + +def _upb_proto_rule_impl(ctx): + if len(ctx.attr.deps) != 1: + fail("only one deps dependency allowed.") + dep = ctx.attr.deps[0] + if _WrappedCcInfo not in dep or _WrappedGeneratedSrcs not in dep: + fail("proto_library rule must generate _WrappedCcInfo and " + + "_WrappedGeneratedSrcs (aspect should have handled this).") + cc_info = dep[_WrappedCcInfo].cc_info + srcs = dep[_WrappedGeneratedSrcs].srcs + lib = cc_info.linking_context.libraries_to_link[0] + files = _filter_none([ + lib.static_library, + lib.pic_static_library, + lib.dynamic_library, + ]) + return [ + DefaultInfo(files = depset(files + srcs.hdrs + srcs.srcs)), + srcs, + cc_info, + ] + +def _upb_proto_aspect_impl(target, ctx): + proto_info = target[ProtoInfo] + files = _compile_upb_protos(ctx, proto_info, proto_info.direct_sources, ctx.attr._ext) + deps = ctx.rule.attr.deps + ctx.attr._upb + dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep] + dep_ccinfos += [dep[_WrappedCcInfo].cc_info for dep in deps if _WrappedCcInfo in dep] + cc_info = _cc_library_func( + ctx = ctx, + name = ctx.rule.attr.name + ctx.attr._ext, + hdrs = files.hdrs, + srcs = files.srcs, + dep_ccinfos = dep_ccinfos, + ) + return [_WrappedCcInfo(cc_info = cc_info), _WrappedGeneratedSrcs(srcs = files)] + +def _maybe_add(d): + if not _is_bazel: + d["_grep_includes"] = attr.label( + allow_single_file = True, + cfg = "host", + default = "//tools/cpp:grep-includes", + ) + return d + +# upb_proto_library() ########################################################## + +_upb_proto_library_aspect = aspect( + attrs = _maybe_add({ + "_upbc": attr.label( + executable = True, + cfg = "host", + default = ":protoc-gen-upb", + ), + "_protoc": attr.label( + executable = True, + cfg = "host", + default = "@com_google_protobuf//:protoc", + ), + "_cc_toolchain": attr.label( + default = "@bazel_tools//tools/cpp:current_cc_toolchain", + ), + "_upb": attr.label_list(default = [":upb"]), + "_ext": attr.string(default = ".upb"), + }), + implementation = _upb_proto_aspect_impl, + attr_aspects = ["deps"], + fragments = ["cpp"], +) + +upb_proto_library = rule( + output_to_genfiles = True, + implementation = _upb_proto_rule_impl, + attrs = { + "deps": attr.label_list( + aspects = [_upb_proto_library_aspect], + allow_rules = ["proto_library"], + providers = [ProtoInfo], + ), + }, +) + +# upb_proto_reflection_library() ############################################### + +_upb_proto_reflection_library_aspect = aspect( + attrs = _maybe_add({ + "_upbc": attr.label( + executable = True, + cfg = "host", + default = ":protoc-gen-upb", + ), + "_protoc": attr.label( + executable = True, + cfg = "host", + default = "@com_google_protobuf//:protoc", + ), + "_cc_toolchain": attr.label( + default = "@bazel_tools//tools/cpp:current_cc_toolchain", + ), + "_upb": attr.label_list( + default = [ + ":upb", + ":reflection", + ], + ), + "_ext": attr.string(default = ".upbdefs"), + }), + implementation = _upb_proto_aspect_impl, + attr_aspects = ["deps"], + fragments = ["cpp"], +) + +upb_proto_reflection_library = rule( + output_to_genfiles = True, + implementation = _upb_proto_rule_impl, + attrs = { + "deps": attr.label_list( + aspects = [_upb_proto_reflection_library_aspect], + allow_rules = ["proto_library"], + providers = [ProtoInfo], + ), + }, +) diff --git a/upbc/generator.cc b/upbc/generator.cc index 5843597..53d849e 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -1,8 +1,7 @@ -#include <unordered_map> -#include <unordered_set> #include <memory> +#include "absl/container/flat_hash_map.h" #include "absl/strings/ascii.h" #include "absl/strings/str_replace.h" #include "absl/strings/substitute.h" @@ -488,7 +487,7 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output } } - output("\n\n"); + output("\n"); } void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { @@ -496,15 +495,30 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { output( "#ifndef $0_UPB_H_\n" "#define $0_UPB_H_\n\n" - "#include \"upb/generated_util.h\"\n\n" - "#include \"upb/msg.h\"\n\n" + "#include \"upb/generated_util.h\"\n" + "#include \"upb/msg.h\"\n" "#include \"upb/decode.h\"\n" - "#include \"upb/encode.h\"\n" + "#include \"upb/encode.h\"\n\n", + ToPreproc(file->name())); + + for (int i = 0; i < file->public_dependency_count(); i++) { + const auto& name = file->public_dependency(i)->name(); + if (i == 0) { + output("/* Public Imports. */\n"); + } + output("#include \"$0\"\n", HeaderFilename(name)); + if (i == file->public_dependency_count() - 1) { + output("\n"); + } + } + + output( "#include \"upb/port_def.inc\"\n" + "\n" "#ifdef __cplusplus\n" "extern \"C\" {\n" - "#endif\n\n", - ToPreproc(file->name())); + "#endif\n" + "\n"); std::vector<const protobuf::Descriptor*> this_file_messages = SortedMessages(file); @@ -541,12 +555,13 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { output("extern const upb_msglayout $0;\n", MessageInit(pair.second)); } + if (!this_file_messages.empty()) { + output("\n"); + } + std::vector<const protobuf::EnumDescriptor*> this_file_enums = SortedEnums(file); - output( - "\n" - "/* Enums */\n\n"); for (auto enumdesc : this_file_enums) { output("typedef enum {\n"); DumpEnumValues(enumdesc, output); @@ -594,7 +609,7 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) { std::string fields_array_ref = "NULL"; std::string submsgs_array_ref = "NULL"; std::string oneofs_array_ref = "NULL"; - std::unordered_map<const protobuf::Descriptor*, int> submsg_indexes; + absl::flat_hash_map<const protobuf::Descriptor*, int> submsg_indexes; MessageLayout layout(message); std::vector<const protobuf::FieldDescriptor*> sorted_submsgs = SortedSubmessages(message); diff --git a/upbc/message_layout.h b/upbc/message_layout.h index a4cb289..c2446a0 100644 --- a/upbc/message_layout.h +++ b/upbc/message_layout.h @@ -2,8 +2,8 @@ #ifndef UPBC_MESSAGE_LAYOUT_H #define UPBC_MESSAGE_LAYOUT_H -#include <unordered_map> #include "absl/base/macros.h" +#include "absl/container/flat_hash_map.h" #include "google/protobuf/descriptor.h" namespace upbc { @@ -70,7 +70,7 @@ class MessageLayout { Size Place(SizeAndAlign size_and_align); template <class K, class V> - static V GetMapValue(const std::unordered_map<K, V>& map, K key) { + static V GetMapValue(const absl::flat_hash_map<K, V>& map, K key) { auto iter = map.find(key); if (iter == map.end()) { fprintf(stderr, "No value for field.\n"); @@ -92,11 +92,11 @@ class MessageLayout { static int64_t FieldLayoutRank( const google::protobuf::FieldDescriptor* field); - std::unordered_map<const google::protobuf::FieldDescriptor*, Size> + absl::flat_hash_map<const google::protobuf::FieldDescriptor*, Size> field_offsets_; - std::unordered_map<const google::protobuf::FieldDescriptor*, int> + absl::flat_hash_map<const google::protobuf::FieldDescriptor*, int> hasbit_indexes_; - std::unordered_map<const google::protobuf::OneofDescriptor*, Size> + absl::flat_hash_map<const google::protobuf::OneofDescriptor*, Size> oneof_case_offsets_; Size maxalign_; Size size_; |