generator.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "generatorbase.h"
  31. namespace google { namespace protobuf {
  32. class FileDescriptor;
  33. namespace compiler {
  34. class GeneratorContext;
  35. }}}
  36. namespace QtProtobuf {
  37. namespace generator {
  38. /*!
  39. * \defgroup generator
  40. *
  41. * \brief QtProtobuf code generator for protobuf messages and grpc services
  42. *
  43. * \details QtProtobuf code generator is program to be used in conjunction with proto compiler aka protoc.
  44. *
  45. * It might be used in few ways:
  46. * - manualy
  47. * - \ref cmake "CMake macro"
  48. * - \ref qmake "qmake macro"
  49. *
  50. * \section Manual usage
  51. *
  52. * \code
  53. * [QT_PROTOBUF_OPTIONS="[SINGLE|MULTI]:QML:COMMENTS"] protoc --plugin=protoc-gen-qtprotobuf=<path/to/bin/>qtprotobufgen --qtprotobuf_out=<output_dir> [-I/extra/proto/include/path] <protofile>.proto
  54. * \endcode
  55. *
  56. * Generator supports options that could be provided as environment variable to tune generation.
  57. * Following options are supported:
  58. *
  59. * *SINGLE* - enables single-file generation when for each *.proto* file single pair of *.h* *.cpp* files is generated
  60. *
  61. * \note Enabled by default. Excluded by SINGLE
  62. *
  63. * *MULTI* - enables multi-file generation when for each message separate pair of *.h* *.cpp*
  64. *
  65. * \note Has higher priority than SINGLE
  66. *
  67. * *QML* - enables QML code generation in protobuf classes. If is set QML-related code for lists and QML registration to be generated.
  68. *
  69. * *COMMENTS* - enables comments copying from .proto files
  70. *
  71. * \section cmake CMake
  72. *
  73. * For CMake based project QtProtobuf has macroses those should be used to generate code and in link it to your project:
  74. *
  75. * - \ref cmake_qtprotobuf_generate
  76. * - \ref cmake_qtprotobuf_link_archive
  77. *
  78. *
  79. * \subsection cmake_qtprotobuf_generate qtprotobuf_generate
  80. *
  81. * \brief qtprotobuf_generate is cmake helper function that automatically generates STATIC library target from your .proto files
  82. *
  83. * \param TARGET name of you target that generated code archive will be linked to
  84. * \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.
  85. * \param OUT_DIR output directory that will contain generated artifacts. Usually subfolder in build directory should be used
  86. * \param GENERATED_HEADERS List of header files expected after generator job finished
  87. * \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)
  88. * \param PROTO_FILES List of .proto files that will be used in generation procedure
  89. * \param MULTI Enables multi-files generation mode. If provided in parameter list generator will create pair of header/source files for each message
  90. * \note multi-files generation mode is defined as deprecated by QtProtobuf team, and might have poor support in future
  91. * \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.
  92. * \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.
  93. *
  94. *
  95. * \subsection cmake_qtprotobuf_link_archive qtprotobuf_link_archive
  96. *
  97. * \brief qtprotobuf_link_archive is cmake helper function that links whole archive to your library or executable target.
  98. *
  99. * \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.
  100. *
  101. * \param TARGET name of target to link to
  102. * \param GENERATED_TARGET protobuf generated target name
  103. *
  104. * \section qmake
  105. * - \ref qmake_qtprotobuf_generate
  106. *
  107. * \subsection qmake_qtprotobuf_generate qtprotobuf_generate
  108. *
  109. * \brief qtprotobuf_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
  110. * \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.
  111. */
  112. /*!
  113. * \ingroup generator
  114. * \private
  115. * \brief The QtGenerator class
  116. */
  117. class QtGenerator : public GeneratorBase
  118. {
  119. public:
  120. QtGenerator();
  121. bool Generate(const ::google::protobuf::FileDescriptor *file,
  122. const std::string &parameter,
  123. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  124. std::string *error) const override;
  125. bool GenerateAll(const std::vector<const ::google::protobuf::FileDescriptor *> &files,
  126. const std::string &parameter,
  127. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  128. std::string *error) const override;
  129. };
  130. } //namespace generator
  131. } // namespace QtProtobuf