AUTOMOC.rst 9.0 KB

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