将汇编标志限制为某些文件

发布于 2025-01-23 12:35:24 字数 252 浏览 3 评论 0原文

我正在尝试向相当大的遗产项目介绍-Werror Flag。不出所料,它会完全打破汇编。因此,我决定逐步介绍它,并首先为新代码介绍。我最初的方法是将新功能汇编为单独的静态目标,并将其链接到项目,该项目在项目结构和可读性方面都有点好。持续存在预先存在的问题包括。基本上,即使在用新代码中修复了所有警告后,我还列出了链条,包括引入新警告。是否有任何方法将警告标志的使用量限制为严格的源文件? 我确实了解其中包括手段基本上将标头复制到CPP中,因此仅使用CMAKE设置似乎不可能。然后是布拉格马斯?

I'm trying to introduce -Werror flag to rather big legacy project. As expected, it breaks the compilation completely. Therefore I've decided to introduce it gradually, and for the new code first of all. My original approach was to compile new features as separate static targets and link them to the project, which works kind of good both in terms of project structure and readability. The problem which persist are pre-existing tangled includes. Basically, even after fixing all warnings in new code I'm left with chain of includes introducing new warnings. Is there any way to limit warning flags usage to given source files strictly?
I do understand that include means basically copy/pasting headers into cpps, so it does not seem possible with just cmake settings. Then pragmas, perhaps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2025-01-30 12:35:24

您可以使用设置

这样的事情:

set_source_files_properties(bad.cpp PROPERTIES COMPILE_OPTIONS -Werror)

You can use the set_source_files_properties command to set the COMPILE_OPTIONS property on the files you need.

Something like this:

set_source_files_properties(bad.cpp PROPERTIES COMPILE_OPTIONS -Werror)
懵少女 2025-01-30 12:35:24

我不知道任何允许您仅在其中包含的文件上应用标志的编译器标志,因此CMAKE不能为您做得更好。因此,布拉格马是必经之路。

基本上,您在CPP文件中有效的内容是这样的:

#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wall"

#include "updated_lib/header1.hpp"
#include "updated_lib/header2.hpp"
...

#pragma GCC diagnostic pop

#include "non_updated_lib/header1.hpp"
#include "non_updated_lib/header2.hpp"

请注意,如果您随着时间的推移逐渐更新标头,则需要在多个翻译单元中重复此逻辑。

作为替代方案,您可以复制标头文件子目录,并通过一条路径和通过另一个路径更新标头提供未更新的版本,例如header foo/bar/bar/baz.hpp使标题要么使标题可通过路径获得旧/foo/bar/baz.hppnew/foo/bar/bar/baz.hpp并创建一个可通过foo/bar/提供的新标头baz.hpp看起来像这样:

#if __has_include("new/foo/bar/baz.hpp")
#   pragma GCC diagnostic push
#   pragma GCC diagnostic error "-Wall"

#   include "new/foo/bar/baz.hpp"

#   pragma GCC diagnostic pop
#else
#   pragma GCC diagnostic push
#   pragma GCC diagnostic warning "-Wall"

#   include "old/foo/bar/baz.hpp"

#   pragma GCC diagnostic pop
#endif

请注意,您可能需要为您编写这些标题。您甚至可以在项目的一代过程中通过CMAKE生成实际包含,该项目将把标题缩短到3个PRAGMAS加一个包括:这将具有与不支持__ has_include的编译器版本合作的其他好处。

function(my_generate_include OUT_LIST DESTINATION_DIR FIRST_HEADER)
    set(GENERATED_HEADERS ${${OUT_LIST}})
    foreach(HEADER IN ITEMS ${FIRST_HEADER} ${ARGN})
        if (HEADER MATCHES "^old/(.*)$")
            configure_file(old_include.hpp.in "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
            list(APPEND GENERATED_HEADERS "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
        elseif (HEADER MATCHES "^new/(.*)$")
            configure_file(new_include.hpp.in "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
            list(APPEND GENERATED_HEADERS "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
        else()
            message(FATAL_ERROR "Header '${HEADER}' doesn't start with 'new/' or 'old/'")
        endif()
    endforeach()
    set(${OUT_LIST} ${GENERATED_HEADERS} PARENT_SCOPE)
endfunction()

...

set(HEADERS)

my_generate_include(HEADERS ${CMAKE_CURRENT_BINARY_DIR}/generated_includes
   old/a/b/c.hpp
   new/d/e/f.hpp
)

I don't know about any compiler flags that allow you to apply flags to only some of the files included, so cmake cannot do better for you. Therefore pragmas are the way to go.

Basically what you effectively want in your cpp files is something like this:

#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wall"

#include "updated_lib/header1.hpp"
#include "updated_lib/header2.hpp"
...

#pragma GCC diagnostic pop

#include "non_updated_lib/header1.hpp"
#include "non_updated_lib/header2.hpp"

Note that this would require this logic to be repeated in multiple translation units which you may want to avoid, if you're updating the headers gradually over time.

As an alternative you could duplicate the header file subdirectories, and make the non-updated versions available via one path and updated headers via another, e.g. for a header foo/bar/baz.hpp either make the header available via path old/foo/bar/baz.hpp or new/foo/bar/baz.hpp and create a new header available via foo/bar/baz.hpp that looks like this:

#if __has_include("new/foo/bar/baz.hpp")
#   pragma GCC diagnostic push
#   pragma GCC diagnostic error "-Wall"

#   include "new/foo/bar/baz.hpp"

#   pragma GCC diagnostic pop
#else
#   pragma GCC diagnostic push
#   pragma GCC diagnostic warning "-Wall"

#   include "old/foo/bar/baz.hpp"

#   pragma GCC diagnostic pop
#endif

Note that you'll probably need to write these kind of headers for you. You could even generate the actual includes via cmake during the generation of the project which which would shorten the headers to 3 pragmas plus one include; this would have the additional benefit of working with compiler versions not supporting __has_include.

function(my_generate_include OUT_LIST DESTINATION_DIR FIRST_HEADER)
    set(GENERATED_HEADERS ${${OUT_LIST}})
    foreach(HEADER IN ITEMS ${FIRST_HEADER} ${ARGN})
        if (HEADER MATCHES "^old/(.*)
quot;)
            configure_file(old_include.hpp.in "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
            list(APPEND GENERATED_HEADERS "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
        elseif (HEADER MATCHES "^new/(.*)
quot;)
            configure_file(new_include.hpp.in "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
            list(APPEND GENERATED_HEADERS "${DESTINATION_DIR}/${CMAKE_MATCH_1}")
        else()
            message(FATAL_ERROR "Header '${HEADER}' doesn't start with 'new/' or 'old/'")
        endif()
    endforeach()
    set(${OUT_LIST} ${GENERATED_HEADERS} PARENT_SCOPE)
endfunction()

...

set(HEADERS)

my_generate_include(HEADERS ${CMAKE_CURRENT_BINARY_DIR}/generated_includes
   old/a/b/c.hpp
   new/d/e/f.hpp
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文