Skip to content

Commit 75e8d66

Browse files
habermancopybara-github
authored andcommitted
Factored out logic for undefing/restoring OS macros so it can be reused by hpb.
PiperOrigin-RevId: 804513731
1 parent c9c7c43 commit 75e8d66

File tree

13 files changed

+330
-246
lines changed

13 files changed

+330
-246
lines changed

cmake/installed_include_golden.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ google/protobuf/message_lite.h
105105
google/protobuf/metadata.h
106106
google/protobuf/metadata_lite.h
107107
google/protobuf/micro_string.h
108+
google/protobuf/os_macros_restore.inc
109+
google/protobuf/os_macros_undef.inc
108110
google/protobuf/parse_context.h
109111
google/protobuf/port.h
110112
google/protobuf/port_def.inc

hpb/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ cc_library(
111111
deps = [
112112
":hpb",
113113
":repeated_field",
114+
"//hpb/internal:os_macros",
114115
"//upb/mem",
115116
"//upb/message",
116117
],

hpb/bazel/hpb_proto_library.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ _hpb_proto_library_aspect = aspect(
133133
# TODO: Add dependencies for cc runtime (absl/string etc..)
134134
"//upb:generated_cpp_support",
135135
"//hpb:generated_hpb_support",
136+
"//hpb/internal:os_macros",
136137
"@abseil-cpp//absl/log:absl_check",
137138
"@abseil-cpp//absl/strings",
138139
"@abseil-cpp//absl/status:statusor",

hpb/internal/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ load("@rules_cc//cc:cc_test.bzl", "cc_test")
1010

1111
package(default_applicable_licenses = ["//:license"])
1212

13+
cc_library(
14+
name = "os_macros",
15+
textual_hdrs = [
16+
"os_macros_undef.inc",
17+
"os_macros_restore.inc",
18+
],
19+
visibility = ["//hpb:__subpackages__"],
20+
deps = [
21+
"//src/google/protobuf:os_macros",
22+
],
23+
)
24+
1325
cc_library(
1426
name = "template_help",
1527
hdrs = ["template_help.h"],

hpb/internal/os_macros_restore.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2025 Google LLC. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
#include "google/protobuf/os_macros_restore.inc"
9+
10+
// For legacy reasons, the major/minor macros are not handled in the include
11+
// above.
12+
#pragma pop_macro("major")
13+
#pragma pop_macro("minor")

hpb/internal/os_macros_undef.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2025 Google LLC. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
#include "google/protobuf/os_macros_undef.inc"
9+
10+
// For legacy reasons, the major/minor macros are not handled in the include
11+
// above.
12+
#pragma push_macro("major")
13+
#undef major
14+
#pragma push_macro("minor")
15+
#undef minor

hpb_generator/generator.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
113113
}
114114

115115
ctx.Emit("#include \"upb/port/def.inc\"\n");
116+
ctx.Emit(
117+
"#include \"third_party/protobuf/hpb/internal/os_macros_undef.inc\"\n");
116118

117119
const std::vector<const google::protobuf::Descriptor*> this_file_messages =
118120
SortedMessages(file);
@@ -144,6 +146,8 @@ void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
144146
ctx.Emit("\n");
145147
});
146148

149+
ctx.Emit(
150+
"#include \"third_party/protobuf/hpb/internal/os_macros_restore.inc\"\n");
147151
ctx.Emit("\n#include \"upb/port/undef.inc\"\n\n");
148152
// End of "C" section.
149153

hpb_generator/tests/naming_conflict.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ message HasChildCount {
1313
HasChildCount has_child_count = 1;
1414
int32 child_count = 2;
1515
}
16+
17+
message ConflictingOsMacros {
18+
int32 linux = 1;
19+
int32 major = 2;
20+
int32 minor = 3;
21+
}

src/google/protobuf/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ cc_proto_library(
356356
# C++ Runtime Library
357357
################################################################################
358358

359+
cc_library(
360+
name = "os_macros",
361+
hdrs = [
362+
"os_macros_restore.inc",
363+
"os_macros_undef.inc",
364+
],
365+
strip_include_prefix = "/src",
366+
visibility = ["//hpb:__subpackages__"],
367+
)
368+
359369
cc_library(
360370
name = "port",
361371
srcs = ["port.cc"],
@@ -370,6 +380,7 @@ cc_library(
370380
"//src/google/protobuf:__subpackages__",
371381
],
372382
deps = [
383+
":os_macros",
373384
"@abseil-cpp//absl/base:config",
374385
"@abseil-cpp//absl/base:core_headers",
375386
"@abseil-cpp//absl/base:prefetch",
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2025 Google LLC. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
// Restore macros that may have been #undef'd in os_macros_undef.inc.
9+
10+
#ifdef PROTOBUF_DID_UNDEF_noreturn
11+
#pragma pop_macro("noreturn")
12+
#undef PROTOBUF_DID_UNDEF_noreturn
13+
#endif
14+
15+
#ifdef PROTOBUF_DID_UNDEF_PACKAGE
16+
#pragma pop_macro("PACKAGE")
17+
#undef PROTOBUF_DID_UNDEF_PACKAGE
18+
#endif
19+
20+
#ifdef PROTOBUF_DID_UNDEF_PACKED
21+
#pragma pop_macro("PACKED")
22+
#undef PROTOBUF_DID_UNDEF_PACKED
23+
#endif
24+
25+
#ifdef PROTOBUF_DID_UNDEF_DOMAIN
26+
#pragma pop_macro("DOMAIN")
27+
#undef PROTOBUF_DID_UNDEF_DOMAIN
28+
#endif
29+
30+
#ifdef PROTOBUF_DID_UNDEF_LINUX
31+
#pragma pop_macro("linux")
32+
#endif
33+
34+
#ifdef PROTOBUF_DID_UNDEF_NO_DATA
35+
#pragma pop_macro("NO_DATA")
36+
#undef PROTOBUF_DID_UNDEF_NO_DATA
37+
#endif
38+
39+
#ifdef _WIN32
40+
#pragma pop_macro("CompareString")
41+
#pragma pop_macro("CREATE_NEW")
42+
#pragma pop_macro("DELETE")
43+
#pragma pop_macro("DOUBLE_CLICK")
44+
#pragma pop_macro("ERROR")
45+
#pragma pop_macro("ERROR_BUSY")
46+
#pragma pop_macro("ERROR_INSTALL_FAILED")
47+
#pragma pop_macro("ERROR_NOT_FOUND")
48+
#pragma pop_macro("ERROR_RETRY")
49+
#pragma pop_macro("ERROR_TIMEOUT")
50+
#pragma pop_macro("GetClassName")
51+
#pragma pop_macro("GetCurrentTime")
52+
#pragma pop_macro("GetMessage")
53+
#pragma pop_macro("GetObject")
54+
#pragma pop_macro("IGNORE")
55+
#pragma pop_macro("IN")
56+
#pragma pop_macro("INPUT_KEYBOARD")
57+
#pragma pop_macro("OUT")
58+
#pragma pop_macro("OPTIONAL")
59+
#pragma pop_macro("min")
60+
#pragma pop_macro("max")
61+
#pragma pop_macro("NEAR")
62+
#pragma pop_macro("NO_DATA")
63+
#pragma pop_macro("NO_ERROR")
64+
#pragma pop_macro("REASON_UNKNOWN")
65+
#pragma pop_macro("SERVICE_DISABLED")
66+
#pragma pop_macro("SERVICE_STOP")
67+
#pragma pop_macro("SEVERITY_ERROR")
68+
#pragma pop_macro("STRICT")
69+
#pragma pop_macro("STATUS_PENDING")
70+
#pragma pop_macro("timezone")
71+
#pragma pop_macro("TRUE")
72+
#pragma pop_macro("FALSE")
73+
#endif
74+
75+
#ifdef __APPLE__
76+
#pragma pop_macro("TRUE")
77+
#pragma pop_macro("FALSE")
78+
#pragma pop_macro("UID_MAX")
79+
#pragma pop_macro("GID_MAX")
80+
#pragma pop_macro("TYPE_BOOL")
81+
#endif // __APPLE__
82+
83+
#if defined(ANDROID) || defined(__ANDROID__)
84+
#pragma pop_macro("UID_MAX")
85+
#pragma pop_macro("GID_MAX")
86+
#endif // defined(ANDROID) || defined(__ANDROID__)
87+
88+
#if defined(__FreeBSD__) || defined(__OpenBSD__)
89+
#pragma pop_macro("TRUE")
90+
#pragma pop_macro("FALSE")
91+
#pragma pop_macro("UID_MAX")
92+
#pragma pop_macro("GID_MAX")
93+
#endif // defined(__FreeBSD__) || defined(__OpenBSD__)
94+
95+
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
96+
#pragma pop_macro("DEBUG")
97+
#endif // defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)

0 commit comments

Comments
 (0)