1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_INTERNAL_COMPILER_CPP_PLUGIN_H 20 #define GRPC_INTERNAL_COMPILER_CPP_PLUGIN_H 21 22 #include <memory> 23 #include <sstream> 24 25 #include "src/compiler/config.h" 26 27 #include "src/compiler/cpp_generator.h" 28 #include "src/compiler/generator_helpers.h" 29 #include "src/compiler/protobuf_plugin.h" 30 31 // Cpp Generator for Protobug IDL 32 class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { 33 public: CppGrpcGenerator()34 CppGrpcGenerator() {} ~CppGrpcGenerator()35 virtual ~CppGrpcGenerator() {} 36 GetSupportedFeatures()37 uint64_t GetSupportedFeatures() const override { 38 return FEATURE_PROTO3_OPTIONAL; 39 } 40 Generate(const grpc::protobuf::FileDescriptor * file,const std::string & parameter,grpc::protobuf::compiler::GeneratorContext * context,std::string * error)41 virtual bool Generate(const grpc::protobuf::FileDescriptor* file, 42 const std::string& parameter, 43 grpc::protobuf::compiler::GeneratorContext* context, 44 std::string* error) const override { 45 if (file->options().cc_generic_services()) { 46 *error = 47 "cpp grpc proto compiler plugin does not work with generic " 48 "services. To generate cpp grpc APIs, please set \"" 49 "cc_generic_service = false\"."; 50 return false; 51 } 52 53 grpc_cpp_generator::Parameters generator_parameters; 54 generator_parameters.use_system_headers = true; 55 generator_parameters.generate_mock_code = false; 56 generator_parameters.include_import_headers = false; 57 58 ProtoBufFile pbfile(file); 59 60 if (!parameter.empty()) { 61 std::vector<std::string> parameters_list = 62 grpc_generator::tokenize(parameter, ","); 63 for (auto parameter_string = parameters_list.begin(); 64 parameter_string != parameters_list.end(); parameter_string++) { 65 std::vector<std::string> param = 66 grpc_generator::tokenize(*parameter_string, "="); 67 if (param[0] == "services_namespace") { 68 generator_parameters.services_namespace = param[1]; 69 } else if (param[0] == "use_system_headers") { 70 if (param[1] == "true") { 71 generator_parameters.use_system_headers = true; 72 } else if (param[1] == "false") { 73 generator_parameters.use_system_headers = false; 74 } else { 75 *error = std::string("Invalid parameter: ") + *parameter_string; 76 return false; 77 } 78 } else if (param[0] == "grpc_search_path") { 79 generator_parameters.grpc_search_path = param[1]; 80 } else if (param[0] == "generate_mock_code") { 81 if (param[1] == "true") { 82 generator_parameters.generate_mock_code = true; 83 } else if (param[1] != "false") { 84 *error = std::string("Invalid parameter: ") + *parameter_string; 85 return false; 86 } 87 } else if (param[0] == "gmock_search_path") { 88 generator_parameters.gmock_search_path = param[1]; 89 } else if (param[0] == "additional_header_includes") { 90 generator_parameters.additional_header_includes = 91 grpc_generator::tokenize(param[1], ":"); 92 } else if (param[0] == "message_header_extension") { 93 generator_parameters.message_header_extension = param[1]; 94 } else if (param[0] == "include_import_headers") { 95 if (param[1] == "true") { 96 generator_parameters.include_import_headers = true; 97 } else if (param[1] != "false") { 98 *error = std::string("Invalid parameter: ") + *parameter_string; 99 return false; 100 } 101 } else { 102 *error = std::string("Unknown parameter: ") + *parameter_string; 103 return false; 104 } 105 } 106 } 107 108 std::string file_name = grpc_generator::StripProto(file->name()); 109 110 std::string header_code = 111 grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) + 112 grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) + 113 grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) + 114 grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters); 115 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( 116 context->Open(file_name + ".grpc.pb.h")); 117 grpc::protobuf::io::CodedOutputStream header_coded_out(header_output.get()); 118 header_coded_out.WriteRaw(header_code.data(), header_code.size()); 119 120 std::string source_code = 121 grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) + 122 grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) + 123 grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) + 124 grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters); 125 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( 126 context->Open(file_name + ".grpc.pb.cc")); 127 grpc::protobuf::io::CodedOutputStream source_coded_out(source_output.get()); 128 source_coded_out.WriteRaw(source_code.data(), source_code.size()); 129 130 if (!generator_parameters.generate_mock_code) { 131 return true; 132 } 133 std::string mock_code = 134 grpc_cpp_generator::GetMockPrologue(&pbfile, generator_parameters) + 135 grpc_cpp_generator::GetMockIncludes(&pbfile, generator_parameters) + 136 grpc_cpp_generator::GetMockServices(&pbfile, generator_parameters) + 137 grpc_cpp_generator::GetMockEpilogue(&pbfile, generator_parameters); 138 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> mock_output( 139 context->Open(file_name + "_mock.grpc.pb.h")); 140 grpc::protobuf::io::CodedOutputStream mock_coded_out(mock_output.get()); 141 mock_coded_out.WriteRaw(mock_code.data(), mock_code.size()); 142 143 return true; 144 } 145 146 private: 147 // Insert the given code into the given file at the given insertion point. Insert(grpc::protobuf::compiler::GeneratorContext * context,const std::string & filename,const std::string & insertion_point,const std::string & code)148 void Insert(grpc::protobuf::compiler::GeneratorContext* context, 149 const std::string& filename, const std::string& insertion_point, 150 const std::string& code) const { 151 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( 152 context->OpenForInsert(filename, insertion_point)); 153 grpc::protobuf::io::CodedOutputStream coded_out(output.get()); 154 coded_out.WriteRaw(code.data(), code.size()); 155 } 156 }; 157 158 #endif // GRPC_INTERNAL_COMPILER_CPP_PLUGIN_H 159