cotire icon indicating copy to clipboard operation
cotire copied to clipboard

CMake 3.12 / Visual Studio - cotire stops custom build rules (Qt moc file creation) from being run

Open macdew opened this issue 7 years ago • 16 comments

We use qt5_wrap_cpp manually instead of AUTOMOC, and add the resulting moc files to our build target as part of the add_library command: add_library(BLAH_LIB <… sources here, including moc files>)

we then cotire(BLAH_LIB)

This approach used to work well for us, but lately we've seen a lot of machines where cotire fails because it tries to process a moc file before it's been generated - resulting in file not found error.

I'd rather not go back to automoc as we use Qt sparingly and I don't really fancy the overhead of all the file scanning.

As a temporary workaround, it appears to work if I cotire a target without the moc files added, and then add the moc files after using target_sources… But then the moc files don't benefit from precompiled headers.

Is there a way to make the cotire step depend explicitly on the moc target? The problem appears to manifest itself mainly on the MSVC generator - I don't think I've seen reported cases of this with ninja generator.

Qt 5.9.2, CMake 3.12, cotire 1.7.10, VS 2017 (15.7 and 15.8 patch releases)

macdew avatar Aug 17 '18 08:08 macdew

Ok, it looks like this may be a CMake 3.12 regression... All the machines that are failing are running CMake 3.12, but the ones that still appear to work are all on CMake 3.11.1

I'll try and get some of the failing machines downgraded to CMake 3.11.1 and see if that cures their problem.

macdew avatar Aug 22 '18 14:08 macdew

I can confirm it works for me with 3.11.4 but breaks with 3.12.

Kaylx avatar Aug 23 '18 07:08 Kaylx

We've reproduced using a small stand-alone test project and confirmed that qt5_wrap_cpp + Cotire break on CMake 3.12 but work on 3.11. Moc files do not created sufficiently early (or at all?) with CMake 3.12. I'll consider this a CMake regression and file a report with them.

macdew avatar Aug 23 '18 09:08 macdew

Apologies for the long delay. We have reported the issue to Kitware as a CMake 3.12 regression - they have investigated and recommended changes to Cotire. The CMake changes in behaviour are intentional.

The bug report and response/recommendations from Kitware can be found here: https://gitlab.kitware.com/cmake/cmake/issues/18353

macdew avatar Sep 11 '18 16:09 macdew

Also run into this problem. Tried doing what it said on the cmake issue, and commented out line https://github.com/sakra/cotire/blob/391bf6b7609e14f5976bd5247b68d63cbf8d4d12/CMake/cotire.cmake#L3517 And that seems to have fixed it for now. Don't know if this has any other bad side effect? Looking forward to a official solution!

petersteneteg avatar Sep 27 '18 11:09 petersteneteg

The fix by @petersteneteg has a negative side effect: the pch_pre projects are now always out-of-date in Visual Studio.

martinfalk avatar Nov 05 '18 12:11 martinfalk

I've also run into this problem. Has anyone come up with a better fix than @petersteneteg ?

jking777 avatar Nov 07 '18 20:11 jking777

The fix by @petersteneteg has a negative side effect: the pch_pre projects are now always out-of-date in Visual Studio.

I'm not seeing that behavior. With @petersteneteg fix Visual Studio is building (or not building) projects as I would expect.

Update: OK, now I see the issue. When I do Build Solution the pch_pre projects are built as I expected, but when I do Start Debugging the pch_pre projects are always thought to be out of date and I get prompted to build them. Yeah, that's annoying.

jking777 avatar Nov 07 '18 21:11 jking777

@jking777 that was what I was referring to. They are "marked" as out of date, hence VS prompts to rebuild them prior debugging. Selecting build then just runs a check returning that everything is up-to-date for those targets.

martinfalk avatar Nov 08 '18 17:11 martinfalk

Here is a workaround : you can put all your generated moc_ files as dependency of a new custom target and make your target dependent of this one:

#moc files
qt5_wrap_cpp(mocs ${my_qt_headers})

# qrc files
qt5_add_resources(src_qrc ${my_qrc_resources})
add_custom_target(QtInternalFilesGeneration DEPENDS ${mocs} ${src_qrc})

...

add_dependencies(MyActualTarget QtInternalFilesGeneration)

gbottesi avatar Jan 15 '19 16:01 gbottesi

Thanks @gbottesi, that workaround appears to be work quite well! No issues rebuilding or debugging my projects.

However, I notice as a side-effect, my .ui / .qrc files are now moved to the Generation project instead of my original project. Do you see the same?

macdew avatar Jan 18 '19 11:01 macdew

True. Hadn't noticed this, but indeed, qrc files have moved project...

gbottesi avatar Jan 18 '19 13:01 gbottesi

Has there been any fix or any work towards one? This is rather incovenient.

Philippe-Cote-Morneault avatar Aug 06 '19 11:08 Philippe-Cote-Morneault

Sadly we resorted to removing Cotire and replacing it with our own pch solution. Whilst not perfect, it gets the job done for us.

Also keep an eye on https://gitlab.kitware.com/cmake/cmake/merge_requests/3553

macdew avatar Aug 06 '19 16:08 macdew

@macdew That sucks. From what I understood, we can't use Cotire from CMake 3.12.0 onwards?

Philippe-Cote-Morneault avatar Aug 06 '19 18:08 Philippe-Cote-Morneault

@JigsawCorp That's right, we started to see things break when we moved to CMake 3.12.0. As we wanted support for the newer Visual Studio generators and CMake features, we had little choice but to create our own solution. Depending on which compiler(s) you want to support, PCH can be achieved with a few compiler flags only. MSVC is trivial, clang and gcc were a little bit trickier.

We also decided to specify our own precompiled headers for a bit more control (and in the process found some dodgy dependencies and a lot of stuff that could be cleared out). Cotire tended to pull in headers from our own source tree and make the build process more fragile.

macdew avatar Aug 06 '19 19:08 macdew