AUTOMOC.rst 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. AUTOMOC
  2. -------
  3. Should the target be processed with auto-moc (for Qt projects).
  4. ``AUTOMOC`` is a boolean specifying whether CMake will handle the Qt ``moc``
  5. preprocessor automatically, i.e. without having to use commands like
  6. :module:`QT4_WRAP_CPP() <FindQt4>`, `qt5_wrap_cpp()`_, etc.
  7. Currently, Qt versions 4 to 6 are supported.
  8. .. _qt5_wrap_cpp(): https://doc.qt.io/qt-5/qtcore-cmake-qt5-wrap-cpp.html
  9. This property is initialized by the value of the :variable:`CMAKE_AUTOMOC`
  10. variable if it is set when a target is created.
  11. When this property is set ``ON``, CMake will scan the header and
  12. source files at build time and invoke ``moc`` accordingly.
  13. Header file processing
  14. ^^^^^^^^^^^^^^^^^^^^^^
  15. At configuration time, a list of header files that should be scanned by
  16. ``AUTOMOC`` is computed from the target's sources.
  17. - All header files in the target's sources are added to the scan list.
  18. - For all C++ source files ``<source_base>.<source_extension>`` in the
  19. target's sources, CMake searches for
  20. - a regular header with the same base name
  21. (``<source_base>.<header_extension>``) and
  22. - a private header with the same base name and a ``_p`` suffix
  23. (``<source_base>_p.<header_extension>``)
  24. and adds these to the scan list.
  25. At build time, CMake scans each unknown or modified header file from the
  26. list and searches for
  27. - a Qt macro from :prop_tgt:`AUTOMOC_MACRO_NAMES`,
  28. - additional file dependencies from the ``FILE`` argument of a
  29. ``Q_PLUGIN_METADATA`` macro and
  30. - additional file dependencies detected by filters defined in
  31. :prop_tgt:`AUTOMOC_DEPEND_FILTERS`.
  32. If a Qt macro is found, then the header will be compiled by the ``moc`` to the
  33. output file ``moc_<base_name>.cpp``. The complete output file path is
  34. described in the section `Output file location`_.
  35. The header will be ``moc`` compiled again if a file from the additional file
  36. dependencies changes.
  37. Header ``moc`` output files ``moc_<base_name>.cpp`` can be included in source
  38. files. In the section `Including header moc files in sources`_ there is more
  39. information on that topic.
  40. Source file processing
  41. ^^^^^^^^^^^^^^^^^^^^^^
  42. At build time, CMake scans each unknown or modified C++ source file from the
  43. target's sources for
  44. - a Qt macro from :prop_tgt:`AUTOMOC_MACRO_NAMES`,
  45. - includes of header ``moc`` files
  46. (see `Including header moc files in sources`_),
  47. - additional file dependencies from the ``FILE`` argument of a
  48. ``Q_PLUGIN_METADATA`` macro and
  49. - additional file dependencies detected by filters defined in
  50. :prop_tgt:`AUTOMOC_DEPEND_FILTERS`.
  51. If a Qt macro is found, then the C++ source file
  52. ``<base>.<source_extension>`` is expected to as well contain an include
  53. statement
  54. .. code-block:: c++
  55. #include <<base>.moc> // or
  56. #include "<base>.moc"
  57. The source file then will be compiled by the ``moc`` to the output file
  58. ``<base>.moc``. A description of the complete output file path is in section
  59. `Output file location`_.
  60. The source will be ``moc`` compiled again if a file from the additional file
  61. dependencies changes.
  62. Including header moc files in sources
  63. """""""""""""""""""""""""""""""""""""
  64. A source file can include the ``moc`` output file of a header
  65. ``<header_base>.<header_extension>`` by using an include statement of
  66. the form
  67. .. code-block:: c++
  68. #include <moc_<header_base>.cpp> // or
  69. #include "moc_<header_base>.cpp"
  70. If the ``moc`` output file of a header is included by a source, it will
  71. be generated in a different location than if it was not included. This is
  72. described in the section `Output file location`_.
  73. Output file location
  74. ^^^^^^^^^^^^^^^^^^^^
  75. Included moc output files
  76. """""""""""""""""""""""""
  77. ``moc`` output files that are included by a source file will be generated in
  78. - ``<AUTOGEN_BUILD_DIR>/include``
  79. for single configuration generators or in
  80. - ``<AUTOGEN_BUILD_DIR>/include_<CONFIG>``
  81. for :prop_gbl:`multi configuration <GENERATOR_IS_MULTI_CONFIG>` generators.
  82. Where ``<AUTOGEN_BUILD_DIR>`` is the value of the target property
  83. :prop_tgt:`AUTOGEN_BUILD_DIR`.
  84. The include directory is automatically added to the target's
  85. :prop_tgt:`INCLUDE_DIRECTORIES`.
  86. Not included moc output files
  87. """""""""""""""""""""""""""""
  88. ``moc`` output files that are not included in a source file will be generated
  89. in
  90. - ``<AUTOGEN_BUILD_DIR>/<SOURCE_DIR_CHECKSUM>``
  91. for single configuration generators or in,
  92. - ``<AUTOGEN_BUILD_DIR>/include_<CONFIG>/<SOURCE_DIR_CHECKSUM>``
  93. for :prop_gbl:`multi configuration <GENERATOR_IS_MULTI_CONFIG>` generators.
  94. Where ``<SOURCE_DIR_CHECKSUM>`` is a checksum computed from the relative
  95. parent directory path of the ``moc`` input file. This scheme allows to have
  96. ``moc`` input files with the same name in different directories.
  97. All not included ``moc`` output files will be included automatically by the
  98. CMake generated file
  99. - ``<AUTOGEN_BUILD_DIR>/mocs_compilation.cpp``, or
  100. - ``<AUTOGEN_BUILD_DIR>/mocs_compilation_$<CONFIG>.cpp``,
  101. which is added to the target's sources.
  102. Qt version detection
  103. ^^^^^^^^^^^^^^^^^^^^
  104. ``AUTOMOC`` enabled targets need to know the Qt major and minor
  105. version they're working with. The major version usually is provided by the
  106. ``INTERFACE_QT_MAJOR_VERSION`` property of the ``Qt[456]Core`` library,
  107. that the target links to. To find the minor version, CMake builds a list of
  108. available Qt versions from
  109. - ``Qt6Core_VERSION_MAJOR`` and ``Qt6Core_VERSION_MINOR`` variables
  110. (usually set by ``find_package(Qt6...)``)
  111. - ``Qt6Core_VERSION_MAJOR`` and ``Qt6Core_VERSION_MINOR`` directory properties
  112. - ``Qt5Core_VERSION_MAJOR`` and ``Qt5Core_VERSION_MINOR`` variables
  113. (usually set by ``find_package(Qt5...)``)
  114. - ``Qt5Core_VERSION_MAJOR`` and ``Qt5Core_VERSION_MINOR`` directory properties
  115. - ``QT_VERSION_MAJOR`` and ``QT_VERSION_MINOR`` variables
  116. (usually set by ``find_package(Qt4...)``)
  117. - ``QT_VERSION_MAJOR`` and ``QT_VERSION_MINOR`` directory properties
  118. in the context of the :command:`add_executable` or :command:`add_library` call.
  119. Assumed ``INTERFACE_QT_MAJOR_VERSION`` is a valid number, the first
  120. entry in the list with a matching major version is taken. If no matching major
  121. version was found, an error is generated.
  122. If ``INTERFACE_QT_MAJOR_VERSION`` is not a valid number, the first
  123. entry in the list is taken.
  124. A ``find_package(Qt[456]...)`` call sets the ``QT/Qt[56]Core_VERSION_MAJOR/MINOR``
  125. variables. If the call is in a different context than the
  126. :command:`add_executable` or :command:`add_library` call, e.g. in a function,
  127. then the version variables might not be available to the ``AUTOMOC``
  128. enabled target.
  129. In that case the version variables can be forwarded from the
  130. ``find_package(Qt[456]...)`` calling context to the :command:`add_executable`
  131. or :command:`add_library` calling context as directory properties.
  132. The following Qt5 example demonstrates the procedure.
  133. .. code-block:: cmake
  134. function (add_qt5_client)
  135. find_package(Qt5 REQUIRED QUIET COMPONENTS Core Widgets)
  136. ...
  137. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  138. PROPERTY Qt5Core_VERSION_MAJOR "${Qt5Core_VERSION_MAJOR}")
  139. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  140. PROPERTY Qt5Core_VERSION_MINOR "${Qt5Core_VERSION_MAJOR}")
  141. ...
  142. endfunction ()
  143. ...
  144. add_qt5_client()
  145. add_executable(myTarget main.cpp)
  146. target_link_libraries(myTarget Qt5::QtWidgets)
  147. set_property(TARGET myTarget PROPERTY AUTOMOC ON)
  148. Modifiers
  149. ^^^^^^^^^
  150. :prop_tgt:`AUTOMOC_EXECUTABLE`:
  151. The ``moc`` executable will be detected automatically, but can be forced to
  152. a certain binary using this target property.
  153. :prop_tgt:`AUTOMOC_MOC_OPTIONS`:
  154. Additional command line options for ``moc`` can be set in this target property.
  155. :prop_tgt:`AUTOMOC_MACRO_NAMES`:
  156. This list of Qt macro names can be extended to search for additional macros in
  157. headers and sources.
  158. :prop_tgt:`AUTOMOC_DEPEND_FILTERS`:
  159. ``moc`` dependency file names can be extracted from headers or sources by
  160. defining file name filters in this target property.
  161. :prop_tgt:`AUTOMOC_COMPILER_PREDEFINES`:
  162. Compiler pre definitions for ``moc`` are written to the ``moc_predefs.h`` file.
  163. The generation of this file can be enabled or disabled in this target property.
  164. :prop_sf:`SKIP_AUTOMOC`:
  165. Sources and headers can be excluded from ``AUTOMOC`` processing by
  166. setting this source file property.
  167. :prop_sf:`SKIP_AUTOGEN`:
  168. Source files can be excluded from ``AUTOMOC``,
  169. :prop_tgt:`AUTOUIC` and :prop_tgt:`AUTORCC` processing by
  170. setting this source file property.
  171. :prop_gbl:`AUTOGEN_SOURCE_GROUP`:
  172. This global property can be used to group files generated by
  173. ``AUTOMOC`` or :prop_tgt:`AUTORCC` together in an IDE, e.g. in MSVS.
  174. :prop_gbl:`AUTOGEN_TARGETS_FOLDER`:
  175. This global property can be used to group ``AUTOMOC``,
  176. :prop_tgt:`AUTOUIC` and :prop_tgt:`AUTORCC` targets together in an IDE,
  177. e.g. in MSVS.
  178. :variable:`CMAKE_GLOBAL_AUTOGEN_TARGET`:
  179. A global ``autogen`` target, that depends on all ``AUTOMOC`` or
  180. :prop_tgt:`AUTOUIC` generated :ref:`<ORIGIN>_autogen` targets in the project,
  181. will be generated when this variable is ``ON``.
  182. :prop_tgt:`AUTOGEN_PARALLEL`:
  183. This target property controls the number of ``moc`` or ``uic`` processes to
  184. start in parallel during builds.
  185. :prop_tgt:`AUTOGEN_COMMAND_LINE_LENGTH_MAX`:
  186. This target property controls the limit when to use response files for
  187. ``moc`` or ``uic`` processes on Windows.
  188. See the :manual:`cmake-qt(7)` manual for more information on using CMake
  189. with Qt.