protoc-gen-gogoproto.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "google/protobuf/compiler/code_generator.h"
  2. #include "google/protobuf/io/zero_copy_stream.h"
  3. #include "google/protobuf/io/printer.h"
  4. #include "google/protobuf/descriptor.h"
  5. #include "google/protobuf/descriptor.pb.h"
  6. #include "schema_proto2_to_proto3_util.h"
  7. #include "google/protobuf/compiler/plugin.h"
  8. using google::protobuf::FileDescriptorProto;
  9. using google::protobuf::FileDescriptor;
  10. using google::protobuf::DescriptorPool;
  11. using google::protobuf::io::Printer;
  12. using google::protobuf::util::SchemaGroupStripper;
  13. using google::protobuf::util::EnumScrubber;
  14. namespace google {
  15. namespace protobuf {
  16. namespace compiler {
  17. namespace {
  18. string StripProto(string filename) {
  19. if (filename.substr(filename.size() - 11) == ".protodevel") {
  20. // .protodevel
  21. return filename.substr(0, filename.size() - 11);
  22. } else {
  23. // .proto
  24. return filename.substr(0, filename.size() - 6);
  25. }
  26. }
  27. DescriptorPool new_pool_;
  28. } // namespace
  29. class GoGoProtoGenerator : public CodeGenerator {
  30. public:
  31. virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
  32. const string& parameter,
  33. GeneratorContext* context,
  34. string* error) const {
  35. for (int i = 0; i < files.size(); i++) {
  36. for (auto file : files) {
  37. bool can_generate =
  38. (new_pool_.FindFileByName(file->name()) == nullptr);
  39. for (int j = 0; j < file->dependency_count(); j++) {
  40. can_generate &= (new_pool_.FindFileByName(
  41. file->dependency(j)->name()) != nullptr);
  42. }
  43. for (int j = 0; j < file->public_dependency_count(); j++) {
  44. can_generate &= (new_pool_.FindFileByName(
  45. file->public_dependency(j)->name()) != nullptr);
  46. }
  47. for (int j = 0; j < file->weak_dependency_count(); j++) {
  48. can_generate &= (new_pool_.FindFileByName(
  49. file->weak_dependency(j)->name()) != nullptr);
  50. }
  51. if (can_generate) {
  52. Generate(file, parameter, context, error);
  53. break;
  54. }
  55. }
  56. }
  57. return true;
  58. }
  59. virtual bool Generate(const FileDescriptor* file,
  60. const string& parameter,
  61. GeneratorContext* context,
  62. string* error) const {
  63. FileDescriptorProto new_file;
  64. file->CopyTo(&new_file);
  65. SchemaGroupStripper::StripFile(file, &new_file);
  66. EnumScrubber enum_scrubber;
  67. enum_scrubber.ScrubFile(&new_file);
  68. string filename = file->name();
  69. string basename = StripProto(filename);
  70. std::vector<std::pair<string,string>> option_pairs;
  71. ParseGeneratorParameter(parameter, &option_pairs);
  72. std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
  73. context->Open(basename + ".proto"));
  74. string content = new_pool_.BuildFile(new_file)->DebugString();
  75. Printer printer(output.get(), '$');
  76. printer.WriteRaw(content.c_str(), content.size());
  77. return true;
  78. }
  79. };
  80. } // namespace compiler
  81. } // namespace protobuf
  82. } // namespace google
  83. int main(int argc, char* argv[]) {
  84. google::protobuf::compiler::GoGoProtoGenerator generator;
  85. return google::protobuf::compiler::PluginMain(argc, argv, &generator);
  86. }