Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • CMake CMake
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,813
    • Issues 3,813
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 7
    • Merge requests 7
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • External wiki
    • External wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • CMake
  • CMakeCMake
  • Issues
  • #19441
Closed
Open
Created Jul 01, 2019 by Alvaro Palma@alvaropalmaaste

add_compile_options() modifying results of check_cxx_compiler_flag()

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):

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  

In fact, the second case fails on compile time later:

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

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

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

and that works.

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

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.

Thanks a lot for your help.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking