1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 // Generates cpp gRPC service interface out of Protobuf IDL.
20 //
21
22 #include <memory>
23 #include <sstream>
24
25 #include "src/compiler/config.h"
26
27 #include "src/compiler/cpp_generator.h"
28 #include "src/compiler/generator_helpers.h"
29 #include "src/compiler/protobuf_plugin.h"
30
31 class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
32 public:
CppGrpcGenerator()33 CppGrpcGenerator() {}
~CppGrpcGenerator()34 virtual ~CppGrpcGenerator() {}
35
Generate(const grpc::protobuf::FileDescriptor * file,const grpc::string & parameter,grpc::protobuf::compiler::GeneratorContext * context,grpc::string * error) const36 virtual bool Generate(const grpc::protobuf::FileDescriptor* file,
37 const grpc::string& parameter,
38 grpc::protobuf::compiler::GeneratorContext* context,
39 grpc::string* error) const {
40 if (file->options().cc_generic_services()) {
41 *error =
42 "cpp grpc proto compiler plugin does not work with generic "
43 "services. To generate cpp grpc APIs, please set \""
44 "cc_generic_service = false\".";
45 return false;
46 }
47
48 grpc_cpp_generator::Parameters generator_parameters;
49 generator_parameters.use_system_headers = true;
50 generator_parameters.generate_mock_code = false;
51
52 ProtoBufFile pbfile(file);
53
54 if (!parameter.empty()) {
55 std::vector<grpc::string> parameters_list =
56 grpc_generator::tokenize(parameter, ",");
57 for (auto parameter_string = parameters_list.begin();
58 parameter_string != parameters_list.end(); parameter_string++) {
59 std::vector<grpc::string> param =
60 grpc_generator::tokenize(*parameter_string, "=");
61 if (param[0] == "services_namespace") {
62 generator_parameters.services_namespace = param[1];
63 } else if (param[0] == "use_system_headers") {
64 if (param[1] == "true") {
65 generator_parameters.use_system_headers = true;
66 } else if (param[1] == "false") {
67 generator_parameters.use_system_headers = false;
68 } else {
69 *error = grpc::string("Invalid parameter: ") + *parameter_string;
70 return false;
71 }
72 } else if (param[0] == "grpc_search_path") {
73 generator_parameters.grpc_search_path = param[1];
74 } else if (param[0] == "generate_mock_code") {
75 if (param[1] == "true") {
76 generator_parameters.generate_mock_code = true;
77 } else if (param[1] != "false") {
78 *error = grpc::string("Invalid parameter: ") + *parameter_string;
79 return false;
80 }
81 } else if (param[0] == "gmock_search_path") {
82 generator_parameters.gmock_search_path = param[1];
83 } else if (param[0] == "additional_header_includes") {
84 generator_parameters.additional_header_includes =
85 grpc_generator::tokenize(param[1], ":");
86 } else {
87 *error = grpc::string("Unknown parameter: ") + *parameter_string;
88 return false;
89 }
90 }
91 }
92
93 grpc::string file_name = grpc_generator::StripProto(file->name());
94
95 grpc::string header_code =
96 grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) +
97 grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) +
98 grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) +
99 grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters);
100 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output(
101 context->Open(file_name + ".grpc.pb.h"));
102 grpc::protobuf::io::CodedOutputStream header_coded_out(header_output.get());
103 header_coded_out.WriteRaw(header_code.data(), header_code.size());
104
105 grpc::string source_code =
106 grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) +
107 grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) +
108 grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) +
109 grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters);
110 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output(
111 context->Open(file_name + ".grpc.pb.cc"));
112 grpc::protobuf::io::CodedOutputStream source_coded_out(source_output.get());
113 source_coded_out.WriteRaw(source_code.data(), source_code.size());
114
115 if (!generator_parameters.generate_mock_code) {
116 return true;
117 }
118 grpc::string mock_code =
119 grpc_cpp_generator::GetMockPrologue(&pbfile, generator_parameters) +
120 grpc_cpp_generator::GetMockIncludes(&pbfile, generator_parameters) +
121 grpc_cpp_generator::GetMockServices(&pbfile, generator_parameters) +
122 grpc_cpp_generator::GetMockEpilogue(&pbfile, generator_parameters);
123 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> mock_output(
124 context->Open(file_name + "_mock.grpc.pb.h"));
125 grpc::protobuf::io::CodedOutputStream mock_coded_out(mock_output.get());
126 mock_coded_out.WriteRaw(mock_code.data(), mock_code.size());
127
128 return true;
129 }
130
131 private:
132 // Insert the given code into the given file at the given insertion point.
Insert(grpc::protobuf::compiler::GeneratorContext * context,const grpc::string & filename,const grpc::string & insertion_point,const grpc::string & code) const133 void Insert(grpc::protobuf::compiler::GeneratorContext* context,
134 const grpc::string& filename, const grpc::string& insertion_point,
135 const grpc::string& code) const {
136 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
137 context->OpenForInsert(filename, insertion_point));
138 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
139 coded_out.WriteRaw(code.data(), code.size());
140 }
141 };
142
main(int argc,char * argv[])143 int main(int argc, char* argv[]) {
144 CppGrpcGenerator generator;
145 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
146 }
147