plugin.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. #include <google/protobuf/compiler/plugin.h>
  32. #include <iostream>
  33. #include <set>
  34. #ifdef _WIN32
  35. #include <fcntl.h>
  36. #else
  37. #include <unistd.h>
  38. #endif
  39. #include <google/protobuf/stubs/logging.h>
  40. #include <google/protobuf/stubs/common.h>
  41. #include <google/protobuf/compiler/plugin.pb.h>
  42. #include <google/protobuf/compiler/code_generator.h>
  43. #include <google/protobuf/io/zero_copy_stream_impl.h>
  44. #include <google/protobuf/descriptor.h>
  45. #include <google/protobuf/stubs/io_win32.h>
  46. namespace google {
  47. namespace protobuf {
  48. namespace compiler {
  49. #if defined(_WIN32)
  50. // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
  51. // them like we do below.
  52. using google::protobuf::internal::win32::setmode;
  53. #endif
  54. class GeneratorResponseContext : public GeneratorContext {
  55. public:
  56. GeneratorResponseContext(
  57. const Version& compiler_version,
  58. CodeGeneratorResponse* response,
  59. const std::vector<const FileDescriptor*>& parsed_files)
  60. : compiler_version_(compiler_version),
  61. response_(response),
  62. parsed_files_(parsed_files) {}
  63. virtual ~GeneratorResponseContext() {}
  64. // implements GeneratorContext --------------------------------------
  65. virtual io::ZeroCopyOutputStream* Open(const string& filename) {
  66. CodeGeneratorResponse::File* file = response_->add_file();
  67. file->set_name(filename);
  68. return new io::StringOutputStream(file->mutable_content());
  69. }
  70. virtual io::ZeroCopyOutputStream* OpenForInsert(
  71. const string& filename, const string& insertion_point) {
  72. CodeGeneratorResponse::File* file = response_->add_file();
  73. file->set_name(filename);
  74. file->set_insertion_point(insertion_point);
  75. return new io::StringOutputStream(file->mutable_content());
  76. }
  77. void ListParsedFiles(std::vector<const FileDescriptor*>* output) {
  78. *output = parsed_files_;
  79. }
  80. void GetCompilerVersion(Version* version) const {
  81. *version = compiler_version_;
  82. }
  83. private:
  84. Version compiler_version_;
  85. CodeGeneratorResponse* response_;
  86. const std::vector<const FileDescriptor*>& parsed_files_;
  87. };
  88. bool GenerateCode(const CodeGeneratorRequest& request,
  89. const CodeGenerator& generator, CodeGeneratorResponse* response,
  90. string* error_msg) {
  91. DescriptorPool pool;
  92. for (int i = 0; i < request.proto_file_size(); i++) {
  93. const FileDescriptor* file = pool.BuildFile(request.proto_file(i));
  94. if (file == NULL) {
  95. // BuildFile() already wrote an error message.
  96. return false;
  97. }
  98. }
  99. std::vector<const FileDescriptor*> parsed_files;
  100. for (int i = 0; i < request.file_to_generate_size(); i++) {
  101. parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i)));
  102. if (parsed_files.back() == NULL) {
  103. *error_msg = "protoc asked plugin to generate a file but "
  104. "did not provide a descriptor for the file: " +
  105. request.file_to_generate(i);
  106. return false;
  107. }
  108. }
  109. GeneratorResponseContext context(
  110. request.compiler_version(), response, parsed_files);
  111. string error;
  112. bool succeeded = generator.GenerateAll(
  113. parsed_files, request.parameter(), &context, &error);
  114. if (!succeeded && error.empty()) {
  115. error = "Code generator returned false but provided no error "
  116. "description.";
  117. }
  118. if (!error.empty()) {
  119. response->set_error(error);
  120. }
  121. return true;
  122. }
  123. int PluginMain(int argc, char* argv[], const CodeGenerator* generator) {
  124. if (argc > 1) {
  125. std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl;
  126. return 1;
  127. }
  128. #ifdef _WIN32
  129. setmode(STDIN_FILENO, _O_BINARY);
  130. setmode(STDOUT_FILENO, _O_BINARY);
  131. #endif
  132. CodeGeneratorRequest request;
  133. if (!request.ParseFromFileDescriptor(STDIN_FILENO)) {
  134. std::cerr << argv[0] << ": protoc sent unparseable request to plugin."
  135. << std::endl;
  136. return 1;
  137. }
  138. string error_msg;
  139. CodeGeneratorResponse response;
  140. if (GenerateCode(request, *generator, &response, &error_msg)) {
  141. if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) {
  142. std::cerr << argv[0] << ": Error writing to stdout." << std::endl;
  143. return 1;
  144. }
  145. } else {
  146. if (!error_msg.empty()) {
  147. std::cerr << argv[0] << ": " << error_msg << std::endl;
  148. }
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. } // namespace compiler
  154. } // namespace protobuf
  155. } // namespace google