Qt autogen source files custom compilation flags

I want to customize warning flags in my Qt5 project.

It can be done with with simple target_compile_options(Library PRIVATE /Wall).
But, the Library target is Qt5 with AUTOMOC logic enabled.

This makes autogenerated Library_autogen/mocs_compilation.cpp file being compiled with /Wall, which is quite noisy.

The only way I found to disable these is:

set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/Library_autogen/mocs_compilation.cpp PROPERTIES COMPILE_OPTIONS /W0)

but is not scaling. I have to manually do it for all libraries + does not take AUTORRC or AUTOUI files into account.

I’ve given a try to AUTOGEN_TARGET_DEPENDS

set_target_properties(Library PROPERTIES AUTOGEN_TARGET_DEPENDS disable_warnings)

with disable_warnings being interface library exposing /W0 flag, but with no luck.

Is there any other approach I could take? That utilizes autogen target, which is quite handy (especially for other, non-advanced in CMake developers).

I wandered whenever generator expression could read GENERATED property of target source file, but AFAIK, generator expression would try to read it from the Library. $<$<TARGET_PROPERTY:GENERATED>:/W0> passed to target_compile_options did not work.

Yes. There’s no GENERATED property on targets. There are on source files. I suppose a $<SOURCE_FILE_PROPERTY> genex might make sense (only in certain contexts though, namely per-object cases).

Other than that, there’s no mechanism other than the tedious per-source file property setting you’ve found.

Could you please file an issue for a $<SOURCE_FILE_PROPERTY> genex? I don’t know how feasible such a genex is. We might also want to consider something like a AUTOGEN_OPTIONS property on targets to add compile options to AUTOMOC-generated sources (though I think adding a property like AUTOMOC 1 to its files for filtering by the new genex would be suitable).

Thanks for quick reply. I’ve created the issue here: https://gitlab.kitware.com/cmake/cmake/-/issues/21986

Also, I find this genex as much cleaner API than adding several new properties on targets. What if someone wanted to modify not compile options, but include paths/features/defines? Should we add AUTOGEN_OPTIONS, AUTOGEN_INCLUDES, AUTOGEN_COMPILE_FEATURES etc.?

Maaaaaybe, some day, someone would want to distinguish how the file was generated (MOC / RRC / UI / another tool), but in this case I’d rather add AUTOGEN_LABEL property, which appends its value into source file LABELS property.

We already have $<TARGET_PROPERTY:AUTOMOC>, so which targets have various autogen bits enabled is already there. The issue is adding bits to the source files. Something like this:

target_compile_options(automoc_tgt PRIVATE "$<$<SOURCE_FILE_PROPERTY:AUTOMOC>:-fflag-for-automoc")
target_compile_definitions(automoc_tgt PRIVATE "$<$<SOURCE_FILE_PROPERTY:AUTOMOC>:CUSTOM_AUTOMOC_FLAG=1")

I don’t believe we currently support per-source file include directories at all today, so that is a feature even further down the road.

I’ve checked this list before the API proposal: https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-source-files

There is INCLUDE_DIRECTORIES listed, but no AUTOMOC one. Anyway, I think we agree that SOURCE_FILE_PROPERTY genex is the solution-to-go here.

1 Like

The AUTORCC_OPTIONS and AUTOUIC_OPTIONS source file properties already exist. Why not simply add AUTOMOC_OPTIONS for consistency?

They modify compilation flags for MOC/RRC compilers, not intermediate source .cpp files generated by these compilers.

Right. Sorry, I didn’t explain what I had in mind fully.

moc has a -b option, which you can use to include a header file:

  -b <file>                   Prepend #include <file> (preserve default
                              include).

In that header file, you can use #pragma statements to disable all warnings (or just the ones you want). That way, you don’t need to change any compiler flag.

I hope this helps!

1 Like