From 2573146b62da155f9d4fb7e1c1eb50d1077714f5 Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Mon, 25 Dec 2023 10:15:54 -0800 Subject: [PATCH] upb: start consolidating the upb/message/ build targets PiperOrigin-RevId: 593625460 --- protos/BUILD | 4 +-- upb/BUILD | 29 ++++----------- upb/message/BUILD | 74 +++++++++------------------------------ upb/message/accessors.c | 25 ------------- upb/message/accessors.h | 4 --- upb/message/compare.c | 43 +++++++++++++++++++++++ upb/message/compare.h | 32 +++++++++++++++++ upb/mini_descriptor/BUILD | 2 +- upb/reflection/BUILD | 2 -- upb/text/BUILD | 1 - upb/upb_so.c | 3 ++ upb/wire/BUILD | 4 +-- 12 files changed, 105 insertions(+), 118 deletions(-) create mode 100644 upb/message/compare.c create mode 100644 upb/message/compare.h diff --git a/protos/BUILD b/protos/BUILD index b68a27eaac64d..7712c38bcb971 100644 --- a/protos/BUILD +++ b/protos/BUILD @@ -55,9 +55,9 @@ cc_library( ":protos_extension_lock", "//upb:base", "//upb:mem", - "//upb:message_accessors_internal", + "//upb:message", + "//upb:message_accessors", "//upb:message_copy", - "//upb:message_internal", "//upb:message_promote", "//upb:message_types", "//upb:mini_table", diff --git a/upb/BUILD b/upb/BUILD index 6ac5299826ce2..ff7ff72c49204 100644 --- a/upb/BUILD +++ b/upb/BUILD @@ -112,8 +112,6 @@ cc_library( ":mem", ":message", ":message_accessors", - ":message_accessors_internal", - ":message_internal", ":mini_descriptor", ":mini_table", ":wire", @@ -213,8 +211,8 @@ alias( ) alias( - name = "message_accessors_internal", - actual = "//upb/message:accessors_internal", + name = "message_compare", + actual = "//upb/message:compare", visibility = ["//upb:friends"], ) @@ -224,18 +222,6 @@ alias( visibility = ["//visibility:public"], ) -alias( - name = "message_internal", - actual = "//upb/message:internal", - visibility = ["//visibility:public"], -) - -alias( - name = "message_internal_types", - actual = "//upb/message:internal_types", - visibility = ["//visibility:public"], -) - alias( name = "message_promote", actual = "//upb/message:promote", @@ -351,10 +337,12 @@ cc_binary( ":mem", ":message", ":message_accessors", + ":message_compare", ":message_split64", ":mini_descriptor", ":mini_table", ":port", + ":wire", ], ) @@ -380,9 +368,8 @@ upb_amalgamation( ":mem", ":message", ":message_accessors", + ":message_compare", ":message_copy", - ":message_internal", - ":message_internal_types", ":message_tagged_ptr", ":message_types", ":message_value", @@ -429,9 +416,8 @@ upb_amalgamation( ":mem", ":message", ":message_accessors", + ":message_compare", ":message_copy", - ":message_internal", - ":message_internal_types", ":message_tagged_ptr", ":message_types", ":message_value", @@ -479,9 +465,8 @@ upb_amalgamation( ":mem", ":message", ":message_accessors", + ":message_compare", ":message_copy", - ":message_internal", - ":message_internal_types", ":message_tagged_ptr", ":message_types", ":message_value", diff --git a/upb/message/BUILD b/upb/message/BUILD index cfb883e24e90f..298acd918573d 100644 --- a/upb/message/BUILD +++ b/upb/message/BUILD @@ -20,44 +20,41 @@ cc_library( name = "accessors", srcs = [ "accessors.c", - "internal/accessors.h", ], hdrs = [ "accessors.h", + "internal/accessors.h", ], copts = UPB_DEFAULT_COPTS, visibility = ["//visibility:public"], deps = [ - ":internal", - ":internal_types", ":message", ":tagged_ptr", + ":types", "//upb:base", "//upb:mem", - "//upb:message_types", "//upb:mini_table", "//upb:port", - "//upb:wire", - "//upb:wire_reader", ], ) cc_library( - name = "accessors_internal", + name = "compare", + srcs = [ + "compare.c", + ], hdrs = [ - "internal/accessors.h", + "compare.h", ], copts = UPB_DEFAULT_COPTS, visibility = ["//visibility:public"], deps = [ - ":internal", - ":internal_types", ":message", - ":tagged_ptr", - "//upb:base", + ":types", "//upb:mem", "//upb:mini_table", "//upb:port", + "//upb:wire", ], ) @@ -73,8 +70,6 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":accessors", - ":accessors_internal", - ":internal", ":message", ":tagged_ptr", ":types", @@ -87,31 +82,33 @@ cc_library( ) cc_library( - name = "internal", + name = "message", srcs = [ "array.c", - "array.h", + "compat.c", "internal/extension.c", "internal/message.c", "map.c", - "map.h", "map_sorter.c", "message.c", - "message.h", ], hdrs = [ + "array.h", + "compat.h", "internal/array.h", "internal/extension.h", "internal/map.h", "internal/map_entry.h", "internal/map_sorter.h", "internal/message.h", + "internal/types.h", + "map.h", "map_gencode_util.h", + "message.h", ], copts = UPB_DEFAULT_COPTS, visibility = ["//visibility:public"], deps = [ - ":internal_types", ":types", ":value", "//upb:base", @@ -124,41 +121,6 @@ cc_library( ], ) -cc_library( - name = "internal_types", - hdrs = [ - "internal/types.h", - ], - copts = UPB_DEFAULT_COPTS, - visibility = ["//visibility:public"], - deps = [ - ], -) - -cc_library( - name = "message", - srcs = [ - "compat.c", - ], - hdrs = [ - "array.h", - "compat.h", - "map.h", - "message.h", - ], - copts = UPB_DEFAULT_COPTS, - visibility = ["//visibility:public"], - deps = [ - ":internal", - ":types", - ":value", - "//upb:base", - "//upb:mem", - "//upb:mini_table", - "//upb:port", - ], -) - cc_library( name = "promote", srcs = [ @@ -171,8 +133,6 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":accessors", - ":accessors_internal", - ":internal", ":message", ":tagged_ptr", ":types", @@ -302,7 +262,6 @@ cc_test( deps = [ ":accessors", ":copy", - ":internal", ":message", "//:protobuf", "@com_google_googletest//:gtest_main", @@ -335,7 +294,6 @@ cc_test( deps = [ ":accessors", ":copy", - ":internal", ":message", ":promote", ":tagged_ptr", diff --git a/upb/message/accessors.c b/upb/message/accessors.c index 0153956f12d22..2b02c82571a6c 100644 --- a/upb/message/accessors.c +++ b/upb/message/accessors.c @@ -16,7 +16,6 @@ #include "upb/mini_table/field.h" #include "upb/mini_table/message.h" #include "upb/mini_table/sub.h" -#include "upb/wire/encode.h" // Must be last. #include "upb/port/def.inc" @@ -43,27 +42,3 @@ bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table, map_entry_message, map_entry_value_field, default_val); return upb_Map_Set(map, map_entry_key, map_entry_value, arena); } - -bool upb_Message_IsExactlyEqual(const upb_Message* m1, const upb_Message* m2, - const upb_MiniTable* layout) { - if (m1 == m2) return true; - - int opts = kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic; - upb_Arena* a = upb_Arena_New(); - - // Compare deterministically serialized payloads with no unknown fields. - size_t size1, size2; - char *data1, *data2; - upb_EncodeStatus status1 = upb_Encode(m1, layout, opts, a, &data1, &size1); - upb_EncodeStatus status2 = upb_Encode(m2, layout, opts, a, &data2, &size2); - - if (status1 != kUpb_EncodeStatus_Ok || status2 != kUpb_EncodeStatus_Ok) { - // TODO: How should we fail here? (In Ruby we throw an exception.) - upb_Arena_Free(a); - return false; - } - - const bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0); - upb_Arena_Free(a); - return ret; -} diff --git a/upb/message/accessors.h b/upb/message/accessors.h index ecd1751cf834b..ac5788ebf8123 100644 --- a/upb/message/accessors.h +++ b/upb/message/accessors.h @@ -449,10 +449,6 @@ bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table, const upb_MiniTableField* field, upb_Message* map_entry_message, upb_Arena* arena); -// Compares two messages by serializing them and calling memcmp(). -bool upb_Message_IsExactlyEqual(const upb_Message* m1, const upb_Message* m2, - const upb_MiniTable* layout); - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/upb/message/compare.c b/upb/message/compare.c new file mode 100644 index 0000000000000..5a10164b7c9c6 --- /dev/null +++ b/upb/message/compare.c @@ -0,0 +1,43 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "upb/message/compare.h" + +#include + +#include "upb/mem/arena.h" +#include "upb/message/message.h" +#include "upb/mini_table/message.h" +#include "upb/wire/encode.h" + +// Must be last. +#include "upb/port/def.inc" + +bool upb_Message_IsExactlyEqual(const upb_Message* msg1, + const upb_Message* msg2, + const upb_MiniTable* m) { + if (msg1 == msg2) return true; + + int opts = kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic; + upb_Arena* a = upb_Arena_New(); + + // Compare deterministically serialized payloads with no unknown fields. + size_t size1, size2; + char *data1, *data2; + upb_EncodeStatus status1 = upb_Encode(msg1, m, opts, a, &data1, &size1); + upb_EncodeStatus status2 = upb_Encode(msg2, m, opts, a, &data2, &size2); + + if (status1 != kUpb_EncodeStatus_Ok || status2 != kUpb_EncodeStatus_Ok) { + // TODO: How should we fail here? (In Ruby we throw an exception.) + upb_Arena_Free(a); + return false; + } + + const bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0); + upb_Arena_Free(a); + return ret; +} diff --git a/upb/message/compare.h b/upb/message/compare.h new file mode 100644 index 0000000000000..98d165c5de2e8 --- /dev/null +++ b/upb/message/compare.h @@ -0,0 +1,32 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef UPB_MESSAGE_COMPARE_H_ +#define UPB_MESSAGE_COMPARE_H_ + +#include "upb/message/types.h" +#include "upb/mini_table/message.h" + +// Must be last. +#include "upb/port/def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +// Compares two messages by serializing them and calling memcmp(). +UPB_API bool upb_Message_IsExactlyEqual(const upb_Message* msg1, + const upb_Message* msg2, + const upb_MiniTable* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port/undef.inc" + +#endif // UPB_MESSAGE_COMPARE_H_ diff --git a/upb/mini_descriptor/BUILD b/upb/mini_descriptor/BUILD index fafcf419c931e..c4247eb01840b 100644 --- a/upb/mini_descriptor/BUILD +++ b/upb/mini_descriptor/BUILD @@ -63,7 +63,7 @@ cc_test( "@com_google_googletest//:gtest_main", "//upb:base", "//upb:mem", - "//upb:message_accessors_internal", + "//upb:message_accessors", "//upb:mini_table", "//upb:port", "//upb:wire", diff --git a/upb/reflection/BUILD b/upb/reflection/BUILD index c3407c93d3156..72562999b6334 100644 --- a/upb/reflection/BUILD +++ b/upb/reflection/BUILD @@ -138,9 +138,7 @@ bootstrap_cc_library( "//upb:mem", "//upb:message", "//upb:message_accessors", - "//upb:message_accessors_internal", "//upb:message_copy", - "//upb:message_internal", "//upb:message_value", "//upb:mini_descriptor", "//upb:mini_descriptor_internal", diff --git a/upb/text/BUILD b/upb/text/BUILD index e3826c92ce3a9..7bc0953946e9f 100644 --- a/upb/text/BUILD +++ b/upb/text/BUILD @@ -21,7 +21,6 @@ cc_library( "//upb:eps_copy_input_stream", "//upb:lex", "//upb:message", - "//upb:message_internal", "//upb:port", "//upb:reflection", "//upb:wire", diff --git a/upb/upb_so.c b/upb/upb_so.c index a7bb3e7ffc812..a8769ab79d7d4 100644 --- a/upb/upb_so.c +++ b/upb/upb_so.c @@ -10,7 +10,10 @@ // IWYU pragma: begin_exports #include "upb/message/accessors_split64.h" #include "upb/message/array_split64.h" +#include "upb/message/compare.h" #include "upb/message/map.h" #include "upb/message/message.h" #include "upb/mini_descriptor/decode.h" +#include "upb/wire/decode.h" +#include "upb/wire/encode.h" // IWYU pragma: end_exports diff --git a/upb/wire/BUILD b/upb/wire/BUILD index 3e93221067a21..5130a711cd478 100644 --- a/upb/wire/BUILD +++ b/upb/wire/BUILD @@ -30,9 +30,7 @@ cc_library( "//upb:hash", "//upb:mem", "//upb:message", - "//upb:message_accessors_internal", - "//upb:message_internal", - "//upb:message_internal_types", + "//upb:message_accessors", "//upb:message_tagged_ptr", "//upb:message_types", "//upb:mini_table",