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