From a37877a32679edd455c5480644134282c3aa0460 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 13 May 2019 15:43:08 -0700 Subject: Fixes for Blaze. --- BUILD | 2 +- build_defs.bzl | 87 ++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/BUILD b/BUILD index 6ad365e..cefbc81 100644 --- a/BUILD +++ b/BUILD @@ -68,7 +68,7 @@ cc_library( upb_proto_library( name = "descriptor_upbproto", - deps = ["@com_google_protobuf//:descriptor_proto"], + deps = [map_dep("@com_google_protobuf//:descriptor_proto")], ) cc_library( diff --git a/build_defs.bzl b/build_defs.bzl index 38df359..db7e668 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -2,7 +2,10 @@ 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") + +# copybara:strip_for_google3_begin load("@bazel_version//:bazel_version.bzl", "bazel_version") +# copybara:strip_end _shell_find_runfiles = """ # --- begin runfiles.bash initialization --- @@ -227,6 +230,8 @@ 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", + "@bazel_tools//tools/cpp:current_cc_toolchain": "//tools/cpp:current_cc_toolchain", + "@com_google_protobuf//:descriptor_proto": "//net/proto2/proto:descriptor", "@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", @@ -260,7 +265,10 @@ def _get_real_roots(files): return roots.keys() def _generate_output_file(ctx, src, extension): - real_short_path = _get_real_short_path(src) + 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 @@ -284,31 +292,37 @@ def cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos): 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") + # 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, @@ -318,6 +332,7 @@ def cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos): 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, @@ -326,6 +341,7 @@ def cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos): cc_toolchain = toolchain, compilation_outputs = compilation_outputs, linking_contexts = linking_contexts, + **blaze_only_args ) return CcInfo( @@ -383,7 +399,7 @@ def _upb_proto_aspect_impl(target, ctx): dep_ccinfos = [dep[CcInfo] for dep in deps if CcInfo in dep] cc_info = cc_library_func( ctx = ctx, - name = ctx.rule.attr.name, + name = ctx.rule.attr.name + "_upb", hdrs = files.hdrs, srcs = files.srcs, dep_ccinfos = dep_ccinfos, @@ -392,8 +408,17 @@ def _upb_proto_aspect_impl(target, ctx): # upb_proto_library() ########################################################## +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_aspect = aspect( - attrs = { + attrs = maybe_add({ "_upbc": attr.label( executable = True, cfg = "host", @@ -404,10 +429,12 @@ _upb_proto_library_aspect = aspect( cfg = "host", default = map_dep("@com_google_protobuf//:protoc"), ), - "_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"), + "_cc_toolchain": attr.label( + default = map_dep("@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"], -- cgit v1.2.3 From 13eb14986b3fbce692c7ce5ef62adc8d4bf84f2b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 13 May 2019 15:55:44 -0700 Subject: More fixes for Blaze. --- BUILD | 7 +++++-- CMakeLists.txt | 2 -- build_defs.bzl | 17 ++++++++++------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/BUILD b/BUILD index cefbc81..dd3f80c 100644 --- a/BUILD +++ b/BUILD @@ -49,8 +49,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", @@ -62,6 +60,11 @@ cc_library( "upb/msg.h", "upb/upb.h", ], + # Internal-only, but we have to make them public for generated code. + textual_hdrs = [ + "upb/port_def.inc", + "upb/port_undef.inc", + ], copts = COPTS, visibility = ["//visibility:public"], ) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6bc5b4..675f41a 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 diff --git a/build_defs.bzl b/build_defs.bzl index db7e668..70854d9 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -192,6 +192,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( @@ -372,16 +374,16 @@ def _compile_upb_protos(ctx, proto_info, proto_sources, ext): # upb_proto_library() shared code ############################################# -# Can share these with upb_proto_library() once cc_common.link() supports name -# param. +_WrappedCcInfo = provider(fields = ["cc_info"]) 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] + if _WrappedCcInfo not in dep: + fail("proto_library rule must generate _WrappedCcInfo (aspect should have handled this).") + cc_info = dep[_WrappedCcInfo].cc_info + lib = cc_info.linking_context.libraries_to_link[0] files = filter_none([ lib.static_library, lib.pic_static_library, @@ -389,7 +391,7 @@ def _upb_proto_rule_impl(ctx): ]) return [ DefaultInfo(files = depset(files)), - dep[CcInfo], + cc_info, ] def _upb_proto_aspect_impl(target, ctx): @@ -397,6 +399,7 @@ def _upb_proto_aspect_impl(target, ctx): 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 + "_upb", @@ -404,7 +407,7 @@ def _upb_proto_aspect_impl(target, ctx): srcs = files.srcs, dep_ccinfos = dep_ccinfos, ) - return [cc_info] + return [_WrappedCcInfo(cc_info = cc_info)] # upb_proto_library() ########################################################## -- cgit v1.2.3 From 9a66356d95f9aff04570a6b4e9e34cdc8cd6fa6b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 13 May 2019 16:13:39 -0700 Subject: More Blaze fixes. --- BUILD | 41 ++++++++++++++++++++++++----------------- CMakeLists.txt | 1 + 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/BUILD b/BUILD index dd3f80c..bb89ebb 100644 --- a/BUILD +++ b/BUILD @@ -60,12 +60,12 @@ cc_library( "upb/msg.h", "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", ], - copts = COPTS, visibility = ["//visibility:public"], ) @@ -88,7 +88,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":descriptor_upbproto", - ":upb" + ":upb", ], ) @@ -108,8 +108,11 @@ cc_library( "upb/legacy_msg_reflection.c", ], hdrs = ["upb/legacy_msg_reflection.h"], - deps = [":upb"], copts = COPTS, + deps = [ + ":table", + ":upb", + ], ) cc_library( @@ -154,6 +157,7 @@ cc_library( ], ) +# copybara:strip_for_google3_begin cc_library( name = "upb_json", srcs = [ @@ -170,6 +174,7 @@ cc_library( ":upb_pb", ], ) +# copybara:strip_end cc_library( name = "upb_cc_bindings", @@ -189,31 +194,29 @@ 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"), ], - 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"), ], - 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( @@ -232,11 +235,11 @@ cc_library( cc_test( name = "test_varint", srcs = ["tests/pb/test_varint.c"], + copts = COPTS, deps = [ ":upb_pb", ":upb_test", ], - copts = COPTS, ) proto_library( @@ -254,12 +257,12 @@ upb_proto_reflection_library( cc_test( name = "test_decoder", srcs = ["tests/pb/test_decoder.cc"], + copts = CPPOPTS, deps = [ ":test_decoder_upbproto", ":upb_pb", ":upb_test", ], - copts = CPPOPTS, ) upb_proto_reflection_library( @@ -270,13 +273,13 @@ upb_proto_reflection_library( cc_test( name = "test_encoder", srcs = ["tests/pb/test_encoder.cc"], + copts = CPPOPTS, deps = [ "descriptor_upbreflection", ":upb_cc_bindings", ":upb_pb", ":upb_test", ], - copts = CPPOPTS, ) proto_library( @@ -294,25 +297,26 @@ upb_proto_reflection_library( cc_test( name = "test_cpp", srcs = ["tests/test_cpp.cc"], + copts = CPPOPTS, deps = [ ":test_cpp_upbproto", ":upb", ":upb_pb", ":upb_test", ], - copts = CPPOPTS, ) cc_test( name = "test_table", srcs = ["tests/test_table.cc"], + copts = CPPOPTS, deps = [ ":upb", ":upb_test", ], - copts = CPPOPTS, ) +# copybara:strip_for_google3_begin proto_library( name = "test_json_enum_from_separate", srcs = ["tests/json/enum_from_separate_file.proto"], @@ -344,14 +348,15 @@ 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", @@ -393,6 +398,8 @@ sh_test( ], ) +# copybara:strip_for_google3_begin + # Amalgamation ################################################################# py_binary( @@ -497,7 +504,7 @@ filegroup( "upbc/**/*", "upb/**/*", "tests/**/*", - ]) + ]), ) make_shell_script( @@ -571,9 +578,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 675f41a..d085f48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,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 -- cgit v1.2.3 From 301b6e2d7839b7f835d410a59f76c7fcf3e692f2 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 10:15:32 -0700 Subject: Removed map_dep() in favor of rewrites. --- BUILD | 15 +++++++-------- build_defs.bzl | 12 +++--------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/BUILD b/BUILD index bb89ebb..c7e24c7 100644 --- a/BUILD +++ b/BUILD @@ -7,7 +7,6 @@ load( "lua_library", "lua_test", "make_shell_script", - "map_dep", "upb_amalgamation", "upb_proto_library", "upb_proto_reflection_library", @@ -71,7 +70,7 @@ cc_library( upb_proto_library( name = "descriptor_upbproto", - deps = [map_dep("@com_google_protobuf//:descriptor_proto")], + deps = ["@com_google_protobuf//:descriptor_proto"], ) cc_library( @@ -196,10 +195,10 @@ cc_library( 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/strings", + "@com_google_protobuf//:protobuf", + "@com_google_protobuf//:protoc_lib", ], ) @@ -210,7 +209,7 @@ cc_binary( visibility = ["//visibility:public"], deps = [ ":upbc_generator", - map_dep("@com_google_protobuf//:protoc_lib"), + "@com_google_protobuf//:protoc_lib", ], ) @@ -384,7 +383,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 = "$(rlocation @com_google_protobuf/conformance_test_runner) $(rlocation upb/conformance_upb)", ) sh_test( diff --git a/build_defs.bzl b/build_defs.bzl index 70854d9..5517933 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -239,12 +239,6 @@ google3_dep_map = { "@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 @@ -430,10 +424,10 @@ _upb_proto_library_aspect = aspect( "_protoc": attr.label( executable = True, cfg = "host", - default = map_dep("@com_google_protobuf//:protoc"), + default = "@com_google_protobuf//:protoc", ), "_cc_toolchain": attr.label( - default = map_dep("@bazel_tools//tools/cpp:current_cc_toolchain"), + default = "@bazel_tools//tools/cpp:current_cc_toolchain", ), "_upb": attr.label(default = ":upb"), "_ext": attr.string(default = ".upb"), @@ -481,7 +475,7 @@ upb_proto_srcs = rule( "_protoc": attr.label( executable = True, cfg = "host", - default = map_dep("@com_google_protobuf//:protoc"), + default = "@com_google_protobuf//:protoc", ), "deps": attr.label_list(), "ext": attr.string(default = ".upb") -- cgit v1.2.3 From 81e813d3894f1d2600c270468ca795b0271646d4 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 10:39:54 -0700 Subject: Made upb_proto_reflection_library() a true aspect. --- BUILD | 2 +- build_defs.bzl | 69 ++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/BUILD b/BUILD index c7e24c7..ec24310 100644 --- a/BUILD +++ b/BUILD @@ -250,7 +250,7 @@ proto_library( upb_proto_reflection_library( name = "test_decoder_upbproto", - deps = ["test_decoder_proto"], + deps = [":test_decoder_proto"], ) cc_test( diff --git a/build_defs.bzl b/build_defs.bzl index 5517933..d925c9b 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -229,16 +229,6 @@ upb_amalgamation = rule( 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", - "@bazel_tools//tools/cpp:current_cc_toolchain": "//tools/cpp:current_cc_toolchain", - "@com_google_protobuf//:descriptor_proto": "//net/proto2/proto:descriptor", - "@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 _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 @@ -396,15 +386,13 @@ def _upb_proto_aspect_impl(target, ctx): 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 + "_upb", + name = ctx.rule.attr.name + ctx.attr._ext, hdrs = files.hdrs, srcs = files.srcs, dep_ccinfos = dep_ccinfos, ) return [_WrappedCcInfo(cc_info = cc_info)] -# upb_proto_library() ########################################################## - def maybe_add(d): if not is_bazel: d["_grep_includes"] = attr.label( @@ -414,6 +402,8 @@ def maybe_add(d): ) return d +# upb_proto_library() ########################################################## + _upb_proto_library_aspect = aspect( attrs = maybe_add({ "_upbc": attr.label( @@ -449,6 +439,43 @@ upb_proto_library = rule( }, ) +# 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(default = ":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], + ), + }, +) + # upb_proto_srcs() ############################################################# def _upb_proto_srcs_impl(ctx): @@ -483,22 +510,6 @@ upb_proto_srcs = rule( 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 -- cgit v1.2.3 From 7913bc678aed392908b2defaf4f5e2e8a5e8599c Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 10:49:02 -0700 Subject: Removed obsolete references to runfiles scripts. --- BUILD | 9 ++++----- build_defs.bzl | 31 +++---------------------------- 2 files changed, 7 insertions(+), 33 deletions(-) diff --git a/BUILD b/BUILD index ec24310..6e62423 100644 --- a/BUILD +++ b/BUILD @@ -360,11 +360,13 @@ cc_test( upb_proto_library( name = "conformance_proto_upb", deps = ["@com_google_protobuf//:conformance_proto"], + testonly = 1, ) upb_proto_library( name = "test_messages_proto3_proto_upb", deps = ["@com_google_protobuf//:test_messages_proto3_proto"], + testonly = 1, ) cc_binary( @@ -378,6 +380,7 @@ cc_binary( ":test_messages_proto3_proto_upb", ":upb", ], + testonly = 1, ) make_shell_script( @@ -392,7 +395,6 @@ sh_test( data = [ "tests/conformance_upb_failures.txt", ":conformance_upb", - "@bazel_tools//tools/bash/runfiles", "@com_google_protobuf//:conformance_test_runner", ], ) @@ -515,10 +517,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 ############################################################## diff --git a/build_defs.bzl b/build_defs.bzl index d925c9b..d30fdc5 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -7,31 +7,6 @@ load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") load("@bazel_version//:bazel_version.bzl", "bazel_version") # copybara:strip_end -_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 --- -""" - def _librule(name): return name + "_lib" @@ -96,11 +71,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): @@ -120,7 +95,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 = []): -- cgit v1.2.3 From 9b07311c7c0ca0f34f3fea54df478ea6d15dd827 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 11:12:13 -0700 Subject: More Blaze fixes. --- BUILD | 15 +++++++++++---- CMakeLists.txt | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/BUILD b/BUILD index 6e62423..2197d2c 100644 --- a/BUILD +++ b/BUILD @@ -87,6 +87,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":descriptor_upbproto", + ":table", ":upb", ], ) @@ -150,6 +151,7 @@ cc_library( ], copts = COPTS, deps = [ + ":descriptor_upbproto", ":handlers", ":table", ":upb", @@ -180,7 +182,11 @@ cc_library( hdrs = [ "upb/bindings/stdc++/string.h", ], - deps = [":upb"], + deps = [ + ":descriptor_upbproto", + ":handlers", + ":upb", + ], ) # upb compiler ################################################################# @@ -229,6 +235,7 @@ cc_library( "tests/upb_test.h", ], copts = CPPOPTS, + deps = [":handlers"], ) cc_test( @@ -359,18 +366,19 @@ cc_test( upb_proto_library( name = "conformance_proto_upb", - deps = ["@com_google_protobuf//:conformance_proto"], testonly = 1, + deps = ["@com_google_protobuf//:conformance_proto"], ) upb_proto_library( name = "test_messages_proto3_proto_upb", - deps = ["@com_google_protobuf//:test_messages_proto3_proto"], testonly = 1, + deps = ["@com_google_protobuf//:test_messages_proto3_proto"], ) cc_binary( name = "conformance_upb", + testonly = 1, srcs = [ "tests/conformance_upb.c", ], @@ -380,7 +388,6 @@ cc_binary( ":test_messages_proto3_proto_upb", ":upb", ], - testonly = 1, ) make_shell_script( diff --git a/CMakeLists.txt b/CMakeLists.txt index d085f48..1ff4788 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,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 @@ -110,6 +111,7 @@ add_library(upb_pb upb/pb/encoder.h upb/pb/textprinter.h) target_link_libraries(upb_pb + descriptor_upbproto handlers table upb) @@ -123,10 +125,14 @@ 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) -- cgit v1.2.3 From c0a1afa16f5c29fc4edb229efa5b6682b69ebd4b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 11:26:54 -0700 Subject: More Blaze fixes. --- BUILD | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/BUILD b/BUILD index 2197d2c..4d3cdc8 100644 --- a/BUILD +++ b/BUILD @@ -129,6 +129,7 @@ cc_library( copts = COPTS, deps = [ ":reflection", + ":table", ":upb", ], ) @@ -240,7 +241,10 @@ cc_library( 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_pb", @@ -281,7 +285,8 @@ cc_test( srcs = ["tests/pb/test_encoder.cc"], copts = CPPOPTS, deps = [ - "descriptor_upbreflection", + ":descriptor_upbproto", + ":descriptor_upbreflection", ":upb_cc_bindings", ":upb_pb", ":upb_test", -- cgit v1.2.3 From e04216eb0fb70cbd1371a942aaeaf90b1f1b5cd8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 11:30:51 -0700 Subject: More Blaze fixes. --- BUILD | 2 +- CMakeLists.txt | 1 + build_defs.bzl | 11 ++++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/BUILD b/BUILD index 4d3cdc8..2b99cf7 100644 --- a/BUILD +++ b/BUILD @@ -242,7 +242,7 @@ cc_library( cc_test( name = "test_varint", srcs = [ - "tests/pb/test_varint.c" + + "tests/pb/test_varint.c", "upb/pb/varint.int.h", ], copts = COPTS, diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ff4788..24e0b54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/build_defs.bzl b/build_defs.bzl index d30fdc5..fc2c0a4 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -356,7 +356,7 @@ def _upb_proto_rule_impl(ctx): 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] + 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( @@ -394,7 +394,7 @@ _upb_proto_library_aspect = aspect( "_cc_toolchain": attr.label( default = "@bazel_tools//tools/cpp:current_cc_toolchain", ), - "_upb": attr.label(default = ":upb"), + "_upb": attr.label_list(default = [":upb"]), "_ext": attr.string(default = ".upb"), }), implementation = _upb_proto_aspect_impl, @@ -431,7 +431,12 @@ _upb_proto_reflection_library_aspect = aspect( "_cc_toolchain": attr.label( default = "@bazel_tools//tools/cpp:current_cc_toolchain", ), - "_upb": attr.label(default = ":reflection"), + "_upb": attr.label_list( + default = [ + ":upb", + ":reflection", + ], + ), "_ext": attr.string(default = ".upbdefs"), }), implementation = _upb_proto_aspect_impl, -- cgit v1.2.3 From 4451b790bd911743d2d4ee8783fe1bb93c2f4c54 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 11:35:49 -0700 Subject: More Blaze fixes. --- BUILD | 1 + upb/handlers-inl.h | 4 ++++ upb/handlers.h | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/BUILD b/BUILD index 2b99cf7..436b845 100644 --- a/BUILD +++ b/BUILD @@ -247,6 +247,7 @@ cc_test( ], copts = COPTS, deps = [ + ":upb", ":upb_pb", ":upb_test", ], 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 #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::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 */ -- cgit v1.2.3 From 5e5e0cf24aa630d5ee1deb1ebf1032b7b788d9b8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 14:12:33 -0700 Subject: More Blaze fixes. --- BUILD | 14 ++++++++++++-- WORKSPACE | 6 ++++-- tests/conformance_upb.c | 28 +++++++++------------------- tests/test_cpp.cc | 5 ++--- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/BUILD b/BUILD index 436b845..65eb95a 100644 --- a/BUILD +++ b/BUILD @@ -154,6 +154,7 @@ cc_library( deps = [ ":descriptor_upbproto", ":handlers", + ":reflection", ":table", ":upb", ], @@ -236,7 +237,10 @@ cc_library( "tests/upb_test.h", ], copts = CPPOPTS, - deps = [":handlers"], + deps = [ + ":handlers", + ":upb", + ], ) cc_test( @@ -270,7 +274,9 @@ cc_test( srcs = ["tests/pb/test_decoder.cc"], copts = CPPOPTS, deps = [ + ":handlers", ":test_decoder_upbproto", + ":upb", ":upb_pb", ":upb_test", ], @@ -288,6 +294,7 @@ cc_test( deps = [ ":descriptor_upbproto", ":descriptor_upbreflection", + ":upb", ":upb_cc_bindings", ":upb_pb", ":upb_test", @@ -311,6 +318,8 @@ cc_test( srcs = ["tests/test_cpp.cc"], copts = CPPOPTS, deps = [ + ":handlers", + ":reflection", ":test_cpp_upbproto", ":upb", ":upb_pb", @@ -323,6 +332,7 @@ cc_test( srcs = ["tests/test_table.cc"], copts = CPPOPTS, deps = [ + ":table", ":upb", ":upb_test", ], @@ -399,7 +409,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( diff --git a/WORKSPACE b/WORKSPACE index bcb00f0..8a4ef8e 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -22,8 +22,10 @@ http_archive( git_repository( name = "com_google_protobuf", - remote = "https://github.com/protocolbuffers/protobuf.git", - commit = "78ca77ac8799f67fda7b9a01cc691cd9fe526f25", + #remote = "https://github.com/protocolbuffers/protobuf.git", + #commit = "78ca77ac8799f67fda7b9a01cc691cd9fe526f25", + remote = "https://github.com/haberman/protobuf.git", + commit = "c659a4a4db2e27463e51c732df25730973956be2", ) http_archive( 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 #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 void AssertInsert(T* const container, const typename T::value_type& val) { -- cgit v1.2.3 From 1cf4af7b2c3ef7d4795aed2bda936e8e5c43fd02 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 15:04:30 -0700 Subject: A (final?) round of Blaze fixes. --- BUILD | 38 +++++++++++++++++++------------------- WORKSPACE | 15 +++++++-------- build_defs.bzl | 6 +++--- repository_defs.bzl | 5 ++--- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/BUILD b/BUILD index 65eb95a..96d7b7d 100644 --- a/BUILD +++ b/BUILD @@ -282,25 +282,6 @@ cc_test( ], ) -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( name = "test_cpp_proto", srcs = [ @@ -339,6 +320,25 @@ cc_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( name = "test_json_enum_from_separate", srcs = ["tests/json/enum_from_separate_file.proto"], diff --git a/WORKSPACE b/WORKSPACE index 8a4ef8e..a847b51 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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( @@ -22,10 +21,10 @@ http_archive( git_repository( name = "com_google_protobuf", + commit = "c659a4a4db2e27463e51c732df25730973956be2", #remote = "https://github.com/protocolbuffers/protobuf.git", #commit = "78ca77ac8799f67fda7b9a01cc691cd9fe526f25", remote = "https://github.com/haberman/protobuf.git", - commit = "c659a4a4db2e27463e51c732df25730973956be2", ) http_archive( @@ -40,19 +39,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 fc2c0a4..f62f42f 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -1,4 +1,3 @@ - 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") @@ -278,6 +277,7 @@ def cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos): 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 = {} @@ -327,7 +327,7 @@ def _compile_upb_protos(ctx, proto_info, proto_sources, ext): "--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, + progress_message = "Generating upb protos for :" + ctx.label.name, ) return SrcList(srcs = srcs, hdrs = hdrs) @@ -485,7 +485,7 @@ upb_proto_srcs = rule( default = "@com_google_protobuf//:protoc", ), "deps": attr.label_list(), - "ext": attr.string(default = ".upb") + "ext": attr.string(default = ".upb"), }, implementation = _upb_proto_srcs_impl, ) 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, ) -- cgit v1.2.3 From 717db51700c0477397867d4ff5370cd194cd7991 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 15:07:05 -0700 Subject: Updated protobuf to the main branch again. --- CMakeLists.txt | 4 +++- WORKSPACE | 6 ++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 24e0b54..2573173 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,6 +114,7 @@ add_library(upb_pb target_link_libraries(upb_pb descriptor_upbproto handlers + reflection table upb) add_library(upb_json @@ -134,6 +135,7 @@ add_library(upb_test tests/test_util.h tests/upb_test.h) target_link_libraries(upb_test - handlers) + handlers + upb) diff --git a/WORKSPACE b/WORKSPACE index a847b51..74c41c5 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -21,10 +21,8 @@ http_archive( git_repository( name = "com_google_protobuf", - commit = "c659a4a4db2e27463e51c732df25730973956be2", - #remote = "https://github.com/protocolbuffers/protobuf.git", - #commit = "78ca77ac8799f67fda7b9a01cc691cd9fe526f25", - remote = "https://github.com/haberman/protobuf.git", + remote = "https://github.com/protocolbuffers/protobuf.git", + commit = "d41002663fd04325ead28439dfd5ce2822b0d6fb", ) http_archive( -- cgit v1.2.3 From d94c2d3c74377d087e82a99d80e751405f8cca11 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 15:08:56 -0700 Subject: Another fix of course. --- BUILD | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/BUILD b/BUILD index 96d7b7d..da2454b 100644 --- a/BUILD +++ b/BUILD @@ -271,7 +271,10 @@ upb_proto_reflection_library( cc_test( name = "test_decoder", - srcs = ["tests/pb/test_decoder.cc"], + srcs = [ + "tests/pb/test_decoder.cc", + "upb/pb/varint.int.h", + ], copts = CPPOPTS, deps = [ ":handlers", -- cgit v1.2.3 From 06b90f9b693b43a007ee2c20f5e7a98595bd8721 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 15:58:58 -0700 Subject: Split upb_proto_library.bzl into its own .bzl file. --- BUILD | 14 +-- build_defs.bzl | 324 +++++--------------------------------------------- upb_proto_library.bzl | 291 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 324 insertions(+), 305 deletions(-) create mode 100644 upb_proto_library.bzl diff --git a/BUILD b/BUILD index da2454b..025f728 100644 --- a/BUILD +++ b/BUILD @@ -8,9 +8,12 @@ load( "lua_test", "make_shell_script", "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) @@ -434,11 +437,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 = [ @@ -448,7 +446,7 @@ upb_amalgamation( amalgamator = ":amalgamate", libs = [ ":upb", - ":descriptor_upbproto_srcs", + ":descriptor_upbproto", ":reflection", ":handlers", ":upb_pb", @@ -590,7 +588,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", diff --git a/build_defs.bzl b/build_defs.bzl index f62f42f..44ef0a3 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -1,14 +1,31 @@ -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") +"""Internal rules for building upb.""" -# copybara:strip_for_google3_begin -load("@bazel_version//:bazel_version.bzl", "bazel_version") -# copybara:strip_end +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" @@ -157,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 = [] @@ -188,6 +206,7 @@ def _upb_amalgamation(ctx): progress_message = "Making amalgamation", executable = ctx.executable.amalgamator, ) + return [] upb_amalgamation = rule( attrs = { @@ -201,295 +220,6 @@ upb_amalgamation = rule( implementation = _upb_amalgamation, ) -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 - -# 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, - ) - - # 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, - ) - -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 ############################################# - -_WrappedCcInfo = provider(fields = ["cc_info"]) - -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: - fail("proto_library rule must generate _WrappedCcInfo (aspect should have handled this).") - cc_info = dep[_WrappedCcInfo].cc_info - 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)), - 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)] - -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], - ), - }, -) - -# 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 = "@com_google_protobuf//:protoc", - ), - "deps": attr.label_list(), - "ext": attr.string(default = ".upb"), - }, - implementation = _upb_proto_srcs_impl, -) - def licenses(*args): # No-op (for Google-internal usage). pass 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], + ), + }, +) -- cgit v1.2.3 From ef9499cb44f58904d55997a9f934f1e477bcba18 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 14 May 2019 16:14:22 -0700 Subject: Migrate std::unordered_map -> absl::flat_hash_map. --- BUILD | 1 + upbc/generator.cc | 5 ++--- upbc/message_layout.h | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/BUILD b/BUILD index 025f728..66672ce 100644 --- a/BUILD +++ b/BUILD @@ -207,6 +207,7 @@ cc_library( copts = CPPOPTS, deps = [ "@absl//absl/base:core_headers", + "@absl//absl/container:flat_hash_map", "@absl//absl/strings", "@com_google_protobuf//:protobuf", "@com_google_protobuf//:protoc_lib", diff --git a/upbc/generator.cc b/upbc/generator.cc index 5843597..1337e28 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -1,8 +1,7 @@ -#include -#include #include +#include "absl/container/flat_hash_map.h" #include "absl/strings/ascii.h" #include "absl/strings/str_replace.h" #include "absl/strings/substitute.h" @@ -594,7 +593,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 submsg_indexes; + absl::flat_hash_map submsg_indexes; MessageLayout layout(message); std::vector 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 #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 - static V GetMapValue(const std::unordered_map& map, K key) { + static V GetMapValue(const absl::flat_hash_map& 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 + absl::flat_hash_map field_offsets_; - std::unordered_map + absl::flat_hash_map hasbit_indexes_; - std::unordered_map + absl::flat_hash_map oneof_case_offsets_; Size maxalign_; Size size_; -- cgit v1.2.3 From c58541ea04b882b03b7868b60166518c550e6dca Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 15 May 2019 09:49:29 -0700 Subject: Added support for public dependencies. --- BUILD | 1 + .../google/protobuf/descriptor.upb.h | 33 ++------------------- upbc/generator.cc | 34 ++++++++++++++++------ 3 files changed, 28 insertions(+), 40 deletions(-) diff --git a/BUILD b/BUILD index 66672ce..362417a 100644 --- a/BUILD +++ b/BUILD @@ -74,6 +74,7 @@ cc_library( upb_proto_library( name = "descriptor_upbproto", deps = ["@com_google_protobuf//:descriptor_proto"], + visibility = ["//visibility:public"], ) cc_library( 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/upbc/generator.cc b/upbc/generator.cc index 1337e28..53d849e 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -487,7 +487,7 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output } } - output("\n\n"); + output("\n"); } void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { @@ -495,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 this_file_messages = SortedMessages(file); @@ -540,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 this_file_enums = SortedEnums(file); - output( - "\n" - "/* Enums */\n\n"); for (auto enumdesc : this_file_enums) { output("typedef enum {\n"); DumpEnumValues(enumdesc, output); -- cgit v1.2.3 From 44817c02faad747f2c8d314ea240de3f91e30cfe Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 15 May 2019 10:10:52 -0700 Subject: Added package() statement. --- BUILD | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/BUILD b/BUILD index 362417a..55f18c7 100644 --- a/BUILD +++ b/BUILD @@ -16,6 +16,10 @@ load( "upb_proto_reflection_library", ) +package( + default_visibility = ["//visibility:private"], +) + licenses(["notice"]) # BSD (Google-authored w/ possible external contributions) exports_files([ -- cgit v1.2.3 From a126b9a046d8a5e0b73e363d815ae45f0ae748b6 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 15 May 2019 10:27:18 -0700 Subject: Removed package(), Bazel doesn't support it. --- BUILD | 4 ---- 1 file changed, 4 deletions(-) diff --git a/BUILD b/BUILD index 55f18c7..362417a 100644 --- a/BUILD +++ b/BUILD @@ -16,10 +16,6 @@ load( "upb_proto_reflection_library", ) -package( - default_visibility = ["//visibility:private"], -) - licenses(["notice"]) # BSD (Google-authored w/ possible external contributions) exports_files([ -- cgit v1.2.3