0

I use a custom macro to check and add a compiler flag to the compilation in CMAKE. The code I use for this is that:

MACRO(CheckAndAddFlag flag)
    # We cannot check for -Wno-foo as this won't throw a warning so we must check for the -Wfoo option directly
    # http://stackoverflow.com/questions/38785168/cc1plus-unrecognized-command-line-option-warning-on-any-other-warning
    STRING(REGEX REPLACE "^-Wno-" "-W" checkedFlag ${flag})
    SET(VarName ${checkedFlag})
    STRING(REPLACE "+" "X" VarName ${VarName})
    STRING(REPLACE "-" "_" VarName ${VarName})
    # Avoid double checks. A compiler will not magically support a flag it did not before
    MESSAGE(STATUS "Checking CXX_FLAG_${VarName}_SUPPORTED. Checked? ${VarName}_CHECKED= ${${VarName}_CHECKED} end")
    if(NOT ${VarName}_CHECKED)
        CHECK_CXX_COMPILER_FLAG(${checkedFlag} CXX_FLAG_${VarName}_SUPPORTED)
        CHECK_C_COMPILER_FLAG(${checkedFlag} C_FLAG_${VarName}_SUPPORTED)
        set(${VarName}_CHECKED YES CACHE INTERNAL "")
        MESSAGE(STATUS "Checked CXX_FLAG_${VarName}_SUPPORTED. Checked? ${VarName}_CHECKED= ${${VarName}_CHECKED} end")
    endif()
    IF (CXX_FLAG_${VarName}_SUPPORTED)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
    ENDIF ()
    IF (C_FLAG_${VarName}_SUPPORTED)
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
    ENDIF ()
    unset(VarName)
    unset(checkedFlag)
ENDMACRO()

And I call it e.g. with:

CheckAndAddFlag("-Wno-error=type-limits")
CheckAndAddFlag("-Wfloat-conversion")

Normally it should only check each flag once, set its variable and following runs of CMake should find this and don't check it again (try_compile with MSVC is painfully slow)
It does work for most of the flags, but it does not work with -Wno-error=type-limits. Here the flag gets double checked according to the output. Hence I added the _CHECKED variable to work around this, but even this does not work. It sill gets checked again.

Some output:

Checking CXX_FLAG__Werror=type_limits_SUPPORTED. Checked? _Werror=type_limits_CHECKED=  end
Performing Test CXX_FLAG__Werror=type_limits_SUPPORTED
Performing Test CXX_FLAG__Werror=type_limits_SUPPORTED - Failed
Performing Test C_FLAG__Werror=type_limits_SUPPORTED
Performing Test C_FLAG__Werror=type_limits_SUPPORTED - Failed
Checked CXX_FLAG__Werror=type_limits_SUPPORTED. Checked? _Werror=type_limits_CHECKED= YES end
Checking CXX_FLAG__Wfloat_conversion_SUPPORTED. Checked? _Wfloat_conversion_CHECKED= YES end
Checking CXX_FLAG__Wlong_long_SUPPORTED. Checked? _Wlong_long_CHECKED= YES end

Any ideas why this is happening? Why does the variable get set but is unset in the next CMAKE run and why only this?

Flamefire
  • 5,313
  • 3
  • 35
  • 70

1 Answers1

1

You are confusing CMake with your = sign in variable names?

Try this instead:

STRING(REGEX REPLACE "[-=]" "_" VarName ${VarName})
utopia
  • 1,477
  • 8
  • 7