Knitschi
Knitschi

Reputation: 3112

How to set compile flags for external INTERFACE_SOURCES in CMake?

I use an external package in cmake, that uses INTERFACE_SOURCES. This means when I link the imported library to my target, the interface source files are automatically added to my target. When I compile my target, those external files are compiled too.

This causes a problem for me, because the external files cause compile warnings. I want to remove the warnings by setting a lower warning level when compiling the external files. But I do not know how to do this.

This is what I got so far.

# reduce the warning level for some files over which we have no control.
macro( remove_warning_flags_for_some_external_files myTarget )

    # blacklist of files that throw warnings
    set( blackListedExternalFiles 
        static_qt_plugins.cpp
    )

    get_target_property( linkedLibraries ${myTarget} LINK_LIBRARIES )

    foreach(library ${linkedLibraries})

        get_property( sources TARGET ${library} PROPERTY INTERFACE_SOURCES )

        foreach(source ${sources})

            get_filename_component(shortName ${source} NAME)

            if( ${shortName} IN_LIST blackListedExternalFiles)

                # everything works until here
                # does not work
                get_source_file_property( flags1 ${source} COMPILE_FLAGS)     
                # does not work
                get_property(flags2 SOURCE ${source} PROPERTY COMPILE_FLAGS) 

                # exchange flags in list, this I can do

                # set flags to source file, do not know how to

            endif()

        endforeach()
    endforeach()
endmacro()

This is what this should do

The problem I have is with getting and setting the compile flags for those INTERFACE_SOURCES. The get_source_file_property() and get_property() calls return nothing.

How can I get and set the flags for these files that seem to not belong to my target, but are compiled at the same time?

Upvotes: 1

Views: 1095

Answers (1)

user2288008
user2288008

Reputation:

The get_source_file_property() and get_property() calls return nothing

COMPILE_FLAGS property of source file is not modified by "target" commands like target_compile_options. Final set of flags is a mix of global + target specific + source specific. As far as I understand there is no way to turn off "inheriting" of global or target flags.

As a workaround you can disable warning by adding -w option:

get_source_file_property(flags "${external_source}" COMPILE_FLAGS)
if(NOT flags)
  set(flags "") # convert "NOTFOUND" to "" if needed
endif()
set_source_files_properties(
    "${external_source}"
    PROPERTIES
    COMPILE_FLAGS "-w ${flags}"
)

Just for your information: How to set warning level in CMake?.

For the record: originally from issue #408.

Upvotes: 2

Related Questions