1 // Copyright 2016 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "grpc_tools/main.h"
16 
17 #include <algorithm>
18 #include <map>
19 #include <string>
20 #include <tuple>
21 #include <unordered_set>
22 #include <vector>
23 
24 #include <google/protobuf/compiler/code_generator.h>
25 #include <google/protobuf/compiler/command_line_interface.h>
26 #include <google/protobuf/compiler/importer.h>
27 #include <google/protobuf/compiler/python/generator.h>
28 #include <google/protobuf/compiler/python/pyi_generator.h>
29 #include <google/protobuf/descriptor.h>
30 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
31 
32 #include "src/compiler/python_generator.h"
33 
34 using ::google::protobuf::FileDescriptor;
35 using ::google::protobuf::compiler::CodeGenerator;
36 using ::google::protobuf::compiler::DiskSourceTree;
37 using ::google::protobuf::compiler::GeneratorContext;
38 using ::google::protobuf::compiler::Importer;
39 using ::google::protobuf::compiler::MultiFileErrorCollector;
40 using ::google::protobuf::io::StringOutputStream;
41 using ::google::protobuf::io::ZeroCopyOutputStream;
42 
43 namespace grpc_tools {
protoc_main(int argc,char * argv[])44 int protoc_main(int argc, char* argv[]) {
45   google::protobuf::compiler::CommandLineInterface cli;
46   cli.AllowPlugins("protoc-");
47 
48   // Proto2 Python
49   google::protobuf::compiler::python::Generator py_generator;
50   cli.RegisterGenerator("--python_out", &py_generator,
51                         "Generate Python source file.");
52 
53   // pyi files for type checking
54   google::protobuf::compiler::python::PyiGenerator pyi_generator;
55   cli.RegisterGenerator("--pyi_out", &pyi_generator,
56                         "Generate Python pyi stub.");
57 
58   // gRPC Python
59   grpc_python_generator::GeneratorConfiguration grpc_py_config;
60   grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config);
61   cli.RegisterGenerator("--grpc_python_out", &grpc_py_generator,
62                         "Generate Python source file.");
63 
64   return cli.Run(argc, argv);
65 }
66 
67 namespace internal {
68 
69 class GeneratorContextImpl : public GeneratorContext {
70  public:
GeneratorContextImpl(const std::vector<const FileDescriptor * > & parsed_files,std::vector<std::pair<std::string,std::string>> * files_out)71   GeneratorContextImpl(
72       const std::vector<const FileDescriptor*>& parsed_files,
73       std::vector<std::pair<std::string, std::string>>* files_out)
74       : files_(files_out), parsed_files_(parsed_files) {}
75 
Open(const std::string & filename)76   ZeroCopyOutputStream* Open(const std::string& filename) {
77     files_->emplace_back(filename, "");
78     return new StringOutputStream(&(files_->back().second));
79   }
80 
81   // NOTE(rbellevi): Equivalent to Open, since all files start out empty.
OpenForAppend(const std::string & filename)82   ZeroCopyOutputStream* OpenForAppend(const std::string& filename) {
83     return Open(filename);
84   }
85 
86   // NOTE(rbellevi): Equivalent to Open, since all files start out empty.
OpenForInsert(const std::string & filename,const std::string & insertion_point)87   ZeroCopyOutputStream* OpenForInsert(const std::string& filename,
88                                       const std::string& insertion_point) {
89     return Open(filename);
90   }
91 
ListParsedFiles(std::vector<const::google::protobuf::FileDescriptor * > * output)92   void ListParsedFiles(
93       std::vector<const ::google::protobuf::FileDescriptor*>* output) {
94     *output = parsed_files_;
95   }
96 
97  private:
98   std::vector<std::pair<std::string, std::string>>* files_;
99   const std::vector<const FileDescriptor*>& parsed_files_;
100 };
101 
102 class ErrorCollectorImpl : public MultiFileErrorCollector {
103  public:
ErrorCollectorImpl(std::vector<::grpc_tools::ProtocError> * errors,std::vector<::grpc_tools::ProtocWarning> * warnings)104   ErrorCollectorImpl(std::vector<::grpc_tools::ProtocError>* errors,
105                      std::vector<::grpc_tools::ProtocWarning>* warnings)
106       : errors_(errors), warnings_(warnings) {}
107 
AddError(const std::string & filename,int line,int column,const std::string & message)108   void AddError(const std::string& filename, int line, int column,
109                 const std::string& message) {
110     errors_->emplace_back(filename, line, column, message);
111   }
112 
AddWarning(const std::string & filename,int line,int column,const std::string & message)113   void AddWarning(const std::string& filename, int line, int column,
114                   const std::string& message) {
115     warnings_->emplace_back(filename, line, column, message);
116   }
117 
118  private:
119   std::vector<::grpc_tools::ProtocError>* errors_;
120   std::vector<::grpc_tools::ProtocWarning>* warnings_;
121 };
122 
calculate_transitive_closure(const FileDescriptor * descriptor,std::vector<const FileDescriptor * > * transitive_closure,std::unordered_set<const::google::protobuf::FileDescriptor * > * visited)123 static void calculate_transitive_closure(
124     const FileDescriptor* descriptor,
125     std::vector<const FileDescriptor*>* transitive_closure,
126     std::unordered_set<const ::google::protobuf::FileDescriptor*>* visited) {
127   for (int i = 0; i < descriptor->dependency_count(); ++i) {
128     const FileDescriptor* dependency = descriptor->dependency(i);
129     if (visited->find(dependency) == visited->end()) {
130       calculate_transitive_closure(dependency, transitive_closure, visited);
131     }
132   }
133   transitive_closure->push_back(descriptor);
134   visited->insert(descriptor);
135 }
136 
137 }  // end namespace internal
138 
generate_code(CodeGenerator * code_generator,char * protobuf_path,const std::vector<std::string> * include_paths,std::vector<std::pair<std::string,std::string>> * files_out,std::vector<::grpc_tools::ProtocError> * errors,std::vector<::grpc_tools::ProtocWarning> * warnings)139 static int generate_code(
140     CodeGenerator* code_generator, char* protobuf_path,
141     const std::vector<std::string>* include_paths,
142     std::vector<std::pair<std::string, std::string>>* files_out,
143     std::vector<::grpc_tools::ProtocError>* errors,
144     std::vector<::grpc_tools::ProtocWarning>* warnings) {
145   std::unique_ptr<internal::ErrorCollectorImpl> error_collector(
146       new internal::ErrorCollectorImpl(errors, warnings));
147   std::unique_ptr<DiskSourceTree> source_tree(new DiskSourceTree());
148   for (const auto& include_path : *include_paths) {
149     source_tree->MapPath("", include_path);
150   }
151   Importer importer(source_tree.get(), error_collector.get());
152   const FileDescriptor* parsed_file = importer.Import(protobuf_path);
153   if (parsed_file == nullptr) {
154     return 1;
155   }
156   std::vector<const FileDescriptor*> transitive_closure;
157   std::unordered_set<const FileDescriptor*> visited;
158   internal::calculate_transitive_closure(parsed_file, &transitive_closure,
159                                          &visited);
160   internal::GeneratorContextImpl generator_context(transitive_closure,
161                                                    files_out);
162   std::string error;
163   for (const auto descriptor : transitive_closure) {
164     code_generator->Generate(descriptor, "", &generator_context, &error);
165   }
166   return 0;
167 }
168 
protoc_get_protos(char * protobuf_path,const std::vector<std::string> * include_paths,std::vector<std::pair<std::string,std::string>> * files_out,std::vector<::grpc_tools::ProtocError> * errors,std::vector<::grpc_tools::ProtocWarning> * warnings)169 int protoc_get_protos(
170     char* protobuf_path, const std::vector<std::string>* include_paths,
171     std::vector<std::pair<std::string, std::string>>* files_out,
172     std::vector<::grpc_tools::ProtocError>* errors,
173     std::vector<::grpc_tools::ProtocWarning>* warnings) {
174   ::google::protobuf::compiler::python::Generator python_generator;
175   return generate_code(&python_generator, protobuf_path, include_paths,
176                        files_out, errors, warnings);
177 }
178 
protoc_get_services(char * protobuf_path,const std::vector<std::string> * include_paths,std::vector<std::pair<std::string,std::string>> * files_out,std::vector<::grpc_tools::ProtocError> * errors,std::vector<::grpc_tools::ProtocWarning> * warnings)179 int protoc_get_services(
180     char* protobuf_path, const std::vector<std::string>* include_paths,
181     std::vector<std::pair<std::string, std::string>>* files_out,
182     std::vector<::grpc_tools::ProtocError>* errors,
183     std::vector<::grpc_tools::ProtocWarning>* warnings) {
184   grpc_python_generator::GeneratorConfiguration grpc_py_config;
185   grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config);
186   return generate_code(&grpc_py_generator, protobuf_path, include_paths,
187                        files_out, errors, warnings);
188 }
189 }  // end namespace grpc_tools
190