AUTOMOC.rst 9.7 KB

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