blob: 88c704948ecc59aa5f8952b63145c691386ce7be [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
nnobleebebb7e2014-12-10 16:31:01 -08004 * 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 Nobled446eb82015-03-12 17:22:33 -070038
39#include "src/compiler/config.h"
Nicolas "Pixel" Noble36f53232015-01-16 06:39:58 +010040
nnobleebebb7e2014-12-10 16:31:01 -080041#include "src/compiler/cpp_generator.h"
42#include "src/compiler/cpp_generator_helpers.h"
nnobleebebb7e2014-12-10 16:31:01 -080043
Nicolas Nobled446eb82015-03-12 17:22:33 -070044class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
nnobleebebb7e2014-12-10 16:31:01 -080045 public:
46 CppGrpcGenerator() {}
47 virtual ~CppGrpcGenerator() {}
48
Nicolas Nobled446eb82015-03-12 17:22:33 -070049 virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
50 const grpc::string &parameter,
51 grpc::protobuf::compiler::GeneratorContext *context,
52 grpc::string *error) const {
nnobleebebb7e2014-12-10 16:31:01 -080053 if (file->options().cc_generic_services()) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080054 *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\".";
nnobleebebb7e2014-12-10 16:31:01 -080058 return false;
59 }
60
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010061 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" Noble1e475142015-03-25 23:35:09 +010066 for (auto parameter_string = parameters_list.begin();
67 parameter_string != parameters_list.end();
68 parameter_string++) {
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010069 std::vector<grpc::string> param =
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +010070 grpc_generator::tokenize(*parameter_string, "=");
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010071 if (param[0] == "services_namespace") {
72 generator_parameters.services_namespace = param[1];
73 } else {
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +010074 *error = grpc::string("Unknown parameter: ") + *parameter_string;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010075 return false;
76 }
77 }
78 }
79
Nicolas Nobled446eb82015-03-12 17:22:33 -070080 grpc::string file_name = grpc_generator::StripProto(file->name());
nnobleebebb7e2014-12-10 16:31:01 -080081
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020082 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());
nnobleebebb7e2014-12-10 16:31:01 -0800103
104 return true;
105 }
106
107 private:
108 // Insert the given code into the given file at the given insertion point.
Nicolas Nobled446eb82015-03-12 17:22:33 -0700109 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(
nnobleebebb7e2014-12-10 16:31:01 -0800113 context->OpenForInsert(filename, insertion_point));
Nicolas Nobled446eb82015-03-12 17:22:33 -0700114 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
nnobleebebb7e2014-12-10 16:31:01 -0800115 coded_out.WriteRaw(code.data(), code.size());
116 }
117};
118
Craig Tillerecd49342015-01-18 14:36:47 -0800119int main(int argc, char *argv[]) {
nnobleebebb7e2014-12-10 16:31:01 -0800120 CppGrpcGenerator generator;
Nicolas Nobled446eb82015-03-12 17:22:33 -0700121 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
Craig Tiller190d3602015-02-18 09:23:38 -0800122}