nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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> |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 38 | |
| 39 | #include "src/compiler/config.h" |
Nicolas "Pixel" Noble | 36f5323 | 2015-01-16 06:39:58 +0100 | [diff] [blame] | 40 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 41 | #include "src/compiler/cpp_generator.h" |
| 42 | #include "src/compiler/cpp_generator_helpers.h" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 43 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 44 | class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 45 | public: |
| 46 | CppGrpcGenerator() {} |
| 47 | virtual ~CppGrpcGenerator() {} |
| 48 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 49 | virtual bool Generate(const grpc::protobuf::FileDescriptor *file, |
| 50 | const grpc::string ¶meter, |
| 51 | grpc::protobuf::compiler::GeneratorContext *context, |
| 52 | grpc::string *error) const { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 53 | if (file->options().cc_generic_services()) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 54 | *error = |
| 55 | "cpp grpc proto compiler plugin does not work with generic " |
| 56 | "services. To generate cpp grpc APIs, please set \"" |
| 57 | "cc_generic_service = false\"."; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 61 | grpc_cpp_generator::Parameters generator_parameters; |
| 62 | |
| 63 | if (!parameter.empty()) { |
| 64 | std::vector<grpc::string> parameters_list = |
| 65 | grpc_generator::tokenize(parameter, ","); |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 66 | for (auto parameter_string = parameters_list.begin(); |
| 67 | parameter_string != parameters_list.end(); |
| 68 | parameter_string++) { |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 69 | std::vector<grpc::string> param = |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 70 | grpc_generator::tokenize(*parameter_string, "="); |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 71 | if (param[0] == "services_namespace") { |
| 72 | generator_parameters.services_namespace = param[1]; |
| 73 | } else { |
Nicolas "Pixel" Noble | 1e47514 | 2015-03-25 23:35:09 +0100 | [diff] [blame] | 74 | *error = grpc::string("Unknown parameter: ") + *parameter_string; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 80 | grpc::string file_name = grpc_generator::StripProto(file->name()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 81 | |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 82 | grpc::string header_code = |
| 83 | grpc_cpp_generator::GetHeaderPrologue(file, generator_parameters) + |
| 84 | grpc_cpp_generator::GetHeaderIncludes(file, generator_parameters) + |
| 85 | grpc_cpp_generator::GetHeaderServices(file, generator_parameters) + |
| 86 | grpc_cpp_generator::GetHeaderEpilogue(file, generator_parameters); |
| 87 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( |
| 88 | context->Open(file_name + ".grpc.pb.h")); |
| 89 | grpc::protobuf::io::CodedOutputStream header_coded_out( |
| 90 | header_output.get()); |
| 91 | header_coded_out.WriteRaw(header_code.data(), header_code.size()); |
| 92 | |
| 93 | grpc::string source_code = |
| 94 | grpc_cpp_generator::GetSourcePrologue(file, generator_parameters) + |
| 95 | grpc_cpp_generator::GetSourceIncludes(file, generator_parameters) + |
| 96 | grpc_cpp_generator::GetSourceServices(file, generator_parameters) + |
| 97 | grpc_cpp_generator::GetSourceEpilogue(file, generator_parameters); |
| 98 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( |
| 99 | context->Open(file_name + ".grpc.pb.cc")); |
| 100 | grpc::protobuf::io::CodedOutputStream source_coded_out( |
| 101 | source_output.get()); |
| 102 | source_coded_out.WriteRaw(source_code.data(), source_code.size()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | // 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] | 109 | void Insert(grpc::protobuf::compiler::GeneratorContext *context, |
| 110 | const grpc::string &filename, const grpc::string &insertion_point, |
| 111 | const grpc::string &code) const { |
| 112 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 113 | context->OpenForInsert(filename, insertion_point)); |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 114 | grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 115 | coded_out.WriteRaw(code.data(), code.size()); |
| 116 | } |
| 117 | }; |
| 118 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 119 | int main(int argc, char *argv[]) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 120 | CppGrpcGenerator generator; |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 121 | return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 122 | } |