tools/distrib/python/grpcio_tools/grpc_tools/main.cc
Go to the documentation of this file.
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/python_generator.h>
28 #include <google/protobuf/descriptor.h>
29 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
30 
32 
41 
42 namespace grpc_tools {
43 int protoc_main(int argc, char* argv[]) {
45  cli.AllowPlugins("protoc-");
46 
47  // Proto2 Python
49  cli.RegisterGenerator("--python_out", &py_generator,
50  "Generate Python source file.");
51 
52  // gRPC Python
54  grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config);
55  cli.RegisterGenerator("--grpc_python_out", &grpc_py_generator,
56  "Generate Python source file.");
57 
58  return cli.Run(argc, argv);
59 }
60 
61 namespace internal {
62 
64  public:
66  const std::vector<const FileDescriptor*>& parsed_files,
67  std::vector<std::pair<std::string, std::string>>* files_out)
68  : files_(files_out), parsed_files_(parsed_files) {}
69 
71  files_->emplace_back(filename, "");
72  return new StringOutputStream(&(files_->back().second));
73  }
74 
75  // NOTE(rbellevi): Equivalent to Open, since all files start out empty.
77  return Open(filename);
78  }
79 
80  // NOTE(rbellevi): Equivalent to Open, since all files start out empty.
82  const std::string& insertion_point) {
83  return Open(filename);
84  }
85 
87  std::vector<const ::google::protobuf::FileDescriptor*>* output) {
89  }
90 
91  private:
92  std::vector<std::pair<std::string, std::string>>* files_;
93  const std::vector<const FileDescriptor*>& parsed_files_;
94 };
95 
97  public:
98  ErrorCollectorImpl(std::vector<::grpc_tools::ProtocError>* errors,
99  std::vector<::grpc_tools::ProtocWarning>* warnings)
100  : errors_(errors), warnings_(warnings) {}
101 
102  void AddError(const std::string& filename, int line, int column,
103  const std::string& message) {
104  errors_->emplace_back(filename, line, column, message);
105  }
106 
107  void AddWarning(const std::string& filename, int line, int column,
108  const std::string& message) {
109  warnings_->emplace_back(filename, line, column, message);
110  }
111 
112  private:
113  std::vector<::grpc_tools::ProtocError>* errors_;
114  std::vector<::grpc_tools::ProtocWarning>* warnings_;
115 };
116 
118  const FileDescriptor* descriptor,
119  std::vector<const FileDescriptor*>* transitive_closure,
120  std::unordered_set<const ::google::protobuf::FileDescriptor*>* visited) {
121  for (int i = 0; i < descriptor->dependency_count(); ++i) {
122  const FileDescriptor* dependency = descriptor->dependency(i);
123  if (visited->find(dependency) == visited->end()) {
124  calculate_transitive_closure(dependency, transitive_closure, visited);
125  }
126  }
127  transitive_closure->push_back(descriptor);
128  visited->insert(descriptor);
129 }
130 
131 } // end namespace internal
132 
133 static int generate_code(
134  CodeGenerator* code_generator, char* protobuf_path,
135  const std::vector<std::string>* include_paths,
136  std::vector<std::pair<std::string, std::string>>* files_out,
137  std::vector<::grpc_tools::ProtocError>* errors,
138  std::vector<::grpc_tools::ProtocWarning>* warnings) {
139  std::unique_ptr<internal::ErrorCollectorImpl> error_collector(
140  new internal::ErrorCollectorImpl(errors, warnings));
141  std::unique_ptr<DiskSourceTree> source_tree(new DiskSourceTree());
142  for (const auto& include_path : *include_paths) {
143  source_tree->MapPath("", include_path);
144  }
145  Importer importer(source_tree.get(), error_collector.get());
146  const FileDescriptor* parsed_file = importer.Import(protobuf_path);
147  if (parsed_file == nullptr) {
148  return 1;
149  }
150  std::vector<const FileDescriptor*> transitive_closure;
151  std::unordered_set<const FileDescriptor*> visited;
152  internal::calculate_transitive_closure(parsed_file, &transitive_closure,
153  &visited);
154  internal::GeneratorContextImpl generator_context(transitive_closure,
155  files_out);
157  for (const auto descriptor : transitive_closure) {
158  code_generator->Generate(descriptor, "", &generator_context, &error);
159  }
160  return 0;
161 }
162 
164  char* protobuf_path, const std::vector<std::string>* include_paths,
165  std::vector<std::pair<std::string, std::string>>* files_out,
166  std::vector<::grpc_tools::ProtocError>* errors,
167  std::vector<::grpc_tools::ProtocWarning>* warnings) {
169  return generate_code(&python_generator, protobuf_path, include_paths,
170  files_out, errors, warnings);
171 }
172 
174  char* protobuf_path, const std::vector<std::string>* include_paths,
175  std::vector<std::pair<std::string, std::string>>* files_out,
176  std::vector<::grpc_tools::ProtocError>* errors,
177  std::vector<::grpc_tools::ProtocWarning>* warnings) {
179  grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config);
180  return generate_code(&grpc_py_generator, protobuf_path, include_paths,
181  files_out, errors, warnings);
182 }
183 } // end namespace grpc_tools
grpc_tools::internal::ErrorCollectorImpl::errors_
std::vector<::grpc_tools::ProtocError > * errors_
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:113
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
grpc_tools::internal::GeneratorContextImpl::OpenForAppend
ZeroCopyOutputStream * OpenForAppend(const std::string &filename)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:76
google::protobuf::compiler::CommandLineInterface::RegisterGenerator
void RegisterGenerator(const std::string &flag_name, CodeGenerator *generator, const std::string &help_text)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc:788
grpc_tools::internal::GeneratorContextImpl::Open
ZeroCopyOutputStream * Open(const std::string &filename)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:70
grpc_tools::internal::ErrorCollectorImpl::warnings_
std::vector<::grpc_tools::ProtocWarning > * warnings_
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:114
FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:128
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
grpc_tools::protoc_get_protos
int 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)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:163
visited
bool visited
Definition: abseil-cpp/absl/synchronization/internal/graphcycles.cc:282
grpc::protobuf::compiler::Importer
GRPC_CUSTOM_IMPORTER Importer
Definition: config_grpc_cli.h:63
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
grpc_tools::internal::GeneratorContextImpl
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:63
grpc::protobuf::compiler::DiskSourceTree
GRPC_CUSTOM_DISKSOURCETREE DiskSourceTree
Definition: config_grpc_cli.h:62
grpc_tools::internal::GeneratorContextImpl::ListParsedFiles
void ListParsedFiles(std::vector< const ::google::protobuf::FileDescriptor * > *output)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:86
google::protobuf::compiler::CommandLineInterface::Run
int Run(int argc, const char *const argv[])
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc:814
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
grpc_tools::internal::ErrorCollectorImpl::AddWarning
void AddWarning(const std::string &filename, int line, int column, const std::string &message)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:107
grpc::protobuf::io::StringOutputStream
GRPC_CUSTOM_STRINGOUTPUTSTREAM StringOutputStream
Definition: src/compiler/config.h:56
grpc_python_generator::GeneratorConfiguration
Definition: src/compiler/python_generator.h:32
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
google::protobuf::compiler::CommandLineInterface
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.h:109
grpc::protobuf::compiler::CodeGenerator
GRPC_CUSTOM_CODEGENERATOR CodeGenerator
Definition: src/compiler/config.h:41
errors
const char * errors
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:841
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc_tools::internal::GeneratorContextImpl::GeneratorContextImpl
GeneratorContextImpl(const std::vector< const FileDescriptor * > &parsed_files, std::vector< std::pair< std::string, std::string >> *files_out)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:65
grpc_tools::internal::GeneratorContextImpl::files_
std::vector< std::pair< std::string, std::string > > * files_
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:92
grpc_tools::protoc_main
int protoc_main(int argc, char *argv[])
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:43
grpc::protobuf::compiler::MultiFileErrorCollector
GRPC_CUSTOM_MULTIFILEERRORCOLLECTOR MultiFileErrorCollector
Definition: config_grpc_cli.h:64
grpc_tools::internal::ErrorCollectorImpl
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:96
grpc_tools::internal::GeneratorContextImpl::parsed_files_
const std::vector< const FileDescriptor * > & parsed_files_
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:93
grpc_tools::generate_code
static int 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)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:133
grpc_python_generator::PythonGrpcGenerator
Definition: src/compiler/python_generator.h:42
main.h
grpc_tools::internal::calculate_transitive_closure
static void calculate_transitive_closure(const FileDescriptor *descriptor, std::vector< const FileDescriptor * > *transitive_closure, std::unordered_set< const ::google::protobuf::FileDescriptor * > *visited)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:117
google::protobuf::compiler::CommandLineInterface::AllowPlugins
void AllowPlugins(const std::string &exe_name_prefix)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc:810
grpc_tools::protoc_get_services
int 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)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:173
regen-readme.line
line
Definition: regen-readme.py:30
grpc_tools::internal::GeneratorContextImpl::OpenForInsert
ZeroCopyOutputStream * OpenForInsert(const std::string &filename, const std::string &insertion_point)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:81
grpc_tools::internal::ErrorCollectorImpl::ErrorCollectorImpl
ErrorCollectorImpl(std::vector<::grpc_tools::ProtocError > *errors, std::vector<::grpc_tools::ProtocWarning > *warnings)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:98
grpc_tools
Definition: tools/distrib/python/grpcio_tools/grpc_tools/__init__.py:1
FileDescriptor
struct FileDescriptor FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:56
google::protobuf::compiler::python::Generator
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/compiler/python/python_generator.h:66
internal
Definition: benchmark/test/output_test_helper.cc:20
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
grpc_tools::internal::ErrorCollectorImpl::AddError
void AddError(const std::string &filename, int line, int column, const std::string &message)
Definition: tools/distrib/python/grpcio_tools/grpc_tools/main.cc:102
python_generator.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:17