Skip to content

Commit

Permalink
Support protobuf generation for proto files that are located within
Browse files Browse the repository at this point in the history
subdirectories of the proto_library rule that consumed it.

- Adds _get_relative_dirname utility method.
- Adds test case.
  • Loading branch information
pcj committed Oct 5, 2016
1 parent 6b0e1ae commit cc4556c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ script:
examples/helloworld/java/... \
examples/helloworld/closure/... \
examples/wkt/... \
tests/... \
$FLAGS
notifications:
Expand Down
25 changes: 23 additions & 2 deletions protobuf/internal/proto_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ def _emit_params_file_action(ctx, path, mnemonic, cmds):
return f


def _get_relative_dirname(base, file):
"""Return a dirname in the form of path segments relative to base.
If the file.short_path is not within base, return empty list.
Example: if base="foo/bar/baz.txt"
and file.short_path="bar/baz.txt",
return ["bar"].
Args:
base (string): the base dirname (ctx.label.package)
file (File): the file to calculate relative dirname.
Returns:
(list<string>): path
"""
path = file.short_path
if not path.startswith(base):
return []
path = path[len(base)+1:] # remove trailing slash
parts = path.split("/")
return parts[:-1]


def _get_offset_path(root, path):
"""Adjust path relative to offset"""

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


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

Expand All @@ -162,7 +181,9 @@ def _build_output_files(run, builder):
if run.lang.output_file_style == 'capitalize':
base = _capitalize(base)
for ext in exts:
pbfile = ctx.new_file(base + ext)
path = _get_relative_dirname(ctx.label.package, file)
path.append(base + ext)
pbfile = ctx.new_file("/".join(path))
builder["outputs"] += [pbfile]


Expand Down
20 changes: 20 additions & 0 deletions tests/proto_file_in_subdirectory/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("//cpp:rules.bzl", "cc_proto_library")

cc_test(
name = "test",
size = "small",
srcs = ["qux_test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
":protolib",
"@gtest//:gtest",
],
)

cc_proto_library(
name = "protolib",
protos = [
"foo/bar/baz.proto",
],
verbose = 3,
)
8 changes: 8 additions & 0 deletions tests/proto_file_in_subdirectory/foo/bar/baz.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package foo.bar.baz;

message Qux {
bool verbose = 1;
}

15 changes: 15 additions & 0 deletions tests/proto_file_in_subdirectory/qux_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "gtest/gtest.h"

#include "tests/proto_file_in_subdirectory/foo/bar/baz.pb.h"

TEST(QuxTest, CanCreateMessage) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
foo::bar::baz::Qux msg = foo::bar::baz::Qux();
msg.set_verbose(1);
EXPECT_EQ(1, msg.verbose());
}

int main(int ac, char* av[]) {
testing::InitGoogleTest(&ac, av);
return RUN_ALL_TESTS();
}

0 comments on commit cc4556c

Please sign in to comment.