Skip to content

Commit 8f60bd3

Browse files
authored
Add standalone mode for gapic-generator (googleapis#31)
This is a prerequisite for cc_gapic_library rule (will be submitted as a separate PR). Most of the pr is `#include` changes. For actual changes please check the following files: - `standalone.cc/standalone.h` - `main.cc` - `gapic_generator_unittest.cc` - `BUILD.bazel` - `gax/BUILD.bazel` - `gax/repositories.bzl` - `generator/testdata/BUILD.bazel` Plus various small refactorings. This includes: 1) "Fix" `#include` statements for external dependencies (`protobuf` and `gtest`): use `< >` instead of `" "`, also shorten path and conform to how it is done in protobuf repository itself (i.e. seems like and idiomatic way of making the imports). This also allows to get rid from `src/` prefix in the imports. 2) Gapic generator main binary can be executed in a plugin and a standalone mode (if command line args are provided, standalone mode is assumed automatically). 3) Standalone mode accepts standalone args according to the stand-alone interface spec: `--descriptor` - a list of descriptor sets (delimited with ':' or ';' character) (the spec does not require list, but accepting list is an extension, which still conforms to the spec); `--package` - a list (delimited with ':' or ';') which defines the proto packages designated for generation (to distinguish between common protos and actual api-specific protos, both of which must be specified in --descriptor); ability to specify a list is an extension to the spec; `--output` - the output folder (also supports outputting to zip archive, if output ends with .zip or .jar, this is an extension to the spec). 4) Refactor `BUILD.bazel` files and package structure. 5) Rename plugin binary to `protoc-gen-cpp_gapic` (to conform to spec and simplify usage as a plugin). 6) Refactor `gapic_generator_unittest.cc` (it will be simplified further, once `cc_gapic_library` is submitted) 7) Move gax-related repositories.bzl portion to its own `gax/repositories.bzl`
1 parent 93f102e commit 8f60bd3

26 files changed

+323
-125
lines changed

WORKSPACE

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,3 @@ load(
2020
)
2121

2222
com_google_gapic_generator_cpp_repositories()
23-
24-
bind(
25-
name = "zlib",
26-
actual = "@net_zlib//:zlib",
27-
)
28-

gax/backoff_policy_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <memory>
1717
#include <random>
1818

19-
#include "googletest/include/gtest/gtest.h"
19+
#include <gtest/gtest.h>
2020

2121
#include "backoff_policy.h"
2222

gax/repositories.bzl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2019 Google Inc. All rights reserved
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
def com_google_gapic_generator_cpp_gax_repositories():
16+
_maybe(
17+
http_archive,
18+
name = "com_github_grpc_grpc",
19+
strip_prefix = "grpc-1.18.0",
20+
urls = ["https://github.com/grpc/grpc/archive/v1.18.0.tar.gz"]
21+
)
22+
23+
24+
def _maybe(repo_rule, name, **kwargs):
25+
if name not in native.existing_rules():
26+
repo_rule(name = name, **kwargs)

gax/retry_policy_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#include <chrono>
1616
#include <memory>
1717

18-
#include "googletest/include/gtest/gtest.h"
1918
#include "retry_policy.h"
2019
#include "status.h"
20+
#include <gtest/gtest.h>
2121

2222
namespace {
2323
using namespace ::google;

gax/status_or_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414

1515
#include "status_or.h"
16-
#include "googletest/include/gtest/gtest.h"
1716
#include "status.h"
17+
#include <gtest/gtest.h>
1818

1919
#include <memory>
2020
#include <string>

gax/status_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "status.h"
16-
#include "googletest/include/gtest/gtest.h"
16+
#include <gtest/gtest.h>
1717

1818
#include <sstream>
1919
#include <string>

generator/BUILD.bazel

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
package(default_visibility = ["//visibility:public"])
16-
1715
licenses(["notice"]) # Apache 2.0
1816

1917
cc_library(
20-
name = "cpp_gapic_generator",
18+
name = "gapic_generator",
2119
srcs = [
2220
"gapic_generator.cc",
2321
"internal/client_cc_generator.cc",
2422
"internal/client_cc_generator.h",
25-
"internal/stub_cc_generator.cc",
26-
"internal/stub_cc_generator.h",
2723
"internal/client_header_generator.cc",
2824
"internal/client_header_generator.h",
29-
"internal/stub_header_generator.cc",
30-
"internal/stub_header_generator.h",
3125
"internal/data_model.h",
3226
"internal/gapic_utils.cc",
3327
"internal/gapic_utils.h",
3428
"internal/printer.h",
29+
"internal/stub_cc_generator.cc",
30+
"internal/stub_cc_generator.h",
31+
"internal/stub_header_generator.cc",
32+
"internal/stub_header_generator.h",
33+
"standalone.cc",
34+
"standalone.h",
3535
],
3636
hdrs = [
3737
"gapic_generator.h",
3838
],
39+
includes = ["."],
3940
deps = [
4041
"@absl//absl/base",
4142
"@absl//absl/strings",
@@ -44,41 +45,43 @@ cc_library(
4445
],
4546
)
4647

48+
# The protoc plugin/standalone binary. The name (proto-gen-cpp_gapic) is ugly
49+
# but it is a naming requirement for protoc plugins. To use --cpp_gapic_out
50+
# command line argument without need to provide additional --plugin argument,
51+
# the binary must be named proto-gen-cpp_gapic and placed in a PATH.
4752
cc_binary(
48-
name = "cpp_gapic_generator_plugin",
49-
srcs = ["plugin.cc"],
50-
deps = [
51-
":cpp_gapic_generator",
52-
],
53+
name = "protoc-gen-cpp_gapic",
54+
srcs = ["main.cc"],
55+
includes = ["."],
56+
deps = [":gapic_generator"],
5357
)
5458

5559
cc_test(
5660
name = "gapic_generator_unittest",
5761
size = "small",
5862
srcs = ["gapic_generator_unittest.cc"],
59-
data = glob(
60-
["testdata/**"],
61-
) + [
63+
data = [
64+
"//generator/testdata:library_proto",
65+
"//generator/testdata:library_service_baseline",
6266
"@api_common_protos//google/api:client_proto",
67+
"@com_google_protobuf//:descriptor_proto",
6368
],
6469
deps = [
65-
"//generator:cpp_gapic_generator",
70+
":gapic_generator",
6671
"@gtest//:gtest_main",
6772
],
6873
)
6974

70-
generator_unit_tests = [
71-
"internal/gapic_utils_unittest.cc",
72-
]
73-
7475
[cc_test(
7576
name = "generator_" + test.replace("/", "_").replace(".cc", ""),
7677
size = "small",
7778
srcs = [test],
7879
deps = [
79-
"//generator:cpp_gapic_generator",
80+
"//generator:gapic_generator",
8081
"@absl//absl/base",
8182
"@absl//absl/strings",
8283
"@gtest//:gtest_main",
8384
],
84-
) for test in generator_unit_tests]
85+
) for test in [
86+
"internal/gapic_utils_unittest.cc",
87+
]]

generator/gapic_generator.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
#ifndef GAPIC_GENERATOR_CPP_GENERATOR_GAPIC_GENERATOR_H_
1515
#define GAPIC_GENERATOR_CPP_GENERATOR_GAPIC_GENERATOR_H_
1616

17-
#include <memory>
18-
#include <sstream>
1917
#include <string>
2018

21-
#include "src/google/protobuf/compiler/code_generator.h"
22-
#include "src/google/protobuf/descriptor.h"
19+
#include <google/protobuf/compiler/code_generator.h>
20+
#include <google/protobuf/descriptor.h>
2321

2422
namespace google {
2523
namespace api {

generator/gapic_generator_unittest.cc

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,45 @@
1717
#include <string>
1818
#include <vector>
1919

20-
#include <dirent.h>
2120
#include <iostream>
2221

23-
#include "absl/strings/str_cat.h"
22+
#include <gtest/gtest.h>
2423

25-
#include "src/google/protobuf/compiler/command_line_interface.h"
26-
#include "src/google/protobuf/io/printer.h"
27-
#include "src/google/protobuf/io/zero_copy_stream.h"
24+
#include <gapic_generator.h>
25+
#include <standalone.h>
2826

29-
#include "googletest/include/gtest/gtest.h"
27+
namespace google {
28+
namespace api {
29+
namespace codegen {
3030

31-
#include "generator/gapic_generator.h"
32-
33-
namespace {
3431
namespace pb = google::protobuf;
35-
namespace codegen = google::api::codegen;
3632

37-
inline std::string LoadContent(std::string f) {
33+
inline std::string LoadContent(std::string const& f) {
3834
std::ifstream ifs(f);
3935
EXPECT_TRUE(ifs.good()) << "Could not open " << f;
4036
return std::string((std::istreambuf_iterator<char>(ifs)),
4137
(std::istreambuf_iterator<char>()));
4238
}
4339

44-
TEST(CppGapicPluginTest, GapicPluginTest) {
45-
std::string workspace_dir = ".";
46-
std::string external_dir = workspace_dir + "/..";
47-
std::string test_dir = workspace_dir + "/generator";
48-
std::string test_data_dir = test_dir + "/testdata";
49-
std::string gapic_out_dir = workspace_dir;
50-
51-
DIR* dirp = opendir(".");
52-
struct dirent* dp;
53-
while ((dp = readdir(dirp)) != NULL) {
54-
std::cout << dp->d_name << std::endl;
55-
}
56-
57-
pb::compiler::CommandLineInterface cli;
58-
cli.SetInputsAreProtoPathRelative(true);
59-
60-
codegen::GapicGenerator generator;
61-
cli.RegisterGenerator("--cpp_gapic_out", &generator, "");
40+
TEST(GapicGeneratorBaselineTest, StandaloneTest) {
41+
std::string const input_dir("../");
42+
std::string const output_dir("./");
43+
std::string const data_dir("./generator/testdata/");
6244

63-
std::string workspace_proto_path = "-I" + workspace_dir;
64-
std::string annotations_proto_path =
65-
"-I" + external_dir + "/api_common_protos";
66-
std::string well_known_types_proto_path =
67-
"-I" + external_dir + "/com_google_protobuf";
68-
std::string cpp_out = "--cpp_gapic_out=" + gapic_out_dir;
69-
std::string library_proto = test_data_dir + "/library.proto";
45+
std::vector<std::string> descriptors = {
46+
input_dir +
47+
"com_google_gapic_generator_cpp/generator/testdata/"
48+
"library_proto-descriptor-set.proto.bin",
49+
input_dir +
50+
"api_common_protos/google/api/client_proto-descriptor-set.proto.bin",
51+
input_dir +
52+
"com_google_protobuf/descriptor_proto-descriptor-set.proto.bin"};
53+
std::string package = "google.example.library.v1";
7054

71-
char const* argv[] = {"protoc",
72-
workspace_proto_path.c_str(),
73-
annotations_proto_path.c_str(),
74-
well_known_types_proto_path.c_str(),
75-
cpp_out.c_str(),
76-
library_proto.c_str()};
55+
GapicGenerator generator;
7756

78-
EXPECT_EQ(0, cli.Run(sizeof(argv) / sizeof(argv[0]), argv))
79-
<< "cli.Run failed";
57+
int res = StandaloneMain(descriptors, package, output_dir, &generator);
58+
EXPECT_EQ(0, res) << "StandaloneMain failed";
8059

8160
std::vector<std::string> files_to_check{
8261
"google/example/library/v1/library_service.gapic.h",
@@ -85,16 +64,17 @@ TEST(CppGapicPluginTest, GapicPluginTest) {
8564
"google/example/library/v1/library_service_stub.gapic.cc",
8665
};
8766

88-
for (std::string const& f : files_to_check) {
89-
std::string actual_file = absl::StrCat(gapic_out_dir, "/", f);
90-
std::string expected_file =
91-
absl::StrCat(test_data_dir, "/", f, ".baseline");
67+
for (auto const& file : files_to_check) {
68+
std::string const expected_file(data_dir + file + ".baseline");
69+
std::string const actual_file(output_dir + file);
9270

93-
std::string actual_file_content = LoadContent(actual_file);
94-
std::string expected_file_content = LoadContent(expected_file);
71+
std::string const& expected_file_content = LoadContent(expected_file);
72+
std::string const& actual_file_content = LoadContent(actual_file);
9573

9674
EXPECT_EQ(expected_file_content, actual_file_content);
9775
}
9876
}
9977

100-
} // namespace
78+
} // namespace codegen
79+
} // namespace api
80+
} // namespace google

generator/internal/client_cc_generator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
#include "gapic_utils.h"
2020
#include "printer.h"
21-
#include "src/google/protobuf/descriptor.h"
2221
#include "generator/internal/client_cc_generator.h"
2322
#include "generator/internal/data_model.h"
23+
#include <google/protobuf/descriptor.h>
2424

2525
namespace pb = google::protobuf;
2626

0 commit comments

Comments
 (0)