objective_c_plugin.cc
Go to the documentation of this file.
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/objectivec_helpers.h>
24 
25 #include "src/compiler/config.h"
28 
32 #ifdef SUPPORT_OBJC_PREFIX_VALIDATION
34 #endif
40 
41 namespace {
42 
43 inline ::std::string ImportProtoHeaders(
44  const grpc::protobuf::FileDescriptor* dep, const char* indent,
46  const ::std::string& pb_runtime_import_prefix) {
48 
50  if (framework.empty()) {
51  return indent + LocalImport(header);
52  } else {
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 =
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 
78  public:
81 
82  public:
83  uint64_t GetSupportedFeatures() const override {
84  return FEATURE_PROTO3_OPTIONAL;
85  }
86 
88  const ::std::string& parameter,
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;
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 =
150 
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 =
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);
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 +=
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 +=
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 +=
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.
317  std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
318  context->Open(filename));
320  coded_out.WriteRaw(code.data(), code.size());
321  }
322 };
323 
324 int main(int argc, char* argv[]) {
325  ObjectiveCGrpcGenerator generator;
326  return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
327 }
google::protobuf::compiler::objectivec::ProtobufLibraryFrameworkName
const char *const ProtobufLibraryFrameworkName
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:984
grpc_objective_c_generator::LocalImport
inline ::std::string LocalImport(const ::std::string &import)
Definition: objective_c_generator_helpers.h:65
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
objective_c_generator_helpers.h
ObjectiveCGrpcGenerator::GetSupportedFeatures
uint64_t GetSupportedFeatures() const override
Definition: objective_c_plugin.cc:83
grpc::protobuf::FileDescriptor
GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:85
config.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
ObjectiveCGrpcGenerator::~ObjectiveCGrpcGenerator
virtual ~ObjectiveCGrpcGenerator()
Definition: objective_c_plugin.cc:80
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
grpc_objective_c_generator::GetAllMessageClasses
::std::string GetAllMessageClasses(const FileDescriptor *file)
Definition: objective_c_generator.cc:263
python_utils.upload_rbe_results.indent
indent
Definition: upload_rbe_results.py:183
grpc_generator::tokenize
std::vector< std::string > tokenize(const std::string &input, const std::string &delimiters)
Definition: generator_helpers.h:82
grpc_objective_c_generator::SystemImport
inline ::std::string SystemImport(const ::std::string &import)
Definition: objective_c_generator_helpers.h:79
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
grpc_objective_c_generator::MessageHeaderName
string MessageHeaderName(const FileDescriptor *file)
Definition: objective_c_generator_helpers.h:35
ObjectiveCGrpcGenerator::Write
void Write(grpc::protobuf::compiler::GeneratorContext *context, const ::std::string &filename, const ::std::string &code) const
Definition: objective_c_plugin.cc:315
ObjectiveCGrpcGenerator::ObjectiveCGrpcGenerator
ObjectiveCGrpcGenerator()
Definition: objective_c_plugin.cc:79
grpc::protobuf::compiler::CodeGenerator
GRPC_CUSTOM_CODEGENERATOR CodeGenerator
Definition: src/compiler/config.h:41
grpc_objective_c_generator::GetProtocol
::std::string GetProtocol(const ServiceDescriptor *service, const Parameters &generator_params)
Definition: objective_c_generator.cc:281
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
ObjectiveCGrpcGenerator::Generate
virtual bool Generate(const grpc::protobuf::FileDescriptor *file, const ::std::string &parameter, grpc::protobuf::compiler::GeneratorContext *context, ::std::string *error) const override
Definition: objective_c_plugin.cc:87
header
struct absl::base_internal::@2940::AllocList::Header header
grpc_objective_c_generator::GetSource
::std::string GetSource(const ServiceDescriptor *service, const Parameters &generator_params)
Definition: objective_c_generator.cc:372
grpc_objective_c_generator::PreprocIfElse
inline ::std::string PreprocIfElse(const ::std::string &symbol, const ::std::string &if_true, const ::std::string &if_false)
Definition: objective_c_generator_helpers.h:100
grpc::protobuf::io::CodedOutputStream
GRPC_CUSTOM_CODEDOUTPUTSTREAM CodedOutputStream
Definition: src/compiler/config.h:55
grpc_objective_c_generator::FrameworkImport
inline ::std::string FrameworkImport(const ::std::string &import, const ::std::string &framework)
Definition: objective_c_generator_helpers.h:69
grpc_objective_c_generator::GetInterface
::std::string GetInterface(const ServiceDescriptor *service, const Parameters &generator_params)
Definition: objective_c_generator.cc:329
google::protobuf::compiler::objectivec::ValidateObjCClassPrefixes
bool ValidateObjCClassPrefixes(const std::vector< const FileDescriptor * > &files, const Options &generation_options, string *out_error)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:1202
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
grpc_objective_c_generator::GetV2Protocol
::std::string GetV2Protocol(const ServiceDescriptor *service)
Definition: objective_c_generator.cc:310
grpc::protobuf::compiler::PluginMain
static int PluginMain(int argc, char *argv[], const CodeGenerator *generator)
Definition: src/compiler/config.h:43
framework
Definition: tools/run_tests/xds_k8s_test_driver/framework/__init__.py:1
grpc_objective_c_generator::Parameters
Definition: objective_c_generator.h:26
grpc_generator::StripSuffix
bool StripSuffix(std::string *filename, const std::string &suffix)
Definition: generator_helpers.h:32
ObjectiveCGrpcGenerator
Definition: objective_c_plugin.cc:77
main
int main(int argc, char *argv[])
Definition: objective_c_plugin.cc:324
grpc_objective_c_generator::Parameters::no_v1_compatibility
bool no_v1_compatibility
Definition: objective_c_generator.h:28
google::protobuf::compiler::objectivec::IsProtobufLibraryBundledProtoFile
bool IsProtobufLibraryBundledProtoFile(const FileDescriptor *file)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:994
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
file::name
char * name
Definition: bloaty/third_party/zlib/examples/gzappend.c:176
google::protobuf::compiler::objectivec::FilePath
string FilePath(const FileDescriptor *file)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:404
objective_c_generator.h
grpc::protobuf::ServiceDescriptor
GRPC_CUSTOM_SERVICEDESCRIPTOR ServiceDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:88
fix_build_deps.dep
string dep
Definition: fix_build_deps.py:439
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_generator::StripPrefix
bool StripPrefix(std::string *name, const std::string &prefix)
Definition: generator_helpers.h:44
grpc_objective_c_generator::PreprocIfNot
inline ::std::string PreprocIfNot(const ::std::string &symbol, const ::std::string &if_true)
Definition: objective_c_generator_helpers.h:94


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:44