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 | |
| 46 | grpc::string GenerateComments(const std::vector<grpc::string> &in) { |
| 47 | std::ostringstream oss; |
| 48 | for (const grpc::string &elem : in) { |
| 49 | if (elem.empty()) { |
| 50 | oss << "//\n"; |
| 51 | } else if (elem[0] == ' ') { |
| 52 | oss << "//" << elem << "\n"; |
| 53 | } else { |
| 54 | oss << "// " << elem << "\n"; |
| 55 | } |
| 56 | } |
| 57 | return oss.str(); |
| 58 | } |
| 59 | |
| 60 | // Get leading or trailing comments in a string. Comment lines start with "// ". |
| 61 | // Leading detached comments are put in in front of leading comments. |
| 62 | template <typename DescriptorType> |
| 63 | grpc::string GetComments(const DescriptorType *desc, bool leading) { |
| 64 | std::vector<grpc::string> out; |
| 65 | if (leading) { |
| 66 | grpc_generator::GetComment( |
| 67 | desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &out); |
| 68 | std::vector<grpc::string> leading; |
| 69 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING, |
| 70 | &leading); |
| 71 | out.insert(out.end(), leading.begin(), leading.end()); |
| 72 | } else { |
| 73 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING, |
| 74 | &out); |
| 75 | } |
| 76 | return GenerateComments(out); |
| 77 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 78 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 79 | class ProtoBufMethod : public grpc_cpp_generator::Method { |
| 80 | public: |
| 81 | ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method) |
| 82 | : method_(method) {} |
| 83 | |
| 84 | grpc::string name() const { return method_->name(); } |
| 85 | |
| 86 | grpc::string input_type_name() const { |
| 87 | return grpc_cpp_generator::ClassName(method_->input_type(), true); |
| 88 | } |
| 89 | grpc::string output_type_name() const { |
| 90 | return grpc_cpp_generator::ClassName(method_->output_type(), true); |
| 91 | } |
| 92 | |
| 93 | bool NoStreaming() const { |
| 94 | return !method_->client_streaming() && !method_->server_streaming(); |
| 95 | } |
| 96 | |
| 97 | bool ClientOnlyStreaming() const { |
| 98 | return method_->client_streaming() && !method_->server_streaming(); |
| 99 | } |
| 100 | |
| 101 | bool ServerOnlyStreaming() const { |
| 102 | return !method_->client_streaming() && method_->server_streaming(); |
| 103 | } |
| 104 | |
| 105 | bool BidiStreaming() const { |
| 106 | return method_->client_streaming() && method_->server_streaming(); |
| 107 | } |
| 108 | |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 109 | grpc::string GetLeadingComments() const { return GetComments(method_, true); } |
| 110 | |
| 111 | grpc::string GetTrailingComments() const { |
| 112 | return GetComments(method_, false); |
| 113 | } |
| 114 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 115 | private: |
| 116 | const grpc::protobuf::MethodDescriptor *method_; |
| 117 | }; |
| 118 | |
| 119 | class ProtoBufService : public grpc_cpp_generator::Service { |
| 120 | public: |
| 121 | ProtoBufService(const grpc::protobuf::ServiceDescriptor *service) |
| 122 | : service_(service) {} |
| 123 | |
| 124 | grpc::string name() const { return service_->name(); } |
| 125 | |
| 126 | int method_count() const { return service_->method_count(); }; |
| 127 | std::unique_ptr<const grpc_cpp_generator::Method> method(int i) const { |
| 128 | return std::unique_ptr<const grpc_cpp_generator::Method>( |
| 129 | new ProtoBufMethod(service_->method(i))); |
| 130 | }; |
| 131 | |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 132 | grpc::string GetLeadingComments() const { |
| 133 | return GetComments(service_, true); |
| 134 | } |
| 135 | |
| 136 | grpc::string GetTrailingComments() const { |
| 137 | return GetComments(service_, false); |
| 138 | } |
| 139 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 140 | private: |
| 141 | const grpc::protobuf::ServiceDescriptor *service_; |
| 142 | }; |
| 143 | |
| 144 | class ProtoBufPrinter : public grpc_cpp_generator::Printer { |
| 145 | public: |
| 146 | ProtoBufPrinter(grpc::string *str) |
| 147 | : output_stream_(str), printer_(&output_stream_, '$') {} |
| 148 | |
| 149 | void Print(const std::map<grpc::string, grpc::string> &vars, |
| 150 | const char *string_template) { |
| 151 | printer_.Print(vars, string_template); |
| 152 | } |
| 153 | |
| 154 | void Print(const char *string) { printer_.Print(string); } |
| 155 | void Indent() { printer_.Indent(); } |
| 156 | void Outdent() { printer_.Outdent(); } |
| 157 | |
| 158 | private: |
| 159 | grpc::protobuf::io::StringOutputStream output_stream_; |
| 160 | grpc::protobuf::io::Printer printer_; |
| 161 | }; |
| 162 | |
| 163 | class ProtoBufFile : public grpc_cpp_generator::File { |
| 164 | public: |
| 165 | ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {} |
| 166 | |
| 167 | grpc::string filename() const { return file_->name(); } |
| 168 | grpc::string filename_without_ext() const { |
| 169 | return grpc_generator::StripProto(filename()); |
| 170 | } |
| 171 | |
| 172 | grpc::string package() const { return file_->package(); } |
| 173 | std::vector<grpc::string> package_parts() const { |
| 174 | return grpc_generator::tokenize(package(), "."); |
| 175 | } |
| 176 | |
| 177 | int service_count() const { return file_->service_count(); }; |
| 178 | std::unique_ptr<const grpc_cpp_generator::Service> service(int i) const { |
| 179 | return std::unique_ptr<const grpc_cpp_generator::Service> ( |
| 180 | new ProtoBufService(file_->service(i))); |
| 181 | } |
| 182 | |
| 183 | std::unique_ptr<grpc_cpp_generator::Printer> CreatePrinter(grpc::string *str) const { |
| 184 | return std::unique_ptr<grpc_cpp_generator::Printer>( |
| 185 | new ProtoBufPrinter(str)); |
| 186 | } |
| 187 | |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 188 | grpc::string GetLeadingComments() const { return GetComments(file_, true); } |
| 189 | |
| 190 | grpc::string GetTrailingComments() const { return GetComments(file_, false); } |
| 191 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 192 | private: |
| 193 | const grpc::protobuf::FileDescriptor *file_; |
| 194 | }; |
| 195 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 196 | class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 197 | public: |
| 198 | CppGrpcGenerator() {} |
| 199 | virtual ~CppGrpcGenerator() {} |
| 200 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 201 | virtual bool Generate(const grpc::protobuf::FileDescriptor *file, |
| 202 | const grpc::string ¶meter, |
| 203 | grpc::protobuf::compiler::GeneratorContext *context, |
| 204 | grpc::string *error) const { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 205 | if (file->options().cc_generic_services()) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 206 | *error = |
| 207 | "cpp grpc proto compiler plugin does not work with generic " |
| 208 | "services. To generate cpp grpc APIs, please set \"" |
| 209 | "cc_generic_service = false\"."; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 213 | grpc_cpp_generator::Parameters generator_parameters; |
Nicolas "Pixel" Noble | 931bdce | 2016-01-12 03:08:11 +0100 | [diff] [blame] | 214 | generator_parameters.use_system_headers = true; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 215 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 216 | ProtoBufFile pbfile(file); |
| 217 | |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 218 | if (!parameter.empty()) { |
| 219 | std::vector<grpc::string> parameters_list = |
| 220 | grpc_generator::tokenize(parameter, ","); |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 221 | for (auto parameter_string = parameters_list.begin(); |
| 222 | parameter_string != parameters_list.end(); |
| 223 | parameter_string++) { |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 224 | std::vector<grpc::string> param = |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 225 | grpc_generator::tokenize(*parameter_string, "="); |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 226 | if (param[0] == "services_namespace") { |
| 227 | generator_parameters.services_namespace = param[1]; |
Nicolas "Pixel" Noble | 931bdce | 2016-01-12 03:08:11 +0100 | [diff] [blame] | 228 | } else if (param[0] == "use_system_headers") { |
| 229 | if (param[1] == "true") { |
| 230 | generator_parameters.use_system_headers = true; |
| 231 | } else if (param[1] == "false") { |
| 232 | generator_parameters.use_system_headers = false; |
| 233 | } else { |
| 234 | *error = grpc::string("Invalid parameter: ") + *parameter_string; |
| 235 | return false; |
| 236 | } |
| 237 | } else if (param[0] == "grpc_search_path") { |
| 238 | generator_parameters.grpc_search_path = param[1]; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 239 | } else { |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 240 | *error = grpc::string("Unknown parameter: ") + *parameter_string; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 246 | grpc::string file_name = grpc_generator::StripProto(file->name()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 247 | |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 248 | grpc::string header_code = |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 249 | grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) + |
| 250 | grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) + |
| 251 | grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) + |
| 252 | grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 253 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( |
| 254 | context->Open(file_name + ".grpc.pb.h")); |
| 255 | grpc::protobuf::io::CodedOutputStream header_coded_out( |
| 256 | header_output.get()); |
| 257 | header_coded_out.WriteRaw(header_code.data(), header_code.size()); |
| 258 | |
| 259 | grpc::string source_code = |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 260 | grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) + |
| 261 | grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) + |
| 262 | grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) + |
| 263 | grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 264 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( |
| 265 | context->Open(file_name + ".grpc.pb.cc")); |
| 266 | grpc::protobuf::io::CodedOutputStream source_coded_out( |
| 267 | source_output.get()); |
| 268 | source_coded_out.WriteRaw(source_code.data(), source_code.size()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | // 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] | 275 | void Insert(grpc::protobuf::compiler::GeneratorContext *context, |
| 276 | const grpc::string &filename, const grpc::string &insertion_point, |
| 277 | const grpc::string &code) const { |
| 278 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 279 | context->OpenForInsert(filename, insertion_point)); |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 280 | grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 281 | coded_out.WriteRaw(code.data(), code.size()); |
| 282 | } |
| 283 | }; |
| 284 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 285 | int main(int argc, char *argv[]) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 286 | CppGrpcGenerator generator; |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 287 | return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 288 | } |