murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 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 | |
Makarand Dharmapurikar | 2b220f8 | 2016-07-08 10:47:57 -0700 | [diff] [blame] | 27 | #include <google/protobuf/compiler/objectivec/objectivec_helpers.h> |
| 28 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 29 | using ::google::protobuf::compiler::objectivec:: |
| 30 | IsProtobufLibraryBundledProtoFile; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 31 | using ::google::protobuf::compiler::objectivec::ProtobufLibraryFrameworkName; |
Makarand Dharmapurikar | 46cc9ee | 2016-07-08 11:42:07 -0700 | [diff] [blame] | 32 | |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 33 | class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
| 34 | public: |
| 35 | ObjectiveCGrpcGenerator() {} |
| 36 | virtual ~ObjectiveCGrpcGenerator() {} |
| 37 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 38 | virtual bool Generate(const grpc::protobuf::FileDescriptor* file, |
| 39 | const ::grpc::string& parameter, |
| 40 | grpc::protobuf::compiler::GeneratorContext* context, |
| 41 | ::grpc::string* error) const { |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 42 | if (file->service_count() == 0) { |
| 43 | // No services. Do nothing. |
| 44 | return true; |
| 45 | } |
| 46 | |
Muxi Yan | d044281 | 2017-06-08 10:06:39 -0700 | [diff] [blame] | 47 | ::grpc::string file_name = |
| 48 | google::protobuf::compiler::objectivec::FilePath(file); |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 49 | ::grpc::string prefix = file->options().objc_class_prefix(); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 50 | |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 51 | { |
| 52 | // Generate .pbrpc.h |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 53 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 54 | ::grpc::string imports = ::grpc::string("#import \"") + file_name + |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 55 | ".pbobjc.h\"\n\n" |
| 56 | "#import <ProtoRPC/ProtoService.h>\n" |
Muxi Yan | 56369ea | 2017-04-06 11:52:43 -0700 | [diff] [blame] | 57 | "#import <ProtoRPC/ProtoRPC.h>\n" |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 58 | "#import <RxLibrary/GRXWriteable.h>\n" |
| 59 | "#import <RxLibrary/GRXWriter.h>\n"; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 60 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 61 | ::grpc::string proto_imports; |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 62 | proto_imports += "#if GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO\n" + |
| 63 | grpc_objective_c_generator::GetAllMessageClasses(file) + |
| 64 | "#else\n"; |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 65 | for (int i = 0; i < file->dependency_count(); i++) { |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 66 | ::grpc::string header = |
| 67 | grpc_objective_c_generator::MessageHeaderName(file->dependency(i)); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 68 | const grpc::protobuf::FileDescriptor* dependency = file->dependency(i); |
Makarand Dharmapurikar | 46cc9ee | 2016-07-08 11:42:07 -0700 | [diff] [blame] | 69 | if (IsProtobufLibraryBundledProtoFile(dependency)) { |
Makarand Dharmapurikar | 2b220f8 | 2016-07-08 10:47:57 -0700 | [diff] [blame] | 70 | ::grpc::string base_name = header; |
| 71 | grpc_generator::StripPrefix(&base_name, "google/protobuf/"); |
Makarand Dharmapurikar | 77f8da2 | 2016-07-08 15:03:20 -0700 | [diff] [blame] | 72 | // create the import code snippet |
Makarand Dharmapurikar | 2b220f8 | 2016-07-08 10:47:57 -0700 | [diff] [blame] | 73 | proto_imports += |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 74 | " #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS\n" |
| 75 | " #import <" + |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 76 | ::grpc::string(ProtobufLibraryFrameworkName) + "/" + base_name + |
| 77 | ">\n" |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 78 | " #else\n" |
| 79 | " #import \"" + |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 80 | header + |
| 81 | "\"\n" |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 82 | " #endif\n"; |
Makarand Dharmapurikar | 8620252 | 2016-07-08 12:41:12 -0700 | [diff] [blame] | 83 | } else { |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 84 | proto_imports += ::grpc::string(" #import \"") + header + "\"\n"; |
Makarand Dharmapurikar | 8620252 | 2016-07-08 12:41:12 -0700 | [diff] [blame] | 85 | } |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 86 | } |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 87 | proto_imports += "#endif\n"; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 88 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 89 | ::grpc::string declarations; |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 90 | for (int i = 0; i < file->service_count(); i++) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 91 | const grpc::protobuf::ServiceDescriptor* service = file->service(i); |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 92 | declarations += grpc_objective_c_generator::GetHeader(service); |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 93 | } |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 94 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 95 | static const ::grpc::string kNonNullBegin = |
| 96 | "\nNS_ASSUME_NONNULL_BEGIN\n\n"; |
Benjamin Herzog | 1843ccb | 2016-04-27 17:39:26 +0200 | [diff] [blame] | 97 | static const ::grpc::string kNonNullEnd = "\nNS_ASSUME_NONNULL_END\n"; |
Benjamin Herzog | 208795c | 2016-04-18 18:10:14 +0200 | [diff] [blame] | 98 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 99 | Write(context, file_name + ".pbrpc.h", |
| 100 | imports + '\n' + proto_imports + '\n' + kNonNullBegin + |
| 101 | declarations + kNonNullEnd); |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | { |
| 105 | // Generate .pbrpc.m |
| 106 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 107 | ::grpc::string imports = ::grpc::string("#import \"") + file_name + |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 108 | ".pbrpc.h\"\n\n" |
| 109 | "#import <ProtoRPC/ProtoRPC.h>\n" |
| 110 | "#import <RxLibrary/GRXWriter+Immediate.h>\n"; |
Muxi Yan | 1e32fea | 2017-09-19 09:46:12 -0700 | [diff] [blame] | 111 | for (int i = 0; i < file->dependency_count(); i++) { |
| 112 | ::grpc::string header = |
| 113 | grpc_objective_c_generator::MessageHeaderName(file->dependency(i)); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 114 | const grpc::protobuf::FileDescriptor* dependency = file->dependency(i); |
Muxi Yan | 1e32fea | 2017-09-19 09:46:12 -0700 | [diff] [blame] | 115 | if (IsProtobufLibraryBundledProtoFile(dependency)) { |
| 116 | ::grpc::string base_name = header; |
| 117 | grpc_generator::StripPrefix(&base_name, "google/protobuf/"); |
| 118 | // create the import code snippet |
| 119 | imports += |
| 120 | "#if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS\n" |
| 121 | " #import <" + |
| 122 | ::grpc::string(ProtobufLibraryFrameworkName) + "/" + base_name + |
| 123 | ">\n" |
| 124 | "#else\n" |
| 125 | " #import \"" + |
| 126 | header + |
| 127 | "\"\n" |
| 128 | "#endif\n"; |
| 129 | } else { |
| 130 | imports += ::grpc::string("#import \"") + header + "\"\n"; |
| 131 | } |
| 132 | } |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 133 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 134 | ::grpc::string definitions; |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 135 | for (int i = 0; i < file->service_count(); i++) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 136 | const grpc::protobuf::ServiceDescriptor* service = file->service(i); |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 137 | definitions += grpc_objective_c_generator::GetSource(service); |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Write(context, file_name + ".pbrpc.m", imports + '\n' + definitions); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | private: |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 147 | // Write the given code into the given file. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 148 | void Write(grpc::protobuf::compiler::GeneratorContext* context, |
| 149 | const ::grpc::string& filename, const ::grpc::string& code) const { |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 150 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
| 151 | context->Open(filename)); |
| 152 | grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
| 153 | coded_out.WriteRaw(code.data(), code.size()); |
| 154 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 157 | int main(int argc, char* argv[]) { |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 158 | ObjectiveCGrpcGenerator generator; |
| 159 | return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); |
| 160 | } |