Autogen: option to have automoc warn about non-explicit moc includes
Target group
There are projects which enjoy CMake's automoc for automatically finding all files that need to have moc files generated for them, yet still desire to explicitly include/source all such moc files in their code (see below for motivation).
During development on such projects, if missing out an otherwise needed explicit moc include, it would be nice to not have a breaking build, yet get nudged by the system to improve things once there is time (i.e. just be warned, not run into failing build).
Proposed solution
For such projects it would be welcome to have an option for automoc to emit warnings if no explicit moc include has been found and instead an include is added to the target's mocs_compilation.cpp file. Currently the latter happens without further notice in the log.
- There would be a new target property named
AUTOMOC_MISSING_INCLUDE_WARNINGS
(or similar, whatever matches CMake naming pattern). - The default would be
OFF
to be behaviour-backward-compatible (perhaps could change later as part of some CMake policy). - As usual, default set by a respective global CMake var
CMAKE_AUTOMOC_MISSING_INCLUDE_WARNINGS
. - If this target property is
ON
, automoc would emit for any moc file it adds an include to the target's mocs_compilation.cpp file a warning similar to the one emitted when finding an explicit moc include, but without a matching known moc file. E.g.
AutoMoc warning: SRC:/foo.h contains a Q_OBJECT macro, but no file includes
the moc file "moc_foo.cpp". Added to the target's mocs_compilation.cpp
instead.
This idea had been before proposed on the Qt Development mailing list, with some on-list & off-list thumbs-up: https://lists.qt-project.org/pipermail/development/2023-July/044157.html
Motivation
While it is convenient as developer not to have to care about adding any needed moc includes for moc files generated for headers (other than for source files, where there is no way around), this convenience is found to come at some cost:
- increased full build time in general
- increased incremental build time in general
- missing out some compiler sanity checks
- missing out some compiler optimizations
See https://frinring.wordpress.com/2023/06/28/include-also-moc-files-of-headers for some closer look at that, including some numbers on seen build time changes.
For those reasons also the Qt developers themselves now and then run respective scripts (kept in their sources here: https://code.qt.io/cgit/qt/qtbase.git/tree/util/includemocs) over the Qt sources, to ensure more explicit moc includes are done. See the resulting "QtFoo: includemocs" commits as found in this search: https://code.qt.io/cgit/qt/qtbase.git/log/?qt=grep&q=includemocs