Skip to content

Commit d6722c7

Browse files
committed
Fix transitive includes
94b36f1500126afc596f281469a94124efa9cda4
1 parent b949747 commit d6722c7

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"by_platform": {
3-
"darwin-arm64": {
4-
"uri": "sbr:3519867173"
3+
"darwin": {
4+
"uri": "sbr:7041923114"
55
},
6-
"darwin-x86_64": {
7-
"uri": "sbr:3519867173"
6+
"darwin-arm64": {
7+
"uri": "sbr:7041925436"
88
},
9-
"linux-x86_64": {
10-
"uri": "sbr:3519867841"
9+
"linux": {
10+
"uri": "sbr:7041926554"
1111
},
12-
"win32-x86_64": {
13-
"uri": "sbr:3519866587"
12+
"win32": {
13+
"uri": "sbr:7041924289"
1414
}
1515
}
16-
}
16+
}

build/scripts/cpp_proto_wrapper.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
import re
55
import argparse
6-
6+
import shutil
77

88
FROM_RE = re.compile(r"((?:struct|class)\s+\S+\s+)final\s*:")
99
TO_RE = r"\1:"
@@ -17,18 +17,67 @@ def parse_args() -> argparse.Namespace:
1717

1818

1919
def patch_proto_file(text: str) -> tuple[str, int]:
20-
return re.subn(FROM_RE, TO_RE, text)
20+
num_patches = 0
21+
patches = [
22+
(re.compile(r"((?:struct|class)\s+\S+\s+)final\s*:"), r"\1:"),
23+
(re.compile(r'(#include.*?)(\.proto\.h)"'), r'\1.pb.h"')
24+
]
25+
for from_re, to_re in patches:
26+
text, n = re.subn(from_re, to_re, text)
27+
num_patches += n
28+
return text, num_patches
29+
30+
31+
def strip_file_ext(path: str) -> str:
32+
dirname, filename = os.path.dirname(path), os.path.basename(path)
33+
filename = filename.split('.')[0]
34+
return os.path.join(dirname, filename)
35+
36+
37+
def change_file_ext(path: str, change_map: dict[str, str]) -> str:
38+
dirname, filename = os.path.dirname(path), os.path.basename(path)
39+
filename = filename.split('.')
40+
filename, ext = filename[0], '.' + '.'.join(filename[1:])
41+
if not change_map.get(ext):
42+
return
43+
new_ext = change_map[ext]
44+
old = os.path.join(dirname, filename + ext)
45+
new = os.path.join(dirname, filename + new_ext)
46+
shutil.move(old, new)
47+
return new
2148

2249

2350
def main(namespace: argparse.Namespace) -> int:
51+
lite_protobuf_headers = any(out.endswith('.deps.pb.h') for out in namespace.outputs)
52+
ev_proto = any(out.endswith('.ev.pb.h') for out in namespace.outputs)
53+
if ev_proto:
54+
pattern = re.compile(r'proto_h=true:')
55+
disable_lite_headers = lambda s: re.sub(pattern, '', s)
56+
namespace.subcommand = [disable_lite_headers(argv) for argv in namespace.subcommand]
2457
try:
25-
subprocess.check_output(namespace.subcommand, stdin=None, stderr=subprocess.STDOUT)
58+
env = os.environ.copy()
59+
if lite_protobuf_headers:
60+
env['PROTOC_PLUGINS_LITE_HEADERS']='1'
61+
subprocess.check_output(namespace.subcommand, stdin=None, stderr=subprocess.STDOUT, env=env)
2662
except subprocess.CalledProcessError as e:
2763
sys.stderr.write(
2864
'{} returned non-zero exit code {}.\n{}\n'.format(' '.join(e.cmd), e.returncode, e.output.decode('utf-8', errors='ignore'))
2965
)
3066
return e.returncode
3167

68+
if lite_protobuf_headers:
69+
paths = [strip_file_ext(out) for out in namespace.outputs if out.endswith('.deps.pb.h')]
70+
proto_h_files = [out + '.proto.h' for out in paths]
71+
pb_h_files = [out + '.pb.h' for out in paths]
72+
73+
change_map = {
74+
'.proto.h': '.pb.h',
75+
'.pb.h': '.deps.pb.h',
76+
}
77+
[change_file_ext(out, change_map) for out in pb_h_files]
78+
[change_file_ext(out, change_map) for out in proto_h_files]
79+
80+
3281
for output in namespace.outputs:
3382
with open(output, 'rt', encoding="utf-8") as f:
3483
patched_text, num_patches = patch_proto_file(f.read())

build/ymake.core.conf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,10 @@ module _BASE_UNIT: _BARE_UNIT {
626626
PROTOBUF_INCLUDE_PATH=${ARCADIA_ROOT}/contrib/libs/protobuf_std/src
627627
_PROTO_CMDLINE=$_CPP_VANILLA_PROTO_CMDLINE
628628
}
629-
#when ($PROTOC_TRANSITIVE_HEADERS == "no") {
630-
# CPP_PROTO_PLUGINS=transitive_pb_h=false:${CPP_PROTO_PLUGINS}
631-
# CPP_PROTO_OUTS+=${output;main;norel;nopath;noext:File.deps.pb.h}
632-
#}
629+
when ($PROTOC_TRANSITIVE_HEADERS == "no") {
630+
CPP_PROTO_PLUGINS=proto_h=true:${CPP_PROTO_PLUGINS}
631+
CPP_PROTO_OUTS+=${output;main;norel;nopath;noext:File.deps.pb.h}
632+
}
633633
}
634634

635635
SANITIZER_DEFINED=no

contrib/libs/protoc/src/google/protobuf/compiler/cpp/file.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ void FileGenerator::GenerateProtoHeader(io::Printer* p,
285285
}
286286
if (IsBootstrapProto(options_, file_)) {
287287
p->Emit({{"name", StripProto(file_->name())}}, R"cc(
288-
// IWYU pragma: private, include "$name$.proto.h"
288+
// IWYU pragma: private, include "$name$.pb.h"
289289
)cc");
290290
}
291291

@@ -297,7 +297,7 @@ void FileGenerator::GenerateProtoHeader(io::Printer* p,
297297
for (int i = 0; i < file_->public_dependency_count(); ++i) {
298298
const FileDescriptor* dep = file_->public_dependency(i);
299299
p->Emit({{"name", StripProto(dep->name())}}, R"(
300-
#include "$name$.proto.h"
300+
#)" R"(include "$name$.pb.h"
301301
)");
302302
}
303303
}},
@@ -485,7 +485,7 @@ void FileGenerator::GenerateSourceIncludes(io::Printer* p) {
485485
GetBootstrapBasename(options_, basename, &basename);
486486
}
487487
p->Emit({{"name", basename}}, R"(
488-
#include "$name$.proto.h"
488+
#)" R"(include "$name$.pb.h"
489489
)");
490490
}
491491
}

contrib/tools/protoc/plugins/cpp_styleguide/cpp_styleguide.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ namespace NPlugins {
4040

4141
TProtoStringType HeaderFileName(const FileDescriptor* file) {
4242
TProtoStringType basename = compiler::StripProto(file->name());
43-
return basename.append(".pb.h");
43+
bool use_proto_h = !!getenv("PROTOC_PLUGINS_LITE_HEADERS");
44+
return use_proto_h ? basename.append(".proto.h") : basename.append(".pb.h");
4445
}
4546

4647
TProtoStringType SourceFileName(const FileDescriptor* file) {

0 commit comments

Comments
 (0)