main.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "grpc_tools/main.h"
  15. #include <algorithm>
  16. #include <map>
  17. #include <string>
  18. #include <tuple>
  19. #include <unordered_set>
  20. #include <vector>
  21. #include <google/protobuf/compiler/code_generator.h>
  22. #include <google/protobuf/compiler/command_line_interface.h>
  23. #include <google/protobuf/compiler/importer.h>
  24. #include <google/protobuf/compiler/python/python_generator.h>
  25. #include <google/protobuf/descriptor.h>
  26. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  27. #include "src/compiler/python_generator.h"
  28. using ::google::protobuf::FileDescriptor;
  29. using ::google::protobuf::compiler::CodeGenerator;
  30. using ::google::protobuf::compiler::DiskSourceTree;
  31. using ::google::protobuf::compiler::GeneratorContext;
  32. using ::google::protobuf::compiler::Importer;
  33. using ::google::protobuf::compiler::MultiFileErrorCollector;
  34. using ::google::protobuf::io::StringOutputStream;
  35. using ::google::protobuf::io::ZeroCopyOutputStream;
  36. namespace grpc_tools {
  37. int protoc_main(int argc, char* argv[]) {
  38. google::protobuf::compiler::CommandLineInterface cli;
  39. cli.AllowPlugins("protoc-");
  40. // Proto2 Python
  41. google::protobuf::compiler::python::Generator py_generator;
  42. cli.RegisterGenerator("--python_out", &py_generator,
  43. "Generate Python source file.");
  44. // gRPC Python
  45. grpc_python_generator::GeneratorConfiguration grpc_py_config;
  46. grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config);
  47. cli.RegisterGenerator("--grpc_python_out", &grpc_py_generator,
  48. "Generate Python source file.");
  49. return cli.Run(argc, argv);
  50. }
  51. namespace internal {
  52. class GeneratorContextImpl : public GeneratorContext {
  53. public:
  54. GeneratorContextImpl(
  55. const std::vector<const FileDescriptor*>& parsed_files,
  56. std::vector<std::pair<std::string, std::string>>* files_out)
  57. : files_(files_out), parsed_files_(parsed_files) {}
  58. ZeroCopyOutputStream* Open(const std::string& filename) {
  59. files_->emplace_back(filename, "");
  60. return new StringOutputStream(&(files_->back().second));
  61. }
  62. // NOTE(rbellevi): Equivalent to Open, since all files start out empty.
  63. ZeroCopyOutputStream* OpenForAppend(const std::string& filename) {
  64. return Open(filename);
  65. }
  66. // NOTE(rbellevi): Equivalent to Open, since all files start out empty.
  67. ZeroCopyOutputStream* OpenForInsert(const std::string& filename,
  68. const std::string& insertion_point) {
  69. return Open(filename);
  70. }
  71. void ListParsedFiles(
  72. std::vector<const ::google::protobuf::FileDescriptor*>* output) {
  73. *output = parsed_files_;
  74. }
  75. private:
  76. std::vector<std::pair<std::string, std::string>>* files_;
  77. const std::vector<const FileDescriptor*>& parsed_files_;
  78. };
  79. class ErrorCollectorImpl : public MultiFileErrorCollector {
  80. public:
  81. ErrorCollectorImpl(std::vector<::grpc_tools::ProtocError>* errors,
  82. std::vector<::grpc_tools::ProtocWarning>* warnings)
  83. : errors_(errors), warnings_(warnings) {}
  84. void AddError(const std::string& filename, int line, int column,
  85. const std::string& message) {
  86. errors_->emplace_back(filename, line, column, message);
  87. }
  88. void AddWarning(const std::string& filename, int line, int column,
  89. const std::string& message) {
  90. warnings_->emplace_back(filename, line, column, message);
  91. }
  92. private:
  93. std::vector<::grpc_tools::ProtocError>* errors_;
  94. std::vector<::grpc_tools::ProtocWarning>* warnings_;
  95. };
  96. static void calculate_transitive_closure(
  97. const FileDescriptor* descriptor,
  98. std::vector<const FileDescriptor*>* transitive_closure,
  99. std::unordered_set<const ::google::protobuf::FileDescriptor*>* visited) {
  100. for (int i = 0; i < descriptor->dependency_count(); ++i) {
  101. const FileDescriptor* dependency = descriptor->dependency(i);
  102. if (visited->find(dependency) == visited->end()) {
  103. calculate_transitive_closure(dependency, transitive_closure, visited);
  104. }
  105. }
  106. transitive_closure->push_back(descriptor);
  107. visited->insert(descriptor);
  108. }
  109. } // end namespace internal
  110. static int generate_code(
  111. CodeGenerator* code_generator, char* protobuf_path,
  112. const std::vector<std::string>* include_paths,
  113. std::vector<std::pair<std::string, std::string>>* files_out,
  114. std::vector<::grpc_tools::ProtocError>* errors,
  115. std::vector<::grpc_tools::ProtocWarning>* warnings) {
  116. std::unique_ptr<internal::ErrorCollectorImpl> error_collector(
  117. new internal::ErrorCollectorImpl(errors, warnings));
  118. std::unique_ptr<DiskSourceTree> source_tree(new DiskSourceTree());
  119. for (const auto& include_path : *include_paths) {
  120. source_tree->MapPath("", include_path);
  121. }
  122. Importer importer(source_tree.get(), error_collector.get());
  123. const FileDescriptor* parsed_file = importer.Import(protobuf_path);
  124. if (parsed_file == nullptr) {
  125. return 1;
  126. }
  127. std::vector<const FileDescriptor*> transitive_closure;
  128. std::unordered_set<const FileDescriptor*> visited;
  129. internal::calculate_transitive_closure(parsed_file, &transitive_closure,
  130. &visited);
  131. internal::GeneratorContextImpl generator_context(transitive_closure,
  132. files_out);
  133. std::string error;
  134. for (const auto descriptor : transitive_closure) {
  135. code_generator->Generate(descriptor, "", &generator_context, &error);
  136. }
  137. return 0;
  138. }
  139. int protoc_get_protos(
  140. char* protobuf_path, const std::vector<std::string>* include_paths,
  141. std::vector<std::pair<std::string, std::string>>* files_out,
  142. std::vector<::grpc_tools::ProtocError>* errors,
  143. std::vector<::grpc_tools::ProtocWarning>* warnings) {
  144. ::google::protobuf::compiler::python::Generator python_generator;
  145. return generate_code(&python_generator, protobuf_path, include_paths,
  146. files_out, errors, warnings);
  147. }
  148. int protoc_get_services(
  149. char* protobuf_path, const std::vector<std::string>* include_paths,
  150. std::vector<std::pair<std::string, std::string>>* files_out,
  151. std::vector<::grpc_tools::ProtocError>* errors,
  152. std::vector<::grpc_tools::ProtocWarning>* warnings) {
  153. grpc_python_generator::GeneratorConfiguration grpc_py_config;
  154. grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config);
  155. return generate_code(&grpc_py_generator, protobuf_path, include_paths,
  156. files_out, errors, warnings);
  157. }
  158. } // end namespace grpc_tools