• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 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 // Generates Objective C gRPC service interface out of Protobuf IDL.
20 
21 #include <memory>
22 
23 #include "src/compiler/config.h"
24 #include "src/compiler/objective_c_generator.h"
25 #include "src/compiler/objective_c_generator_helpers.h"
26 
27 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
28 
29 using ::google::protobuf::compiler::objectivec::
30     IsProtobufLibraryBundledProtoFile;
31 using ::google::protobuf::compiler::objectivec::ProtobufLibraryFrameworkName;
32 using ::grpc_objective_c_generator::FrameworkImport;
33 using ::grpc_objective_c_generator::LocalImport;
34 using ::grpc_objective_c_generator::PreprocIfElse;
35 using ::grpc_objective_c_generator::PreprocIfNot;
36 using ::grpc_objective_c_generator::SystemImport;
37 
38 namespace {
39 
ImportProtoHeaders(const grpc::protobuf::FileDescriptor * dep,const char * indent,const::std::string & framework,const::std::string & pb_runtime_import_prefix)40 inline ::std::string ImportProtoHeaders(
41     const grpc::protobuf::FileDescriptor* dep, const char* indent,
42     const ::std::string& framework,
43     const ::std::string& pb_runtime_import_prefix) {
44   ::std::string header = grpc_objective_c_generator::MessageHeaderName(dep);
45 
46   if (!IsProtobufLibraryBundledProtoFile(dep)) {
47     if (framework.empty()) {
48       return indent + LocalImport(header);
49     } else {
50       return indent + FrameworkImport(header, framework);
51     }
52   }
53 
54   ::std::string base_name = header;
55   grpc_generator::StripPrefix(&base_name, "google/protobuf/");
56   ::std::string file_name = "GPB" + base_name;
57   // create the import code snippet
58   ::std::string framework_header =
59       ::std::string(ProtobufLibraryFrameworkName) + "/" + file_name;
60   ::std::string local_header = file_name;
61   if (!pb_runtime_import_prefix.empty()) {
62     local_header = pb_runtime_import_prefix + "/" + file_name;
63   }
64 
65   static const ::std::string kFrameworkImportsCondition =
66       "GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS";
67   return PreprocIfElse(kFrameworkImportsCondition,
68                        indent + SystemImport(framework_header),
69                        indent + LocalImport(local_header));
70 }
71 
72 }  // namespace
73 
74 class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
75  public:
ObjectiveCGrpcGenerator()76   ObjectiveCGrpcGenerator() {}
~ObjectiveCGrpcGenerator()77   virtual ~ObjectiveCGrpcGenerator() {}
78 
79  public:
GetSupportedFeatures() const80   uint64_t GetSupportedFeatures() const override {
81     return FEATURE_PROTO3_OPTIONAL;
82   }
83 
Generate(const grpc::protobuf::FileDescriptor * file,const::std::string & parameter,grpc::protobuf::compiler::GeneratorContext * context,::std::string * error) const84   virtual bool Generate(const grpc::protobuf::FileDescriptor* file,
85                         const ::std::string& parameter,
86                         grpc::protobuf::compiler::GeneratorContext* context,
87                         ::std::string* error) const override {
88     if (file->service_count() == 0) {
89       // No services.  Do nothing.
90       return true;
91     }
92 
93     bool grpc_local_import = false;
94     ::std::string framework;
95     ::std::string pb_runtime_import_prefix;
96     ::std::string grpc_local_import_prefix;
97     std::vector<::std::string> params_list =
98         grpc_generator::tokenize(parameter, ",");
99     for (auto param_str = params_list.begin(); param_str != params_list.end();
100          ++param_str) {
101       std::vector<::std::string> param =
102           grpc_generator::tokenize(*param_str, "=");
103       if (param[0] == "generate_for_named_framework") {
104         if (param.size() != 2) {
105           *error =
106               std::string("Format: generate_for_named_framework=<Framework>");
107           return false;
108         } else if (param[1].empty()) {
109           *error =
110               std::string("Name of framework cannot be empty for parameter: ") +
111               param[0];
112           return false;
113         }
114         framework = param[1];
115       } else if (param[0] == "runtime_import_prefix") {
116         if (param.size() != 2) {
117           *error = grpc::string("Format: runtime_import_prefix=dir/");
118           return false;
119         }
120         pb_runtime_import_prefix = param[1];
121         grpc_generator::StripSuffix(&pb_runtime_import_prefix, "/");
122       } else if (param[0] == "grpc_local_import_prefix") {
123         grpc_local_import = true;
124         if (param.size() != 2) {
125           *error = grpc::string("Format: grpc_local_import_prefix=dir/");
126           return false;
127         }
128         grpc_local_import_prefix = param[1];
129       }
130     }
131 
132     static const ::std::string kNonNullBegin = "NS_ASSUME_NONNULL_BEGIN\n";
133     static const ::std::string kNonNullEnd = "NS_ASSUME_NONNULL_END\n";
134     static const ::std::string kProtocolOnly = "GPB_GRPC_PROTOCOL_ONLY";
135     static const ::std::string kForwardDeclare =
136         "GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO";
137 
138     ::std::string file_name =
139         google::protobuf::compiler::objectivec::FilePath(file);
140 
141     grpc_objective_c_generator::Parameters generator_params;
142     generator_params.no_v1_compatibility = false;
143 
144     if (!parameter.empty()) {
145       std::vector<std::string> parameters_list =
146           grpc_generator::tokenize(parameter, ",");
147       for (auto parameter_string = parameters_list.begin();
148            parameter_string != parameters_list.end(); parameter_string++) {
149         std::vector<std::string> param =
150             grpc_generator::tokenize(*parameter_string, "=");
151         if (param[0] == "no_v1_compatibility") {
152           generator_params.no_v1_compatibility = true;
153         }
154       }
155     }
156 
157     // Write out a file header.
158     ::std::string file_header =
159         "// Code generated by gRPC proto compiler.  DO NOT EDIT!\n"
160         "// source: " +
161         file->name() + "\n\n";
162 
163     {
164       // Generate .pbrpc.h
165 
166       ::std::string imports;
167       if (framework.empty()) {
168         imports = LocalImport(file_name + ".pbobjc.h");
169       } else {
170         imports = FrameworkImport(file_name + ".pbobjc.h", framework);
171       }
172 
173       ::std::string system_imports;
174       if (grpc_local_import) {
175         system_imports =
176             LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoService.h");
177         if (generator_params.no_v1_compatibility) {
178           system_imports +=
179               LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoRPC.h");
180         } else {
181           system_imports += LocalImport(grpc_local_import_prefix +
182                                         "ProtoRPC/ProtoRPCLegacy.h");
183           system_imports += LocalImport(grpc_local_import_prefix +
184                                         "RxLibrary/GRXWriteable.h");
185           system_imports +=
186               LocalImport(grpc_local_import_prefix + "RxLibrary/GRXWriter.h");
187         }
188       } else {
189         system_imports = SystemImport("ProtoRPC/ProtoService.h");
190         if (generator_params.no_v1_compatibility) {
191           system_imports += SystemImport("ProtoRPC/ProtoRPC.h");
192         } else {
193           system_imports += SystemImport("ProtoRPC/ProtoRPCLegacy.h");
194           system_imports += SystemImport("RxLibrary/GRXWriteable.h");
195           system_imports += SystemImport("RxLibrary/GRXWriter.h");
196         }
197       }
198 
199       ::std::string forward_declarations =
200           "@class GRPCUnaryProtoCall;\n"
201           "@class GRPCStreamingProtoCall;\n"
202           "@class GRPCCallOptions;\n"
203           "@protocol GRPCProtoResponseHandler;\n";
204       if (!generator_params.no_v1_compatibility) {
205         forward_declarations += "@class GRPCProtoCall;\n";
206       }
207       forward_declarations += "\n";
208 
209       ::std::string class_declarations =
210           grpc_objective_c_generator::GetAllMessageClasses(file);
211 
212       ::std::string class_imports;
213       for (int i = 0; i < file->dependency_count(); i++) {
214         class_imports += ImportProtoHeaders(
215             file->dependency(i), "  ", framework, pb_runtime_import_prefix);
216       }
217 
218       ::std::string ng_protocols;
219       for (int i = 0; i < file->service_count(); i++) {
220         const grpc::protobuf::ServiceDescriptor* service = file->service(i);
221         ng_protocols += grpc_objective_c_generator::GetV2Protocol(service);
222       }
223 
224       ::std::string protocols;
225       for (int i = 0; i < file->service_count(); i++) {
226         const grpc::protobuf::ServiceDescriptor* service = file->service(i);
227         protocols +=
228             grpc_objective_c_generator::GetProtocol(service, generator_params);
229       }
230 
231       ::std::string interfaces;
232       for (int i = 0; i < file->service_count(); i++) {
233         const grpc::protobuf::ServiceDescriptor* service = file->service(i);
234         interfaces +=
235             grpc_objective_c_generator::GetInterface(service, generator_params);
236       }
237 
238       Write(context, file_name + ".pbrpc.h",
239             file_header + PreprocIfNot(kForwardDeclare, imports) + "\n" +
240                 PreprocIfNot(kProtocolOnly, system_imports) + "\n" +
241                 class_declarations + "\n" +
242                 PreprocIfNot(kForwardDeclare, class_imports) + "\n" +
243                 forward_declarations + "\n" + kNonNullBegin + "\n" +
244                 ng_protocols + protocols + "\n" +
245                 PreprocIfNot(kProtocolOnly, interfaces) + "\n" + kNonNullEnd +
246                 "\n");
247     }
248 
249     {
250       // Generate .pbrpc.m
251 
252       ::std::string imports;
253       if (framework.empty()) {
254         imports = LocalImport(file_name + ".pbrpc.h") +
255                   LocalImport(file_name + ".pbobjc.h");
256       } else {
257         imports = FrameworkImport(file_name + ".pbrpc.h", framework) +
258                   FrameworkImport(file_name + ".pbobjc.h", framework);
259       }
260 
261       if (grpc_local_import) {
262         if (generator_params.no_v1_compatibility) {
263           imports +=
264               LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoRPC.h");
265         } else {
266           imports += LocalImport(grpc_local_import_prefix +
267                                  "ProtoRPC/ProtoRPCLegacy.h");
268           imports += LocalImport(grpc_local_import_prefix +
269                                  "RxLibrary/GRXWriter+Immediate.h");
270         }
271       } else {
272         if (generator_params.no_v1_compatibility) {
273           imports += SystemImport("ProtoRPC/ProtoRPC.h");
274         } else {
275           imports += SystemImport("ProtoRPC/ProtoRPCLegacy.h");
276           imports += SystemImport("RxLibrary/GRXWriter+Immediate.h");
277         }
278       }
279 
280       ::std::string class_imports;
281       for (int i = 0; i < file->dependency_count(); i++) {
282         class_imports += ImportProtoHeaders(file->dependency(i), "", framework,
283                                             pb_runtime_import_prefix);
284       }
285 
286       ::std::string definitions;
287       for (int i = 0; i < file->service_count(); i++) {
288         const grpc::protobuf::ServiceDescriptor* service = file->service(i);
289         definitions +=
290             grpc_objective_c_generator::GetSource(service, generator_params);
291       }
292 
293       Write(context, file_name + ".pbrpc.m",
294             file_header +
295                 PreprocIfNot(kProtocolOnly, imports + "\n" + class_imports +
296                                                 "\n" + definitions));
297     }
298 
299     return true;
300   }
301 
302  private:
303   // Write the given code into the given file.
Write(grpc::protobuf::compiler::GeneratorContext * context,const::std::string & filename,const::std::string & code) const304   void Write(grpc::protobuf::compiler::GeneratorContext* context,
305              const ::std::string& filename, const ::std::string& code) const {
306     std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
307         context->Open(filename));
308     grpc::protobuf::io::CodedOutputStream coded_out(output.get());
309     coded_out.WriteRaw(code.data(), code.size());
310   }
311 };
312 
main(int argc,char * argv[])313 int main(int argc, char* argv[]) {
314   ObjectiveCGrpcGenerator generator;
315   return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
316 }
317