CMake incorrectly identifying valid C++ compiler options?

戏子无情 提交于 2021-02-11 18:16:48

问题


I have a set of C++ potential compiler flags stored in a variable and over it, I'm running the following test on CMake 3.14.5, to see which ones applies and which doesn't to a certain version of the compiler (I'm using GCC, CLANG and ICC to compile the same project, hence the necessity to apply to each only the relevant flags):

foreach (FLAG IN LISTS CXX_COMPILER_FLAGS_TO_USE)
    # Check if the compiler supports the flag.
    string(REGEX REPLACE "[-=+]" "" FLAG_NO_SIGNS ${FLAG}) # <- The variable recieving the result of the test can't have those signs in its name
    check_cxx_compiler_flag(${FLAG} CXX_COMPILER_SUPPORTS_${FLAG_NO_SIGNS})
    if(CXX_COMPILER_SUPPORTS_${FLAG_NO_SIGNS})
        message(STATUS "Flag ${FLAG} accepted by C++ compiler")
        string(APPEND CMAKE_CXX_FLAGS " ${FLAG}")
    else()
        message(STATUS "Flag ${FLAG} not accepted by C++ compiler")
    endif()
endforeach()

For GCC 8 and the -Wabi flag, I get:

-- Performing Test CXX_COMPILER_SUPPORTS_Wabi
-- Performing Test CXX_COMPILER_SUPPORTS_Wabi - Failed
-- Flag -Wabi not accepted by C++ compiler

Now, if instead of pushing the flags inside CMAKE_CXX_FLAGS, I use add_compile_options(), the result of my test changes!!!:

foreach (FLAG IN LISTS CXX_COMPILER_FLAGS_TO_USE)
    # Check if the compiler supports the flag.
    string(REGEX REPLACE "[-=+]" "" FLAG_NO_SIGNS ${FLAG})
    check_cxx_compiler_flag(${FLAG} CXX_COMPILER_SUPPORTS_${FLAG_NO_SIGNS})
    if(CXX_COMPILER_SUPPORTS_${FLAG_NO_SIGNS})
        message(STATUS "Flag ${FLAG} accepted by C++ compiler")
        add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${FLAG}>)  # <- ONLY THIS LINE CHANGED
    else()
        message(STATUS "Flag ${FLAG} not accepted by C++ compiler")
    endif()
endforeach()

The test for -Wabi now reports:

-- Performing Test CXX_COMPILER_SUPPORTS_Wabi
-- Performing Test CXX_COMPILER_SUPPORTS_Wabi - Success
-- Flag -Wabi accepted by C++ compiler  

Which leads the second case to fail on compile time later:

cc1plus: error: -Wabi won't warn about anything [-Werror=abi]

It's like add_compile_options() would have modified the result of check_cxx_compiler_flag(), which is weird since the latter runs BEFORE the former.

Just out of curiosity, I combined both approaches in the same test (which could sound redundant):

add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${FLAG}>)
string(APPEND CMAKE_CXX_FLAGS " ${FLAG}")

and that works, meaning -Wabi is NOT added to the compilation options for C++ files.

I wouldn't expect that replacing the usage of CMAKE_CXX_FLAGS by add_compile_options() would change the result of a test made PREVIOUSLY and using a third function.

So, the question is: Am I doing something I'm not supposed to? Or did I hit a real bug?

I couldn't find anything similar in the knowledge base, and when I post the issue to the CMake bug tracker, I really didn't understand the response.

Thanks a lot for your help.

来源:https://stackoverflow.com/questions/56856057/cmake-incorrectly-identifying-valid-c-compiler-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!