如何在CMake中为外部INTERFACE_SOURCES设置编译标志?

时间:2016-05-04 19:45:45

标签: cmake compiler-flags

我在cmake中使用外部包,它使用INTERFACE_SOURCES。这意味着当我将导入的库链接到目标时,接口源文件会自动添加到目标。当我编译目标时,那些外部文件也被编译。

这对我来说是个问题,因为外部文件会导致编译警告。我想通过在编译外部文件时设置较低的警告级别来删除警告。但我不知道该怎么做。

这是我到目前为止所得到的。

# 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()

这是应该做的

  • 浏览所有链接库并获取外部INTERFACE_SOURCES源文件。
  • 检查每个外部源文件是否出现在黑名单中。如果是这样,请将其编译标志更改为较低级别。

我遇到的问题是获取并设置INTERFACE_SOURCES的编译标志。 get_source_file_property()和get_property()调用什么都不返回。

如何获取和设置这些看似不属于我的目标但同时编译的文件的标志?

1 个答案:

答案 0 :(得分:1)

  

get_source_file_property()和get_property()调用什么都不返回

源文件的

COMPILE_FLAGS属性未被" target"修改。像target_compile_options这样的命令。最后一组标志是全局+目标特定+源特定的混合。据我所知,没有办法关闭"继承"全局或目标旗帜。

作为一种变通方法,您可以通过添加-w选项来禁用警告:

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}"
)

仅供参考:How to set warning level in CMake?

记录:最初来自issue #408

相关问题