# Qt autogen source files custom compilation flags

**URL:** https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002
**Category:** Code
**Created:** [March 24, 2021, 8:26am UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002 "2021-03-24T08:26:29Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![R2RT](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/e47774/32.png) [@R2RT](https://discourse.cmake.org/u/R2RT)
#### Post date: [March 24, 2021, 8:26am UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/1 "2021-03-24T08:26:29Z")

</div>

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:

```cmake
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`

```cmake
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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 25, 2021, 6:01pm UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/2 "2021-03-25T18:01:44Z")

</div>

> [@R2RT](#):
>
> 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](https://gitlab.kitware.com/cmake/cmake/-/issues) 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).

---

<div class="post-metadata">

### Author: ![R2RT](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/e47774/32.png) [@R2RT](https://discourse.cmake.org/u/R2RT)
#### Post date: [March 25, 2021, 8:58pm UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/3 "2021-03-25T20:58:07Z")

</div>

Thanks for quick reply. I’ve created the issue here: [https://gitlab.kitware.com/cmake/cmake/-/issues/21986](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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 25, 2021, 9:01pm UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/4 "2021-03-25T21:01:05Z")

</div>

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:

```cmake
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.

---

<div class="post-metadata">

### Author: ![R2RT](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/e47774/32.png) [@R2RT](https://discourse.cmake.org/u/R2RT)
#### Post date: [March 25, 2021, 9:10pm UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/5 "2021-03-25T21:10:48Z")

</div>

I’ve checked this list before the API proposal: [https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-source-files](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.

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [March 25, 2021, 10:02pm UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/6 "2021-03-25T22:02:18Z")

</div>

The [`AUTORCC_OPTIONS`](https://cmake.org/cmake/help/latest/prop_sf/AUTORCC_OPTIONS.html) and [`AUTOUIC_OPTIONS`](https://cmake.org/cmake/help/latest/prop_sf/AUTOUIC_OPTIONS.html) source file properties already exist. Why not simply add `AUTOMOC_OPTIONS` for consistency?

---

<div class="post-metadata">

### Author: ![R2RT](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/e47774/32.png) [@R2RT](https://discourse.cmake.org/u/R2RT)
#### Post date: [March 25, 2021, 10:56pm UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/7 "2021-03-25T22:56:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [March 26, 2021, 6:28am UTC](https://discourse.cmake.org/t/qt-autogen-source-files-custom-compilation-flags/3002/8 "2021-03-26T06:28:48Z")

</div>

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:

```cpp
  -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!
