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

Simon Richter Simon.Richter at hogyros.de
Fri Aug 3 19:12:22 EDT 2018


Hi,

I'd like to compile different source files with slightly different
flags, calculating the flags dependent on source file properties.

The easiest example would be different warning flags for all generated
sources, as I have little influence over this code.

The only way I can think of doing this would be to remove the global
warning flag definitions from the toplevel CMakeLists.txt, and instead
have that define two different variables for the positive and negative
forms of a particular warning flag, dependent on compiler, and then
define the flag explicitly as source properties in each directory:

if(GCC or CLANG)
    # determine if it actually supports -Wshadow ...
    set(WARN_SHADOW_ON "-Wshadow")
    set(WARN_SHADOW_OFF "-Wno-shadow")
endif()

and later on

    set(TARGET1_NON_GENERATED_FILES ... )
    set(TARGET1_GENERATED_FILES ...)

    add_executable(target1
        ${TARGET1_GENERATED_FILES}
        ${TARGET1_NON_GENERATED_FILES})

    set_property(
        SOURCE ${TARGET1_NON_GENERATED_FILES}
        APPEND_STRING
        PROPERTY COMPILE_FLAGS
            "${WARN_SHADOW_ON} ")
    set_property(
        SOURCE ${TARGET1_GENERATED_FILES}
        APPEND_STRING
        PROPERTY COMPILE_FLAGS
            "${WARN_SHADOW_OFF} ")

All of this is rather ugly, and escalates even more as there are more
different classes of source files.

What I'd like to see is something like a generator expression that is
evaluated once per source file, that would allow me to have a global
definition

    string(APPEND CMAKE_CXX_FLAGS
"$<IF:$<SOURCE_PROPERTY:GENERATED>,${WARN_SHADOW_OFF},${WARN_SHADOW_ON}> ")

Since there is already a mechanism to carry the per-source COMPILE_FLAGS
property into the compile flags for the source file, I wonder if that
could be extended to a generic mechanism to derive source specific
compile flags from source properties?

   Simon

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://cmake.org/pipermail/cmake-developers/attachments/20180804/38b4404c/attachment.sig>


More information about the cmake-developers mailing list