Skip to content

Commit cc4556c

Browse files
committed
Support protobuf generation for proto files that are located within
subdirectories of the proto_library rule that consumed it. - Adds _get_relative_dirname utility method. - Adds test case.
1 parent 6b0e1ae commit cc4556c

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ script:
3636
examples/helloworld/java/... \
3737
examples/helloworld/closure/... \
3838
examples/wkt/... \
39+
tests/... \
3940
$FLAGS
4041
4142
notifications:

protobuf/internal/proto_compile.bzl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ def _emit_params_file_action(ctx, path, mnemonic, cmds):
2424
return f
2525

2626

27+
def _get_relative_dirname(base, file):
28+
"""Return a dirname in the form of path segments relative to base.
29+
If the file.short_path is not within base, return empty list.
30+
Example: if base="foo/bar/baz.txt"
31+
and file.short_path="bar/baz.txt",
32+
return ["bar"].
33+
Args:
34+
base (string): the base dirname (ctx.label.package)
35+
file (File): the file to calculate relative dirname.
36+
Returns:
37+
(list<string>): path
38+
"""
39+
path = file.short_path
40+
if not path.startswith(base):
41+
return []
42+
path = path[len(base)+1:] # remove trailing slash
43+
parts = path.split("/")
44+
return parts[:-1]
45+
46+
2747
def _get_offset_path(root, path):
2848
"""Adjust path relative to offset"""
2949

@@ -144,7 +164,6 @@ def _build_output_srcjar(run, builder):
144164
if run.data.verbose > 2:
145165
print("Copied jar %s srcjar to %s" % (protojar.path, srcjar.path))
146166

147-
148167
def _build_output_files(run, builder):
149168
"""Build a list of files we expect to be generated."""
150169

@@ -162,7 +181,9 @@ def _build_output_files(run, builder):
162181
if run.lang.output_file_style == 'capitalize':
163182
base = _capitalize(base)
164183
for ext in exts:
165-
pbfile = ctx.new_file(base + ext)
184+
path = _get_relative_dirname(ctx.label.package, file)
185+
path.append(base + ext)
186+
pbfile = ctx.new_file("/".join(path))
166187
builder["outputs"] += [pbfile]
167188

168189

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("//cpp:rules.bzl", "cc_proto_library")
2+
3+
cc_test(
4+
name = "test",
5+
size = "small",
6+
srcs = ["qux_test.cc"],
7+
copts = ["-Iexternal/gtest/include"],
8+
deps = [
9+
":protolib",
10+
"@gtest//:gtest",
11+
],
12+
)
13+
14+
cc_proto_library(
15+
name = "protolib",
16+
protos = [
17+
"foo/bar/baz.proto",
18+
],
19+
verbose = 3,
20+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
3+
package foo.bar.baz;
4+
5+
message Qux {
6+
bool verbose = 1;
7+
}
8+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "gtest/gtest.h"
2+
3+
#include "tests/proto_file_in_subdirectory/foo/bar/baz.pb.h"
4+
5+
TEST(QuxTest, CanCreateMessage) {
6+
GOOGLE_PROTOBUF_VERIFY_VERSION;
7+
foo::bar::baz::Qux msg = foo::bar::baz::Qux();
8+
msg.set_verbose(1);
9+
EXPECT_EQ(1, msg.verbose());
10+
}
11+
12+
int main(int ac, char* av[]) {
13+
testing::InitGoogleTest(&ac, av);
14+
return RUN_ALL_TESTS();
15+
}

0 commit comments

Comments
 (0)