generatorbase.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #pragma once
  26. #include <google/protobuf/compiler/code_generator.h>
  27. #include <google/protobuf/io/zero_copy_stream.h>
  28. #include <string>
  29. #include <memory>
  30. #include <functional>
  31. #include <google/protobuf/descriptor.h>
  32. namespace google { namespace protobuf {
  33. class FileDescriptor;
  34. class Descriptor;
  35. namespace compiler {
  36. class GeneratorContext;
  37. }}}
  38. namespace QtProtobuf {
  39. namespace generator {
  40. /*!
  41. * \defgroup generator
  42. *
  43. * \brief QtProtobuf code generator for protobuf messages and grpc services
  44. *
  45. * \details QtProtobuf code generator is program to be used in conjunction with proto compiler aka protoc.
  46. *
  47. * It might be used in few ways:
  48. * - manualy
  49. * - \ref cmake "CMake macro"
  50. * - \ref qmake "qmake macro"
  51. *
  52. * \section Manual usage
  53. *
  54. * \code
  55. * [QT_PROTOBUF_OPTIONS="[SINGLE|MULTI]:QML:COMMENTS:FOLDER"] protoc --plugin=protoc-gen-qtprotobuf=<path/to/bin/>qtprotobufgen --qtprotobuf_out=<output_dir> [-I/extra/proto/include/path] <protofile>.proto
  56. * \endcode
  57. *
  58. * Generator supports options that could be provided as environment variable to tune generation.
  59. * Following options are supported:
  60. *
  61. * *SINGLE* - enables single-file generation when for each *.proto* file single pair of *.h* *.cpp* files is generated
  62. *
  63. * \note Enabled by default. Excluded by SINGLE
  64. *
  65. * *MULTI* - enables multi-file generation when for each message separate pair of *.h* *.cpp*
  66. *
  67. * \note Has higher priority than SINGLE
  68. *
  69. * *QML* - enables QML code generation in protobuf classes. If is set QML-related code for lists and QML registration to be generated.
  70. *
  71. * *COMMENTS* - enables comments copying from .proto files
  72. *
  73. * *FOLDER* - enables folder-based generation
  74. *
  75. * \section cmake CMake
  76. *
  77. * For CMake based project QtProtobuf has macroses those should be used to generate code and in link it to your project:
  78. *
  79. * - \ref cmake_qt6_protobuf_generate
  80. * - \ref cmake_qt_internal_link_protobuf_objects
  81. *
  82. *
  83. * \subsection cmake_qt6_protobuf_generate qt6_protobuf_generate
  84. *
  85. * \brief qt6_protobuf_generate is cmake helper function that automatically generates STATIC library target from your .proto files
  86. *
  87. * \param TARGET a name of your target that the generated code will be linked to.
  88. * \param GENERATED_TARGET name that will be used for generated archive library target. It's usefull when you supposed to have multiple generated targets to be linked to single one.
  89. * \param OUTPUT_DIRECTORY output directory that will contain generated artifacts. Usually subfolder in build directory should be used
  90. * \param EXCLUDE_HEADERS List of header files to be excluded from pre-parsed list of expected header files (e.g. nested messages that are not supported by QtProtobuf generator)
  91. * \param PROTO_FILES List of .proto files that will be used in generation procedure
  92. * \param MULTI Enables multi-files generation mode. If provided in parameter list generator will create pair of header/source files for each message
  93. * \note multi-files generation mode is defined as deprecated by QtProtobuf team, and might have poor support in future
  94. * \param QML Enables QML code generation in protobuf classes. If provided in parameter list QML related code for lists and QML registration to be generated.
  95. * \param COMMENTS Enables comments copying from .proto files. If provided in parameter list message and field related comments will be copied to generated header files.
  96. * \param FOLDER Enables folder based generation. If provided in parameter list generator will place generated artifacts to folder structure according to package of corresponding .proto file
  97. * \param FIELDENUM Enables generation of field numbers as an enum within the message class.
  98. * \param EXTRA_NAMESPACE <namespace> Wraps the generated code with the specified namespace(EXPERIMENTAL).
  99. *
  100. * \subsection cmake_qt_internal_link_protobuf_objects _qt_internal_link_protobuf_objects
  101. *
  102. * \brief _qt_internal_link_protobuf_objects is cmake helper function that links generated protobuf target to your binary
  103. *
  104. * \details It's useful when you try to link generated target to shared library or/and to executable that doesn't utilize all protobuf generated classes directly from C++ code, but requires them from QML.
  105. *
  106. * \param TARGET name of target to link to
  107. * \param GENERATED_TARGET protobuf generated target name
  108. *
  109. * \section qmake
  110. * - \ref qmake_qt6_protobuf_generate
  111. *
  112. * \subsection qmake_qt6_protobuf_generate qt6_protobuf_generate
  113. *
  114. * \brief qt6_protobuf_generate is qmake helper <a href="https://doc.qt.io/qt-5/qmake-language.html#test-functions">test function</a> that generates QtProtobuf source code based on files provided by PROTO_FILES global context variable
  115. * \param generate_qml generate_qml enables/disables QML code generation in protobuf classes. If set to `true` QML-related code for lists and QML registration to be generated.
  116. */
  117. /*!
  118. * \ingroup generator
  119. * \private
  120. * \brief The GeneratorBase class
  121. */
  122. class GeneratorBase: public ::google::protobuf::compiler::CodeGenerator
  123. {
  124. public:
  125. enum Mode {
  126. SingleMode = 0,
  127. MultiMode,
  128. };
  129. GeneratorBase(Mode mode);
  130. virtual ~GeneratorBase() = default;
  131. virtual bool GenerateAll(const std::vector<const ::google::protobuf::FileDescriptor *> &files,
  132. const std::string &parameter,
  133. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  134. std::string *error) const override;
  135. bool HasGenerateAll() const override { return true; }
  136. static void printDisclaimer(const std::shared_ptr<::google::protobuf::io::Printer> printer);
  137. static void printPreamble(const std::shared_ptr<::google::protobuf::io::Printer> printer);
  138. static void printInclude(const std::shared_ptr<::google::protobuf::io::Printer> printer, const google::protobuf::Descriptor *message, const google::protobuf::FieldDescriptor *field, std::set<std::string> &existingIncludes);
  139. static void printNamespaces(const std::shared_ptr<::google::protobuf::io::Printer> printer, const std::vector<std::string> namespaces);
  140. protected:
  141. static std::string generateBaseName(const ::google::protobuf::FileDescriptor *file, std::string name);
  142. private:
  143. Mode m_mode;
  144. };
  145. }
  146. }