Skip to content

Commit

Permalink
upb: start consolidating the upb/message/ build targets
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 593625460
  • Loading branch information
ericsalo authored and copybara-github committed Dec 27, 2023
1 parent 2f7b283 commit a75144c
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 118 deletions.
4 changes: 2 additions & 2 deletions protos/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 7 additions & 22 deletions upb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ cc_library(
":mem",
":message",
":message_accessors",
":message_accessors_internal",
":message_internal",
":mini_descriptor",
":mini_table",
":wire",
Expand Down Expand Up @@ -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"],
)

Expand All @@ -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",
Expand Down Expand Up @@ -351,10 +337,12 @@ cc_binary(
":mem",
":message",
":message_accessors",
":message_compare",
":message_split64",
":mini_descriptor",
":mini_table",
":port",
":wire",
],
)

Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
74 changes: 16 additions & 58 deletions upb/message/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand All @@ -73,8 +70,6 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
":accessors",
":accessors_internal",
":internal",
":message",
":tagged_ptr",
":types",
Expand All @@ -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",
Expand All @@ -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 = [
Expand All @@ -171,8 +133,6 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
":accessors",
":accessors_internal",
":internal",
":message",
":tagged_ptr",
":types",
Expand Down Expand Up @@ -302,7 +262,6 @@ cc_test(
deps = [
":accessors",
":copy",
":internal",
":message",
"//:protobuf",
"@com_google_googletest//:gtest_main",
Expand Down Expand Up @@ -335,7 +294,6 @@ cc_test(
deps = [
":accessors",
":copy",
":internal",
":message",
":promote",
":tagged_ptr",
Expand Down
25 changes: 0 additions & 25 deletions upb/message/accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}
4 changes: 0 additions & 4 deletions upb/message/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions upb/message/compare.c
Original file line number Diff line number Diff line change
@@ -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 <string.h>

#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;
}
32 changes: 32 additions & 0 deletions upb/message/compare.h
Original file line number Diff line number Diff line change
@@ -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_
2 changes: 1 addition & 1 deletion upb/mini_descriptor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit a75144c

Please sign in to comment.