[cmake-developers] Compile flags depending on source file property

Brad King brad.king at kitware.com
Mon Aug 6 07:43:47 EDT 2018


On 08/03/2018 07:12 PM, Simon Richter wrote:
>     string(APPEND CMAKE_CXX_FLAGS
> "$<IF:$<SOURCE_PROPERTY:GENERATED>,${WARN_SHADOW_OFF},${WARN_SHADOW_ON}> ")

The generators put the project-/target-wide flags in a place that
does not depend on the source being compiled so there would be no
way to evaluate that.  On some generators per-source flags produce
a huge slowdown in the build (e.g. VS) so splitting up the settings
for every source is not practical.

We already run into this in the COMPILE_LANGUAGE generator expression:

  https://cmake.org/cmake/help/v3.12/manual/cmake-generator-expressions.7.html

See the note there about how it is handled on VS and Xcode.

> The only way I can think of doing this would be to remove the global
> warning flag definitions from the toplevel CMakeLists.txt

Per-source flags are placed after the target-wide flags on the command
line so you could just keep -Wshadow by default and then add -Wno-shadow
to the COMPILE_FLAGS source file property of the generated sources.

Alternatively you could use an object library for all the generated
sources and then set its target-wide flags to use "-Wno-shadow", or
even "-w".

-Brad


More information about the cmake-developers mailing list