objective_c_plugin.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. // Generates Objective C gRPC service interface out of Protobuf IDL.
  34. #include <memory>
  35. #include "src/compiler/config.h"
  36. #include "src/compiler/objective_c_generator.h"
  37. #include "src/compiler/objective_c_generator_helpers.h"
  38. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  39. using ::google::protobuf::compiler::objectivec::ProtobufLibraryFrameworkName;
  40. using ::google::protobuf::compiler::objectivec::
  41. IsProtobufLibraryBundledProtoFile;
  42. class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
  43. public:
  44. ObjectiveCGrpcGenerator() {}
  45. virtual ~ObjectiveCGrpcGenerator() {}
  46. virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
  47. const ::grpc::string &parameter,
  48. grpc::protobuf::compiler::GeneratorContext *context,
  49. ::grpc::string *error) const {
  50. if (file->service_count() == 0) {
  51. // No services. Do nothing.
  52. return true;
  53. }
  54. ::grpc::string file_name;
  55. // Simple parameter parsing as we have only one parameter.
  56. // TODO(mxyan): Complete parameter parsing.
  57. bool dash_as_separator = (0 == parameter.compare("--filename-dash-as-separator"));
  58. if (dash_as_separator) {
  59. file_name = google::protobuf::compiler::objectivec::FilePath(file);
  60. } else {
  61. file_name = grpc_generator::FileNameInUpperCamel(file);
  62. }
  63. ::grpc::string prefix = file->options().objc_class_prefix();
  64. {
  65. // Generate .pbrpc.h
  66. ::grpc::string imports = ::grpc::string("#import \"") + file_name +
  67. ".pbobjc.h\"\n\n"
  68. "#import <ProtoRPC/ProtoService.h>\n"
  69. "#import <ProtoRPC/ProtoRPC.h>\n"
  70. "#import <RxLibrary/GRXWriteable.h>\n"
  71. "#import <RxLibrary/GRXWriter.h>\n";
  72. // TODO(jcanizales): Instead forward-declare the input and output types
  73. // and import the files in the .pbrpc.m
  74. ::grpc::string proto_imports;
  75. for (int i = 0; i < file->dependency_count(); i++) {
  76. ::grpc::string header =
  77. grpc_objective_c_generator::MessageHeaderName(file->dependency(i), dash_as_separator);
  78. const grpc::protobuf::FileDescriptor *dependency = file->dependency(i);
  79. if (IsProtobufLibraryBundledProtoFile(dependency)) {
  80. ::grpc::string base_name = header;
  81. grpc_generator::StripPrefix(&base_name, "google/protobuf/");
  82. // create the import code snippet
  83. proto_imports +=
  84. "#if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS\n"
  85. " #import <" +
  86. ::grpc::string(ProtobufLibraryFrameworkName) + "/" + base_name +
  87. ">\n"
  88. "#else\n"
  89. " #import \"" +
  90. header +
  91. "\"\n"
  92. "#endif\n";
  93. } else {
  94. proto_imports += ::grpc::string("#import \"") + header + "\"\n";
  95. }
  96. }
  97. ::grpc::string declarations;
  98. for (int i = 0; i < file->service_count(); i++) {
  99. const grpc::protobuf::ServiceDescriptor *service = file->service(i);
  100. declarations += grpc_objective_c_generator::GetHeader(service);
  101. }
  102. static const ::grpc::string kNonNullBegin =
  103. "\nNS_ASSUME_NONNULL_BEGIN\n\n";
  104. static const ::grpc::string kNonNullEnd = "\nNS_ASSUME_NONNULL_END\n";
  105. Write(context, file_name + ".pbrpc.h", imports + '\n' + proto_imports +
  106. '\n' + kNonNullBegin +
  107. declarations + kNonNullEnd);
  108. }
  109. {
  110. // Generate .pbrpc.m
  111. ::grpc::string imports = ::grpc::string("#import \"") + file_name +
  112. ".pbrpc.h\"\n\n"
  113. "#import <ProtoRPC/ProtoRPC.h>\n"
  114. "#import <RxLibrary/GRXWriter+Immediate.h>\n";
  115. ::grpc::string definitions;
  116. for (int i = 0; i < file->service_count(); i++) {
  117. const grpc::protobuf::ServiceDescriptor *service = file->service(i);
  118. definitions += grpc_objective_c_generator::GetSource(service);
  119. }
  120. Write(context, file_name + ".pbrpc.m", imports + '\n' + definitions);
  121. }
  122. return true;
  123. }
  124. private:
  125. // Write the given code into the given file.
  126. void Write(grpc::protobuf::compiler::GeneratorContext *context,
  127. const ::grpc::string &filename, const ::grpc::string &code) const {
  128. std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
  129. context->Open(filename));
  130. grpc::protobuf::io::CodedOutputStream coded_out(output.get());
  131. coded_out.WriteRaw(code.data(), code.size());
  132. }
  133. };
  134. int main(int argc, char *argv[]) {
  135. ObjectiveCGrpcGenerator generator;
  136. return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
  137. }