nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | // Generates cpp gRPC service interface out of Protobuf IDL. |
| 35 | // |
| 36 | |
| 37 | #include <memory> |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 38 | #include <sstream> |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 39 | |
| 40 | #include "src/compiler/config.h" |
Nicolas "Pixel" Noble | 36f5323 | 2015-01-16 06:39:58 +0100 | [diff] [blame] | 41 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 42 | #include "src/compiler/cpp_generator.h" |
| 43 | #include "src/compiler/cpp_generator_helpers.h" |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 44 | #include "src/compiler/generator_helpers.h" |
| 45 | |
murgatroid99 | 210f3c4 | 2016-05-20 13:24:59 -0700 | [diff] [blame] | 46 | using grpc_cpp_generator::GetCppComments; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 47 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 48 | class ProtoBufMethod : public grpc_cpp_generator::Method { |
| 49 | public: |
| 50 | ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method) |
| 51 | : method_(method) {} |
| 52 | |
| 53 | grpc::string name() const { return method_->name(); } |
| 54 | |
| 55 | grpc::string input_type_name() const { |
| 56 | return grpc_cpp_generator::ClassName(method_->input_type(), true); |
| 57 | } |
| 58 | grpc::string output_type_name() const { |
| 59 | return grpc_cpp_generator::ClassName(method_->output_type(), true); |
| 60 | } |
| 61 | |
| 62 | bool NoStreaming() const { |
| 63 | return !method_->client_streaming() && !method_->server_streaming(); |
| 64 | } |
| 65 | |
| 66 | bool ClientOnlyStreaming() const { |
| 67 | return method_->client_streaming() && !method_->server_streaming(); |
| 68 | } |
| 69 | |
| 70 | bool ServerOnlyStreaming() const { |
| 71 | return !method_->client_streaming() && method_->server_streaming(); |
| 72 | } |
| 73 | |
| 74 | bool BidiStreaming() const { |
| 75 | return method_->client_streaming() && method_->server_streaming(); |
| 76 | } |
| 77 | |
yang-g | 57d1e08 | 2016-04-25 15:31:39 -0700 | [diff] [blame] | 78 | grpc::string GetLeadingComments() const { |
| 79 | return GetCppComments(method_, true); |
| 80 | } |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 81 | |
| 82 | grpc::string GetTrailingComments() const { |
yang-g | 57d1e08 | 2016-04-25 15:31:39 -0700 | [diff] [blame] | 83 | return GetCppComments(method_, false); |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 86 | private: |
| 87 | const grpc::protobuf::MethodDescriptor *method_; |
| 88 | }; |
| 89 | |
| 90 | class ProtoBufService : public grpc_cpp_generator::Service { |
| 91 | public: |
| 92 | ProtoBufService(const grpc::protobuf::ServiceDescriptor *service) |
| 93 | : service_(service) {} |
| 94 | |
| 95 | grpc::string name() const { return service_->name(); } |
| 96 | |
| 97 | int method_count() const { return service_->method_count(); }; |
| 98 | std::unique_ptr<const grpc_cpp_generator::Method> method(int i) const { |
| 99 | return std::unique_ptr<const grpc_cpp_generator::Method>( |
| 100 | new ProtoBufMethod(service_->method(i))); |
| 101 | }; |
| 102 | |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 103 | grpc::string GetLeadingComments() const { |
yang-g | 57d1e08 | 2016-04-25 15:31:39 -0700 | [diff] [blame] | 104 | return GetCppComments(service_, true); |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | grpc::string GetTrailingComments() const { |
yang-g | 57d1e08 | 2016-04-25 15:31:39 -0700 | [diff] [blame] | 108 | return GetCppComments(service_, false); |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 111 | private: |
| 112 | const grpc::protobuf::ServiceDescriptor *service_; |
| 113 | }; |
| 114 | |
| 115 | class ProtoBufPrinter : public grpc_cpp_generator::Printer { |
| 116 | public: |
| 117 | ProtoBufPrinter(grpc::string *str) |
| 118 | : output_stream_(str), printer_(&output_stream_, '$') {} |
| 119 | |
| 120 | void Print(const std::map<grpc::string, grpc::string> &vars, |
| 121 | const char *string_template) { |
| 122 | printer_.Print(vars, string_template); |
| 123 | } |
| 124 | |
| 125 | void Print(const char *string) { printer_.Print(string); } |
| 126 | void Indent() { printer_.Indent(); } |
| 127 | void Outdent() { printer_.Outdent(); } |
| 128 | |
| 129 | private: |
| 130 | grpc::protobuf::io::StringOutputStream output_stream_; |
| 131 | grpc::protobuf::io::Printer printer_; |
| 132 | }; |
| 133 | |
| 134 | class ProtoBufFile : public grpc_cpp_generator::File { |
| 135 | public: |
| 136 | ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {} |
| 137 | |
| 138 | grpc::string filename() const { return file_->name(); } |
| 139 | grpc::string filename_without_ext() const { |
| 140 | return grpc_generator::StripProto(filename()); |
| 141 | } |
| 142 | |
Wouter van Oortmerssen | b695b9b | 2016-04-13 18:18:08 -0700 | [diff] [blame] | 143 | grpc::string message_header_ext() const { return ".pb.h"; } |
| 144 | grpc::string service_header_ext() const { return ".grpc.pb.h"; } |
| 145 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 146 | grpc::string package() const { return file_->package(); } |
| 147 | std::vector<grpc::string> package_parts() const { |
| 148 | return grpc_generator::tokenize(package(), "."); |
| 149 | } |
| 150 | |
Wouter van Oortmerssen | b695b9b | 2016-04-13 18:18:08 -0700 | [diff] [blame] | 151 | grpc::string additional_headers() const { return ""; } |
| 152 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 153 | int service_count() const { return file_->service_count(); }; |
| 154 | std::unique_ptr<const grpc_cpp_generator::Service> service(int i) const { |
| 155 | return std::unique_ptr<const grpc_cpp_generator::Service> ( |
| 156 | new ProtoBufService(file_->service(i))); |
| 157 | } |
| 158 | |
| 159 | std::unique_ptr<grpc_cpp_generator::Printer> CreatePrinter(grpc::string *str) const { |
| 160 | return std::unique_ptr<grpc_cpp_generator::Printer>( |
| 161 | new ProtoBufPrinter(str)); |
| 162 | } |
| 163 | |
yang-g | 57d1e08 | 2016-04-25 15:31:39 -0700 | [diff] [blame] | 164 | grpc::string GetLeadingComments() const { |
| 165 | return GetCppComments(file_, true); |
| 166 | } |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 167 | |
yang-g | 57d1e08 | 2016-04-25 15:31:39 -0700 | [diff] [blame] | 168 | grpc::string GetTrailingComments() const { |
| 169 | return GetCppComments(file_, false); |
| 170 | } |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 171 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 172 | private: |
| 173 | const grpc::protobuf::FileDescriptor *file_; |
| 174 | }; |
| 175 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 176 | class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 177 | public: |
| 178 | CppGrpcGenerator() {} |
| 179 | virtual ~CppGrpcGenerator() {} |
| 180 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 181 | virtual bool Generate(const grpc::protobuf::FileDescriptor *file, |
| 182 | const grpc::string ¶meter, |
| 183 | grpc::protobuf::compiler::GeneratorContext *context, |
| 184 | grpc::string *error) const { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 185 | if (file->options().cc_generic_services()) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 186 | *error = |
| 187 | "cpp grpc proto compiler plugin does not work with generic " |
| 188 | "services. To generate cpp grpc APIs, please set \"" |
| 189 | "cc_generic_service = false\"."; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 193 | grpc_cpp_generator::Parameters generator_parameters; |
Nicolas "Pixel" Noble | 931bdce | 2016-01-12 03:08:11 +0100 | [diff] [blame] | 194 | generator_parameters.use_system_headers = true; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 195 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 196 | ProtoBufFile pbfile(file); |
| 197 | |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 198 | if (!parameter.empty()) { |
| 199 | std::vector<grpc::string> parameters_list = |
| 200 | grpc_generator::tokenize(parameter, ","); |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 201 | for (auto parameter_string = parameters_list.begin(); |
| 202 | parameter_string != parameters_list.end(); |
| 203 | parameter_string++) { |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 204 | std::vector<grpc::string> param = |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 205 | grpc_generator::tokenize(*parameter_string, "="); |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 206 | if (param[0] == "services_namespace") { |
| 207 | generator_parameters.services_namespace = param[1]; |
Nicolas "Pixel" Noble | 931bdce | 2016-01-12 03:08:11 +0100 | [diff] [blame] | 208 | } else if (param[0] == "use_system_headers") { |
| 209 | if (param[1] == "true") { |
| 210 | generator_parameters.use_system_headers = true; |
| 211 | } else if (param[1] == "false") { |
| 212 | generator_parameters.use_system_headers = false; |
| 213 | } else { |
| 214 | *error = grpc::string("Invalid parameter: ") + *parameter_string; |
| 215 | return false; |
| 216 | } |
| 217 | } else if (param[0] == "grpc_search_path") { |
| 218 | generator_parameters.grpc_search_path = param[1]; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 219 | } else { |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 220 | *error = grpc::string("Unknown parameter: ") + *parameter_string; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 226 | grpc::string file_name = grpc_generator::StripProto(file->name()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 227 | |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 228 | grpc::string header_code = |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 229 | grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) + |
| 230 | grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) + |
| 231 | grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) + |
| 232 | grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 233 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( |
| 234 | context->Open(file_name + ".grpc.pb.h")); |
| 235 | grpc::protobuf::io::CodedOutputStream header_coded_out( |
| 236 | header_output.get()); |
| 237 | header_coded_out.WriteRaw(header_code.data(), header_code.size()); |
| 238 | |
| 239 | grpc::string source_code = |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 240 | grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) + |
| 241 | grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) + |
| 242 | grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) + |
| 243 | grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 244 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( |
| 245 | context->Open(file_name + ".grpc.pb.cc")); |
| 246 | grpc::protobuf::io::CodedOutputStream source_coded_out( |
| 247 | source_output.get()); |
| 248 | source_coded_out.WriteRaw(source_code.data(), source_code.size()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 249 | |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | private: |
| 254 | // Insert the given code into the given file at the given insertion point. |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 255 | void Insert(grpc::protobuf::compiler::GeneratorContext *context, |
| 256 | const grpc::string &filename, const grpc::string &insertion_point, |
| 257 | const grpc::string &code) const { |
| 258 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 259 | context->OpenForInsert(filename, insertion_point)); |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 260 | grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 261 | coded_out.WriteRaw(code.data(), code.size()); |
| 262 | } |
| 263 | }; |
| 264 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 265 | int main(int argc, char *argv[]) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 266 | CppGrpcGenerator generator; |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 267 | return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 268 | } |