Skip to content

CXX and CXXFLAGS used during feature tests are not accurate in CmakeList.txt

We are having trouble finding a working around for a failed compile due to our CMakeList.txt. The related issue in our issue tracker is at CheckCompileLinkOption fails when Xcode compiler is invoked from command line.

Our CMakeList.txt uses a clean compile as a feature gate. Any compiler output triggers a feature failure. We needed to switch to the clean compile after we learned CMake was misreporting feature availability on AIX and Solaris. The related issue is at How to have CHECK_CXX_COMPILER_FLAG fail on illegal value? on Stack Overflow.

Apparently CMake calculates CXXFLAGS internally (like -isysroot) but does not make them available to the CmakeList.txt script. Then, our CmakeList.txt calculates features and flags incorrectly because we are not given accurate CXXFLAGS during our compile and link tests.

Please advise how to have CMake populate CXX and CXXFLAGS as expected (or ${CMAKE_CXX_COMPILER} and ${CMAKE_CXX_FLAGS}). Or advise of another workaround for the feature test.

We support back to CMake 2.8. It does not matter (to us) if this gets fixed in a future version of CMake. We still need a workaround going back to CMake 2.8.


function(CheckCompileLinkOption opt var prog)

  if (MSVC)

    # TODO: improve this...
    CHECK_CXX_COMPILER_FLAG(${opt} ${var})

  else ()

    message(STATUS "Performing Test ${var}")
    execute_process(
      COMMAND sh -c "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${opt} ${prog} 2>&1"
      RESULT_VARIABLE COMMAND_RESULT
      OUTPUT_VARIABLE COMMAND_OUTPUT
      OUTPUT_STRIP_TRAILING_WHITESPACE)

    # This test is strict. We require two things. First, the invocation
    # of the compile command must return 0. Second, there must be no
    # messages on the console. We are interested in diagnostics like
    # warnings to decide when to reject an option. But we will probably
    # capture chatty compiler that want to say, "Hooray, success". For
    # chatty compilers we will need to find a quiet option and use it
    # for the test. Microsoft compilers come to mind.
    if ("${COMMAND_RESULT}" EQUAL 0 AND "${COMMAND_OUTPUT}" STREQUAL "")
      set(${var} 1 PARENT_SCOPE)
      message(STATUS "Performing Test ${var} - Success")
    else ()
      set(${var} 0 PARENT_SCOPE)
      message(STATUS "Performing Test ${var} - Failed")
    endif ()

  endif ()

endfunction(CheckCompileLinkOption)
Edited by Jeffrey Walton
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information