Automoc doesn't scan for modifications of dependent include headers
Example:
- Have a foo.cpp file that includes foo.h that includes bar.h
- Have foo.cpp #include "foo.moc"
- Have bar.h have a #define MY_FEATURE
- Have a slot definition my_feature_func() #ifdef'ed behind MY_FEATURE
- Have automoc enabled.
If you build the project, then comment out the #define in bar.h and rebuild the project, automoc does not rerun moc on foo.cpp, and you end up with a build error:
/.../projects/test_cmake_automoc_scanning/build/myapp_autogen/include/foo.moc:70:21: error: no member named 'my_feature_func' in 'Foo'
case 0: _t->my_feature_func(); break;
because the generated moc cpp file has a reference to a now non-existing slot.
This happens because automoc does not extract dependencies from the preprocessed foo.cpp file, as mentioned also here #17750 (comment 377396)
Proposal:
Instead of running the preprocessor on the generated foo.moc or foo.cpp a second time, CMake should presumably already know all header dependencies for foo.cpp, because it's a regular cpp file that has to be compiled.
As such, there probably is a mapping from foo.moc to foo.cpp, so we should be able to simply transfer / append the dependencies of foo.cpp to foo.moc, so that AUTOMOC can regenerate foo.moc if it sees that a dependency header has changed.
The same should be possible for the mocs_compilation.cpp aggregate case.
Would such a patch be accepted?
If there are concerns about performance due to having to check the timestamp on many headers each time make is invoked, perhaps we can make this an opt-in option via a new property?
Relevant downstream issue for the Qt CMake port: https://bugreports.qt.io/browse/QTBUG-74521
Sample project attached. Steps to reproduce: 1) Build project, 2) comment out define in bar.h 3) rebuild project Archive.zip