blob: acbe128213a2b34faf97d59f7c7d3e09d171b5d9 [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" Nobled48a9692015-03-27 02:06:44 +010061 if (file->service_count() == 0) {
62 // No services. Do nothing.
63 return true;
64 }
65
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010066 grpc_cpp_generator::Parameters generator_parameters;
67
68 if (!parameter.empty()) {
69 std::vector<grpc::string> parameters_list =
70 grpc_generator::tokenize(parameter, ",");
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +010071 for (auto parameter_string = parameters_list.begin();
72 parameter_string != parameters_list.end();
73 parameter_string++) {
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010074 std::vector<grpc::string> param =
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +010075 grpc_generator::tokenize(*parameter_string, "=");
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010076 if (param[0] == "services_namespace") {
77 generator_parameters.services_namespace = param[1];
78 } else {
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +010079 *error = grpc::string("Unknown parameter: ") + *parameter_string;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010080 return false;
81 }
82 }
83 }
84
Nicolas Nobled446eb82015-03-12 17:22:33 -070085 grpc::string file_name = grpc_generator::StripProto(file->name());
nnobleebebb7e2014-12-10 16:31:01 -080086
87 // Generate .pb.h
88 Insert(context, file_name + ".pb.h", "includes",
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010089 grpc_cpp_generator::GetHeaderIncludes(file, generator_parameters));
nnobleebebb7e2014-12-10 16:31:01 -080090 Insert(context, file_name + ".pb.h", "namespace_scope",
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010091 grpc_cpp_generator::GetHeaderServices(file, generator_parameters));
nnobleebebb7e2014-12-10 16:31:01 -080092 // Generate .pb.cc
93 Insert(context, file_name + ".pb.cc", "includes",
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010094 grpc_cpp_generator::GetSourceIncludes(generator_parameters));
nnobleebebb7e2014-12-10 16:31:01 -080095 Insert(context, file_name + ".pb.cc", "namespace_scope",
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010096 grpc_cpp_generator::GetSourceServices(file, generator_parameters));
nnobleebebb7e2014-12-10 16:31:01 -080097
98 return true;
99 }
100
101 private:
102 // Insert the given code into the given file at the given insertion point.
Nicolas Nobled446eb82015-03-12 17:22:33 -0700103 void Insert(grpc::protobuf::compiler::GeneratorContext *context,
104 const grpc::string &filename, const grpc::string &insertion_point,
105 const grpc::string &code) const {
106 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
nnobleebebb7e2014-12-10 16:31:01 -0800107 context->OpenForInsert(filename, insertion_point));
Nicolas Nobled446eb82015-03-12 17:22:33 -0700108 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
nnobleebebb7e2014-12-10 16:31:01 -0800109 coded_out.WriteRaw(code.data(), code.size());
110 }
111};
112
Craig Tillerecd49342015-01-18 14:36:47 -0800113int main(int argc, char *argv[]) {
nnobleebebb7e2014-12-10 16:31:01 -0800114 CppGrpcGenerator generator;
Nicolas Nobled446eb82015-03-12 17:22:33 -0700115 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
Craig Tiller190d3602015-02-18 09:23:38 -0800116}