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 #ifdef GRPC_PROTOBUF_EDITION_SUPPORT
86 | FEATURE_SUPPORTS_EDITIONS
87 #endif
88 ;
89 }
90
91 #ifdef GRPC_PROTOBUF_EDITION_SUPPORT
GetMinimumEdition() const92 grpc::protobuf::Edition GetMinimumEdition() const override {
93 return grpc::protobuf::Edition::EDITION_PROTO2;
94 }
GetMaximumEdition() const95 grpc::protobuf::Edition GetMaximumEdition() const override {
96 return grpc::protobuf::Edition::EDITION_2023;
97 }
98 #endif
99
Generate(const grpc::protobuf::FileDescriptor * file,const::std::string & parameter,grpc::protobuf::compiler::GeneratorContext * context,::std::string * error) const100 virtual bool Generate(const grpc::protobuf::FileDescriptor* file,
101 const ::std::string& parameter,
102 grpc::protobuf::compiler::GeneratorContext* context,
103 ::std::string* error) const override {
104 if (file->service_count() == 0) {
105 // No services. Do nothing.
106 return true;
107 }
108
109 #ifdef SUPPORT_OBJC_PREFIX_VALIDATION
110 // Default options will use env variables for controls.
111 if (!ValidateObjCClassPrefixes({file}, {}, error)) {
112 return false;
113 }
114 #endif
115
116 bool grpc_local_import = false;
117 ::std::string framework;
118 ::std::string pb_runtime_import_prefix;
119 ::std::string grpc_local_import_prefix;
120 std::vector<::std::string> params_list =
121 grpc_generator::tokenize(parameter, ",");
122 for (auto param_str = params_list.begin(); param_str != params_list.end();
123 ++param_str) {
124 std::vector<::std::string> param =
125 grpc_generator::tokenize(*param_str, "=");
126 if (param[0] == "generate_for_named_framework") {
127 if (param.size() != 2) {
128 *error =
129 std::string("Format: generate_for_named_framework=<Framework>");
130 return false;
131 } else if (param[1].empty()) {
132 *error =
133 std::string("Name of framework cannot be empty for parameter: ") +
134 param[0];
135 return false;
136 }
137 framework = param[1];
138 } else if (param[0] == "runtime_import_prefix") {
139 if (param.size() != 2) {
140 *error = grpc::string("Format: runtime_import_prefix=dir/");
141 return false;
142 }
143 pb_runtime_import_prefix = param[1];
144 grpc_generator::StripSuffix(&pb_runtime_import_prefix, "/");
145 } else if (param[0] == "grpc_local_import_prefix") {
146 grpc_local_import = true;
147 if (param.size() != 2) {
148 *error = grpc::string("Format: grpc_local_import_prefix=dir/");
149 return false;
150 }
151 grpc_local_import_prefix = param[1];
152 }
153 }
154
155 static const ::std::string kNonNullBegin = "NS_ASSUME_NONNULL_BEGIN\n";
156 static const ::std::string kNonNullEnd = "NS_ASSUME_NONNULL_END\n";
157 static const ::std::string kProtocolOnly = "GPB_GRPC_PROTOCOL_ONLY";
158 static const ::std::string kForwardDeclare =
159 "GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO";
160
161 ::std::string file_name =
162 google::protobuf::compiler::objectivec::FilePath(file);
163
164 grpc_objective_c_generator::Parameters generator_params;
165 generator_params.no_v1_compatibility = false;
166
167 if (!parameter.empty()) {
168 std::vector<std::string> parameters_list =
169 grpc_generator::tokenize(parameter, ",");
170 for (auto parameter_string = parameters_list.begin();
171 parameter_string != parameters_list.end(); parameter_string++) {
172 std::vector<std::string> param =
173 grpc_generator::tokenize(*parameter_string, "=");
174 if (param[0] == "no_v1_compatibility") {
175 generator_params.no_v1_compatibility = true;
176 }
177 }
178 }
179
180 // Write out a file header.
181 ::std::string file_header =
182 "// Code generated by gRPC proto compiler. DO NOT EDIT!\n"
183 "// source: " +
184 file->name() + "\n\n";
185
186 {
187 // Generate .pbrpc.h
188
189 ::std::string imports;
190 if (framework.empty()) {
191 imports = LocalImport(file_name + ".pbobjc.h");
192 } else {
193 imports = FrameworkImport(file_name + ".pbobjc.h", framework);
194 }
195
196 ::std::string system_imports;
197 if (grpc_local_import) {
198 system_imports =
199 LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoService.h");
200 if (generator_params.no_v1_compatibility) {
201 system_imports +=
202 LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoRPC.h");
203 } else {
204 system_imports += LocalImport(grpc_local_import_prefix +
205 "ProtoRPC/ProtoRPCLegacy.h");
206 system_imports += LocalImport(grpc_local_import_prefix +
207 "RxLibrary/GRXWriteable.h");
208 system_imports +=
209 LocalImport(grpc_local_import_prefix + "RxLibrary/GRXWriter.h");
210 }
211 } else {
212 system_imports = SystemImport("ProtoRPC/ProtoService.h");
213 if (generator_params.no_v1_compatibility) {
214 system_imports += SystemImport("ProtoRPC/ProtoRPC.h");
215 } else {
216 system_imports += SystemImport("ProtoRPC/ProtoRPCLegacy.h");
217 system_imports += SystemImport("RxLibrary/GRXWriteable.h");
218 system_imports += SystemImport("RxLibrary/GRXWriter.h");
219 }
220 }
221
222 ::std::string forward_declarations =
223 "@class GRPCUnaryProtoCall;\n"
224 "@class GRPCStreamingProtoCall;\n"
225 "@class GRPCCallOptions;\n"
226 "@protocol GRPCProtoResponseHandler;\n";
227 if (!generator_params.no_v1_compatibility) {
228 forward_declarations += "@class GRPCProtoCall;\n";
229 }
230 forward_declarations += "\n";
231
232 ::std::string class_declarations =
233 grpc_objective_c_generator::GetAllMessageClasses(file);
234
235 ::std::string class_imports;
236 for (int i = 0; i < file->dependency_count(); i++) {
237 class_imports += ImportProtoHeaders(
238 file->dependency(i), " ", framework, pb_runtime_import_prefix);
239 }
240
241 ::std::string ng_protocols;
242 for (int i = 0; i < file->service_count(); i++) {
243 const grpc::protobuf::ServiceDescriptor* service = file->service(i);
244 ng_protocols += grpc_objective_c_generator::GetV2Protocol(service);
245 }
246
247 ::std::string protocols;
248 for (int i = 0; i < file->service_count(); i++) {
249 const grpc::protobuf::ServiceDescriptor* service = file->service(i);
250 protocols +=
251 grpc_objective_c_generator::GetProtocol(service, generator_params);
252 }
253
254 ::std::string interfaces;
255 for (int i = 0; i < file->service_count(); i++) {
256 const grpc::protobuf::ServiceDescriptor* service = file->service(i);
257 interfaces +=
258 grpc_objective_c_generator::GetInterface(service, generator_params);
259 }
260
261 Write(context, file_name + ".pbrpc.h",
262 file_header + SystemImport("Foundation/Foundation.h") + "\n" +
263 PreprocIfNot(kForwardDeclare, imports) + "\n" +
264 PreprocIfNot(kProtocolOnly, system_imports) + "\n" +
265 class_declarations + "\n" +
266 PreprocIfNot(kForwardDeclare, class_imports) + "\n" +
267 forward_declarations + "\n" + kNonNullBegin + "\n" +
268 ng_protocols + protocols + "\n" +
269 PreprocIfNot(kProtocolOnly, interfaces) + "\n" + kNonNullEnd +
270 "\n");
271 }
272
273 {
274 // Generate .pbrpc.m
275
276 ::std::string imports;
277 if (framework.empty()) {
278 imports = LocalImport(file_name + ".pbrpc.h") +
279 LocalImport(file_name + ".pbobjc.h");
280 } else {
281 imports = FrameworkImport(file_name + ".pbrpc.h", framework) +
282 FrameworkImport(file_name + ".pbobjc.h", framework);
283 }
284
285 if (grpc_local_import) {
286 if (generator_params.no_v1_compatibility) {
287 imports +=
288 LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoRPC.h");
289 } else {
290 imports += LocalImport(grpc_local_import_prefix +
291 "ProtoRPC/ProtoRPCLegacy.h");
292 imports += LocalImport(grpc_local_import_prefix +
293 "RxLibrary/GRXWriter+Immediate.h");
294 }
295 } else {
296 if (generator_params.no_v1_compatibility) {
297 imports += SystemImport("ProtoRPC/ProtoRPC.h");
298 } else {
299 imports += SystemImport("ProtoRPC/ProtoRPCLegacy.h");
300 imports += SystemImport("RxLibrary/GRXWriter+Immediate.h");
301 }
302 }
303
304 ::std::string class_imports;
305 for (int i = 0; i < file->dependency_count(); i++) {
306 class_imports += ImportProtoHeaders(file->dependency(i), "", framework,
307 pb_runtime_import_prefix);
308 }
309
310 ::std::string definitions;
311 for (int i = 0; i < file->service_count(); i++) {
312 const grpc::protobuf::ServiceDescriptor* service = file->service(i);
313 definitions +=
314 grpc_objective_c_generator::GetSource(service, generator_params);
315 }
316
317 Write(context, file_name + ".pbrpc.m",
318 file_header +
319 PreprocIfNot(kProtocolOnly, imports + "\n" + class_imports +
320 "\n" + definitions));
321 }
322
323 return true;
324 }
325
326 private:
327 // Write the given code into the given file.
Write(grpc::protobuf::compiler::GeneratorContext * context,const::std::string & filename,const::std::string & code) const328 void Write(grpc::protobuf::compiler::GeneratorContext* context,
329 const ::std::string& filename, const ::std::string& code) const {
330 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
331 context->Open(filename));
332 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
333 coded_out.WriteRaw(code.data(), code.size());
334 }
335 };
336
main(int argc,char * argv[])337 int main(int argc, char* argv[]) {
338 ObjectiveCGrpcGenerator generator;
339 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
340 }
341